66 lines
2.8 KiB
Python
66 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
from generate_config_resources import generate, scan_entrypoint
|
|
|
|
|
|
class ConfigResourcesTests(unittest.TestCase):
|
|
def create_tree(self):
|
|
directory = TemporaryDirectory()
|
|
root = Path(directory.name)
|
|
config = root / "linuxcnc/configs/sim/demo"
|
|
config.mkdir(parents=True)
|
|
(config / "main.ini").write_text(
|
|
"[HAL]\nHALFILE = core.hal\nPOSTGUI_HALFILE = post.hal\n"
|
|
"[RS274NGC]\nPARAMETER_FILE = vars.var\nSUBROUTINE_PATH = macros\n"
|
|
"[EMCIO]\nTOOL_TABLE = tools.tbl\n",
|
|
encoding="utf-8",
|
|
)
|
|
(config / "core.hal").write_text("source extra.hal\nloadusr helper.py\n", encoding="utf-8")
|
|
(config / "post.hal").write_text("loadrt demo\n", encoding="utf-8")
|
|
(config / "extra.hal").write_text("setp demo.value 1\n", encoding="utf-8")
|
|
(config / "helper.py").write_text("print('ok')\n", encoding="utf-8")
|
|
(config / "tools.tbl").write_text("T1 P1\n", encoding="utf-8")
|
|
(config / "vars.var").write_text("#1 = 2\n", encoding="utf-8")
|
|
(config / "macros").mkdir()
|
|
return directory, root, config / "main.ini"
|
|
|
|
def test_resolves_ini_and_hal_edges(self):
|
|
directory, root, entrypoint = self.create_tree()
|
|
self.addCleanup(directory.cleanup)
|
|
rows = scan_entrypoint(root, root / "linuxcnc/configs/sim", entrypoint)
|
|
paths = {row["path"] for row in rows}
|
|
self.assertIn("linuxcnc/configs/sim/demo/core.hal", paths)
|
|
self.assertIn("linuxcnc/configs/sim/demo/extra.hal", paths)
|
|
self.assertIn("linuxcnc/configs/sim/demo/helper.py", paths)
|
|
self.assertIn("linuxcnc/configs/sim/demo/tools.tbl", paths)
|
|
|
|
def test_missing_resource_is_retained(self):
|
|
directory, root, entrypoint = self.create_tree()
|
|
self.addCleanup(directory.cleanup)
|
|
entrypoint.write_text("[HAL]\nHALFILE = missing.hal\n", encoding="utf-8")
|
|
rows = scan_entrypoint(root, root / "linuxcnc/configs/sim", entrypoint)
|
|
missing = [row for row in rows if row["path"].endswith("missing.hal")]
|
|
self.assertEqual(len(missing), 1)
|
|
self.assertEqual(missing[0]["parse_status"], "missing")
|
|
|
|
def test_generation_is_deterministic(self):
|
|
directory, root, _ = self.create_tree()
|
|
self.addCleanup(directory.cleanup)
|
|
first = root / "one.jsonl"
|
|
second = root / "two.jsonl"
|
|
report = root / "report.json"
|
|
_, digest_one = generate(root, first, report)
|
|
_, digest_two = generate(root, second, report)
|
|
self.assertEqual(digest_one, digest_two)
|
|
self.assertEqual(first.read_bytes(), second.read_bytes())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|