Print resolved interpreter when using env detection (#379)

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>
This commit is contained in:
Stanislav Filin 2024-06-17 21:44:58 +02:00 committed by GitHub
parent 07503eb294
commit c2245d6050
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -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

View File

@ -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:
@ -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}")