test(hooks): isolate ambient venv and PATH in _detect_run

The emitted detector prefers an active virtualenv and the first python on PATH. Running the suite through uv run exposes the project .venv through both VIRTUAL_ENV and PATH, so the uv-tool machine fixture resolved to the project interpreter and test_shebang_parse_requires_leading_hash_bang failed locally. Scrub the venv variables and rebuild PATH from the stub bin plus os.defpath.

Co-Authored-By: TRAE <noreply@trae.ai>
(cherry picked from commit 6ead523c44)
This commit is contained in:
huangpz6 authored and safishamsi committed 2026-11-05 12:11:30 +00:00
1 parent 892c452d8c
commit 4c16a79817
1 file changed
+10 -1
+10 -1
View File
@@ -872,9 +872,18 @@ def _detect_run(tmp_path, home, stub_bin, env_extra=None):
env = dict(os.environ)
env["HOME"] = str(home)
env.pop("UV_TOOL_DIR", None)
# The emitted detector prefers an active virtualenv. A contributor running
# `uv run pytest` has VIRTUAL_ENV pointing at the project .venv, which masks
# the uv-tool layout this machine fixture constructs (repo rule: tests must
# not depend on the contributor's ambient environment).
for key in ("VIRTUAL_ENV", "PYTHONHOME", "PYTHONPATH", "CONDA_PREFIX"):
env.pop(key, None)
if env_extra:
env.update(env_extra)
env["PATH"] = str(stub_bin) + os.pathsep + env["PATH"]
# Keep the ambient PATH out too: `uv run pytest` puts the project .venv
# ahead of the system directories, so the detector would adopt it before
# reaching the uv-tool env this fixture is about.
env["PATH"] = str(stub_bin) + os.pathsep + os.defpath
return subprocess.run(
["sh", script.name], capture_output=True, text=True,
cwd=str(tmp_path), env=env,