This commit is contained in:
JeanLynn 2026-08-27 21:19:09 +08:00 committed by GitHub
commit 08246ec663
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 2 deletions

View file

@ -652,8 +652,23 @@ def _start_proxy(
The caller is responsible for ensuring *port* is available
(see ``_find_available_port``).
"""
cmd = [sys.executable, "-m", "headroom.cli", "proxy", "--port", str(port)]
# Keep the launching process's cwd out of the proxy's sys.path. Without
# this, a checkout with a top-level `headroom/` package (e.g. this repo)
# shadows the installed package and drops its compiled `headroom._core`
# extension. Python 3.11+ supports -P (PYTHONSAFEPATH); on Python 3.10,
# `-c` puts cwd at sys.path[0], so remove it before running the CLI module.
if sys.version_info >= (3, 11):
cmd = [sys.executable, "-P", "-m", "headroom.cli"]
else:
cmd = [
sys.executable,
"-c",
(
"import runpy, sys; sys.path.pop(0); "
"runpy.run_module('headroom.cli', run_name='__main__', alter_sys=True)"
),
]
cmd.extend(["proxy", "--port", str(port)])
# Forward HEADROOM_MODE env var so the proxy respects the user's mode choice
headroom_mode = os.environ.get("HEADROOM_MODE")

View file

@ -51,6 +51,55 @@ def _capture_popen_kwargs(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> di
return captured
def _capture_proxy_command(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> list[Any]:
"""Invoke ``_start_proxy`` with all I/O stubbed; return its command."""
captured: list[Any] = []
def _fake_popen(cmd: Any, **kwargs: Any) -> _FakeProc:
captured.clear()
captured.extend(cmd)
return _FakeProc()
monkeypatch.setattr(wrap_cli.subprocess, "Popen", _fake_popen)
monkeypatch.setattr(wrap_cli, "_check_proxy", lambda port: True)
monkeypatch.setattr(wrap_cli.time, "sleep", lambda _seconds: None)
monkeypatch.setattr(wrap_cli, "_get_log_path", lambda: tmp_path / "proxy.log")
monkeypatch.setattr(wrap_cli, "_resolve_wrap_proxy_timeout_seconds", lambda: 1)
wrap_cli._start_proxy(8787)
return captured
def test_start_proxy_uses_safe_path(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
"""The proxy subprocess must run with -P so its cwd never shadows the
installed package -- e.g. `headroom wrap claude` invoked from inside a
checkout that has its own top-level `headroom/` package would otherwise
import that unbuilt source tree instead, dropping `headroom._core`."""
monkeypatch.setattr(wrap_cli.sys, "version_info", (3, 11, 0))
captured_cmd = _capture_proxy_command(monkeypatch, tmp_path)
assert captured_cmd[0] == wrap_cli.sys.executable
assert captured_cmd[1] == "-P"
def test_start_proxy_uses_compat_runner_on_python_310(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""Python 3.10 must remove cwd from sys.path without using unsupported ``-P``."""
monkeypatch.setattr(wrap_cli.sys, "version_info", (3, 10, 14))
captured_cmd = _capture_proxy_command(monkeypatch, tmp_path)
assert captured_cmd[:2] == [wrap_cli.sys.executable, "-c"]
assert "-P" not in captured_cmd
assert "sys.path.pop(0)" in captured_cmd[2]
assert (
"runpy.run_module('headroom.cli', run_name='__main__', alter_sys=True)" in captured_cmd[2]
)
assert captured_cmd[3:] == ["proxy", "--port", "8787"]
def test_start_proxy_detaches_on_windows(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
# Force the Windows branch regardless of the host OS, and supply the
# Windows-only ``subprocess`` constants the host lacks on POSIX.