Skip to content

Commit b5ba32f

Browse files
committed
add testcases for --python with --local-only
1 parent a73123a commit b5ba32f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/pipdeptree/_discovery.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@ def get_installed_distributions(
2525
if user_only:
2626
original_dists = distributions(path=[site.getusersitepackages()])
2727
elif using_custom_interpreter:
28-
# We query the interpreter directly to get the `path` which is used by distributions(),
29-
# so we can search the customer dists with it.
30-
# If --python and --local-only are given at the same time, it means the dists only in
31-
# customer's env are needed. (.e.g --system-site-packages is given when creating venv)
28+
# We query the interpreter directly to get its `sys.path` list to be used by `distributions()`.
29+
# If --python and --local-only are given, we ensure that we are only using paths associated to the interpreter's
30+
# environment.
3231
if local_only:
33-
args = "import sys, site; print(site.getsitepackages([sys.prefix]))"
32+
cmd = "import sys; print([p for p in sys.path if p.startswith(sys.prefix)])"
3433
else:
35-
args = "import sys; print(sys.path)"
34+
cmd = "import sys; print(sys.path)"
3635

37-
cmd = [str(py_path), "-c", args]
38-
result = subprocess.run(cmd, stdout=subprocess.PIPE, check=False) # noqa: S603
36+
args = [str(py_path), "-c", cmd]
37+
result = subprocess.run(args, stdout=subprocess.PIPE, check=False) # noqa: S603
3938
original_dists = distributions(path=ast.literal_eval(result.stdout.decode("utf-8")))
4039
elif local_only and in_venv:
41-
venv_site_packages = site.getsitepackages([sys.prefix])
40+
venv_site_packages = [p for p in sys.path if p.startswith(sys.prefix)]
4241
original_dists = distributions(path=venv_site_packages)
4342
else:
4443
original_dists = distributions()

tests/test_discovery.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def test_local_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capfd: pyte
2828

2929
cmd = [str(result.creator.exe.parent / "python3"), "--local-only"]
3030
monkeypatch.setattr(sys, "prefix", venv_path)
31+
for s in venv_site_packages:
32+
monkeypatch.syspath_prepend(s)
3133
monkeypatch.setattr(sys, "argv", cmd)
3234
main()
3335
out, _ = capfd.readouterr()

tests/test_non_host.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,30 @@ def test_custom_interpreter(
3939
if sys.version_info >= (3, 12):
4040
expected -= {"setuptools", "wheel"}
4141
assert found == expected, out
42+
43+
44+
def test_custom_interpreter_with_local_only(
45+
tmp_path: Path,
46+
monkeypatch: pytest.MonkeyPatch,
47+
capfd: pytest.CaptureFixture[str],
48+
) -> None:
49+
venv_path = str(tmp_path / "venv")
50+
51+
result = virtualenv.cli_run([venv_path, "--system-site-packages", "--activators", ""])
52+
53+
cmd = ["", f"--python={result.creator.exe}", "--local-only"]
54+
monkeypatch.setattr(sys, "prefix", venv_path)
55+
monkeypatch.setattr(sys, "argv", cmd)
56+
main()
57+
out, _ = capfd.readouterr()
58+
found = {i.split("==")[0] for i in out.splitlines()}
59+
implementation = python_implementation()
60+
if implementation == "CPython":
61+
expected = {"pip", "setuptools", "wheel"}
62+
elif implementation == "PyPy": # pragma: no cover
63+
expected = {"cffi", "greenlet", "pip", "readline", "setuptools", "wheel"} # pragma: no cover
64+
else:
65+
raise ValueError(implementation) # pragma: no cover
66+
if sys.version_info >= (3, 12):
67+
expected -= {"setuptools", "wheel"} # pragma: no cover
68+
assert found == expected, out

0 commit comments

Comments
 (0)