Files
troupe-standup/wizard-work/forge/verify_roundtrip_refactor.py

133 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Verification harness for the _test_roundtrip_refactored.py refactor.
Checks:
1. The refactored file is git-tracked at HEAD.
2. Original and refactored produce byte-identical stdout+stderr.
3. The refactored file compiles (py_compile).
4. No ``global`` statements remain.
5. No standalone ``import string`` (dead import removed).
6. ``if __name__ == "__main__"`` guard is present.
Exit 0 = all pass; exit 1 = at least one failure.
"""
from __future__ import annotations
import difflib
import os
import subprocess
import sys
REPO_ROOT = "/root/troupe-standup"
WORK_DIR = os.path.join(REPO_ROOT, "wizard-work", "forge")
REFACTORED = "_test_roundtrip_refactored.py"
ORIGINAL = "_test_roundtrip.py"
REL_PATH = "wizard-work/forge/_test_roundtrip_refactored.py"
def _run(*args: str) -> subprocess.CompletedProcess:
return subprocess.run(
args,
cwd=WORK_DIR,
capture_output=True,
text=True,
)
def check_git_tracked() -> bool:
r = _run("git", "cat-file", "-e", f"HEAD:{REL_PATH}")
if r.returncode != 0:
print(f"FAIL: {REFACTORED} not found at HEAD:{REL_PATH}")
return False
print(f"OK: {REFACTORED} is git-tracked at HEAD")
return True
def check_identical_output() -> bool:
orig = _run("python3", ORIGINAL)
ref = _run("python3", REFACTORED)
orig_text = orig.stdout + orig.stderr
ref_text = ref.stdout + ref.stderr
if orig_text != ref_text:
print("FAIL: original and refactored output differ")
diff = difflib.unified_diff(
orig_text.splitlines(keepends=True),
ref_text.splitlines(keepends=True),
fromfile="original",
tofile="refactored",
)
print("".join(diff))
return False
if orig.returncode != ref.returncode:
print(f"FAIL: exit codes differ ({orig.returncode} vs {ref.returncode})")
return False
print(f"OK: identical output (exit {orig.returncode}): {orig_text.strip()}")
return True
def check_py_compile() -> bool:
r = _run("python3", "-m", "py_compile", REFACTORED)
if r.returncode != 0:
print(f"FAIL: py_compile failed: {r.stderr}")
return False
print("OK: py_compile passes")
return True
def check_no_globals() -> bool:
path = os.path.join(WORK_DIR, REFACTORED)
with open(path) as f:
content = f.read()
for i, line in enumerate(content.splitlines(), 1):
stripped = line.lstrip()
if stripped.startswith("global "):
print(f"FAIL: 'global' found at line {i}: {line.strip()}")
return False
print("OK: no 'global' statements")
return True
def check_no_dead_import_string() -> bool:
path = os.path.join(WORK_DIR, REFACTORED)
with open(path) as f:
content = f.read()
for i, line in enumerate(content.splitlines(), 1):
stripped = line.lstrip()
if stripped.startswith("import ") and "string" in stripped.split()[:2]:
print(f"FAIL: 'import string' found at line {i}: {line.strip()}")
return False
print("OK: no 'import string' (dead import removed)")
return True
def check_main_guard() -> bool:
path = os.path.join(WORK_DIR, REFACTORED)
with open(path) as f:
content = f.read()
if '__name__ == "__main__"' not in content:
print("FAIL: no '__name__ == \"__main__\"' guard")
return False
print('OK: __main__ guard present')
return True
def main() -> None:
checks = [
check_git_tracked,
check_identical_output,
check_py_compile,
check_no_globals,
check_no_dead_import_string,
check_main_guard,
]
results = [fn() for fn in checks]
passed = sum(results)
total = len(results)
print(f"\n=== Verification: {passed}/{total} checks passed ===")
if passed != total:
sys.exit(1)
if __name__ == "__main__":
main()