-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathtest_cli.py
96 lines (82 loc) · 2.63 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Basic CLI functionality checks."""
import subprocess
import sys
from pathlib import Path
from textwrap import dedent
import pytest
from pip._internal.commands import commands_dict
from tests.lib import PipTestEnvironment
@pytest.mark.parametrize(
"entrypoint",
[
("fake_pip = pip._internal.main:main",),
("fake_pip = pip._internal:main",),
("fake_pip = pip:main",),
],
)
def test_entrypoints_work(entrypoint: str, script: PipTestEnvironment) -> None:
if script.zipapp:
pytest.skip("Zipapp does not include entrypoints")
fake_pkg = script.scratch_path / "fake_pkg"
fake_pkg.mkdir()
fake_pkg.joinpath("setup.py").write_text(
dedent(
f"""
from setuptools import setup
setup(
name="fake-pip",
version="0.1.0",
entry_points={{
"console_scripts": [
{entrypoint!r}
]
}}
)
"""
)
)
# expect_temp because pip install will generate fake_pkg.egg-info
script.pip("install", "-vvv", str(fake_pkg), expect_temp=True)
result = script.pip("-V")
result2 = script.run("fake_pip", "-V", allow_stderr_warning=True)
assert result.stdout == result2.stdout
assert "old script wrapper" in result2.stderr
@pytest.mark.parametrize(
"command",
sorted(
set(commands_dict).symmetric_difference(
# Exclude commands that are expected to use the network.
{"install", "download", "search", "index", "wheel"}
)
),
)
def test_no_network_imports(command: str, tmp_path: Path) -> None:
"""
Verify that commands that don't access the network do NOT import network code.
This helps to reduce the startup time of these commands.
Note: This won't catch lazy network imports, but it'll catch top-level
network imports which were accidentally added (which is the most likely way
to regress anyway).
"""
file = tmp_path / f"imported_modules_for_{command}.txt"
code = f"""
import runpy
import sys
sys.argv[1:] = [{command!r}, "--help"]
try:
runpy.run_module("pip", alter_sys=True, run_name="__main__")
finally:
with open({str(file)!r}, "w") as f:
print(*sys.modules.keys(), sep="\\n", file=f)
"""
subprocess.run(
[sys.executable],
input=code,
encoding="utf-8",
check=True,
)
imported = file.read_text().splitlines()
assert not any("pip._internal.index" in mod for mod in imported)
assert not any("pip._internal.network" in mod for mod in imported)
assert not any("requests" in mod for mod in imported)
assert not any("urllib3" in mod for mod in imported)