Skip to content

Commit d409f59

Browse files
test: pin the suite interpreter on PATH for parity script subprocesses (#5361)
* test: pin the suite interpreter on PATH for parity script subprocesses The parity tests' script variants resolve python3 -> python -> py -3 through the ambient PATH. Under `uv run pytest` the venv's bin directory is first on PATH, so the scripts see the suite interpreter (with PyYAML) and CI passes. Under the direct `.venv/bin/python -m pytest` invocation that AGENTS.md prescribes, an ambient python3 without PyYAML fails every composed-template test with "PyYAML is required" (11 failures on macOS). Prepend the running interpreter's own bin directory in parity_helpers.clean_env() so both invocations behave the same; tests that install their own shim prepend afterwards and keep precedence. Delegate test_check_prerequisites_python_parity's duplicate _clean_env() to the shared helper so the fix applies there too. Fixes #5359 AI disclosure: agent-authored commit — OpenCode running GLM 5.3 Flash (Z.ai), agent mode under human supervision by mehdikarimi55; root-cause analysis, code change, and test execution performed by the agent. * test: cover the python3 resolver's first candidate on Windows too Copilot review follow-up (#5361): a Windows venv ships python.exe but no python3.exe, so putting the venv's bin directory first on PATH cannot shadow an ambient python3 — the first name the script resolvers probe (command -v python3 succeeds and wins before the python fallback is tried). clean_env() now also prepends a cached python3 shim built with the existing make_python3_path_shim() helper (shell shim plus a .cmd for PowerShell), so parity subprocesses resolve the suite interpreter on every platform. AI disclosure: agent-authored commit — OpenCode running GLM 5.3 Flash (Z.ai), agent mode under human supervision by mehdikarimi55; fix drafted in response to the Copilot review finding, verified by rerunning the affected parity suites. * test: survive missing Git Bash when building the Windows python3 shim Copilot review follow-up (#5361): _bash_posix_path() shells out to bash on Windows to convert the interpreter path, and a Windows machine with Git Bash not on PATH raised FileNotFoundError from clean_env() itself, failing every parity test during environment setup — including the python and PowerShell variants that never invoke bash. Catch the OSError and fall through to the static /mnt/ form, which the existing returncode fallback already accepts; bash-invoked tests skip when bash is absent, so the converted form is only needed where it can be exec'd. AI disclosure: agent-authored commit — OpenCode running GLM 5.3 Flash (Z.ai), agent mode under human supervision by mehdikarimi55; fix for the review finding, verified locally with the parity suites and a simulated no-bash subprocess failure. --------- Co-authored-by: mehdikarimi55 <9496476+mehdikarimi55@users.noreply.github.com>
1 parent d38bf46 commit d409f59

2 files changed

Lines changed: 66 additions & 19 deletions

File tree

‎tests/parity_helpers.py‎

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import shutil
1010
import subprocess
1111
import sys
12+
import tempfile
1213
from pathlib import Path
1314

1415
PROJECT_ROOT = Path(__file__).resolve().parent.parent
@@ -90,9 +91,45 @@ def clean_env() -> dict[str, str]:
9091
# silently override the "unset" baseline for every other test.
9192
env.pop("SPECKIT_PYTHON_EXECUTABLE", None)
9293
env.pop("SPECKIT_PYTHON", None)
94+
# The script variants resolve python3 -> python -> py -3 through PATH.
95+
# Under `uv run` the venv's bin directory is already first on PATH, so
96+
# the scripts see the interpreter the suite runs on, including its
97+
# site-packages (PyYAML among them). A direct
98+
# `.venv/bin/python -m pytest` — the invocation AGENTS.md prescribes —
99+
# leaves PATH untouched, and an ambient python3 without PyYAML then
100+
# fails every composed-template test with "PyYAML is required".
101+
# Prepend the running interpreter's own directory so subprocesses see
102+
# the same Python under either invocation. A Windows venv ships
103+
# ``python.exe`` but no ``python3.exe``, so the bin directory alone
104+
# cannot shadow an ambient ``python3`` — the first name the script
105+
# resolvers probe — and a cached ``python3`` shim (see
106+
# :func:`make_python3_path_shim`, shell shim plus a ``.cmd`` for
107+
# PowerShell) covers that first candidate on every platform.
108+
env["PATH"] = (
109+
f"{_python3_shim_dir()}{os.pathsep}"
110+
f"{Path(sys.executable).absolute().parent}"
111+
f"{os.pathsep}{env.get('PATH', '')}"
112+
)
93113
return env
94114

95115

116+
_PYTHON3_SHIM_DIR: Path | None = None
117+
118+
119+
def _python3_shim_dir() -> Path:
120+
"""A cached directory whose ``python3`` execs the suite interpreter.
121+
122+
Built once per pytest process with :func:`make_python3_path_shim`, which
123+
deliberately does not resolve the interpreter so the venv's
124+
``site-packages`` (PyYAML among them) stay reachable.
125+
"""
126+
global _PYTHON3_SHIM_DIR
127+
if _PYTHON3_SHIM_DIR is None:
128+
_PYTHON3_SHIM_DIR = Path(tempfile.mkdtemp(prefix="spec-kit-parity-shim-"))
129+
make_python3_path_shim(_PYTHON3_SHIM_DIR)
130+
return _PYTHON3_SHIM_DIR
131+
132+
96133
def venv_python3_exe(venv_dir: Path) -> Path:
97134
"""Path to the python3 executable of a venv created with ``--without-pip``."""
98135
if os.name == "nt":
@@ -135,19 +172,27 @@ def _bash_posix_path(path: Path) -> str:
135172
resolved = os.path.abspath(path)
136173
if os.name != "nt":
137174
return resolved
138-
converted = subprocess.run(
139-
[
140-
"bash",
141-
"-lc",
142-
'command -v cygpath >/dev/null 2>&1 && cygpath -u "$1"',
143-
"bash",
144-
resolved,
145-
],
146-
capture_output=True,
147-
text=True,
148-
timeout=30,
149-
)
150-
if converted.returncode == 0 and converted.stdout.strip():
175+
try:
176+
converted = subprocess.run(
177+
[
178+
"bash",
179+
"-lc",
180+
'command -v cygpath >/dev/null 2>&1 && cygpath -u "$1"',
181+
"bash",
182+
resolved,
183+
],
184+
capture_output=True,
185+
text=True,
186+
timeout=30,
187+
)
188+
except OSError:
189+
# Bash is not on PATH (e.g. a Windows machine without Git-for-Windows
190+
# in it). The converted form is only needed by bash-invoked tests,
191+
# which skip when bash is unavailable, so fall through to the static
192+
# /mnt/ form rather than failing environment setup for every parity
193+
# test, including the ones that never invoke bash.
194+
converted = None
195+
if converted is not None and converted.returncode == 0 and converted.stdout.strip():
151196
return converted.stdout.strip()
152197
drive = path.drive.rstrip(":").lower()
153198
posix = path.as_posix()

‎tests/test_check_prerequisites_python_parity.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
import pytest
1313

1414
from tests.conftest import requires_bash
15-
from tests.parity_helpers import install_composition_stack, run
15+
from tests.parity_helpers import (
16+
clean_env,
17+
install_composition_stack,
18+
run,
19+
)
1620

1721
PROJECT_ROOT = Path(__file__).resolve().parent.parent
1822
COMMON_SH = PROJECT_ROOT / "scripts" / "bash" / "common.sh"
@@ -56,11 +60,9 @@ def _write_feature_json(
5660

5761

5862
def _clean_env() -> dict[str, str]:
59-
env = os.environ.copy()
60-
for key in list(env):
61-
if key.startswith("SPECIFY_"):
62-
env.pop(key)
63-
return env
63+
# Delegate to the shared helper so the PATH fix that pins the suite's
64+
# own interpreter (see parity_helpers.clean_env) applies here too.
65+
return clean_env()
6466

6567

6668
def _git_init(repo: Path) -> None:

0 commit comments

Comments
 (0)