| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
import sysconfig |
| 5 |
from pathlib import Path |
| 6 |
from types import SimpleNamespace |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
from unittest.mock import MagicMock, patch |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from tox.tox_env.errors import Fail |
| 13 |
from tox.tox_env.python.api import Python |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from pytest_mock import MockerFixture |
| 17 |
|
| 18 |
from tests.conftest import PatchPrevPy |
| 19 |
from tox.pytest import ToxProjectCreator |
| 20 |
|
| 21 |
|
| 22 |
def test_requirements_txt(tox_project: ToxProjectCreator) -> None: |
| 23 |
prj = tox_project( |
| 24 |
{ |
| 25 |
"tox.ini": "[testenv]\npackage=skip\ndeps=-rrequirements.txt", |
| 26 |
"requirements.txt": "nose", |
| 27 |
}, |
| 28 |
) |
| 29 |
execute_calls = prj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 30 |
result = prj.run("r", "-e", "py") |
| 31 |
result.assert_success() |
| 32 |
|
| 33 |
assert execute_calls.call_count == 1 |
| 34 |
exp = ["python", "-I", "-m", "pip", "install", "-r", "requirements.txt"] |
| 35 |
got_cmd = execute_calls.call_args[0][3].cmd |
| 36 |
|
| 37 |
assert got_cmd == exp |
| 38 |
|
| 39 |
|
| 40 |
def test_conflicting_base_python_factor() -> None: |
| 41 |
major, minor = sys.version_info[0:2] |
| 42 |
name = f"py{major}{minor}-py{major}{minor - 1}" |
| 43 |
with pytest.raises(ValueError, match=f"conflicting factors py{major}{minor}, py{major}{minor - 1} in {name}"): |
| 44 |
Python.extract_base_python(name) |
| 45 |
|
| 46 |
|
| 47 |
def test_conflicting_base_python_factor_explicit_version() -> None: |
| 48 |
with pytest.raises(ValueError, match=r"conflicting factors 3\.10, 3\.11 in 3\.10-3\.11"): |
| 49 |
Python.extract_base_python("3.10-3.11") |
| 50 |
|
| 51 |
|
| 52 |
def test_conflicting_base_python_factor_mixed_style() -> None: |
| 53 |
with pytest.raises(ValueError, match=r"conflicting factors py310, 3\.11 in py310-3\.11"): |
| 54 |
Python.extract_base_python("py310-3.11") |
| 55 |
|
| 56 |
|
| 57 |
def test_conflicting_base_python_factor_ignore(tox_project: ToxProjectCreator) -> None: |
| 58 |
ini = "[tox]\nenv_list=unit-py3.10-2.16\nignore_base_python_conflict=true\n[testenv]\npackage=skip\n" |
| 59 |
project = tox_project({"tox.ini": ini}) |
| 60 |
result = project.run("c", "-e", "unit-py3.10-2.16", "-k", "base_python") |
| 61 |
result.assert_success() |
| 62 |
|
| 63 |
|
| 64 |
def test_conflicting_base_python_factor_no_ignore(tox_project: ToxProjectCreator) -> None: |
| 65 |
ini = "[tox]\nenv_list=unit-py3.10-2.16\n[testenv]\npackage=skip\n" |
| 66 |
project = tox_project({"tox.ini": ini}) |
| 67 |
result = project.run("c", "-e", "unit-py3.10-2.16", "-k", "base_python", raise_on_config_fail=False) |
| 68 |
result.assert_failed(code=-1) |
| 69 |
|
| 70 |
|
| 71 |
def test_validate_base_python_conflicting_factors_ignore() -> None: |
| 72 |
result = Python._validate_base_python("unit-py3.10-2.16", ["python3"], ignore_base_python_conflict=True) # noqa: SLF001 |
| 73 |
assert result == ["python3"] |
| 74 |
|
| 75 |
|
| 76 |
def test_validate_base_python_conflicting_factors_no_ignore() -> None: |
| 77 |
with pytest.raises(ValueError, match=r"conflicting factors py3\.10, 2\.16 in unit-py3\.10-2\.16"): |
| 78 |
Python._validate_base_python("unit-py3.10-2.16", ["python3"], ignore_base_python_conflict=False) # noqa: SLF001 |
| 79 |
|
| 80 |
|
| 81 |
def test_build_wheel_in_non_base_pkg_env( |
| 82 |
tox_project: ToxProjectCreator, |
| 83 |
patch_prev_py: PatchPrevPy, |
| 84 |
demo_pkg_inline: Path, |
| 85 |
mocker: MockerFixture, |
| 86 |
) -> None: |
| 87 |
def _fake_session(env_dir: list[str], **_: object) -> MagicMock: |
| 88 |
bin_dir = Path(env_dir[0]) / "bin" |
| 89 |
bin_dir.mkdir(parents=True, exist_ok=True) |
| 90 |
(bin_dir / "python").symlink_to(sys.executable) |
| 91 |
mock = MagicMock() |
| 92 |
mock.creator.bin_dir = bin_dir |
| 93 |
mock.creator.script_dir = bin_dir |
| 94 |
return mock |
| 95 |
|
| 96 |
mocker.patch("tox.tox_env.python.virtual_env.api.session_via_cli", side_effect=_fake_session) |
| 97 |
prev_ver, impl, _ = patch_prev_py(True) |
| 98 |
prev_py = f"py{prev_ver}" |
| 99 |
prj = tox_project({"tox.ini": f"[tox]\nenv_list= {prev_py}\n[testenv]\npackage=wheel"}) |
| 100 |
execute_calls = prj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 101 |
result = prj.run("-r", "--root", str(demo_pkg_inline), "--workdir", str(prj.path / ".tox")) |
| 102 |
result.assert_success() |
| 103 |
calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list] |
| 104 |
assert calls == [ |
| 105 |
(f".pkg-{impl}{prev_ver}", "_optional_hooks"), |
| 106 |
(f".pkg-{impl}{prev_ver}", "get_requires_for_build_wheel"), |
| 107 |
(f".pkg-{impl}{prev_ver}", "build_wheel"), |
| 108 |
(f"py{prev_ver}", "install_package"), |
| 109 |
(f".pkg-{impl}{prev_ver}", "_exit"), |
| 110 |
] |
| 111 |
|
| 112 |
|
| 113 |
def test_build_wheel_in_free_threaded_pkg_env( |
| 114 |
tox_project: ToxProjectCreator, |
| 115 |
patch_prev_py: PatchPrevPy, |
| 116 |
demo_pkg_inline: Path, |
| 117 |
mocker: MockerFixture, |
| 118 |
) -> None: |
| 119 |
def _fake_session(env_dir: list[str], **_: object) -> MagicMock: |
| 120 |
bin_dir = Path(env_dir[0]) / "bin" |
| 121 |
bin_dir.mkdir(parents=True, exist_ok=True) |
| 122 |
(bin_dir / "python").symlink_to(sys.executable) |
| 123 |
mock = MagicMock() |
| 124 |
mock.creator.bin_dir = bin_dir |
| 125 |
mock.creator.script_dir = bin_dir |
| 126 |
return mock |
| 127 |
|
| 128 |
mocker.patch("tox.tox_env.python.virtual_env.api.session_via_cli", side_effect=_fake_session) |
| 129 |
prev_ver, impl, _ = patch_prev_py(True, free_threaded=True) |
| 130 |
prev_py = f"py{prev_ver}" |
| 131 |
prj = tox_project({"tox.ini": f"[tox]\nenv_list= {prev_py}\n[testenv]\npackage=wheel"}) |
| 132 |
execute_calls = prj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 133 |
result = prj.run("-r", "--root", str(demo_pkg_inline), "--workdir", str(prj.path / ".tox")) |
| 134 |
result.assert_success() |
| 135 |
calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list] |
| 136 |
assert calls == [ |
| 137 |
(f".pkg-{impl}{prev_ver}t", "_optional_hooks"), |
| 138 |
(f".pkg-{impl}{prev_ver}t", "get_requires_for_build_wheel"), |
| 139 |
(f".pkg-{impl}{prev_ver}t", "build_wheel"), |
| 140 |
(f"py{prev_ver}", "install_package"), |
| 141 |
(f".pkg-{impl}{prev_ver}t", "_exit"), |
| 142 |
] |
| 143 |
|
| 144 |
|
| 145 |
def test_diff_msg_added_removed_changed() -> None: |
| 146 |
before = {"A": "1", "F": "8", "C": "3", "D": "4", "E": "6"} |
| 147 |
after = {"G": "9", "B": "2", "C": "3", "D": "5", "E": "7"} |
| 148 |
expected = "python added A='1' | F='8', removed G='9' | B='2', changed D='5'->'4' | E='7'->'6'" |
| 149 |
assert Python._diff_msg(before, after) == expected # noqa: SLF001 |
| 150 |
|
| 151 |
|
| 152 |
def test_diff_msg_no_diff() -> None: |
| 153 |
assert Python._diff_msg({}, {}) == "python " # noqa: SLF001 |
| 154 |
|
| 155 |
|
| 156 |
@pytest.mark.parametrize( |
| 157 |
("env", "base_python"), |
| 158 |
[ |
| 159 |
("py3", "py3"), |
| 160 |
("py3t", "py3t"), |
| 161 |
("py311", "py311"), |
| 162 |
("py311t", "py311t"), |
| 163 |
("py3.12", "py3.12"), |
| 164 |
("py3.12t", "py3.12t"), |
| 165 |
("pypy2", "pypy2"), |
| 166 |
("pypy2t", "pypy2t"), |
| 167 |
("rustpython3", "rustpython3"), |
| 168 |
("rustpython3t", "rustpython3t"), |
| 169 |
("graalpy", "graalpy"), |
| 170 |
("graalpyt", None), |
| 171 |
("jython", "jython"), |
| 172 |
("jythont", None), |
| 173 |
("cpython3.8", "cpython3.8"), |
| 174 |
("cpython3.8t", "cpython3.8t"), |
| 175 |
("ironpython2.7", "ironpython2.7"), |
| 176 |
("ironpython2.7t", "ironpython2.7t"), |
| 177 |
("functional-py310", "py310"), |
| 178 |
("functional-py310t", "py310t"), |
| 179 |
("bar-pypy2-foo", "pypy2"), |
| 180 |
("bar-foo2t-py2", "py2"), |
| 181 |
("bar-pypy2t-foo", "pypy2t"), |
| 182 |
("py", None), |
| 183 |
("pyt", None), |
| 184 |
("django-32", None), |
| 185 |
("django-32t", None), |
| 186 |
("eslint-8.3", None), |
| 187 |
("eslint-8.3t", None), |
| 188 |
("py-310", None), |
| 189 |
("py-310t", None), |
| 190 |
("py3000", None), |
| 191 |
("py3000t", None), |
| 192 |
("4.foo", None), |
| 193 |
("4.foot", None), |
| 194 |
("310", None), |
| 195 |
("310t", None), |
| 196 |
("5", None), |
| 197 |
("5t", None), |
| 198 |
("2000", None), |
| 199 |
("2000t", None), |
| 200 |
("4000", None), |
| 201 |
("4000t", None), |
| 202 |
("3.10", "3.10"), |
| 203 |
("3.10t", "3.10t"), |
| 204 |
("3.9", "3.9"), |
| 205 |
("3.9t", "3.9t"), |
| 206 |
("2.7", "2.7"), |
| 207 |
("2.7t", "2.7t"), |
| 208 |
("pypy-3.10", "pypy3.10"), |
| 209 |
("pypy-3.10t", "pypy3.10t"), |
| 210 |
("3.10-tests", "3.10"), |
| 211 |
("3.10t-tests", "3.10t"), |
| 212 |
("tests-3.10", "3.10"), |
| 213 |
("tests-3.10t", "3.10t"), |
| 214 |
("foo-3.14-bar", "3.14"), |
| 215 |
], |
| 216 |
ids=lambda a: "|".join(a) if isinstance(a, list) else str(a), |
| 217 |
) |
| 218 |
def test_extract_base_python(env: str, base_python: str | None) -> None: |
| 219 |
result = Python.extract_base_python(env) |
| 220 |
assert result == base_python |
| 221 |
|
| 222 |
|
| 223 |
@pytest.mark.parametrize("ignore_conflict", [True, False]) |
| 224 |
@pytest.mark.parametrize( |
| 225 |
("env", "base_python"), |
| 226 |
[ |
| 227 |
("magic", ["pypy"]), |
| 228 |
("magic", ["py39"]), |
| 229 |
], |
| 230 |
ids=lambda a: "|".join(a) if isinstance(a, list) else str(a), |
| 231 |
) |
| 232 |
def test_base_python_env_no_conflict(env: str, base_python: list[str], ignore_conflict: bool) -> None: |
| 233 |
result = Python._validate_base_python(env, base_python, ignore_conflict) # noqa: SLF001 |
| 234 |
assert result is base_python |
| 235 |
|
| 236 |
|
| 237 |
@pytest.mark.parametrize("ignore_conflict", [True, False]) |
| 238 |
@pytest.mark.parametrize( |
| 239 |
("env", "base_python", "expected", "conflict"), |
| 240 |
[ |
| 241 |
("pypy", ["cpython"], "pypy", ["cpython"]), |
| 242 |
("pypy2", ["pypy3"], "pypy2", ["pypy3"]), |
| 243 |
("py3", ["py2"], "py3", ["py2"]), |
| 244 |
("py38", ["py39"], "py38", ["py39"]), |
| 245 |
("py38", ["py38", "py39"], "py38", ["py39"]), |
| 246 |
("py38", ["python3"], "py38", ["python3"]), |
| 247 |
("py310", ["py38", "py39"], "py310", ["py38", "py39"]), |
| 248 |
("py3.11", ["py310"], "py3.11", ["py310"]), |
| 249 |
("py310-magic", ["py39"], "py310", ["py39"]), |
| 250 |
], |
| 251 |
ids=lambda a: "|".join(a) if isinstance(a, list) else str(a), |
| 252 |
) |
| 253 |
def test_base_python_env_conflict( |
| 254 |
env: str, |
| 255 |
base_python: list[str], |
| 256 |
expected: str, |
| 257 |
conflict: list[str], |
| 258 |
ignore_conflict: bool, |
| 259 |
) -> None: |
| 260 |
if ignore_conflict: |
| 261 |
result = Python._validate_base_python(env, base_python, ignore_conflict) # noqa: SLF001 |
| 262 |
assert result == [expected] |
| 263 |
else: |
| 264 |
msg = f"env name {env} conflicting with base python {conflict[0]}" |
| 265 |
with pytest.raises(Fail, match=msg): |
| 266 |
Python._validate_base_python(env, base_python, ignore_conflict) # noqa: SLF001 |
| 267 |
|
| 268 |
|
| 269 |
@pytest.mark.parametrize("ignore_conflict", [True, False, None]) |
| 270 |
def test_base_python_env_conflict_show_conf(tox_project: ToxProjectCreator, ignore_conflict: bool) -> None: |
| 271 |
py_ver = "".join(str(i) for i in sys.version_info[0:2]) |
| 272 |
py_ver_next = "".join(str(i) for i in (sys.version_info[0], sys.version_info[1] + 2)) |
| 273 |
ini = f"[testenv]\npackage=skip\nbase_python=py{py_ver_next}" |
| 274 |
if ignore_conflict is not None: |
| 275 |
ini += f"\n[tox]\nignore_base_python_conflict={ignore_conflict}" |
| 276 |
project = tox_project({"tox.ini": ini}) |
| 277 |
result = project.run("c", "-e", f"py{py_ver}", "-k", "base_python", raise_on_config_fail=False) |
| 278 |
if ignore_conflict: |
| 279 |
result.assert_success() |
| 280 |
else: |
| 281 |
result.assert_failed(code=-1) |
| 282 |
if ignore_conflict: |
| 283 |
out = f"[testenv:py{py_ver}]\nbase_python = py{py_ver}\n" |
| 284 |
else: |
| 285 |
comma_in_exc = sys.version_info[0:2] <= (3, 6) |
| 286 |
out = ( |
| 287 |
f"[testenv:py{py_ver}]\nbase_python = # Exception: Fail('env name py{py_ver} conflicting with" |
| 288 |
f" base python py{py_ver_next}'{',' if comma_in_exc else ''})\n" |
| 289 |
) |
| 290 |
result.assert_out_err(out, "") |
| 291 |
|
| 292 |
|
| 293 |
def test_python_set_hash_seed(tox_project: ToxProjectCreator) -> None: |
| 294 |
ini = "[testenv]\npackage=skip\ncommands=python -c 'import os; print(os.environ[\"PYTHONHASHSEED\"])'" |
| 295 |
prj = tox_project({"tox.ini": ini}) |
| 296 |
result = prj.run("r", "-e", "py", "--hashseed", "10") |
| 297 |
result.assert_success() |
| 298 |
assert result.out.splitlines()[1] == "10" |
| 299 |
|
| 300 |
|
| 301 |
def test_python_generate_hash_seed(tox_project: ToxProjectCreator) -> None: |
| 302 |
ini = "[testenv]\npackage=skip\ncommands=python -c 'import os; print(os.environ[\"PYTHONHASHSEED\"])'" |
| 303 |
prj = tox_project({"tox.ini": ini}) |
| 304 |
result = prj.run("r", "-e", "py") |
| 305 |
result.assert_success() |
| 306 |
assert 1 <= int(result.out.splitlines()[1]) <= (1024 if sys.platform == "win32" else 4294967295) |
| 307 |
|
| 308 |
|
| 309 |
def test_python_keep_hash_seed(tox_project: ToxProjectCreator) -> None: |
| 310 |
ini = """ |
| 311 |
[testenv] |
| 312 |
package=skip |
| 313 |
set_env=PYTHONHASHSEED=12 |
| 314 |
commands=python -c 'import os; print(os.environ["PYTHONHASHSEED"])' |
| 315 |
""" |
| 316 |
result = tox_project({"tox.ini": ini}).run("r", "-e", "py") |
| 317 |
result.assert_success() |
| 318 |
assert result.out.splitlines()[1] == "12" |
| 319 |
|
| 320 |
|
| 321 |
def test_python_hash_seed_via_section_substitution(tox_project: ToxProjectCreator) -> None: |
| 322 |
ini = """ |
| 323 |
[testenv] |
| 324 |
package=skip |
| 325 |
set_env=PYTHONHASHSEED=12 |
| 326 |
[testenv:hs] |
| 327 |
commands=python -c 'import os; print(os.environ["PYTHONHASHSEED"])' |
| 328 |
set_env={[testenv]set_env} |
| 329 |
""" |
| 330 |
result = tox_project({"tox.ini": ini}).run("r", "-e", "hs") |
| 331 |
result.assert_success() |
| 332 |
assert result.out.splitlines()[1] == "12" |
| 333 |
|
| 334 |
|
| 335 |
def test_python_disable_hash_seed(tox_project: ToxProjectCreator) -> None: |
| 336 |
ini = "[testenv]\npackage=skip\ncommands=python -c 'import os; print(os.environ.get(\"PYTHONHASHSEED\"))'" |
| 337 |
prj = tox_project({"tox.ini": ini}) |
| 338 |
result = prj.run("r", "-e", "py", "--hashseed", "notset") |
| 339 |
result.assert_success() |
| 340 |
assert result.out.splitlines()[1] == "None" |
| 341 |
|
| 342 |
|
| 343 |
def test_python_set_hash_seed_negative(tox_project: ToxProjectCreator) -> None: |
| 344 |
result = tox_project({"tox.ini": ""}).run("r", "-e", "py", "--hashseed", "-1") |
| 345 |
result.assert_failed(2) |
| 346 |
assert "tox run: error: argument --hashseed: must be greater than zero" in result.err |
| 347 |
|
| 348 |
|
| 349 |
def test_python_set_hash_seed_incorrect(tox_project: ToxProjectCreator) -> None: |
| 350 |
result = tox_project({"tox.ini": ""}).run("r", "-e", "py", "--hashseed", "ok") |
| 351 |
result.assert_failed(2) |
| 352 |
assert "tox run: error: argument --hashseed: invalid literal for int() with base 10: 'ok'" in result.err |
| 353 |
|
| 354 |
|
| 355 |
def test_python_use_hash_seed_from_env(tox_project: ToxProjectCreator) -> None: |
| 356 |
ini = "[testenv]\npackage=skip" |
| 357 |
with patch.dict("os.environ", {"PYTHONHASHSEED": "13"}): |
| 358 |
result = tox_project({"tox.ini": ini}).run("c", "-e", "py", "-k", "setenv") |
| 359 |
result.assert_success() |
| 360 |
assert "PYTHONHASHSEED=13" in result.out |
| 361 |
|
| 362 |
|
| 363 |
def test_python_hash_seed_from_env_random(tox_project: ToxProjectCreator) -> None: |
| 364 |
ini = "[testenv]\npackage=skip" |
| 365 |
with patch.dict("os.environ", {"PYTHONHASHSEED": "random"}): |
| 366 |
result = tox_project({"tox.ini": ini}).run("c", "-e", "py", "-k", "setenv") |
| 367 |
result.assert_success() |
| 368 |
assert "PYTHONHASHSEED=" in result.out |
| 369 |
|
| 370 |
|
| 371 |
def test_python_hash_seed_from_env_and_override(tox_project: ToxProjectCreator) -> None: |
| 372 |
ini = "[testenv]\npackage=skip\ncommands=python -c 'import os; print(os.environ.get(\"PYTHONHASHSEED\"))'" |
| 373 |
with patch.dict("os.environ", {"PYTHONHASHSEED": "14"}): |
| 374 |
result = tox_project({"tox.ini": ini}).run("r", "-e", "py", "--hashseed", "15") |
| 375 |
result.assert_success() |
| 376 |
assert result.out.splitlines()[1] == "15" |
| 377 |
|
| 378 |
|
| 379 |
def test_python_hash_seed_from_env_and_disable(tox_project: ToxProjectCreator) -> None: |
| 380 |
ini = "[testenv]\npackage=skip\ncommands=python -c 'import os; print(os.environ.get(\"PYTHONHASHSEED\"))'" |
| 381 |
with patch.dict("os.environ", {"PYTHONHASHSEED": "16"}): |
| 382 |
result = tox_project({"tox.ini": ini}).run("r", "-e", "py", "--hashseed", "notset") |
| 383 |
result.assert_success() |
| 384 |
assert result.out.splitlines()[1] == "None" |
| 385 |
|
| 386 |
|
| 387 |
@pytest.mark.parametrize("in_ci", [True, False]) |
| 388 |
def test_list_installed_deps(in_ci: bool, tox_project: ToxProjectCreator, mocker: MockerFixture) -> None: |
| 389 |
mocker.patch("tox.config.cli.parser.is_ci", return_value=in_ci) |
| 390 |
result = tox_project({"tox.ini": "[testenv]\nskip_install = true"}).run("r", "-e", "py") |
| 391 |
if in_ci: |
| 392 |
assert "pip==" in result.out |
| 393 |
else: |
| 394 |
assert "pip==" not in result.out |
| 395 |
|
| 396 |
|
| 397 |
@pytest.mark.parametrize("list_deps", ["--list-dependencies", "--no-list-dependencies"]) |
| 398 |
@pytest.mark.parametrize("in_ci", [True, False]) |
| 399 |
def test_list_installed_deps_explicit_cli( |
| 400 |
list_deps: str, |
| 401 |
in_ci: bool, |
| 402 |
tox_project: ToxProjectCreator, |
| 403 |
mocker: MockerFixture, |
| 404 |
) -> None: |
| 405 |
mocker.patch("tox.config.cli.parser.is_ci", return_value=in_ci) |
| 406 |
result = tox_project({"tox.ini": "[testenv]\nskip_install = true"}).run(list_deps, "r", "-e", "py") |
| 407 |
if list_deps == "--list-dependencies": |
| 408 |
assert "pip==" in result.out |
| 409 |
else: |
| 410 |
assert "pip==" not in result.out |
| 411 |
|
| 412 |
|
| 413 |
def test_usedevelop_with_nonexistent_basepython(tox_project: ToxProjectCreator) -> None: |
| 414 |
ini = "[testenv]\nusedevelop = true\n[testenv:unused]\nbasepython = /nonexistent/bin/python" |
| 415 |
project = tox_project({"tox.ini": ini}) |
| 416 |
result = project.run() |
| 417 |
assert result.code == 0 |
| 418 |
|
| 419 |
|
| 420 |
def test_default_base_python_used_when_no_factor(tox_project: ToxProjectCreator) -> None: |
| 421 |
py_ver = f"{sys.version_info[0]}.{sys.version_info[1]}" |
| 422 |
toml = f"""\ |
| 423 |
env_list = ["lint"] |
| 424 |
|
| 425 |
[env_run_base] |
| 426 |
package = "skip" |
| 427 |
default_base_python = ["python{py_ver}"] |
| 428 |
commands = [["python", "-c", "print('ok')"]] |
| 429 |
""" |
| 430 |
result = tox_project({"tox.toml": toml}).run("c", "-e", "lint", "-k", "base_python") |
| 431 |
result.assert_success() |
| 432 |
assert f"python{py_ver}" in result.out |
| 433 |
|
| 434 |
|
| 435 |
def test_default_base_python_ignored_when_factor_present(tox_project: ToxProjectCreator) -> None: |
| 436 |
py_ver = f"{sys.version_info[0]}{sys.version_info[1]}" |
| 437 |
toml = f"""\ |
| 438 |
env_list = ["py{py_ver}"] |
| 439 |
|
| 440 |
[env_run_base] |
| 441 |
package = "skip" |
| 442 |
default_base_python = ["python3.8"] |
| 443 |
commands = [["python", "-c", "print('ok')"]] |
| 444 |
""" |
| 445 |
result = tox_project({"tox.toml": toml}).run("c", "-e", f"py{py_ver}", "-k", "base_python") |
| 446 |
result.assert_success() |
| 447 |
assert f"py{py_ver}" in result.out |
| 448 |
assert "python3.8" not in result.out |
| 449 |
|
| 450 |
|
| 451 |
def test_default_base_python_ignored_when_base_python_explicit(tox_project: ToxProjectCreator) -> None: |
| 452 |
py_ver = f"{sys.version_info[0]}.{sys.version_info[1]}" |
| 453 |
toml = f"""\ |
| 454 |
env_list = ["lint"] |
| 455 |
|
| 456 |
[env_run_base] |
| 457 |
package = "skip" |
| 458 |
default_base_python = ["python3.8"] |
| 459 |
commands = [["python", "-c", "print('ok')"]] |
| 460 |
|
| 461 |
[env.lint] |
| 462 |
base_python = ["python{py_ver}"] |
| 463 |
""" |
| 464 |
result = tox_project({"tox.toml": toml}).run("c", "-e", "lint", "-k", "base_python") |
| 465 |
result.assert_success() |
| 466 |
assert f"python{py_ver}" in result.out |
| 467 |
assert "python3.8" not in result.out |
| 468 |
|
| 469 |
|
| 470 |
def test_base_python_string_in_toml(tox_project: ToxProjectCreator) -> None: |
| 471 |
py_ver = f"{sys.version_info[0]}.{sys.version_info[1]}" |
| 472 |
toml = f"""\ |
| 473 |
env_list = ["lint"] |
| 474 |
|
| 475 |
[env_run_base] |
| 476 |
package = "skip" |
| 477 |
commands = [["python", "-c", "print('ok')"]] |
| 478 |
|
| 479 |
[env.lint] |
| 480 |
base_python = "python{py_ver}" |
| 481 |
""" |
| 482 |
result = tox_project({"tox.toml": toml}).run("c", "-e", "lint", "-k", "base_python") |
| 483 |
result.assert_success() |
| 484 |
assert f"python{py_ver}" in result.out |
| 485 |
|
| 486 |
|
| 487 |
def test_default_base_python_string_in_toml(tox_project: ToxProjectCreator) -> None: |
| 488 |
py_ver = f"{sys.version_info[0]}.{sys.version_info[1]}" |
| 489 |
toml = f"""\ |
| 490 |
env_list = ["lint"] |
| 491 |
|
| 492 |
[env_run_base] |
| 493 |
package = "skip" |
| 494 |
default_base_python = "python{py_ver}" |
| 495 |
commands = [["python", "-c", "print('ok')"]] |
| 496 |
""" |
| 497 |
result = tox_project({"tox.toml": toml}).run("c", "-e", "lint", "-k", "base_python") |
| 498 |
result.assert_success() |
| 499 |
assert f"python{py_ver}" in result.out |
| 500 |
|
| 501 |
|
| 502 |
def test_default_base_python_falls_back_to_sys_executable(tox_project: ToxProjectCreator) -> None: |
| 503 |
toml = """\ |
| 504 |
env_list = ["lint"] |
| 505 |
|
| 506 |
[env_run_base] |
| 507 |
package = "skip" |
| 508 |
commands = [["python", "-c", "print('ok')"]] |
| 509 |
""" |
| 510 |
result = tox_project({"tox.toml": toml}).run("c", "-e", "lint", "-k", "base_python") |
| 511 |
result.assert_success() |
| 512 |
assert sys.executable in result.out |
| 513 |
|
| 514 |
|
| 515 |
@pytest.mark.parametrize( |
| 516 |
("impl", "major", "minor", "arch", "free_threaded", "platform_str", "expected_machine"), |
| 517 |
[ |
| 518 |
("cpython", 3, 12, 64, None, "linux-x86_64", "x86_64"), |
| 519 |
("cpython", 3, 13, 64, True, "linux-aarch64", "arm64"), |
| 520 |
("cpython", 3, 13, 64, False, "win-amd64", "x86_64"), |
| 521 |
("pypy", 3, 9, 32, None, "win32", None), |
| 522 |
("cpython", 3, 12, 64, None, "macosx-14.0-arm64", "arm64"), |
| 523 |
], |
| 524 |
) |
| 525 |
def test_python_spec_for_sys_executable( |
| 526 |
impl: str, |
| 527 |
major: int, |
| 528 |
minor: int, |
| 529 |
arch: int, |
| 530 |
free_threaded: bool | None, |
| 531 |
platform_str: str, |
| 532 |
expected_machine: str, |
| 533 |
mocker: MockerFixture, |
| 534 |
) -> None: |
| 535 |
get_config_var_ = sysconfig.get_config_var |
| 536 |
|
| 537 |
def get_config_var(name: str) -> object: |
| 538 |
if name == "Py_GIL_DISABLED": |
| 539 |
return free_threaded |
| 540 |
return get_config_var_(name) |
| 541 |
|
| 542 |
version_info = SimpleNamespace(major=major, minor=minor, micro=5, releaselevel="final", serial=0) |
| 543 |
implementation = SimpleNamespace( |
| 544 |
name=impl, |
| 545 |
cache_tag=f"{impl}-{major}{minor}", |
| 546 |
version=version_info, |
| 547 |
hexversion=..., |
| 548 |
_multiarch=..., |
| 549 |
) |
| 550 |
mocker.patch.object(sys, "version_info", version_info) |
| 551 |
mocker.patch.object(sys, "implementation", implementation) |
| 552 |
mocker.patch.object(sys, "maxsize", 2**arch // 2 - 1) |
| 553 |
mocker.patch.object(sysconfig, "get_config_var", get_config_var) |
| 554 |
mocker.patch.object(sysconfig, "get_platform", return_value=platform_str) |
| 555 |
spec = Python._python_spec_for_sys_executable() # noqa: SLF001 |
| 556 |
assert spec.implementation == impl |
| 557 |
assert spec.major == major |
| 558 |
assert spec.minor == minor |
| 559 |
assert spec.architecture == arch |
| 560 |
assert spec.free_threaded == bool(free_threaded) |
| 561 |
assert spec.machine == expected_machine |
| 562 |
|
| 563 |
|
| 564 |
_PY_VER = f"{sys.version_info[0]}.{sys.version_info[1]}" |
| 565 |
_PY_VER_NODOT = f"{sys.version_info[0]}{sys.version_info[1]}" |
| 566 |
|
| 567 |
|
| 568 |
@pytest.mark.parametrize( |
| 569 |
("config_file", "config", "version_content", "env", "expected"), |
| 570 |
[ |
| 571 |
pytest.param( |
| 572 |
"tox.ini", |
| 573 |
"[testenv:mypy]\npackage=skip\nbase_python_file=.python-version\n", |
| 574 |
f"{_PY_VER}\n", |
| 575 |
"mypy", |
| 576 |
_PY_VER, |
| 577 |
id="ini", |
| 578 |
), |
| 579 |
pytest.param( |
| 580 |
"tox.toml", |
| 581 |
'env_list = ["mypy"]\n\n[env.mypy]\npackage = "skip"\nbase_python_file = ".python-version"\n', |
| 582 |
f"{_PY_VER}\n", |
| 583 |
"mypy", |
| 584 |
_PY_VER, |
| 585 |
id="toml", |
| 586 |
), |
| 587 |
pytest.param( |
| 588 |
"tox.ini", |
| 589 |
"[testenv:mypy]\npackage=skip\nbase_python_file=.python-version\n", |
| 590 |
f"# managed by tool\n\n{_PY_VER}\n", |
| 591 |
"mypy", |
| 592 |
_PY_VER, |
| 593 |
id="skips-comments", |
| 594 |
), |
| 595 |
pytest.param( |
| 596 |
"tox.ini", |
| 597 |
"[testenv]\npackage=skip\nbase_python_file=.python-version\n", |
| 598 |
"3.8\n", |
| 599 |
f"py{_PY_VER_NODOT}", |
| 600 |
f"py{_PY_VER_NODOT}", |
| 601 |
id="env-factor-wins", |
| 602 |
), |
| 603 |
], |
| 604 |
) |
| 605 |
def test_base_python_file( |
| 606 |
tox_project: ToxProjectCreator, |
| 607 |
config_file: str, |
| 608 |
config: str, |
| 609 |
version_content: str, |
| 610 |
env: str, |
| 611 |
expected: str, |
| 612 |
) -> None: |
| 613 |
project = tox_project({config_file: config, ".python-version": version_content}) |
| 614 |
result = project.run("c", "-e", env, "-k", "base_python") |
| 615 |
result.assert_success() |
| 616 |
assert expected in result.out |
| 617 |
|
| 618 |
|
| 619 |
@pytest.mark.parametrize( |
| 620 |
("extra_files", "error_fragment"), |
| 621 |
[ |
| 622 |
pytest.param({}, "no valid Python version found", id="not-found"), |
| 623 |
pytest.param({".python-version": "# just a comment\n"}, "no valid Python version found", id="empty"), |
| 624 |
], |
| 625 |
) |
| 626 |
def test_base_python_file_error( |
| 627 |
tox_project: ToxProjectCreator, extra_files: dict[str, str], error_fragment: str |
| 628 |
) -> None: |
| 629 |
ini = "[testenv:mypy]\npackage=skip\nbase_python_file=.python-version\n" |
| 630 |
project = tox_project({"tox.ini": ini, **extra_files}) |
| 631 |
result = project.run("c", "-e", "mypy", "-k", "base_python", raise_on_config_fail=False) |
| 632 |
result.assert_failed(code=-1) |
| 633 |
assert error_fragment in result.out |
| 634 |
|
| 635 |
|
| 636 |
def test_base_python_file_conflict(tox_project: ToxProjectCreator) -> None: |
| 637 |
"""Setting both base_python and base_python_file is an error.""" |
| 638 |
ini = f"[testenv:mypy]\npackage=skip\nbase_python=python{_PY_VER}\nbase_python_file=.python-version\n" |
| 639 |
project = tox_project({"tox.ini": ini, ".python-version": f"{_PY_VER}\n"}) |
| 640 |
result = project.run("c", "-e", "mypy", "-k", "base_python", raise_on_config_fail=False) |
| 641 |
result.assert_failed(code=-1) |
| 642 |
assert "cannot set both base_python and base_python_file" in result.out |
| 643 |
|
| 644 |
|
| 645 |
def test_base_python_file_global_testenv(tox_project: ToxProjectCreator) -> None: |
| 646 |
"""base_python_file set in [testenv] applies to all non-factor envs.""" |
| 647 |
ini = f"[tox]\nenv_list=mypy,py{_PY_VER_NODOT}\n[testenv]\npackage=skip\nbase_python_file=.python-version\n" |
| 648 |
project = tox_project({"tox.ini": ini, ".python-version": f"{_PY_VER}\n"}) |
| 649 |
result = project.run("c", "-e", "mypy", "-k", "base_python") |
| 650 |
result.assert_success() |
| 651 |
assert _PY_VER in result.out |
| 652 |
result = project.run("c", "-e", f"py{_PY_VER_NODOT}", "-k", "base_python") |
| 653 |
result.assert_success() |
| 654 |
assert f"py{_PY_VER_NODOT}" in result.out |
| 655 |
|
| 656 |
|
| 657 |
def test_base_python_file_not_used_when_not_set(tox_project: ToxProjectCreator) -> None: |
| 658 |
toml = 'env_list = ["lint"]\n\n[env_run_base]\npackage = "skip"\ncommands = [["python", "-c", "print(1)"]]\n' |
| 659 |
result = tox_project({"tox.toml": toml}).run("c", "-e", "lint", "-k", "base_python") |
| 660 |
result.assert_success() |
| 661 |
assert sys.executable in result.out |