| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TYPE_CHECKING |
| 4 |
from zipfile import ZipFile |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
if TYPE_CHECKING: |
| 9 |
from collections.abc import Callable |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
from tox.pytest import ToxProjectCreator |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture(scope="session") |
| 16 |
def pkg_with_extras_project_wheel( |
| 17 |
pkg_with_extras_project: Path, |
| 18 |
pkg_builder: Callable[[Path, Path, list[str], bool], Path], |
| 19 |
) -> Path: |
| 20 |
dist = pkg_with_extras_project / "dist" |
| 21 |
pkg_builder(dist, pkg_with_extras_project, ["wheel"], False) |
| 22 |
return next(dist.iterdir()) |
| 23 |
|
| 24 |
|
| 25 |
def test_tox_install_pkg_wheel(tox_project: ToxProjectCreator, pkg_with_extras_project_wheel: Path) -> None: |
| 26 |
proj = tox_project({"tox.ini": "[testenv]\nextras=docs,format"}) |
| 27 |
execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 28 |
result = proj.run("r", "-e", "py", "--installpkg", str(pkg_with_extras_project_wheel)) |
| 29 |
result.assert_success() |
| 30 |
calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd[5:]) for i in execute_calls.call_args_list] |
| 31 |
deps = ["black>=3", "colorama>=0.4.6", "flake8", "platformdirs>=4.3.8", "sphinx-rtd-theme<1,>=0.4.3", "sphinx>=3"] |
| 32 |
expected = [ |
| 33 |
("py", "install_package_deps", deps), |
| 34 |
("py", "install_package", ["--force-reinstall", "--no-deps", str(pkg_with_extras_project_wheel)]), |
| 35 |
] |
| 36 |
assert calls == expected |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture(scope="session") |
| 40 |
def pkg_with_sdist( |
| 41 |
pkg_with_extras_project: Path, |
| 42 |
pkg_builder: Callable[[Path, Path, list[str], bool], Path], |
| 43 |
) -> Path: |
| 44 |
dist = pkg_with_extras_project / "dist" |
| 45 |
pkg_builder(dist, pkg_with_extras_project, ["sdist"], False) |
| 46 |
return next(dist.iterdir()) |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture |
| 50 |
def pkg_with_extras_project_sdist( |
| 51 |
pkg_with_extras_project: Path, |
| 52 |
pkg_builder: Callable[[Path, Path, list[str], bool], Path], |
| 53 |
) -> Path: |
| 54 |
dist = pkg_with_extras_project / "sdist" |
| 55 |
pkg_builder(dist, pkg_with_extras_project, ["sdist"], False) |
| 56 |
return next(dist.iterdir()) |
| 57 |
|
| 58 |
|
| 59 |
def test_tox_install_pkg_sdist(tox_project: ToxProjectCreator, pkg_with_extras_project_sdist: Path) -> None: |
| 60 |
proj = tox_project({"tox.ini": "[testenv]\nextras=docs,format"}) |
| 61 |
execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 62 |
result = proj.run("r", "-e", "py", "--installpkg", str(pkg_with_extras_project_sdist)) |
| 63 |
result.assert_success() |
| 64 |
calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd[5:]) for i in execute_calls.call_args_list] |
| 65 |
deps = ["black>=3", "colorama>=0.4.6", "flake8", "platformdirs>=4.3.8", "sphinx-rtd-theme<1,>=0.4.3", "sphinx>=3"] |
| 66 |
assert calls == [ |
| 67 |
(".pkg_external_sdist_meta", "install_requires", ["setuptools"]), |
| 68 |
(".pkg_external_sdist_meta", "_optional_hooks", []), |
| 69 |
(".pkg_external_sdist_meta", "get_requires_for_build_sdist", []), |
| 70 |
(".pkg_external_sdist_meta", "get_requires_for_build_wheel", []), # required before prepare_metadata* |
| 71 |
(".pkg_external_sdist_meta", "prepare_metadata_for_build_wheel", []), |
| 72 |
("py", "install_package_deps", deps), |
| 73 |
("py", "install_package", ["--force-reinstall", "--no-deps", str(pkg_with_extras_project_sdist)]), |
| 74 |
] |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.parametrize("mode", ["p", "le"]) # no need for r as is tested above |
| 78 |
def test_install_pkg_via(tox_project: ToxProjectCreator, mode: str, pkg_with_extras_project_wheel: Path) -> None: |
| 79 |
proj = tox_project({"tox.ini": "[testenv]\npackage=wheel"}) |
| 80 |
execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 81 |
|
| 82 |
result = proj.run(mode, "--installpkg", str(pkg_with_extras_project_wheel)) |
| 83 |
|
| 84 |
result.assert_success() |
| 85 |
calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list] |
| 86 |
assert calls == [("py", "install_package_deps"), ("py", "install_package")] |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.usefixtures("enable_pip_pypi_access") |
| 90 |
def test_build_wheel_external_runs_once( |
| 91 |
tox_project: ToxProjectCreator, demo_pkg_inline: Path, monkeypatch: pytest.MonkeyPatch |
| 92 |
) -> None: |
| 93 |
toml = """ |
| 94 |
env_list = ["a", "b"] |
| 95 |
[env_run_base] |
| 96 |
package = "external" |
| 97 |
package_env = ".ext" |
| 98 |
commands = [["python", "-c", "from demo_pkg_inline import do; do()"]] |
| 99 |
[env.".ext"] |
| 100 |
deps = ["build"] |
| 101 |
package_glob = "{env_tmp_dir}/dist/*.whl" |
| 102 |
commands = [["pyproject-build", "-w", ".", "-o", "{env_tmp_dir}/dist"]] |
| 103 |
""" |
| 104 |
project = tox_project({"tox.toml": toml}) |
| 105 |
monkeypatch.delenv("PIP_EXTRA_INDEX_URL", raising=False) |
| 106 |
monkeypatch.delenv("PIP_INDEX_URL", raising=False) |
| 107 |
|
| 108 |
result = project.run("r", "--root", str(demo_pkg_inline), "--workdir", str(project.path)) |
| 109 |
|
| 110 |
result.assert_success() |
| 111 |
assert result.out.count("pyproject-build") == 1 |
| 112 |
assert result.out.count("greetings from demo_pkg_inline") == 2 |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.usefixtures("enable_pip_pypi_access") |
| 116 |
def test_build_wheel_external( |
| 117 |
tox_project: ToxProjectCreator, demo_pkg_inline: Path, monkeypatch: pytest.MonkeyPatch |
| 118 |
) -> None: |
| 119 |
ini = """ |
| 120 |
[testenv] |
| 121 |
package = external |
| 122 |
package_env = .ext |
| 123 |
commands = |
| 124 |
python -c 'from demo_pkg_inline import do; do()' |
| 125 |
|
| 126 |
[testenv:.ext] |
| 127 |
deps = build |
| 128 |
package_glob = {envtmpdir}{/}dist{/}*.whl |
| 129 |
commands = |
| 130 |
pyproject-build -w . -o {envtmpdir}{/}dist |
| 131 |
""" |
| 132 |
project = tox_project({"tox.ini": ini}) |
| 133 |
monkeypatch.delenv("PIP_EXTRA_INDEX_URL", raising=False) |
| 134 |
monkeypatch.delenv("PIP_INDEX_URL", raising=False) |
| 135 |
|
| 136 |
result = project.run("r", "--root", str(demo_pkg_inline), "--workdir", str(project.path)) |
| 137 |
|
| 138 |
result.assert_success() |
| 139 |
assert "greetings from demo_pkg_inline" in result.out |
| 140 |
|
| 141 |
|
| 142 |
def test_build_wheel_external_fail_build(tox_project: ToxProjectCreator) -> None: |
| 143 |
ini = """ |
| 144 |
[testenv] |
| 145 |
package = external |
| 146 |
[testenv:.pkg_external] |
| 147 |
commands = xyz |
| 148 |
""" |
| 149 |
project = tox_project({"tox.ini": ini}) |
| 150 |
result = project.run("r") |
| 151 |
|
| 152 |
result.assert_failed() |
| 153 |
assert "stopping as failed to build package" in result.out, result.out |
| 154 |
|
| 155 |
|
| 156 |
def test_build_wheel_external_fail_no_pkg(tox_project: ToxProjectCreator) -> None: |
| 157 |
ini = """ |
| 158 |
[testenv] |
| 159 |
package = external |
| 160 |
""" |
| 161 |
project = tox_project({"tox.ini": ini}) |
| 162 |
result = project.run("r") |
| 163 |
|
| 164 |
result.assert_failed() |
| 165 |
assert "failed with no package found in " in result.out, result.out |
| 166 |
|
| 167 |
|
| 168 |
def test_build_wheel_external_fail_many_pkg(tox_project: ToxProjectCreator) -> None: |
| 169 |
ini = """ |
| 170 |
[testenv] |
| 171 |
package = external |
| 172 |
[testenv:.pkg_external] |
| 173 |
commands = |
| 174 |
python -c 'from pathlib import Path; (Path(r"{env_tmp_dir}") / "dist").mkdir()' |
| 175 |
python -c 'from pathlib import Path; (Path(r"{env_tmp_dir}") / "dist" / "a").write_text("")' |
| 176 |
python -c 'from pathlib import Path; (Path(r"{env_tmp_dir}") / "dist" / "b").write_text("")' |
| 177 |
""" |
| 178 |
project = tox_project({"tox.ini": ini}) |
| 179 |
result = project.run("r") |
| 180 |
|
| 181 |
result.assert_failed() |
| 182 |
assert "failed with found more than one package " in result.out, result.out |
| 183 |
|
| 184 |
|
| 185 |
def test_tox_install_pkg_bad_wheel(tox_project: ToxProjectCreator, tmp_path: Path) -> None: |
| 186 |
wheel = tmp_path / "w.whl" |
| 187 |
with ZipFile(str(wheel), "w"): |
| 188 |
pass |
| 189 |
proj = tox_project({"tox.ini": "[testenv]"}) |
| 190 |
result = proj.run("r", "-e", "py", "--installpkg", str(wheel)) |
| 191 |
|
| 192 |
result.assert_failed() |
| 193 |
assert "failed with no .dist-info inside " in result.out, result.out |
| 194 |
|
| 195 |
|
| 196 |
def test_tox_install_pkg_with_skip_install( |
| 197 |
tox_project: ToxProjectCreator, |
| 198 |
demo_pkg_inline: Path, |
| 199 |
demo_pkg_inline_wheel: Path, |
| 200 |
) -> None: |
| 201 |
ini = "[testenv:foo]\nskip_install = true" |
| 202 |
project = tox_project({"tox.ini": ini, "pyproject.toml": (demo_pkg_inline / "pyproject.toml").read_text()}) |
| 203 |
result = project.run("-e", "py", "--installpkg", str(demo_pkg_inline_wheel)) |
| 204 |
result.assert_success() |
| 205 |
|
| 206 |
|
| 207 |
def test_run_installpkg_targz( |
| 208 |
tox_project: ToxProjectCreator, |
| 209 |
pkg_with_sdist: Path, |
| 210 |
enable_pip_pypi_access: str | None, # noqa: ARG001 |
| 211 |
) -> None: |
| 212 |
project = tox_project({ |
| 213 |
"tox.ini": """ |
| 214 |
[tox] |
| 215 |
envlist = base, flake8 |
| 216 |
[testenv] |
| 217 |
package = sdist |
| 218 |
[testenv:base] |
| 219 |
[testenv:flake8] |
| 220 |
""" |
| 221 |
}) |
| 222 |
outcome = project.run(f"--installpkg={pkg_with_sdist}") |
| 223 |
outcome.assert_success() |