|
4 | 4 | import os |
5 | 5 | import os.path |
6 | 6 | import pathlib |
| 7 | +import platform |
7 | 8 | import subprocess # noqa: S404 |
8 | 9 | import sys |
9 | 10 | from configparser import ConfigParser |
10 | 11 | from importlib.metadata import version |
11 | 12 | from typing import TYPE_CHECKING |
12 | 13 |
|
| 14 | +import pytest |
| 15 | + |
13 | 16 | if TYPE_CHECKING: |
14 | | - import pytest |
15 | 17 | from tox.pytest import ToxProjectCreator |
16 | 18 |
|
17 | 19 |
|
@@ -50,6 +52,68 @@ def test_uv_venv_spec_major_only(tox_project: ToxProjectCreator) -> None: |
50 | 52 | result.assert_success() |
51 | 53 |
|
52 | 54 |
|
| 55 | +@pytest.fixture |
| 56 | +def other_interpreter_exe() -> pathlib.Path: # pragma: no cover |
| 57 | + """Returns an interpreter executable path that is not the exact same as `sys.executable`. |
| 58 | +
|
| 59 | + Necessary because `sys.executable` gets short-circuited when used as `base_python`.""" |
| 60 | + |
| 61 | + exe = pathlib.Path(sys.executable) |
| 62 | + base_python: pathlib.Path | None = None |
| 63 | + if exe.name == "python": |
| 64 | + # python -> pythonX.Y |
| 65 | + ver = sys.version_info |
| 66 | + base_python = exe.with_name(f"python{ver.major}.{ver.minor}") |
| 67 | + elif exe.name[-1].isdigit(): |
| 68 | + # python X[.Y] -> python |
| 69 | + base_python = exe.with_name(exe.stem[:-1]) |
| 70 | + elif exe.suffix == ".exe": |
| 71 | + # python.exe <-> pythonw.exe |
| 72 | + base_python = ( |
| 73 | + exe.with_name(exe.stem[:-1] + ".exe") if exe.stem.endswith("w") else exe.with_name(exe.stem + "w.exe") |
| 74 | + ) |
| 75 | + if not base_python or not base_python.is_file(): |
| 76 | + pytest.fail("Tried to pick a base_python that is not sys.executable, but failed.") |
| 77 | + return base_python |
| 78 | + |
| 79 | + |
| 80 | +def test_uv_venv_spec_abs_path(tox_project: ToxProjectCreator, other_interpreter_exe: pathlib.Path) -> None: |
| 81 | + project = tox_project({"tox.ini": f"[testenv]\npackage=skip\nbase_python={other_interpreter_exe}"}) |
| 82 | + result = project.run("-vv") |
| 83 | + result.assert_success() |
| 84 | + |
| 85 | + |
| 86 | +def test_uv_venv_spec_abs_path_conflict_ver( |
| 87 | + tox_project: ToxProjectCreator, other_interpreter_exe: pathlib.Path |
| 88 | +) -> None: |
| 89 | + # py27 is long gone, but still matches the testenv capture regex, so we know it will fail |
| 90 | + project = tox_project({"tox.ini": f"[testenv:py27]\npackage=skip\nbase_python={other_interpreter_exe}"}) |
| 91 | + result = project.run("-vv", "-e", "py27") |
| 92 | + result.assert_failed() |
| 93 | + assert f"failed with env name py27 conflicting with base python {other_interpreter_exe}" in result.out |
| 94 | + |
| 95 | + |
| 96 | +def test_uv_venv_spec_abs_path_conflict_impl( |
| 97 | + tox_project: ToxProjectCreator, other_interpreter_exe: pathlib.Path |
| 98 | +) -> None: |
| 99 | + env = "pypy" if platform.python_implementation() == "CPython" else "cpython" |
| 100 | + project = tox_project({"tox.ini": f"[testenv:{env}]\npackage=skip\nbase_python={other_interpreter_exe}"}) |
| 101 | + result = project.run("-vv", "-e", env) |
| 102 | + result.assert_failed() |
| 103 | + assert f"failed with env name {env} conflicting with base python {other_interpreter_exe}" in result.out |
| 104 | + |
| 105 | + |
| 106 | +def test_uv_venv_spec_abs_path_conflict_platform( |
| 107 | + tox_project: ToxProjectCreator, other_interpreter_exe: pathlib.Path |
| 108 | +) -> None: |
| 109 | + ver = sys.version_info |
| 110 | + env = f"py{ver.major}{ver.minor}-linux" if sys.platform == "win32" else f"py{ver.major}{ver.minor}-win32" |
| 111 | + project = tox_project({"tox.ini": f"[testenv:{env}]\npackage=skip\nbase_python={other_interpreter_exe}"}) |
| 112 | + result = project.run("-vv", "-e", env) |
| 113 | + result.assert_failed() |
| 114 | + assert f"failed with env name {env} conflicting with base python {other_interpreter_exe}" in result.out |
| 115 | + |
| 116 | + |
53 | 117 | def test_uv_venv_na(tox_project: ToxProjectCreator) -> None: |
54 | 118 | project = tox_project({"tox.ini": "[testenv]\npackage=skip\nbase_python=1.0"}) |
55 | 119 | result = project.run("-vv") |
|
0 commit comments