test_exact_commit_source_snapshot.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python3
  2. """Regression: review-compact source/ is exact DELIVERY tracked subset."""
  3. from __future__ import annotations
  4. import json
  5. import tempfile
  6. import unittest
  7. import zipfile
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parents[2]
  10. import sys
  11. sys.path.insert(0, str(ROOT))
  12. sys.path.insert(0, str(ROOT / "tools"))
  13. import package_for_agent_analysis as analyzer # noqa: E402
  14. import compact_package_selftest as selftest # noqa: E402
  15. class ExactCommitSourceSnapshotTests(unittest.TestCase):
  16. maxDiff = None
  17. def test_collect_exact_commit_keeps_formal_excludes_sby_workdir(self) -> None:
  18. with tempfile.TemporaryDirectory(prefix="prod2u-exact-") as tmp:
  19. temp_root = Path(tmp)
  20. # Plant untracked SBY junk in live tree — must NOT enter exact snapshot.
  21. junk_dir = ROOT / "tests" / "formal" / "alt_config_limit_normalize_bmc" / "src"
  22. junk_dir.mkdir(parents=True, exist_ok=True)
  23. junk = junk_dir / "alt_config_limit_normalize_formal.v"
  24. planted = False
  25. if not junk.is_file():
  26. junk.write_text("// planted untracked junk\n", encoding="utf-8")
  27. planted = True
  28. try:
  29. commit = analyzer._resolve_delivery_commit(ROOT, None)
  30. items, _excl, manifest = analyzer._collect_exact_commit_unified_items(
  31. root=ROOT,
  32. commit=commit,
  33. source_id="workspace-current",
  34. temp_root=temp_root,
  35. extra_exclusions=(),
  36. profile="review-compact",
  37. )
  38. finally:
  39. if planted and junk.is_file():
  40. junk.unlink()
  41. self.assertEqual(manifest["source_snapshot_mode"], "exact_commit_tracked_subset")
  42. source_paths = {
  43. item.candidate.relative
  44. for item in items
  45. if item.candidate.relative.startswith("source/")
  46. }
  47. self.assertIn(
  48. "source/tests/formal/alt_config_limit_normalize_formal.v",
  49. source_paths,
  50. )
  51. self.assertFalse(
  52. any("alt_config_limit_normalize_bmc" in p for p in source_paths)
  53. )
  54. self.assertFalse(
  55. any("alt_config_limit_normalize_cover" in p for p in source_paths)
  56. )
  57. included = {
  58. str(row["path"])
  59. for row in manifest["included_source_files"]
  60. if isinstance(row, dict)
  61. }
  62. self.assertIn("tests/formal/alt_config_limit_normalize_formal.v", included)
  63. def test_content_dedup_never_drops_source_software(self) -> None:
  64. with tempfile.TemporaryDirectory(prefix="prod2u-dedup-") as tmp:
  65. root = Path(tmp)
  66. a = root / "formal.v"
  67. b = root / "copy.v"
  68. payload = b"module x; endmodule\n"
  69. a.write_bytes(payload)
  70. b.write_bytes(payload)
  71. items = (
  72. analyzer.make_package_item(
  73. a,
  74. relative="source/tests/formal/alt_config_limit_normalize_formal.v",
  75. category="source",
  76. source_id="t",
  77. ),
  78. analyzer.make_package_item(
  79. b,
  80. relative="source/tests/formal/alt_config_limit_normalize_bmc/src/alt_config_limit_normalize_formal.v",
  81. category="source",
  82. source_id="t",
  83. ),
  84. )
  85. inventory = [
  86. {
  87. "path": item.candidate.relative,
  88. "sha256": __import__("hashlib").sha256(payload).hexdigest(),
  89. "size": len(payload),
  90. }
  91. for item in items
  92. ]
  93. kept, _records, _excl = analyzer.apply_content_dedup(items, inventory)
  94. kept_paths = {item.candidate.relative for item in kept}
  95. self.assertIn(
  96. "source/tests/formal/alt_config_limit_normalize_formal.v",
  97. kept_paths,
  98. )
  99. if __name__ == "__main__":
  100. unittest.main()