Skip to content

Commit

Permalink
Modify tests to ensure $PYTHONPATH is given to the custom interpreter (
Browse files Browse the repository at this point in the history
  • Loading branch information
kemzeb authored Apr 12, 2024
1 parent 25cbb6f commit bf5a865
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions tests/test_non_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ def test_custom_interpreter(
capfd: pytest.CaptureFixture[str],
args_joined: bool,
) -> None:
result = virtualenv.cli_run([str(tmp_path / "venv"), "--activators", ""])
cmd = [sys.executable]
# Delete $PYTHONPATH so that it cannot be passed to the custom interpreter process (since we don't know what
# distribution metadata to expect when it's used).
monkeypatch.delenv("PYTHONPATH", False)

monkeypatch.chdir(tmp_path)
result = virtualenv.cli_run([str(tmp_path / "venv"), "--activators", ""])
py = str(result.creator.exe.relative_to(tmp_path))
cmd += [f"--python={result.creator.exe}"] if args_joined else ["--python", py]
cmd = ["", f"--python={result.creator.exe}"] if args_joined else ["", "--python", py]
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
main()
out, _ = capfd.readouterr()
Expand Down Expand Up @@ -69,3 +72,37 @@ def test_custom_interpreter_with_local_only(
if sys.version_info >= (3, 12):
expected -= {"setuptools", "wheel"} # pragma: no cover
assert found == expected, out


def test_custom_interpreter_ensure_pythonpath_envar_is_honored(
tmp_path: Path,
mocker: MockerFixture,
monkeypatch: pytest.MonkeyPatch,
capfd: pytest.CaptureFixture[str],
) -> None:
# ensures that we honor $PYTHONPATH when passing it to the custom interpreter process
venv_path = str(tmp_path / "venv")
result = virtualenv.cli_run([venv_path, "--activators", ""])

another_path = tmp_path / "another-path"
fake_dist = another_path / "foo-1.2.3.dist-info"
fake_dist.mkdir(parents=True)
fake_metadata = fake_dist / "METADATA"
with fake_metadata.open("w") as f:
f.write("Metadata-Version: 2.3\n" "Name: foo\n" "Version: 1.2.3\n")
cmd = ["", f"--python={result.creator.exe}"]
mocker.patch("pipdeptree._discovery.sys.argv", cmd)
monkeypatch.setenv("PYTHONPATH", str(another_path))
main()
out, _ = capfd.readouterr()
found = {i.split("==")[0] for i in out.splitlines()}
implementation = python_implementation()
if implementation == "CPython":
expected = {"foo", "pip", "setuptools", "wheel"}
elif implementation == "PyPy": # pragma: cpython no cover
expected = {"foo", "cffi", "greenlet", "pip", "readline", "setuptools", "wheel"}
else: # pragma: no cover
raise ValueError(implementation)
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
expected -= {"setuptools", "wheel"}
assert found == expected, out

0 comments on commit bf5a865

Please sign in to comment.