22
33import importlib .util
44import sys
5+ from collections .abc import Iterator
56from importlib .machinery import SourceFileLoader
67from pathlib import Path
78from types import ModuleType , SimpleNamespace
89
910import pytest
1011from tests .boundary .process .tooling .ci import ROOT
1112
13+ _MISSING = object ()
1214
13- def _load () -> ModuleType :
15+
16+ def _load (module_state : pytest .MonkeyPatch ) -> ModuleType :
1417 path = ROOT / ".github/scripts/plan-benchmarks"
1518 loader = SourceFileLoader ("plan_benchmarks" , str (path ))
1619 spec = importlib .util .spec_from_loader ("plan_benchmarks" , loader )
1720 assert spec is not None
1821 assert spec .loader is not None
1922 module = importlib .util .module_from_spec (spec )
20- sys .modules [ "plan_benchmarks" ] = module
23+ module_state . setitem ( sys .modules , "plan_benchmarks" , module )
2124 spec .loader .exec_module (module )
2225 return module
2326
2427
28+ @pytest .fixture
29+ def isolated_plan_benchmarks_module () -> Iterator [ModuleType ]:
30+ previous = sys .modules .get ("plan_benchmarks" , _MISSING )
31+ with pytest .MonkeyPatch .context () as module_state :
32+ yield _load (module_state )
33+ assert sys .modules .get ("plan_benchmarks" , _MISSING ) is previous
34+
35+
36+ def test_loaded_planner_restores_module_state (
37+ isolated_plan_benchmarks_module : ModuleType ,
38+ ) -> None :
39+ assert sys .modules ["plan_benchmarks" ] is isolated_plan_benchmarks_module
40+
41+
2542def _patch_plan (module : ModuleType , monkeypatch : pytest .MonkeyPatch ) -> None :
2643 suite = SimpleNamespace (
2744 id = "mathematical-benchmarks-v1" ,
@@ -42,7 +59,7 @@ def _patch_plan(module: ModuleType, monkeypatch: pytest.MonkeyPatch) -> None:
4259def test_task_documentation_does_not_select_oracle (
4360 monkeypatch : pytest .MonkeyPatch ,
4461) -> None :
45- module = _load ()
62+ module = _load (monkeypatch )
4663 _patch_plan (module , monkeypatch )
4764
4865 plan = module .plan (
@@ -67,7 +84,7 @@ def test_benchmark_control_tools_run_contract_gate_without_oracle(
6784 path : str ,
6885 monkeypatch : pytest .MonkeyPatch ,
6986) -> None :
70- module = _load ()
87+ module = _load (monkeypatch )
7188 _patch_plan (module , monkeypatch )
7289
7390 plan = module .plan ([path ], base = "a" * 40 , head = "b" * 40 )
@@ -80,7 +97,7 @@ def test_benchmark_control_tools_run_contract_gate_without_oracle(
8097def test_task_environment_selects_exact_task_oracle (
8198 monkeypatch : pytest .MonkeyPatch ,
8299) -> None :
83- module = _load ()
100+ module = _load (monkeypatch )
84101 _patch_plan (module , monkeypatch )
85102
86103 plan = module .plan (
@@ -100,7 +117,7 @@ def test_task_environment_selects_exact_task_oracle(
100117def test_shared_environment_profile_escalates_only_on_integration_event (
101118 monkeypatch : pytest .MonkeyPatch ,
102119) -> None :
103- module = _load ()
120+ module = _load (monkeypatch )
104121 _patch_plan (module , monkeypatch )
105122
106123 pull_request = module .plan (
@@ -124,7 +141,7 @@ def test_shared_environment_profile_escalates_only_on_integration_event(
124141def test_main_push_is_an_integration_owner (
125142 monkeypatch : pytest .MonkeyPatch ,
126143) -> None :
127- module = _load ()
144+ module = _load (monkeypatch )
128145 _patch_plan (module , monkeypatch )
129146
130147 push = module .plan (
0 commit comments