Skip to content

Commit bf5a865

Browse files
authored
Modify tests to ensure $PYTHONPATH is given to the custom interpreter (#353)
1 parent 25cbb6f commit bf5a865

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

tests/test_non_host.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ def test_custom_interpreter(
2323
capfd: pytest.CaptureFixture[str],
2424
args_joined: bool,
2525
) -> None:
26-
result = virtualenv.cli_run([str(tmp_path / "venv"), "--activators", ""])
27-
cmd = [sys.executable]
26+
# Delete $PYTHONPATH so that it cannot be passed to the custom interpreter process (since we don't know what
27+
# distribution metadata to expect when it's used).
28+
monkeypatch.delenv("PYTHONPATH", False)
29+
2830
monkeypatch.chdir(tmp_path)
31+
result = virtualenv.cli_run([str(tmp_path / "venv"), "--activators", ""])
2932
py = str(result.creator.exe.relative_to(tmp_path))
30-
cmd += [f"--python={result.creator.exe}"] if args_joined else ["--python", py]
33+
cmd = ["", f"--python={result.creator.exe}"] if args_joined else ["", "--python", py]
3134
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
3235
main()
3336
out, _ = capfd.readouterr()
@@ -69,3 +72,37 @@ def test_custom_interpreter_with_local_only(
6972
if sys.version_info >= (3, 12):
7073
expected -= {"setuptools", "wheel"} # pragma: no cover
7174
assert found == expected, out
75+
76+
77+
def test_custom_interpreter_ensure_pythonpath_envar_is_honored(
78+
tmp_path: Path,
79+
mocker: MockerFixture,
80+
monkeypatch: pytest.MonkeyPatch,
81+
capfd: pytest.CaptureFixture[str],
82+
) -> None:
83+
# ensures that we honor $PYTHONPATH when passing it to the custom interpreter process
84+
venv_path = str(tmp_path / "venv")
85+
result = virtualenv.cli_run([venv_path, "--activators", ""])
86+
87+
another_path = tmp_path / "another-path"
88+
fake_dist = another_path / "foo-1.2.3.dist-info"
89+
fake_dist.mkdir(parents=True)
90+
fake_metadata = fake_dist / "METADATA"
91+
with fake_metadata.open("w") as f:
92+
f.write("Metadata-Version: 2.3\n" "Name: foo\n" "Version: 1.2.3\n")
93+
cmd = ["", f"--python={result.creator.exe}"]
94+
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
95+
monkeypatch.setenv("PYTHONPATH", str(another_path))
96+
main()
97+
out, _ = capfd.readouterr()
98+
found = {i.split("==")[0] for i in out.splitlines()}
99+
implementation = python_implementation()
100+
if implementation == "CPython":
101+
expected = {"foo", "pip", "setuptools", "wheel"}
102+
elif implementation == "PyPy": # pragma: cpython no cover
103+
expected = {"foo", "cffi", "greenlet", "pip", "readline", "setuptools", "wheel"}
104+
else: # pragma: no cover
105+
raise ValueError(implementation)
106+
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
107+
expected -= {"setuptools", "wheel"}
108+
assert found == expected, out

0 commit comments

Comments
 (0)