Skip to content

Commit 01e122e

Browse files
committed
Add tests
1 parent 24c22a3 commit 01e122e

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
import os
3+
from pathlib import Path
4+
from venv import EnvBuilder
5+
6+
from tests.lib import PipTestEnvironment, TestData
7+
8+
9+
def test_python_interpreter(
10+
script: PipTestEnvironment,
11+
tmpdir: Path,
12+
shared_data: TestData,
13+
) -> None:
14+
env_path = os.fsdecode(tmpdir / "venv")
15+
env = EnvBuilder(with_pip=False)
16+
env.create(env_path)
17+
18+
result = script.pip("--python", env_path, "list", "--format=json")
19+
assert json.loads(result.stdout) == []
20+
script.pip(
21+
"--python",
22+
env_path,
23+
"install",
24+
"-f",
25+
shared_data.find_links,
26+
"--no-index",
27+
"simplewheel==1.0",
28+
)
29+
result = script.pip("--python", env_path, "list", "--format=json")
30+
assert json.loads(result.stdout) == [{"name": "simplewheel", "version": "1.0"}]
31+
script.pip("--python", env_path, "uninstall", "simplewheel", "--yes")
32+
result = script.pip("--python", env_path, "list", "--format=json")
33+
assert json.loads(result.stdout) == []

tests/unit/test_cmdoptions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import os
2+
from pathlib import Path
13
from typing import Optional, Tuple
4+
from venv import EnvBuilder
25

36
import pytest
47

58
from pip._internal.cli.cmdoptions import _convert_python_version
9+
from pip._internal.cli.main_parser import identify_python_interpreter
610

711

812
@pytest.mark.parametrize(
@@ -29,3 +33,30 @@ def test_convert_python_version(
2933
) -> None:
3034
actual = _convert_python_version(value)
3135
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

Comments
 (0)