diff --git a/scripts/pr_review.py b/scripts/pr_review.py index 3c056035a..1fa205590 100755 --- a/scripts/pr_review.py +++ b/scripts/pr_review.py @@ -1648,6 +1648,7 @@ def main(argv: list[str] | None = None) -> int: for flag in ("--repo", "--checkout", "--min-age-hours"): pre.add_argument(flag) if pre.parse_known_args(args)[0].first == "cleanup": + sys.modules.setdefault("pr_review", sys.modules[__name__]) import pr_cleanup rest = list(args) diff --git a/tests/test_pr_cleanup.py b/tests/test_pr_cleanup.py index 134e6740a..aa3976c99 100755 --- a/tests/test_pr_cleanup.py +++ b/tests/test_pr_cleanup.py @@ -939,6 +939,22 @@ def test_a_develop_held_by_another_worktree_stops_the_primary_leaving(self) -> N self.assertIn(CLEANUP_BRANCH, self.branches()) +class TestCleanupLoadsOneGate(unittest.TestCase): + def test_the_gate_run_as_a_script_is_not_imported_again_by_cleanup(self) -> None: + """`pr_review.py cleanup` runs the gate as `__main__`, and `pr_cleanup` must reuse it.""" + script = Path(__file__).resolve().parent.parent / "scripts" / "pr_review.py" + result = subprocess.run( + [sys.executable, "-X", "importtime", str(script), "cleanup", "--help"], + capture_output=True, + text=True, + encoding="utf-8", + check=True, + ) + imported = [ln.rpartition("|")[2].strip() for ln in result.stderr.splitlines()] + self.assertIn("pr_cleanup", imported) + self.assertNotIn("pr_review", imported) + + class TestCleanupOriginParsing(unittest.TestCase): def test_https_and_ssh_forms_name_the_repository_case_insensitively(self) -> None: for url in (