#!/usr/bin/env python3 """Fail-closed dirty/clean gates for tools/package-review-debug.sh.""" from __future__ import annotations import os import subprocess import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] WRAPPER = ROOT / "tools" / "package-review-debug.sh" class PackageReviewDebugWrapperTests(unittest.TestCase): def test_wrapper_exists_and_mentions_clean_gate(self) -> None: text = WRAPPER.read_text(encoding="utf-8") self.assertIn("status --porcelain --untracked-files=all", text) self.assertIn( "review package snapshots exact HEAD only; commit or clean the intended changes first", text, ) self.assertIn("PRODU2_PACKAGE_TOPIC", text) self.assertNotIn("round4-rate-dispatch", text) self.assertIn("mse-round7-verification-closure", text) self.assertNotIn("mse-round6-a1-clean-closure", text) def test_dirty_worktree_negative_exits_nonzero(self) -> None: """Invoke wrapper against this (likely dirty) repo; dirty ⇒ exit 2 + message.""" env = os.environ.copy() for key in ( "LD_LIBRARY_PATH", "LD_PRELOAD", "QT_PLUGIN_PATH", "PYTHONHOME", "PYTHONPATH", ): env.pop(key, None) # Force a dirty marker under an ignored temp path is insufficient — # porcelain already sees Round-6 dirty tree during verification tip build. # If tree is clean, create a disposable untracked file then remove it. marker = ROOT / f".package-review-debug-dirty-probe-{os.getpid()}.tmp" created = False try: status = subprocess.check_output( ["git", "-C", str(ROOT), "status", "--porcelain", "--untracked-files=all"], text=True, ) if not status.strip(): marker.write_text("dirty-probe\n", encoding="utf-8") created = True result = subprocess.run( ["bash", str(WRAPPER)], cwd=str(ROOT), env=env, capture_output=True, text=True, timeout=30, ) self.assertNotEqual(result.returncode, 0, "dirty tree must fail-closed") self.assertEqual(result.returncode, 2) blob = (result.stderr or "") + (result.stdout or "") self.assertIn( "review package snapshots exact HEAD only; commit or clean the intended changes first", blob, ) finally: if created and marker.exists(): marker.unlink() def test_clean_gate_message_stable(self) -> None: # Sanity: script is executable and parses. self.assertTrue(WRAPPER.is_file()) result = subprocess.run( ["bash", "-n", str(WRAPPER)], capture_output=True, text=True, timeout=10, ) self.assertEqual(result.returncode, 0, result.stderr) if __name__ == "__main__": unittest.main()