Skip to content

Commit

Permalink
add testcases for --python with --local-only
Browse files Browse the repository at this point in the history
  • Loading branch information
xiacunshun committed Apr 7, 2024
1 parent a73123a commit a1b013d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/pipdeptree/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ def get_installed_distributions(
if user_only:
original_dists = distributions(path=[site.getusersitepackages()])
elif using_custom_interpreter:
# We query the interpreter directly to get the `path` which is used by distributions(),
# so we can search the customer dists with it.
# If --python and --local-only are given at the same time, it means the dists only in
# customer's env are needed. (.e.g --system-site-packages is given when creating venv)
# We query the interpreter directly to get its `sys.path` list to be used by `distributions()`.
# If --python and --local-only are given, we ensure that we are only using paths associated to the
# interpreter's environment.
if local_only:
args = "import sys, site; print(site.getsitepackages([sys.prefix]))"
cmd = "import sys, site; print(site.getsitepackages([sys.prefix]))"
else:
args = "import sys; print(sys.path)"
cmd = "import sys; print(sys.path)"

cmd = [str(py_path), "-c", args]
result = subprocess.run(cmd, stdout=subprocess.PIPE, check=False) # noqa: S603
args = [str(py_path), "-c", cmd]
result = subprocess.run(args, stdout=subprocess.PIPE, check=False) # noqa: S603
original_dists = distributions(path=ast.literal_eval(result.stdout.decode("utf-8")))
elif local_only and in_venv:
venv_site_packages = site.getsitepackages([sys.prefix])
Expand Down
27 changes: 27 additions & 0 deletions tests/test_non_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,30 @@ def test_custom_interpreter(
if sys.version_info >= (3, 12):
expected -= {"setuptools", "wheel"}
assert found == expected, out


def test_custom_interpreter_with_local_only(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capfd: pytest.CaptureFixture[str],
) -> None:
venv_path = str(tmp_path / "venv")

result = virtualenv.cli_run([venv_path, "--system-site-packages", "--activators", ""])

cmd = ["", f"--python={result.creator.exe}", "--local-only"]
monkeypatch.setattr(sys, "prefix", venv_path)
monkeypatch.setattr(sys, "argv", cmd)
main()
out, _ = capfd.readouterr()
found = {i.split("==")[0] for i in out.splitlines()}
implementation = python_implementation()
if implementation == "CPython":
expected = {"pip", "setuptools", "wheel"}
elif implementation == "PyPy": # pragma: no cover
expected = {"cffi", "greenlet", "pip", "readline", "setuptools", "wheel"} # pragma: no cover
else:
raise ValueError(implementation) # pragma: no cover
if sys.version_info >= (3, 12):
expected -= {"setuptools", "wheel"} # pragma: no cover
assert found == expected, out

0 comments on commit a1b013d

Please sign in to comment.