scripts/tests/reproduction_utils/test_verify_and_run.py
scripts/tests/reproduction_utils/test_verify_and_run.pyBrowse 41 files
1,813 tokens
6,593 bytes
Token encoding: o200k_base
Snapshot a9fb1c3
← Back to SKILL.md
1import subprocess2import sys3from pathlib import Path4 5import pytest6 7sys.path.insert(0, str(Path(__file__).resolve().parents[2]))8 9import mechanical_refactor_reproduction_utils as rr10from mechanical_refactor_reproduction_utils import (11 Repro,12 _def_span,13 _find_class,14 _find_def,15 _replace_span,16 _slice_span,17 dedent,18 exec_command,19 git_add_and_commit,20 verify_mechanical_refactor,21)22from reproduction_testlib import _apply, _commit, _git, _write # noqa: F40123 24# --- verify_mechanical_refactor ------------------------------------------------25 26 27def _silence_precommit(monkeypatch) -> None:28 real = rr.exec_command29 30 def fake(cmd: str, cwd=None, check=True):31 if cmd.startswith("pre-commit"):32 return ""33 return real(cmd, cwd=cwd, check=check)34 35 monkeypatch.setattr(rr, "exec_command", fake)36 37 38def test_reproduce_passes_when_transform_matches_target(39 repo: Path, tmp_path: Path, monkeypatch, capsys40) -> None:41 """A transform that recreates the target tree reports PASS and does not exit."""42 _write(repo, **{"src.py": "line1\nline2\nline3\n"})43 base = _commit(repo, "base")44 _write(repo, **{"src.py": None, "a.py": "line1\nline2\n", "b.py": "line3\n"})45 target = _commit(repo, "split")46 47 def transform(root: Path) -> None:48 lines = (root / "src.py").read_text().splitlines(keepends=True)49 (root / "a.py").write_text("".join(lines[0:2]))50 (root / "b.py").write_text("".join(lines[2:3]))51 (root / "src.py").unlink()52 rr.git_add_and_commit("split", cwd=str(root))53 54 monkeypatch.chdir(repo)55 monkeypatch.setattr(rr.tempfile, "mkdtemp", lambda prefix="": str(tmp_path / "wt"))56 _silence_precommit(monkeypatch)57 58 verify_mechanical_refactor(base, target, transform)59 assert "PASS" in capsys.readouterr().out60 61 62def test_reproduce_exits_when_transform_diverges(63 repo: Path, tmp_path: Path, monkeypatch64) -> None:65 """A transform that produces a different tree fails with a non-zero exit."""66 _write(repo, **{"src.py": "line1\nline2\nline3\n"})67 base = _commit(repo, "base")68 _write(repo, **{"src.py": None, "a.py": "line1\nline2\n", "b.py": "line3\n"})69 target = _commit(repo, "split")70 71 def wrong_transform(root: Path) -> None:72 (root / "a.py").write_text("WRONG\n")73 (root / "b.py").write_text("line3\n")74 (root / "src.py").unlink()75 rr.git_add_and_commit("split", cwd=str(root))76 77 monkeypatch.chdir(repo)78 monkeypatch.setattr(rr.tempfile, "mkdtemp", lambda prefix="": str(tmp_path / "wt"))79 _silence_precommit(monkeypatch)80 81 with pytest.raises(SystemExit):82 verify_mechanical_refactor(base, target, wrong_transform)83 84 85def test_reproduce_creates_verify_branch_on_pass(86 repo: Path, tmp_path: Path, monkeypatch, capsys87) -> None:88 """A PASS run leaves a verify-mechanical-<base[:8]> branch in the repo."""89 _write(repo, **{"src.py": "line1\nline2\nline3\n"})90 base = _commit(repo, "base")91 _write(repo, **{"src.py": None, "a.py": "line1\nline2\n", "b.py": "line3\n"})92 target = _commit(repo, "split")93 94 def transform(root: Path) -> None:95 lines = (root / "src.py").read_text().splitlines(keepends=True)96 (root / "a.py").write_text("".join(lines[0:2]))97 (root / "b.py").write_text("".join(lines[2:3]))98 (root / "src.py").unlink()99 rr.git_add_and_commit("split", cwd=str(root))100 101 monkeypatch.chdir(repo)102 monkeypatch.setattr(rr.tempfile, "mkdtemp", lambda prefix="": str(tmp_path / "wt"))103 _silence_precommit(monkeypatch)104 105 verify_mechanical_refactor(base, target, transform)106 assert "PASS" in capsys.readouterr().out107 branch = f"verify-mechanical-{base[:8]}"108 assert _git(repo, "branch", "--list", branch).endswith(branch)109 110 111def _precommit_writes_file(monkeypatch, filename: str, contents: str) -> None:112 real = rr.exec_command113 114 def fake(cmd: str, cwd=None, check=True):115 if cmd.startswith("pre-commit"):116 (Path(cwd) / filename).write_text(contents)117 return ""118 return real(cmd, cwd=cwd, check=check)119 120 monkeypatch.setattr(rr, "exec_command", fake)121 122 123def test_reproduce_commits_pre_commit_fixes_when_tree_left_dirty(124 repo: Path, tmp_path: Path, monkeypatch, capsys125) -> None:126 """When pre-commit reformats and leaves the tree dirty, a 'pre-commit fixes' commit127 is created on top of the transform commit."""128 _write(repo, **{"src.py": "hello\n"})129 base = _commit(repo, "base")130 _write(repo, **{"src.py": "hello world\n", "formatted.py": "auto\n"})131 target = _commit(repo, "edit")132 133 def transform(root: Path) -> None:134 (root / "src.py").write_text("hello world\n")135 rr.git_add_and_commit("transform", cwd=str(root))136 137 monkeypatch.chdir(repo)138 monkeypatch.setattr(rr.tempfile, "mkdtemp", lambda prefix="": str(tmp_path / "wt"))139 _precommit_writes_file(monkeypatch, "formatted.py", "auto\n")140 141 verify_mechanical_refactor(base, target, transform)142 assert "PASS" in capsys.readouterr().out143 branch = f"verify-mechanical-{base[:8]}"144 subjects = _git(repo, "log", "--format=%s", "-2", branch).splitlines()145 assert subjects == ["pre-commit fixes", "transform"]146 147 148# --- Repro.run end-to-end ------------------------------------------------------149 150 151def test_repro_run_passes_on_a_faithful_call_site_lowering(152 repo: Path, monkeypatch, capsys153) -> None:154 """End-to-end: a lowering reproduces the commit byte-for-byte (pre-commit stubbed)."""155 _write(repo, **{"c.py": "r = Old.foo(self.n, 5)\n"})156 base = _commit(repo, "base")157 _write(repo, **{"c.py": "r = self.n.foo(5)\n"})158 target = _commit(repo, "lower the call site")159 monkeypatch.chdir(repo)160 _silence_precommit(monkeypatch)161 162 diff = Repro(base, target).lower_call_sites("foo", "Old", paths=["c.py"]).run()163 assert diff == ""164 assert "PASS" in capsys.readouterr().out165 166 167def test_repro_run_reports_residual_when_a_change_is_bundled(168 repo: Path, monkeypatch, capsys169) -> None:170 """A bundled non-relocation change surfaces as a non-empty residual diff."""171 _write(repo, **{"c.py": "r = Old.foo(self.n, 5)\nUNRELATED = 1\n"})172 base = _commit(repo, "base")173 _write(repo, **{"c.py": "r = self.n.foo(5)\nUNRELATED = 2\n"})174 target = _commit(repo, "lower the call AND change a constant")175 monkeypatch.chdir(repo)176 _silence_precommit(monkeypatch)177 178 diff = Repro(base, target).lower_call_sites("foo", "Old", paths=["c.py"]).run()179 assert "UNRELATED" in diff180 assert "RESIDUAL" in capsys.readouterr().out181