Debian Codegraph @

tests/tox_env/python/pip/test_pip_install.py

raw
1 from __future__ import annotations
2
3 import os
4 from textwrap import dedent
5 from typing import TYPE_CHECKING, Any
6 from unittest.mock import Mock
7
8 import pytest
9 from packaging.requirements import Requirement
10
11 from tox.tox_env.errors import Fail
12
13 if TYPE_CHECKING:
14 from pathlib import Path
15
16 from tox.pytest import CaptureFixture, SubRequest, ToxProject, ToxProjectCreator
17
18
19 @pytest.mark.parametrize("arg", [object, [object]])
20 def test_pip_install_bad_type(tox_project: ToxProjectCreator, capfd: CaptureFixture, arg: Any) -> None:
21 proj = tox_project({"tox.ini": ""})
22 result = proj.run("l")
23 result.assert_success()
24 pip = result.state.envs["py"].installer
25
26 with pytest.raises(SystemExit, match="1"):
27 pip.install(arg, "section", "type")
28 out, err = capfd.readouterr()
29 assert not err
30 assert f"pip cannot install {object!r}" in out
31
32
33 def test_pip_install_empty_list(tox_project: ToxProjectCreator) -> None:
34 proj = tox_project({"tox.ini": ""})
35 result = proj.run("l")
36 result.assert_success()
37
38 pip = result.state.envs["py"].installer
39 execute_calls = proj.patch_execute(Mock())
40 pip.install([], "section", "type")
41 assert execute_calls.call_count == 0
42
43
44 def test_pip_install_empty_command_error(tox_project: ToxProjectCreator) -> None:
45 proj = tox_project({"tox.ini": "[testenv]\ninstall_command="})
46 result = proj.run("l")
47 pip = result.state.envs["py"].installer
48
49 with pytest.raises(Fail, match="unable to determine pip install command"):
50 pip.install([Requirement("name")], "section", "type")
51
52
53 def test_pip_install_flags_only_error(tox_project: ToxProjectCreator) -> None:
54 proj = tox_project({"tox.ini": "[testenv:py]\ndeps=-i a"})
55 result = proj.run("r")
56 result.assert_failed()
57 assert "no dependencies for tox env py within deps" in result.out
58
59
60 @pytest.mark.parametrize(
61 ("content", "error"),
62 [
63 pytest.param(
64 "-r requirements.txt",
65 "only constraints files or URLs can be provided",
66 id="requirements file are not constraints files",
67 ),
68 pytest.param(
69 "-c constraints.txt",
70 "only constraints files or URLs can be provided",
71 id="constraints flag is unnecessary",
72 ),
73 pytest.param("tox==4.23.0", "Could not open requirements file", id="constraints must be specified in a file"),
74 ],
75 )
76 def test_pip_install_invalid_constraints_error(tox_project: ToxProjectCreator, content: str, error: str) -> None:
77 proj = tox_project({"tox.ini": f"[testenv:py]\nconstraints={content}\nskip_install=true"})
78
79 result = proj.run("r")
80 result.assert_failed()
81 assert error in result.out
82
83
84 def test_pip_install_new_flag_recreates(tox_project: ToxProjectCreator) -> None:
85 proj = tox_project({"tox.ini": "[testenv:py]\ndeps=a\nskip_install=true"})
86 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
87
88 result = proj.run("r")
89 result.assert_success()
90
91 (proj.path / "tox.ini").write_text("[testenv:py]\ndeps=a\n -i i\nskip_install=true")
92 result_second = proj.run("r")
93 result_second.assert_success()
94 assert "recreate env because changed install flag(s) added index_url=['i']" in result_second.out
95 assert "install_deps> python -I -m pip install a -i i" in result_second.out
96
97
98 def test_pip_install_path(tox_project: ToxProjectCreator) -> None:
99 proj = tox_project({"tox.ini": "[testenv:py]\ndeps=.{/}a\nskip_install=true"})
100 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
101
102 result = proj.run("r")
103 result.assert_success()
104 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", f".{os.sep}a"]
105
106
107 @pytest.mark.parametrize(
108 ("content", "args"),
109 [
110 pytest.param("-e .", ["-e", "."], id="short editable"),
111 pytest.param("--editable .", ["-e", "."], id="long editable"),
112 pytest.param(
113 "git+ssh://git.example.com/MyProject\\#egg=MyProject",
114 ["git+ssh://git.example.com/MyProject#egg=MyProject"],
115 id="vcs with ssh",
116 ),
117 pytest.param(
118 "git+https://git.example.com/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709\\#egg=MyProject",
119 ["git+https://git.example.com/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709#egg=MyProject"],
120 id="vcs with commit hash pin",
121 ),
122 ],
123 )
124 def test_pip_install_req_file_req_like(tox_project: ToxProjectCreator, content: str, args: list[str]) -> None:
125 proj = tox_project({"tox.ini": f"[testenv:py]\ndeps={content}\nskip_install=true"})
126 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
127
128 result = proj.run("r")
129 result.assert_success()
130
131 assert execute_calls.call_count == 1
132 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", *args]
133
134 # check that adding a new dependency correctly finds the previous one
135 (proj.path / "tox.ini").write_text(f"[testenv:py]\ndeps={content}\n a\nskip_install=true")
136 execute_calls.reset_mock()
137
138 result_second = proj.run("r")
139 result_second.assert_success()
140 assert execute_calls.call_count == 1
141 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "a", *args]
142
143
144 def test_pip_req_path(tox_project: ToxProjectCreator) -> None:
145 proj = tox_project({"tox.ini": "[testenv:py]\ndeps=.\nskip_install=true"})
146 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
147
148 result = proj.run("r")
149 result.assert_success()
150
151 assert execute_calls.call_count == 1
152 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "."]
153
154
155 def test_deps_remove_recreate(tox_project: ToxProjectCreator) -> None:
156 proj = tox_project({"tox.ini": "[testenv]\npackage=skip\ndeps=wheel\n setuptools"})
157 execute_calls = proj.patch_execute(lambda request: 0) # noqa: ARG005
158 result_first = proj.run("r")
159 result_first.assert_success()
160 assert execute_calls.call_count == 1
161
162 (proj.path / "tox.ini").write_text("[testenv]\npackage=skip\ndeps=setuptools\n")
163 result_second = proj.run("r")
164 result_second.assert_success()
165 assert "py: recreate env because requirements removed: wheel" in result_second.out, result_second.out
166 assert execute_calls.call_count == 2
167
168
169 def test_pkg_dep_remove_recreate(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
170 build = (demo_pkg_inline / "build.py").read_text()
171 build_with_dep = build.replace("Summary: UNKNOWN\n", "Summary: UNKNOWN\n Requires-Dist: wheel\n")
172 proj = tox_project(
173 {
174 "tox.ini": "[testenv]\npackage=wheel",
175 "pyproject.toml": (demo_pkg_inline / "pyproject.toml").read_text(),
176 "build.py": build_with_dep,
177 },
178 )
179 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
180
181 result_first = proj.run("r")
182 result_first.assert_success()
183 run_ids = [i[0][3].run_id for i in execute_calls.call_args_list]
184 assert run_ids == [
185 "_optional_hooks",
186 "get_requires_for_build_wheel",
187 "build_wheel",
188 "install_package_deps",
189 "install_package",
190 "_exit",
191 ]
192 execute_calls.reset_mock()
193
194 (proj.path / "build.py").write_text(build)
195 result_second = proj.run("r")
196 result_second.assert_success()
197 assert "py: recreate env because dependencies removed: wheel" in result_second.out, result_second.out
198 run_ids = [i[0][3].run_id for i in execute_calls.call_args_list]
199 assert run_ids == ["_optional_hooks", "get_requires_for_build_wheel", "build_wheel", "install_package", "_exit"]
200
201
202 def test_force_dep_applies_to_package_deps(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
203 build = (demo_pkg_inline / "build.py").read_text()
204 build_with_dep = build.replace("Summary: UNKNOWN\n", "Summary: UNKNOWN\n Requires-Dist: wheel\n")
205 proj = tox_project(
206 {
207 "tox.ini": "[testenv]\npackage=wheel",
208 "pyproject.toml": (demo_pkg_inline / "pyproject.toml").read_text(),
209 "build.py": build_with_dep,
210 },
211 )
212 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
213 result = proj.run("le", "-e", "py", "--force-dep", "wheel==0.41.0")
214 result.assert_success()
215
216 pkg_deps_calls = [i for i in execute_calls.call_args_list if i[0][3].run_id == "install_package_deps"]
217 assert len(pkg_deps_calls) == 1
218 cmd = pkg_deps_calls[0][0][3].cmd
219 assert "wheel==0.41.0" in cmd
220
221
222 def test_pkg_env_dep_remove_recreate(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
223 toml = (demo_pkg_inline / "pyproject.toml").read_text()
224 proj = tox_project(
225 {
226 "tox.ini": "[testenv]\npackage=wheel",
227 "pyproject.toml": toml.replace("requires = []", 'requires = ["setuptools"]'),
228 "build.py": (demo_pkg_inline / "build.py").read_text(),
229 },
230 )
231 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
232 result_first = proj.run("r")
233 result_first.assert_success()
234 run_ids = [i[0][3].run_id for i in execute_calls.call_args_list]
235 assert run_ids == [
236 "install_requires",
237 "_optional_hooks",
238 "get_requires_for_build_wheel",
239 "build_wheel",
240 "install_package",
241 "_exit",
242 ]
243 execute_calls.reset_mock()
244
245 (proj.path / "pyproject.toml").write_text(toml)
246 result_second = proj.run("r")
247 result_second.assert_success()
248 assert ".pkg: recreate env because dependencies removed: setuptools" in result_second.out, result_second.out
249 run_ids = [i[0][3].run_id for i in execute_calls.call_args_list]
250 assert run_ids == ["_optional_hooks", "get_requires_for_build_wheel", "build_wheel", "install_package", "_exit"]
251
252
253 def test_pip_install_requirements_file_deps(tox_project: ToxProjectCreator) -> None:
254 proj = tox_project({"tox.ini": "[testenv]\ndeps=-r r.txt\nskip_install=true", "r.txt": "a"})
255 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
256 result = proj.run("r")
257 result.assert_success()
258 assert execute_calls.call_count == 1
259 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "-r", "r.txt"]
260
261 # check that adding a new dependency correctly finds the previous one
262 (proj.path / "tox.ini").write_text("[testenv]\ndeps=-r r.txt\n b\nskip_install=true")
263 execute_calls.reset_mock()
264 result_second = proj.run("r")
265 result_second.assert_success()
266 assert execute_calls.call_count == 1
267 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "b", "-r", "r.txt"]
268
269 # if the requirement file changes recreate
270 (proj.path / "r.txt").write_text("c\nd")
271 execute_calls.reset_mock()
272 result_third = proj.run("r")
273 result_third.assert_success()
274 assert "py: recreate env because requirements removed: a" in result_third.out, result_third.out
275 assert execute_calls.call_count == 1
276 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "b", "-r", "r.txt"]
277
278
279 @pytest.fixture(params=[True, False])
280 def use_constraints_opt(request: SubRequest) -> bool:
281 return bool(request.param)
282
283
284 def test_pip_install_constraint_file_create_change(tox_project: ToxProjectCreator, use_constraints_opt: bool) -> None:
285 if use_constraints_opt:
286 toxini = "[testenv]\ndeps=a\nconstraints=c.txt\nskip_install=true"
287 else:
288 toxini = "[testenv]\ndeps=-c c.txt\n a\nskip_install=true"
289 proj = tox_project({"tox.ini": toxini, "c.txt": "b"})
290 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
291 result = proj.run("r")
292 result.assert_success()
293 assert execute_calls.call_count == 1
294 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "a", "-c", "c.txt"]
295
296 # a new dependency triggers an install
297 if use_constraints_opt:
298 toxini = "[testenv]\ndeps=a\n d\nconstraints=c.txt\nskip_install=true"
299 else:
300 toxini = "[testenv]\ndeps=-c c.txt\n a\n d\nskip_install=true"
301 (proj.path / "tox.ini").write_text(toxini)
302 execute_calls.reset_mock()
303 result_second = proj.run("r")
304 result_second.assert_success()
305 assert execute_calls.call_count == 1
306 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "a", "d", "-c", "c.txt"]
307
308 # a new constraints triggers a recreate
309 (proj.path / "c.txt").write_text("")
310 execute_calls.reset_mock()
311 result_third = proj.run("r")
312 result_third.assert_success()
313 assert "py: recreate env because changed constraint(s) removed b" in result_third.out, result_third.out
314 assert execute_calls.call_count == 1
315 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "a", "d", "-c", "c.txt"]
316
317
318 def test_pip_install_constraint_file_new(tox_project: ToxProjectCreator, use_constraints_opt: bool) -> None:
319 proj = tox_project({"tox.ini": "[testenv]\ndeps=a\nskip_install=true"})
320 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
321 result = proj.run("r")
322 result.assert_success()
323 assert execute_calls.call_count == 1
324 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "a"]
325
326 if use_constraints_opt:
327 toxini = "[testenv]\ndeps=a\nconstraints=c.txt\nskip_install=true"
328 else:
329 toxini = "[testenv]\ndeps=a\n -c c.txt\nskip_install=true"
330 (proj.path / "c.txt").write_text("a")
331 (proj.path / "tox.ini").write_text(toxini)
332 execute_calls.reset_mock()
333 result_second = proj.run("r")
334 result_second.assert_success()
335 assert "py: recreate env because changed constraint(s) added a" in result_second.out, result_second.out
336 assert execute_calls.call_count == 1
337 assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "a", "-c", "c.txt"]
338
339
340 @pytest.fixture(params=[True, False])
341 def constrain_package_deps(request: SubRequest) -> bool:
342 return bool(request.param)
343
344
345 @pytest.fixture(params=[True, False])
346 def use_frozen_constraints(request: SubRequest) -> bool:
347 return bool(request.param)
348
349
350 @pytest.fixture(
351 params=[
352 "explicit",
353 "requirements",
354 "constraints",
355 "explicit+requirements",
356 "requirements_indirect",
357 "requirements_constraints_indirect",
358 ],
359 )
360 def constrained_mock_project(
361 request: SubRequest,
362 tox_project: ToxProjectCreator,
363 demo_pkg_inline: Path,
364 constrain_package_deps: bool,
365 use_frozen_constraints: bool,
366 ) -> tuple[ToxProject, list[str]]:
367 toml = (demo_pkg_inline / "pyproject.toml").read_text()
368 files = {
369 "pyproject.toml": toml.replace("requires = []", 'requires = ["setuptools"]')
370 + '\n[project]\nname = "demo"\nversion = "0.1"\ndependencies = ["foo > 2"]',
371 "build.py": (demo_pkg_inline / "build.py").read_text(),
372 }
373 exp_constraints: list[str] = []
374 requirement = "coo==1.2.3"
375 constraint = "coo<2"
376 if request.param == "explicit":
377 deps = requirement
378 exp_constraints.append(requirement)
379 elif request.param == "requirements":
380 files["requirements.txt"] = f"--pre\n{requirement}"
381 deps = "-rrequirements.txt"
382 exp_constraints.append(requirement)
383 elif request.param == "constraints":
384 files["constraints.txt"] = constraint
385 deps = "-cconstraints.txt"
386 exp_constraints.append(constraint)
387 elif request.param == "explicit+requirements":
388 files["requirements.txt"] = f"--pre\n{requirement}"
389 deps = "\n\t-rrequirements.txt\n\tfoo"
390 exp_constraints.extend(["foo", requirement])
391 elif request.param == "requirements_indirect":
392 files["foo.requirements.txt"] = f"--pre\n{requirement}"
393 files["requirements.txt"] = "-r foo.requirements.txt"
394 deps = "-rrequirements.txt"
395 exp_constraints.append(requirement)
396 elif request.param == "requirements_constraints_indirect":
397 files["foo.requirements.txt"] = f"--pre\n{requirement}"
398 files["foo.constraints.txt"] = f"{constraint}"
399 files["requirements.txt"] = "-r foo.requirements.txt\n-c foo.constraints.txt"
400 deps = "-rrequirements.txt"
401 exp_constraints.extend([requirement, constraint])
402 else: # pragma: no cover
403 pytest.fail(f"Missing case: {request.param}")
404 files["tox.ini"] = (
405 "[testenv]\npackage=wheel\n"
406 f"constrain_package_deps = {constrain_package_deps}\n"
407 f"use_frozen_constraints = {use_frozen_constraints}\n"
408 f"deps = {deps}"
409 )
410 return tox_project(files), exp_constraints if constrain_package_deps else []
411
412
413 def test_constrain_package_deps(
414 constrained_mock_project: tuple[ToxProject, list[str]],
415 constrain_package_deps: bool,
416 use_frozen_constraints: bool,
417 ) -> None:
418 proj, exp_constraints = constrained_mock_project
419 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
420 result_first = proj.run("r")
421 result_first.assert_success()
422 exp_run_ids = ["install_deps"]
423 if constrain_package_deps and use_frozen_constraints:
424 exp_run_ids.append("freeze")
425 exp_run_ids.extend(
426 [
427 "install_requires",
428 "_optional_hooks",
429 "get_requires_for_build_wheel",
430 "build_wheel",
431 "install_package_deps",
432 "install_package",
433 "_exit",
434 ],
435 )
436 run_ids = [i[0][3].run_id for i in execute_calls.call_args_list]
437 assert run_ids == exp_run_ids
438 constraints_file = proj.path / ".tox" / "py" / "constraints.txt"
439 if constrain_package_deps:
440 constraints = constraints_file.read_text().splitlines()
441 for call in execute_calls.call_args_list:
442 if call[0][3].run_id == "install_package_deps":
443 assert f"-c{constraints_file}" in call[0][3].cmd
444 if use_frozen_constraints:
445 for c in exp_constraints:
446 # when using frozen constraints with this mock, the mock package does NOT
447 # actually end up in the constraints, so assert it's not there
448 assert c not in constraints
449 for c in constraints:
450 assert c.partition("==")[0] in {"pip", "setuptools", "wheel"}
451 else:
452 for c in constraints:
453 assert c in exp_constraints
454 for c in exp_constraints:
455 assert c in constraints
456 else:
457 assert not constraints_file.exists()
458
459
460 def test_pip_resolution_env_var_change_reinstalls(tox_project: ToxProjectCreator) -> None:
461 proj = tox_project({
462 "tox.ini": """
463 [testenv:py]
464 deps = a
465 skip_install = true
466 """,
467 })
468 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
469
470 result = proj.run("r")
471 result.assert_success()
472 assert execute_calls.call_count == 1
473 execute_calls.reset_mock()
474
475 (proj.path / "tox.ini").write_text(
476 dedent("""
477 [testenv:py]
478 deps = a
479 skip_install = true
480 setenv = PIP_INDEX_URL = https://example.com
481 """)
482 )
483 result = proj.run("r")
484 result.assert_success()
485 assert execute_calls.call_count == 1
486
487
488 def test_pip_non_resolution_env_var_change_no_reinstall(tox_project: ToxProjectCreator) -> None:
489 proj = tox_project({
490 "tox.ini": """
491 [testenv:py]
492 deps = a
493 skip_install = true
494 setenv = PIP_TIMEOUT = 30
495 """,
496 })
497 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
498
499 result = proj.run("r")
500 result.assert_success()
501 assert execute_calls.call_count == 1
502 execute_calls.reset_mock()
503
504 (proj.path / "tox.ini").write_text(
505 dedent("""
506 [testenv:py]
507 deps = a
508 skip_install = true
509 setenv = PIP_TIMEOUT = 60
510 """)
511 )
512 result = proj.run("r")
513 result.assert_success()
514 assert execute_calls.call_count == 0
515
516
517 @pytest.mark.parametrize("conf_key", ["constrain_package_deps", "use_frozen_constraints"])
518 def test_change_constraint_options_recreates(tox_project: ToxProjectCreator, conf_key: str) -> None:
519 tox_ini_content = "[testenv:py]\ndeps=a\nskip_install=true"
520 proj = tox_project({"tox.ini": f"{tox_ini_content}\n{conf_key} = true"})
521 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
522
523 result = proj.run("r")
524 result.assert_success()
525
526 (proj.path / "tox.ini").write_text(f"{tox_ini_content}\n{conf_key} = false")
527 result_second = proj.run("r")
528 result_second.assert_success()
529 assert "recreate env because constraint options changed" in result_second.out
530 assert conf_key in result_second.out