118 lines
5.4 KiB
Python
118 lines
5.4 KiB
Python
"""The exporters delete and rewrite their output folder; they must never
|
|
reach outside it, whatever the page text or the --out flag says."""
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
FIX = ROOT / "tests/fixtures/minimal-kb"
|
|
OKF = ROOT / ".agents/skills/ckb-export-okf/scripts/export_okf.py"
|
|
STARLIGHT = ROOT / ".agents/skills/ckb-export-starlight/scripts/export_starlight.py"
|
|
|
|
|
|
def run(cwd, script, *args):
|
|
return subprocess.run([sys.executable, str(script), *args], cwd=str(cwd),
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
|
|
|
|
class ExportContainment(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.outer = Path(self.tmp.name)
|
|
self.kb = self.outer / "kb"
|
|
shutil.copytree(str(FIX), str(self.kb))
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def test_okf_refuses_out_outside_outputs(self):
|
|
for bad in (".", "wiki", "outputs", "..", str(self.outer)):
|
|
r = run(self.kb, OKF, "--out", bad)
|
|
self.assertEqual(r.returncode, 2, f"--out {bad}: {r.stdout}{r.stderr}")
|
|
self.assertTrue((self.kb / "wiki" / "index.md").is_file(), f"--out {bad} deleted the wiki")
|
|
self.assertTrue((self.kb / "ckb.yaml").is_file(), f"--out {bad} deleted the KB")
|
|
|
|
def test_okf_default_and_nested_out_work(self):
|
|
self.assertIn(run(self.kb, OKF).returncode, (0, 1))
|
|
self.assertTrue((self.kb / "outputs/okf").is_dir())
|
|
self.assertIn(run(self.kb, OKF, "--out", "outputs/okf-test").returncode, (0, 1))
|
|
self.assertTrue((self.kb / "outputs/okf-test").is_dir())
|
|
|
|
def test_okf_check_writes_nothing(self):
|
|
before = sorted(p.relative_to(self.kb) for p in self.kb.rglob("*"))
|
|
self.assertIn(run(self.kb, OKF, "--check").returncode, (0, 1))
|
|
self.assertEqual(sorted(p.relative_to(self.kb) for p in self.kb.rglob("*")), before)
|
|
|
|
def test_starlight_image_traversal_is_not_copied(self):
|
|
(self.outer / "secret.txt").write_text("do not export", encoding="utf-8")
|
|
assets = self.kb / "wiki" / "assets"
|
|
assets.mkdir(parents=True, exist_ok=True)
|
|
(assets / "ok.png").write_bytes(b"\x89PNG")
|
|
page = self.kb / "wiki" / "entities" / "pic.md"
|
|
page.parent.mkdir(parents=True, exist_ok=True)
|
|
page.write_text("---\ntype: entity\ntldr: pictures\n---\n\n# Pic\n\n"
|
|
"\n\n\n"
|
|
"\n", encoding="utf-8")
|
|
r = run(self.kb, STARLIGHT, "--mode", "content-only", "--today", "2026-11-11")
|
|
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
|
out = self.kb / "outputs" / "starlight"
|
|
self.assertTrue((out / "public/assets/ok.png").is_file(), "a normal asset should still be copied")
|
|
leaked = [p for p in self.outer.rglob("secret.txt") if p != self.outer / "secret.txt"]
|
|
self.assertEqual(leaked, [], f"traversal copied the file to {leaked}")
|
|
self.assertIn("not copied", r.stderr)
|
|
|
|
|
|
class Golden(unittest.TestCase):
|
|
"""The exact files and summary each exporter produces from the clean
|
|
fixture. Content is not compared: OKF stamps the export date."""
|
|
|
|
def export(self, script, *args):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
kb = Path(d) / "kb"
|
|
shutil.copytree(str(FIX), str(kb))
|
|
r = run(kb, script, *args)
|
|
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
|
files = sorted(p.relative_to(kb / "outputs").as_posix() for p in (kb / "outputs").rglob("*") if p.is_file())
|
|
report = [l for l in r.stdout.splitlines() if not l.startswith("output:")]
|
|
return files, report
|
|
|
|
def expected(self, name):
|
|
return (ROOT / "tests/expected" / name).read_text(encoding="utf-8").splitlines()
|
|
|
|
def test_okf(self):
|
|
files, report = self.export(OKF)
|
|
self.assertEqual(files, self.expected("okf-files.txt"))
|
|
self.assertEqual(report, self.expected("okf-report.txt"))
|
|
|
|
def test_starlight(self):
|
|
files, report = self.export(STARLIGHT, "--mode", "full", "--today", "2026-11-11")
|
|
self.assertEqual(files, self.expected("starlight-files.txt"))
|
|
self.assertEqual(report, self.expected("starlight-report.txt"))
|
|
|
|
|
|
class OkfScalars(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
import importlib.util
|
|
spec = importlib.util.spec_from_file_location("export_okf", str(OKF))
|
|
cls.okf = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(cls.okf)
|
|
|
|
def test_values_a_yaml_reader_would_misread_are_quoted(self):
|
|
for v in ("See issue #42 for details", "- list?", "? key", "a: b", "end:", "#tag", " pad", "tab\there"):
|
|
out = self.okf.yaml_scalar(v)
|
|
self.assertTrue(out.startswith('"'), f"{v!r} -> {out!r}")
|
|
for v in ("Plain title", "C#", "issue#42", "-5", "0.8", "2026-11-11"):
|
|
self.assertEqual(self.okf.yaml_scalar(v), v)
|
|
|
|
def test_first_h1_ignores_code_fences(self):
|
|
body = "Intro\n\n```bash\n# install deps\nmake\n```\n\n# Real title\n"
|
|
self.assertEqual(self.okf.first_h1(body), "Real title")
|
|
self.assertIsNone(self.okf.first_h1("```\n# only in code\n```\n"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|