Debian Codegraph @

tests/tox_env/python/virtual_env/package/test_package_pyproject.py

raw
1 from __future__ import annotations
2
3 import io
4 import json
5 import tarfile
6 from textwrap import dedent
7 from typing import TYPE_CHECKING, Any
8
9 import pytest
10
11 from tox.execute.local_sub_process import LocalSubprocessExecuteStatus
12 from tox.tox_env.python.virtual_env.package.pyproject import Pep517VirtualEnvFrontend
13
14 if TYPE_CHECKING:
15 from pathlib import Path
16
17 from pytest_mock import MockerFixture
18
19 from tox.pytest import ToxProjectCreator
20
21
22 @pytest.mark.parametrize(
23 "pkg_type",
24 ["editable-legacy", "editable", "sdist", "sdist-wheel", "wheel"],
25 )
26 def test_tox_ini_package_type_valid(tox_project: ToxProjectCreator, pkg_type: str) -> None:
27 proj = tox_project({"tox.ini": f"[testenv]\npackage={pkg_type}", "pyproject.toml": ""})
28 result = proj.run("c", "-k", "package_tox_env_type")
29 result.assert_success()
30 res = result.env_conf("py")["package"]
31 assert res == pkg_type
32 got_type = result.env_conf("py")["package_tox_env_type"]
33 assert got_type == "virtualenv-pep-517"
34
35
36 def test_tox_ini_package_type_invalid(tox_project: ToxProjectCreator) -> None:
37 proj = tox_project({"tox.ini": "[testenv]\npackage=bad", "pyproject.toml": ""})
38 result = proj.run("c", "-k", "package_tox_env_type")
39 result.assert_failed()
40 msg = (
41 " invalid package config type bad requested,"
42 " must be one of wheel, sdist, sdist-wheel, editable, editable-legacy, deps-only, skip"
43 )
44 assert msg in result.out
45
46
47 def test_get_package_deps_different_extras(pkg_with_extras_project: Path, tox_project: ToxProjectCreator) -> None:
48 ini = "[testenv:a]\npackage=editable-legacy\nextras=docs\n[testenv:b]\npackage=sdist\nextras=format"
49 proj = tox_project({"tox.ini": ini})
50 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
51 result = proj.run("r", "--root", str(pkg_with_extras_project), "-e", "a,b")
52 result.assert_success()
53 installs = {
54 i[0][0].conf.name: i[0][3].cmd[5:]
55 for i in execute_calls.call_args_list
56 if i[0][3].run_id.startswith("install_package_deps")
57 }
58 assert installs == {
59 "a": [
60 "colorama>=0.4.6",
61 "platformdirs>=4.3.8",
62 "setuptools",
63 "sphinx-rtd-theme<1,>=0.4.3",
64 "sphinx>=3",
65 ],
66 "b": ["black>=3", "colorama>=0.4.6", "flake8", "platformdirs>=4.3.8"],
67 }
68
69
70 def test_package_root_via_root(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
71 ini = f"[tox]\npackage_root={demo_pkg_inline}\n[testenv]\npackage=wheel\nwheel_build_env=.pkg"
72 proj = tox_project({"tox.ini": ini, "pyproject.toml": ""})
73 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
74 result = proj.run("r", "--notest")
75 result.assert_success()
76
77
78 def test_package_root_via_testenv(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
79 ini = f"[testenv]\npackage=wheel\nwheel_build_env=.pkg\npackage_root={demo_pkg_inline}"
80 proj = tox_project({"tox.ini": ini, "pyproject.toml": ""})
81 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
82 result = proj.run("r", "--notest")
83 result.assert_success()
84
85
86 @pytest.mark.parametrize(
87 ("conf", "extra", "deps"),
88 [
89 pytest.param("[project]", "", [], id="no_deps"),
90 pytest.param("[project]\ndependencies=['B', 'A']", "", ["A", "B"], id="deps"),
91 pytest.param(
92 "[project]\ndependencies=['A']\noptional-dependencies.alpha=['B']\noptional-dependencies.beta=['C']",
93 "alpha",
94 ["A", "B"],
95 id="deps_with_one_extra",
96 ),
97 pytest.param(
98 "[project]\ndependencies=['A']\noptional-dependencies.alpha=['B']\noptional-dependencies.beta=['C']",
99 "alpha,beta",
100 ["A", "B", "C"],
101 id="deps_with_two_extra",
102 ),
103 pytest.param(
104 "[project]\ndependencies=['A']\noptional-dependencies.alpha=[]",
105 "alpha",
106 ["A"],
107 id="deps_with_empty_extra",
108 ),
109 pytest.param(
110 "[project]\ndependencies=['A']\ndynamic=['optional-dependencies']",
111 "",
112 ["A"],
113 id="deps_with_dynamic_optional_no_extra",
114 ),
115 pytest.param(
116 dedent(
117 """
118 [project]
119 name='foo'
120 dependencies=['foo[alpha]']
121 optional-dependencies.alpha=['A']""",
122 ),
123 "",
124 ["A"],
125 id="deps_reference_extra",
126 ),
127 pytest.param(
128 dedent(
129 """
130 [project]
131 name='foo'
132 dependencies=['A']
133 optional-dependencies.alpha=['B']
134 optional-dependencies.beta=['foo[alpha]']""",
135 ),
136 "beta",
137 ["A", "B"],
138 id="deps_with_recursive_extra",
139 ),
140 pytest.param(
141 dedent(
142 """
143 [project]
144 name='foo'
145 dependencies=['A']
146 optional-dependencies.alpha=['B']
147 optional-dependencies.beta=['foo[alpha]']
148 optional-dependencies.delta=['foo[beta]', 'D']""",
149 ),
150 "delta",
151 ["A", "B", "D"],
152 id="deps_with_two_recursive_extra",
153 ),
154 pytest.param(
155 dedent(
156 """
157 [project]
158 name='foo'
159 optional-dependencies.alpha=['foo[beta]', 'A']
160 optional-dependencies.beta=['foo[alpha]', 'B']""",
161 ),
162 "alpha",
163 ["A", "B"],
164 id="deps_with_circular_recursive_extra",
165 ),
166 ],
167 )
168 def test_pyproject_deps_from_static(
169 tox_project: ToxProjectCreator,
170 demo_pkg_inline: Path,
171 conf: str,
172 extra: str,
173 deps: list[str],
174 ) -> None:
175 toml = f"{(demo_pkg_inline / 'pyproject.toml').read_text()}{conf}"
176 proj = tox_project({"tox.ini": f"[testenv]\nextras={extra}", "pyproject.toml": toml}, base=demo_pkg_inline)
177 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
178 result = proj.run("r", "--notest")
179 result.assert_success()
180
181 expected_calls = [(".pkg", "_optional_hooks"), (".pkg", "get_requires_for_build_sdist"), (".pkg", "build_sdist")]
182 if deps:
183 expected_calls.append(("py", "install_package_deps"))
184 expected_calls.extend((("py", "install_package"), (".pkg", "_exit")))
185 found_calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list]
186 assert found_calls == expected_calls
187
188 if deps:
189 expected_args = ["python", "-I", "-m", "pip", "install", *deps]
190 args = execute_calls.call_args_list[-3][0][3].cmd
191 assert expected_args == args
192
193
194 def test_pyproject_invalid_extra_static(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
195 conf = "[project]\nname='demo'\ndependencies=['A']\noptional-dependencies.alpha=['B']"
196 toml = f"{(demo_pkg_inline / 'pyproject.toml').read_text()}{conf}"
197 proj = tox_project({"tox.ini": "[testenv]\nextras=typo", "pyproject.toml": toml}, base=demo_pkg_inline)
198 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
199 result = proj.run("r", "--notest")
200 result.assert_failed()
201 assert "extras not found for package demo: typo (available: alpha)" in result.out
202
203
204 def test_pyproject_invalid_extra_no_optional_deps(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
205 conf = "[project]\nname='demo'"
206 toml = f"{(demo_pkg_inline / 'pyproject.toml').read_text()}{conf}"
207 proj = tox_project({"tox.ini": "[testenv]\nextras=alpha", "pyproject.toml": toml}, base=demo_pkg_inline)
208 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
209 result = proj.run("r", "--notest")
210 result.assert_failed()
211 assert "extras not found for package demo: alpha (available: none)" in result.out
212
213
214 @pytest.mark.parametrize(
215 ("metadata", "dynamic", "extra", "deps"),
216 [
217 pytest.param("Requires-Dist: A", "['dependencies']", "", ["A"], id="deps"),
218 pytest.param(
219 "Provides-Extra: alpha\nRequires-Dist: A\nRequires-Dist: B;extra=='alpha'",
220 "['dependencies']",
221 "alpha",
222 ["A", "B"],
223 id="deps_extra",
224 ),
225 pytest.param(
226 "Provides-Extra: alpha\nRequires-Dist: A\nRequires-Dist: B;extra=='alpha'",
227 "['optional-dependencies']",
228 "alpha",
229 ["A", "B"],
230 id="deps_extra_dynamic_opt",
231 ),
232 ],
233 )
234 def test_pyproject_deps_static_with_dynamic(
235 tox_project: ToxProjectCreator,
236 demo_pkg_inline: Path,
237 monkeypatch: pytest.MonkeyPatch,
238 metadata: str,
239 dynamic: str,
240 extra: str,
241 deps: list[str],
242 ) -> None:
243 monkeypatch.setenv("METADATA_EXTRA", metadata)
244 toml = f"{(demo_pkg_inline / 'pyproject.toml').read_text()}[project]\ndynamic={dynamic}"
245 ini = f"[testenv]\nextras={extra}\n[testenv:.pkg]\npass_env=METADATA_EXTRA"
246 proj = tox_project({"tox.ini": ini, "pyproject.toml": toml}, base=demo_pkg_inline)
247
248 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
249 result = proj.run("r", "--notest")
250 result.assert_success()
251
252 expected_calls = [
253 (".pkg", "_optional_hooks"),
254 (".pkg", "get_requires_for_build_sdist"),
255 (".pkg", "get_requires_for_build_wheel"),
256 (".pkg", "build_wheel"),
257 (".pkg", "build_sdist"),
258 ("py", "install_package_deps"),
259 ("py", "install_package"),
260 (".pkg", "_exit"),
261 ]
262 found_calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list]
263 assert found_calls == expected_calls
264
265 args = execute_calls.call_args_list[-3][0][3].cmd
266 assert args == ["python", "-I", "-m", "pip", "install", *deps]
267
268
269 def test_pyproject_no_build_editable_fallback(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
270 proj = tox_project({"tox.ini": "[tox]\nenv_list=a,b"}, base=demo_pkg_inline)
271 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
272 result = proj.run("r", "-e", "a,b", "--notest", "--develop")
273 result.assert_success()
274 warning = (
275 ".pkg: package config for a, b is editable, however the build backend build does not support PEP-660, "
276 "falling back to editable-legacy - change your configuration to it"
277 )
278 assert warning in result.out.splitlines()
279
280 expected_calls = [
281 (".pkg", "_optional_hooks"),
282 (".pkg", "get_requires_for_build_wheel"),
283 (".pkg", "build_wheel"),
284 (".pkg", "get_requires_for_build_sdist"),
285 ("a", "install_package"),
286 ("b", "install_package"),
287 (".pkg", "_exit"),
288 ]
289 found_calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list]
290 assert found_calls == expected_calls
291
292
293 @pytest.mark.parametrize("package", ["sdist", "sdist-wheel", "wheel", "editable"])
294 def test_pep517_pkg_env_rejects_deps(tox_project: ToxProjectCreator, demo_pkg_setuptools: Path, package: str) -> None:
295 ini = f"[testenv]\npackage={package}\n[pkgenv]\ndeps = A"
296 proj = tox_project({"tox.ini": ini}, base=demo_pkg_setuptools)
297 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
298 result = proj.run("r", "--notest")
299 result.assert_failed()
300 assert "does not support the deps configuration" in result.out
301
302
303 def test_pep517_pkg_env_allows_deps_for_editable_legacy(
304 tox_project: ToxProjectCreator,
305 demo_pkg_setuptools: Path,
306 ) -> None:
307 ini = "[testenv]\npackage=editable-legacy\n[pkgenv]\ndeps = A"
308 proj = tox_project({"tox.ini": ini}, base=demo_pkg_setuptools)
309 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
310 result = proj.run("r", "--notest")
311 result.assert_success()
312 assert "does not support the deps configuration" not in result.out
313
314
315 def test_pep517_pkg_env_rejects_deps_via_testenv(
316 tox_project: ToxProjectCreator,
317 demo_pkg_setuptools: Path,
318 ) -> None:
319 ini = "[testenv]\npackage=sdist\n[testenv:.pkg]\ndeps = B"
320 proj = tox_project({"tox.ini": ini}, base=demo_pkg_setuptools)
321 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
322 result = proj.run("r", "--notest")
323 result.assert_failed()
324 assert "does not support the deps configuration" in result.out
325
326
327 def test_pyproject_build_editable_and_wheel(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
328 # test that build wheel and build editable are cached separately
329
330 ini = """
331 [testenv:.pkg]
332 set_env= BACKEND_HAS_EDITABLE=1
333 [testenv:a,b]
334 package = editable
335 [testenv:c,d]
336 package = wheel
337 """
338 proj = tox_project({"tox.ini": ini}, base=demo_pkg_inline)
339 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
340
341 result = proj.run("r", "-e", "a,b,c,d", "--notest", "--workdir", str(proj.path / ".tox"))
342
343 result.assert_success()
344 found_calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list]
345 assert found_calls == [
346 (".pkg", "_optional_hooks"),
347 (".pkg", "get_requires_for_build_wheel"),
348 (".pkg", "build_editable"),
349 ("a", "install_package"),
350 ("b", "install_package"),
351 (".pkg", "build_wheel"),
352 ("c", "install_package"),
353 ("d", "install_package"),
354 (".pkg", "_exit"),
355 ]
356
357
358 def test_pyproject_config_settings_sdist(
359 tox_project: ToxProjectCreator,
360 demo_pkg_setuptools: Path,
361 mocker: MockerFixture,
362 ) -> None:
363 ini = """
364 [tox]
365 env_list = sdist
366
367 [testenv]
368 wheel_build_env = .pkg
369 package = sdist
370
371 [testenv:.pkg]
372 config_settings_get_requires_for_build_sdist = A = 1
373 config_settings_build_sdist = B = 2
374 config_settings_get_requires_for_build_wheel = C = 3
375 config_settings_prepare_metadata_for_build_wheel = D = 4
376 """
377 proj = tox_project({"tox.ini": ini}, base=demo_pkg_setuptools)
378 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
379
380 write_stdin = mocker.spy(LocalSubprocessExecuteStatus, "write_stdin")
381
382 result = proj.run("r", "--notest", from_cwd=proj.path)
383 result.assert_success()
384
385 found = {
386 message["cmd"]: message["kwargs"]["config_settings"]
387 for message in [json.loads(call[0][1]) for call in write_stdin.call_args_list]
388 if not message["cmd"].startswith("_")
389 }
390 assert found == {
391 "build_sdist": {"B": "2"},
392 "get_requires_for_build_sdist": {"A": "1"},
393 "get_requires_for_build_wheel": {"C": "3"},
394 "prepare_metadata_for_build_wheel": {"D": "4"},
395 }
396
397
398 def test_pyproject_config_settings_wheel(
399 tox_project: ToxProjectCreator,
400 demo_pkg_setuptools: Path,
401 mocker: MockerFixture,
402 ) -> None:
403 ini = """
404 [tox]
405 env_list = wheel
406
407 [testenv]
408 wheel_build_env = .pkg
409 package = wheel
410
411 [testenv:.pkg]
412 config_settings_get_requires_for_build_wheel = C = 3
413 config_settings_prepare_metadata_for_build_wheel = D = 4
414 config_settings_build_wheel = E = 5
415 """
416 proj = tox_project({"tox.ini": ini}, base=demo_pkg_setuptools)
417 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
418
419 write_stdin = mocker.spy(LocalSubprocessExecuteStatus, "write_stdin")
420 mocker.patch.object(Pep517VirtualEnvFrontend, "_can_skip_prepare", return_value=False)
421
422 result = proj.run("r", "--notest", from_cwd=proj.path)
423 result.assert_success()
424
425 found = {
426 message["cmd"]: message["kwargs"]["config_settings"]
427 for message in [json.loads(call[0][1]) for call in write_stdin.call_args_list]
428 if not message["cmd"].startswith("_")
429 }
430 assert found == {
431 "get_requires_for_build_wheel": {"C": "3"},
432 "prepare_metadata_for_build_wheel": {"D": "4"},
433 "build_wheel": {"E": "5"},
434 }
435
436
437 def test_pyproject_config_settings_editable(
438 tox_project: ToxProjectCreator,
439 demo_pkg_setuptools: Path,
440 mocker: MockerFixture,
441 ) -> None:
442 ini = """
443 [tox]
444 env_list = editable
445
446 [testenv:.pkg]
447 config_settings_get_requires_for_build_editable = F = 6
448 config_settings_prepare_metadata_for_build_editable = G = 7
449 config_settings_build_editable = H = 8
450
451 [testenv]
452 wheel_build_env = .pkg
453 package = editable
454 """
455 proj = tox_project({"tox.ini": ini}, base=demo_pkg_setuptools)
456 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
457
458 write_stdin = mocker.spy(LocalSubprocessExecuteStatus, "write_stdin")
459 mocker.patch.object(Pep517VirtualEnvFrontend, "_can_skip_prepare", return_value=False)
460
461 result = proj.run("r", "--notest", from_cwd=proj.path)
462 result.assert_success()
463
464 found = {
465 message["cmd"]: message["kwargs"]["config_settings"]
466 for message in [json.loads(call[0][1]) for call in write_stdin.call_args_list]
467 if not message["cmd"].startswith("_")
468 }
469 assert found == {
470 "get_requires_for_build_editable": {"F": "6"},
471 "prepare_metadata_for_build_editable": {"G": "7"},
472 "build_editable": {"H": "8"},
473 }
474
475
476 def test_pyproject_config_settings_editable_legacy(
477 tox_project: ToxProjectCreator,
478 demo_pkg_setuptools: Path,
479 mocker: MockerFixture,
480 ) -> None:
481 ini = """
482 [tox]
483 env_list = editable
484
485 [testenv:.pkg]
486 config_settings_get_requires_for_build_sdist = A = 1
487 config_settings_get_requires_for_build_wheel = C = 3
488 config_settings_prepare_metadata_for_build_wheel = D = 4
489
490 [testenv]
491 wheel_build_env = .pkg
492 package = editable-legacy
493 """
494 proj = tox_project({"tox.ini": ini}, base=demo_pkg_setuptools)
495 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
496
497 write_stdin = mocker.spy(LocalSubprocessExecuteStatus, "write_stdin")
498 mocker.patch.object(Pep517VirtualEnvFrontend, "_can_skip_prepare", return_value=False)
499
500 result = proj.run("r", "--notest", from_cwd=proj.path)
501 result.assert_success()
502
503 found = {
504 message["cmd"]: message["kwargs"]["config_settings"]
505 for message in [json.loads(call[0][1]) for call in write_stdin.call_args_list]
506 if not message["cmd"].startswith("_")
507 }
508 assert found == {
509 "get_requires_for_build_sdist": {"A": "1"},
510 "get_requires_for_build_wheel": {"C": "3"},
511 "prepare_metadata_for_build_wheel": {"D": "4"},
512 }
513
514
515 def test_pyproject_config_settings_sdist_passed_to_pip_install(
516 tox_project: ToxProjectCreator,
517 demo_pkg_setuptools: Path,
518 mocker: MockerFixture,
519 ) -> None:
520 toml = """
521 [tox]
522 env_list = ["sdist"]
523
524 [env_run_base]
525 wheel_build_env = ".pkg"
526 package = "sdist"
527
528 [env.".pkg"]
529 config_settings_build_wheel = {X = "y", Z = "w"}
530 """
531 proj = tox_project({"tox.toml": dedent(toml)}, base=demo_pkg_setuptools)
532 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
533 mocker.spy(LocalSubprocessExecuteStatus, "write_stdin")
534
535 result = proj.run("r", "--notest", from_cwd=proj.path)
536 result.assert_success()
537
538 pip_install_cmds = [i[0][3].cmd for i in execute_calls.call_args_list if i[0][3].run_id == "install_package"]
539 assert pip_install_cmds, "expected at least one install_package call"
540 cmd = pip_install_cmds[0]
541 assert "--config-settings=X=y" in cmd
542 assert "--config-settings=Z=w" in cmd
543
544
545 @pytest.mark.integration
546 @pytest.mark.usefixtures("enable_pip_pypi_access")
547 def test_pyproject_installpkg_pep517_envs(tox_project: ToxProjectCreator, pkg_with_pdm_backend: Path) -> None:
548 # Regression test for #3512
549 tox_ini = """
550 [tox]
551 envlist = dummy1,dummy2
552
553 [testenv:dummy1]
554 commands =
555 python -c print(1)
556
557 [testenv:dummy2]
558 commands =
559 python -c print(42)
560 """
561 sdist = pkg_with_pdm_backend / "dist" / "skeleton-0.1.1337.tar.gz"
562 proj = tox_project({"tox.ini": tox_ini}, base=pkg_with_pdm_backend)
563 result = proj.run("--installpkg", str(sdist))
564 result.assert_success()
565
566
567 def test_sdist_wheel_package_type(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
568 ini = "[testenv]\npackage=sdist-wheel"
569 proj = tox_project({"tox.ini": ini}, base=demo_pkg_inline)
570 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
571 result = proj.run("r", "--notest")
572 result.assert_success()
573
574 found_calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list]
575
576 # The .pkg env builds the sdist
577 assert (".pkg", "build_sdist") in found_calls, f"expected .pkg to build_sdist, got {found_calls}"
578
579 # A wheel is built from the extracted sdist (may be in .pkg or a wheel_build_env child)
580 wheel_calls = [(i, env, rid) for i, (env, rid) in enumerate(found_calls) if rid == "build_wheel"]
581 assert wheel_calls, f"expected build_wheel call, got {found_calls}"
582
583 # The sdist-wheel flow: build_sdist must happen before the final build_wheel
584 # (an earlier build_wheel may occur for metadata extraction)
585 sdist_idx = found_calls.index((".pkg", "build_sdist"))
586 last_wheel_idx = wheel_calls[-1][0]
587 assert sdist_idx < last_wheel_idx, f"build_sdist must happen before final build_wheel, got {found_calls}"
588
589 # The final package installed should be a wheel
590 install_calls = [(env, rid) for env, rid in found_calls if rid == "install_package"]
591 assert install_calls, "expected install_package call"
592
593
594 def test_sdist_wheel_config(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
595 ini = "[testenv]\npackage=sdist-wheel"
596 proj = tox_project({"tox.ini": ini}, base=demo_pkg_inline)
597 result = proj.run("c", "-k", "package", "-k", "wheel_build_env")
598 result.assert_success()
599 res = result.env_conf("py")["package"]
600 assert res == "sdist-wheel"
601 # wheel_build_env should be present (reusing existing infrastructure)
602 build_env = result.env_conf("py")["wheel_build_env"]
603 assert build_env # has a value (name depends on Python version)
604
605
606 def test_sdist_wheel_custom_build_env(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
607 ini = "[testenv]\npackage=sdist-wheel\nwheel_build_env=.custom-wheel-builder"
608 proj = tox_project({"tox.ini": ini}, base=demo_pkg_inline)
609 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
610 result = proj.run("r", "--notest")
611 result.assert_success()
612
613 found_calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list]
614 env_names = {env for env, _ in found_calls}
615 assert ".custom-wheel-builder" in env_names, f"expected custom child env name, got {env_names}"
616
617
618 def test_wheel_package_does_not_build_sdist(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
619 ini = "[testenv]\npackage=wheel"
620 proj = tox_project({"tox.ini": ini}, base=demo_pkg_inline)
621 execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
622 result = proj.run("r", "--notest")
623 result.assert_success()
624 # Verify that build_sdist was NOT called with plain wheel package type
625 call_ids = [i[0][3].run_id for i in execute_calls.call_args_list]
626 assert "build_sdist" not in call_ids
627 assert "build_wheel" in call_ids
628
629
630 def test_sdist_wheel_rejects_path_traversal(
631 tox_project: ToxProjectCreator, demo_pkg_inline: Path, mocker: MockerFixture
632 ) -> None:
633 ini = "[testenv]\npackage=sdist-wheel"
634 proj = tox_project({"tox.ini": ini}, base=demo_pkg_inline)
635 proj.patch_execute(lambda r: 0 if "install" in r.run_id else None)
636
637 original_build_sdist = Pep517VirtualEnvFrontend.build_sdist
638
639 def _build_malicious_sdist(self: Pep517VirtualEnvFrontend, *args: Any, **kwargs: Any) -> object:
640 result = original_build_sdist(self, *args, **kwargs)
641 buf = io.BytesIO()
642 with tarfile.open(fileobj=buf, mode="w:gz") as tar:
643 info = tarfile.TarInfo(name="../escape.txt")
644 info.size = 4
645 tar.addfile(info, io.BytesIO(b"evil"))
646 result.sdist.write_bytes(buf.getvalue())
647 return result
648
649 mocker.patch.object(Pep517VirtualEnvFrontend, "build_sdist", _build_malicious_sdist)
650 result = proj.run("r", "--notest")
651 result.assert_failed()
652 assert "tar member '../escape.txt' would extract outside of" in result.out