|
| 1 | +import os |
| 2 | +import subprocess |
1 | 3 | import sys |
2 | 4 | from io import StringIO |
| 5 | +from pathlib import Path |
3 | 6 | from unittest.mock import patch |
4 | 7 |
|
5 | 8 | from browser_harness import run |
6 | 9 |
|
7 | 10 |
|
| 11 | +def test_maintenance_commands_do_not_load_agent_helpers(tmp_path): |
| 12 | + (tmp_path / "agent_helpers.py").write_text( |
| 13 | + "raise RuntimeError('agent_helpers should not load for maintenance commands')\n" |
| 14 | + ) |
| 15 | + repo_root = Path(__file__).resolve().parents[2] |
| 16 | + env = os.environ.copy() |
| 17 | + env["BH_AGENT_WORKSPACE"] = str(tmp_path) |
| 18 | + env["PYTHONPATH"] = os.pathsep.join( |
| 19 | + [str(repo_root / "src"), env.get("PYTHONPATH", "")] |
| 20 | + ) |
| 21 | + |
| 22 | + for arg in ("--version", "--help"): |
| 23 | + result = subprocess.run( |
| 24 | + [sys.executable, "-m", "browser_harness.run", arg], |
| 25 | + cwd=repo_root, |
| 26 | + env=env, |
| 27 | + text=True, |
| 28 | + capture_output=True, |
| 29 | + timeout=10, |
| 30 | + ) |
| 31 | + assert result.returncode == 0, result.stderr |
| 32 | + assert "agent_helpers should not load" not in result.stderr |
| 33 | + |
| 34 | + |
| 35 | +def test_execution_globals_load_agent_workspace_helpers(tmp_path, monkeypatch): |
| 36 | + import browser_harness |
| 37 | + |
| 38 | + (tmp_path / "agent_helpers.py").write_text("CUSTOM_AGENT_HELPER = 'loaded'\n") |
| 39 | + monkeypatch.setenv("BH_AGENT_WORKSPACE", str(tmp_path)) |
| 40 | + original_helpers = sys.modules.pop("browser_harness.helpers", None) |
| 41 | + original_package_helper = getattr(browser_harness, "helpers", None) |
| 42 | + if hasattr(browser_harness, "helpers"): |
| 43 | + delattr(browser_harness, "helpers") |
| 44 | + try: |
| 45 | + namespace = run._execution_globals() |
| 46 | + assert namespace["CUSTOM_AGENT_HELPER"] == "loaded" |
| 47 | + finally: |
| 48 | + sys.modules.pop("browser_harness.helpers", None) |
| 49 | + if original_helpers is not None: |
| 50 | + sys.modules["browser_harness.helpers"] = original_helpers |
| 51 | + if original_package_helper is not None: |
| 52 | + browser_harness.helpers = original_package_helper |
| 53 | + |
| 54 | + |
8 | 55 | def test_c_flag_executes_code(): |
9 | 56 | stdout = StringIO() |
10 | 57 | with patch.object(sys, "argv", ["browser-harness", "-c", "print('hello from -c')"]), \ |
|
0 commit comments