test_td_source_lists_fail_closed.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. """Fail-closed: TD .prj / syn TCL must match canonical source closure exactly."""
  3. from __future__ import annotations
  4. import json
  5. import re
  6. import tempfile
  7. import unittest
  8. import xml.etree.ElementTree as ET
  9. from pathlib import Path
  10. ROOT = Path(__file__).resolve().parents[2]
  11. def _closure_sources() -> list[str]:
  12. return list(json.loads((ROOT / "config/source_closure.json").read_text(encoding="utf-8"))["sources"])
  13. def _prj_sources(path: Path) -> list[str]:
  14. tree = ET.parse(path)
  15. return [
  16. str(node.get("Path", "")).removeprefix("../")
  17. for node in tree.findall(".//Verilog/File")
  18. ]
  19. def _tcl_sources(path: Path) -> list[str]:
  20. syn = path.read_text(encoding="ascii")
  21. return re.findall(r"^analyze -file \{([^}]+)\}$", syn, flags=re.MULTILINE)
  22. class TdSourceListsFailClosedTests(unittest.TestCase):
  23. def test_live_lists_match_closure(self) -> None:
  24. want = _closure_sources()
  25. self.assertEqual(_prj_sources(ROOT / "td/prod2u_p4_normal_v3.prj"), want)
  26. self.assertEqual(_tcl_sources(ROOT / "td/syn/prod2u_p4_normal_v3.tcl"), want)
  27. man = json.loads((ROOT / "src/generated/td-source-manifest.json").read_text(encoding="utf-8"))
  28. self.assertEqual(man["sources"], want)
  29. def test_missing_motion_module_fails_precheck(self) -> None:
  30. want = _closure_sources()
  31. drop = "src/modules/door_rate_quantum_builder.v"
  32. self.assertIn(drop, want)
  33. with tempfile.TemporaryDirectory() as tmp:
  34. tmp_path = Path(tmp)
  35. prj = (ROOT / "td/prod2u_p4_normal_v3.prj").read_text(encoding="utf-8")
  36. # Remove the File entry for the motion module
  37. prj_bad = re.sub(
  38. rf'\s*<File Path="\.\./{re.escape(drop)}">.*?</File>',
  39. "",
  40. prj,
  41. count=1,
  42. flags=re.S,
  43. )
  44. bad_prj = tmp_path / "bad.prj"
  45. bad_prj.write_text(prj_bad, encoding="utf-8")
  46. got = _prj_sources(bad_prj)
  47. self.assertNotEqual(got, want)
  48. self.assertNotIn(drop, got)
  49. if __name__ == "__main__":
  50. unittest.main()