Skip to content

Commit

Permalink
Print resolved interpreter when using env detection (#379)
Browse files Browse the repository at this point in the history
Signed-off-by: Stanislav Filin <stasfilin@hotmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Kemal Zebari <60799661+kemzeb@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 17, 2024
1 parent 07503eb commit c2245d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/pipdeptree/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def main(args: Sequence[str] | None = None) -> None | int:
warning_printer.warning_type = options.warn

if options.python == "auto":
options.python = detect_active_interpreter()
resolved_path = detect_active_interpreter()
options.python = resolved_path
print(f"(resolved python: {resolved_path})", file=sys.stderr) # noqa: T201

pkgs = get_installed_distributions(
interpreter=options.python, local_only=options.local_only, user_only=options.user_only
Expand Down
26 changes: 25 additions & 1 deletion tests/test_pipdeptree.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from __future__ import annotations

import sys
from subprocess import check_call # noqa: S404
from subprocess import CompletedProcess, check_call # noqa: S404
from typing import TYPE_CHECKING

from pipdeptree.__main__ import main

if TYPE_CHECKING:
from pathlib import Path

import pytest
from pytest_console_scripts import ScriptRunner
from pytest_mock import MockFixture


def test_main() -> None:
Expand All @@ -15,3 +21,21 @@ def test_main() -> None:
def test_console(script_runner: ScriptRunner) -> None:
result = script_runner.run(["pipdeptree", "--help"])
assert result.success


def test_main_log_resolved(tmp_path: Path, mocker: MockFixture, capsys: pytest.CaptureFixture[str]) -> None:
mocker.patch("sys.argv", ["", "--python", "auto"])
mocker.patch("pipdeptree.__main__.detect_active_interpreter", return_value=str(tmp_path))
mock_subprocess_run = mocker.patch("subprocess.run")
valid_sys_path = str([str(tmp_path)])
mock_subprocess_run.return_value = CompletedProcess(
args=["python", "-c", "import sys; print(sys.path)"],
returncode=0,
stdout=valid_sys_path,
stderr="",
)

main()

captured = capsys.readouterr()
assert captured.err.startswith(f"(resolved python: {tmp_path!s}")

0 comments on commit c2245d6

Please sign in to comment.