1
+ import os
2
+ from pathlib import Path
1
3
from typing import Optional , Tuple
4
+ from venv import EnvBuilder
2
5
3
6
import pytest
4
7
5
8
from pip ._internal .cli .cmdoptions import _convert_python_version
9
+ from pip ._internal .cli .main_parser import identify_python_interpreter
6
10
7
11
8
12
@pytest .mark .parametrize (
@@ -29,3 +33,30 @@ def test_convert_python_version(
29
33
) -> None :
30
34
actual = _convert_python_version (value )
31
35
assert actual == expected , f"actual: { actual !r} "
36
+
37
+
38
+ def test_identify_python_interpreter_py (monkeypatch : pytest .MonkeyPatch ) -> None :
39
+ def which (cmd : str ) -> str :
40
+ assert cmd == "py" or cmd == "python"
41
+ return "dummy_value"
42
+
43
+ monkeypatch .setattr ("shutil.which" , which )
44
+ assert identify_python_interpreter ("py" ) == "dummy_value"
45
+ assert identify_python_interpreter ("python" ) == "dummy_value"
46
+
47
+
48
+ def test_identify_python_interpreter_venv (tmpdir : Path ) -> None :
49
+ env_path = tmpdir / "venv"
50
+ env = EnvBuilder (with_pip = False )
51
+ env .create (env_path )
52
+
53
+ # Passing a virtual environment returns the Python executable
54
+ interp = identify_python_interpreter (os .fsdecode (env_path ))
55
+ assert interp is not None
56
+ assert Path (interp ).exists ()
57
+
58
+ # Passing an executable returns it
59
+ assert identify_python_interpreter (interp ) == interp
60
+
61
+ # Passing a non-existent file returns None
62
+ assert identify_python_interpreter (str (tmpdir / "nonexistent" )) is None
0 commit comments