Skip to content

Commit d728679

Browse files
committed
test: isolate benchmark planner imports
1 parent 41504be commit d728679

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

tests/boundary/process/tooling/test_benchmark_classification.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,43 @@
22

33
import importlib.util
44
import sys
5+
from collections.abc import Iterator
56
from importlib.machinery import SourceFileLoader
67
from pathlib import Path
78
from types import ModuleType, SimpleNamespace
89

910
import pytest
1011
from 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+
2542
def _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:
4259
def 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(
8097
def 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(
100117
def 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(
124141
def 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(

tests/unit/tooling/test_audit_fixes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ def mock_read_json_null(path):
4444
def test_usage_rejects_non_dict_stats() -> None:
4545
"""Formally prove that non-dict stats is rejected."""
4646
from benchmarks.tooling import heldout_runner
47+
from benchmarks.tooling.errors import HarborSuiteError
4748

4849
# Create a temporary result file with stats as null
4950
with tempfile.NamedTemporaryFile(suffix=".json", mode="w", delete=False) as f:
5051
json.dump({"stats": None}, f)
5152
path = Path(f.name)
5253

5354
try:
54-
with pytest.raises(Exception) as exc_info:
55+
with pytest.raises(HarborSuiteError, match="stats must be an object"):
5556
heldout_runner._usage(path)
56-
assert "stats" in str(exc_info.value).lower()
5757
finally:
5858
path.unlink(missing_ok=True)
5959

0 commit comments

Comments
 (0)