#!/usr/bin/env python3 """Fail-closed: TD .prj / syn TCL must match canonical source closure exactly.""" from __future__ import annotations import json import re import tempfile import unittest import xml.etree.ElementTree as ET from pathlib import Path ROOT = Path(__file__).resolve().parents[2] def _closure_sources() -> list[str]: return list(json.loads((ROOT / "config/source_closure.json").read_text(encoding="utf-8"))["sources"]) def _prj_sources(path: Path) -> list[str]: tree = ET.parse(path) return [ str(node.get("Path", "")).removeprefix("../") for node in tree.findall(".//Verilog/File") ] def _tcl_sources(path: Path) -> list[str]: syn = path.read_text(encoding="ascii") return re.findall(r"^analyze -file \{([^}]+)\}$", syn, flags=re.MULTILINE) class TdSourceListsFailClosedTests(unittest.TestCase): def test_live_lists_match_closure(self) -> None: want = _closure_sources() self.assertEqual(_prj_sources(ROOT / "td/prod2u_p4_normal_v3.prj"), want) self.assertEqual(_tcl_sources(ROOT / "td/syn/prod2u_p4_normal_v3.tcl"), want) man = json.loads((ROOT / "src/generated/td-source-manifest.json").read_text(encoding="utf-8")) self.assertEqual(man["sources"], want) def test_missing_motion_module_fails_precheck(self) -> None: want = _closure_sources() drop = "src/modules/door_rate_quantum_builder.v" self.assertIn(drop, want) with tempfile.TemporaryDirectory() as tmp: tmp_path = Path(tmp) prj = (ROOT / "td/prod2u_p4_normal_v3.prj").read_text(encoding="utf-8") # Remove the File entry for the motion module prj_bad = re.sub( rf'\s*.*?', "", prj, count=1, flags=re.S, ) bad_prj = tmp_path / "bad.prj" bad_prj.write_text(prj_bad, encoding="utf-8") got = _prj_sources(bad_prj) self.assertNotEqual(got, want) self.assertNotIn(drop, got) if __name__ == "__main__": unittest.main()