Skip to content

Commit 7bfd18d

Browse files
authored
fix: explicit PYTHONPATH for isolated test subprocesses (#593) (#594)
* fix: explicit PYTHONPATH for isolated test subprocesses (#593) * fix: respect existing PYTHONPATH in subprocess tests Put existing PYTHONPATH first, then append repo_root as fallback. This is less invasive to user environments while still ensuring the local package is available when needed.
1 parent 7e12a12 commit 7bfd18d

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

docs/examples/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,25 @@ def __init__(self, **kwargs):
498498
super().__init__(**kwargs)
499499

500500
def runtest(self):
501+
import os
502+
import pathlib
503+
504+
repo_root = str(pathlib.Path(__file__).parent.parent.parent.resolve())
505+
env = os.environ.copy()
506+
existing_pythonpath = env.get("PYTHONPATH", "")
507+
env["PYTHONPATH"] = (
508+
f"{existing_pythonpath}{os.pathsep}{repo_root}"
509+
if existing_pythonpath
510+
else repo_root
511+
)
512+
501513
process = subprocess.Popen(
502514
[sys.executable, self.path],
503515
stdout=subprocess.PIPE,
504516
stderr=subprocess.PIPE,
505517
text=True,
506518
bufsize=1, # Enable line-buffering
519+
env=env,
507520
)
508521

509522
# Capture stdout output and output it so it behaves like a regular test with -s.

test/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ def _run_heavy_modules_isolated(session, heavy_modules: list[str]) -> int:
251251
if markexpr:
252252
cmd.extend(["-m", markexpr])
253253

254+
import pathlib
255+
256+
repo_root = str(pathlib.Path(__file__).parent.parent.resolve())
257+
env["PYTHONPATH"] = f"{repo_root}{os.pathsep}{env.get('PYTHONPATH', '')}"
258+
254259
# Stream output in real-time while capturing for parsing
255260
process = subprocess.Popen(
256261
cmd,

0 commit comments

Comments
 (0)