| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
import textwrap |
| 5 |
from pathlib import Path |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from tox.tox_env.python.virtual_env.subprocess_adapter import ( |
| 11 |
SubprocessCreator, |
| 12 |
SubprocessPythonInfo, |
| 13 |
SubprocessSession, |
| 14 |
_bootstrap_path, # noqa: PLC2701 |
| 15 |
_has_correct_virtualenv, # noqa: PLC2701 |
| 16 |
_VersionInfo, # noqa: PLC2701 |
| 17 |
ensure_bootstrap, |
| 18 |
probe_python, |
| 19 |
) |
| 20 |
|
| 21 |
if TYPE_CHECKING: |
| 22 |
from pytest_mock import MockerFixture |
| 23 |
|
| 24 |
from tox.pytest import ToxProjectCreator |
| 25 |
|
| 26 |
|
| 27 |
def test_probe_python_current_interpreter() -> None: |
| 28 |
info = probe_python(sys.executable) |
| 29 |
assert info is not None |
| 30 |
assert info.version_info.major == sys.version_info.major |
| 31 |
assert info.version_info.minor == sys.version_info.minor |
| 32 |
assert info.version_info.micro == sys.version_info.micro |
| 33 |
assert info.implementation == sys.implementation.name |
| 34 |
assert info.architecture in {32, 64} |
| 35 |
assert info.system_executable |
| 36 |
assert info.sysconfig_platform is not None |
| 37 |
assert info.machine # should be non-empty on all platforms |
| 38 |
|
| 39 |
|
| 40 |
def test_probe_python_nonexistent() -> None: |
| 41 |
assert probe_python("/nonexistent/python999") is None |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize( |
| 45 |
("sysconfig_platform", "expected_machine"), |
| 46 |
[ |
| 47 |
("linux-x86_64", "x86_64"), |
| 48 |
("linux-aarch64", "aarch64"), |
| 49 |
("win-amd64", "amd64"), |
| 50 |
("macosx-14.0-arm64", "arm64"), |
| 51 |
("win32", ""), |
| 52 |
(None, ""), |
| 53 |
], |
| 54 |
) |
| 55 |
def test_subprocess_python_info_machine(sysconfig_platform: str | None, expected_machine: str) -> None: |
| 56 |
vi = _VersionInfo(major=3, minor=12, micro=0, releaselevel="final", serial=0) |
| 57 |
info = SubprocessPythonInfo( |
| 58 |
implementation="cpython", |
| 59 |
version_info=vi, |
| 60 |
version="3.12.0", |
| 61 |
architecture=64, |
| 62 |
platform="linux", |
| 63 |
system_executable="/usr/bin/python3", |
| 64 |
free_threaded=False, |
| 65 |
sysconfig_platform=sysconfig_platform, |
| 66 |
) |
| 67 |
assert info.machine == expected_machine |
| 68 |
|
| 69 |
|
| 70 |
def test_probe_python_failing_script(tmp_path: Path) -> None: |
| 71 |
if sys.platform == "win32": |
| 72 |
bad_script = tmp_path / "bad_python.cmd" |
| 73 |
bad_script.write_text("@exit /b 1\n") |
| 74 |
else: |
| 75 |
bad_script = tmp_path / "bad_python" |
| 76 |
bad_script.write_text("#!/bin/sh\nexit 1\n") |
| 77 |
bad_script.chmod(0o755) |
| 78 |
assert probe_python(str(bad_script)) is None |
| 79 |
|
| 80 |
|
| 81 |
def test_subprocess_creator_paths_unix(tmp_path: Path) -> None: |
| 82 |
vi = _VersionInfo(major=3, minor=11, micro=0, releaselevel="final", serial=0) |
| 83 |
env_dir = tmp_path / "env" |
| 84 |
info = SubprocessPythonInfo( |
| 85 |
implementation="cpython", |
| 86 |
version_info=vi, |
| 87 |
version="3.11.0", |
| 88 |
architecture=64, |
| 89 |
platform="linux", |
| 90 |
system_executable="/usr/bin/python3.11", |
| 91 |
free_threaded=False, |
| 92 |
) |
| 93 |
creator = SubprocessCreator(_env_dir=env_dir, interpreter=info, _is_win=False) |
| 94 |
assert creator.bin_dir == env_dir / "bin" |
| 95 |
assert creator.script_dir == env_dir / "bin" |
| 96 |
assert creator.purelib == env_dir / "lib" / "python3.11" / "site-packages" |
| 97 |
assert creator.platlib == env_dir / "lib" / "python3.11" / "site-packages" |
| 98 |
assert creator.exe == env_dir / "bin" / "python" |
| 99 |
|
| 100 |
|
| 101 |
def test_subprocess_creator_paths_windows(tmp_path: Path) -> None: |
| 102 |
vi = _VersionInfo(major=3, minor=11, micro=0, releaselevel="final", serial=0) |
| 103 |
env_dir = tmp_path / "env" |
| 104 |
info = SubprocessPythonInfo( |
| 105 |
implementation="cpython", |
| 106 |
version_info=vi, |
| 107 |
version="3.11.0", |
| 108 |
architecture=64, |
| 109 |
platform="win32", |
| 110 |
system_executable="C:\\Python311\\python.exe", |
| 111 |
free_threaded=False, |
| 112 |
) |
| 113 |
creator = SubprocessCreator(_env_dir=env_dir, interpreter=info, _is_win=True) |
| 114 |
assert creator.bin_dir == env_dir / "Scripts" |
| 115 |
assert creator.purelib == env_dir / "Lib" / "site-packages" |
| 116 |
assert creator.exe == env_dir / "Scripts" / "python.exe" |
| 117 |
|
| 118 |
|
| 119 |
def test_subprocess_session_run_not_found(tmp_path: Path) -> None: |
| 120 |
vi = _VersionInfo(major=3, minor=11, micro=0, releaselevel="final", serial=0) |
| 121 |
info = SubprocessPythonInfo( |
| 122 |
implementation="cpython", |
| 123 |
version_info=vi, |
| 124 |
version="3.11.0", |
| 125 |
architecture=64, |
| 126 |
platform=sys.platform, |
| 127 |
system_executable=sys.executable, |
| 128 |
free_threaded=False, |
| 129 |
) |
| 130 |
session = SubprocessSession( |
| 131 |
env_dir=tmp_path / "env", |
| 132 |
bootstrap_python=Path("/nonexistent/python"), |
| 133 |
env_vars={}, |
| 134 |
interpreter=info, |
| 135 |
) |
| 136 |
with pytest.raises(RuntimeError, match="virtualenv subprocess failed"): |
| 137 |
session.run() |
| 138 |
|
| 139 |
|
| 140 |
def test_subprocess_session_run_nonzero_exit(tmp_path: Path, mocker: MockerFixture) -> None: |
| 141 |
vi = _VersionInfo(major=3, minor=11, micro=0, releaselevel="final", serial=0) |
| 142 |
info = SubprocessPythonInfo( |
| 143 |
implementation="cpython", |
| 144 |
version_info=vi, |
| 145 |
version="3.11.0", |
| 146 |
architecture=64, |
| 147 |
platform=sys.platform, |
| 148 |
system_executable=sys.executable, |
| 149 |
free_threaded=False, |
| 150 |
) |
| 151 |
session = SubprocessSession( |
| 152 |
env_dir=tmp_path / "env", |
| 153 |
bootstrap_python=Path(sys.executable), |
| 154 |
env_vars={}, |
| 155 |
interpreter=info, |
| 156 |
) |
| 157 |
mocker.patch( |
| 158 |
"tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run", |
| 159 |
return_value=mocker.MagicMock(returncode=1, stderr="error"), |
| 160 |
) |
| 161 |
with pytest.raises(RuntimeError, match="virtualenv subprocess failed \\(exit 1\\)"): |
| 162 |
session.run() |
| 163 |
|
| 164 |
|
| 165 |
def test_subprocess_session_no_interpreter(tmp_path: Path) -> None: |
| 166 |
session = SubprocessSession( |
| 167 |
env_dir=tmp_path / "env", |
| 168 |
bootstrap_python=tmp_path / "bootstrap" / "bin" / "python", |
| 169 |
env_vars={}, |
| 170 |
interpreter=None, |
| 171 |
) |
| 172 |
with pytest.raises(RuntimeError, match="no interpreter discovered"): |
| 173 |
_ = session.creator |
| 174 |
|
| 175 |
|
| 176 |
def test_bootstrap_path_deterministic(tmp_path: Path) -> None: |
| 177 |
path1 = _bootstrap_path(tmp_path, "virtualenv<20.22.0") |
| 178 |
path2 = _bootstrap_path(tmp_path, "virtualenv<20.22.0") |
| 179 |
path3 = _bootstrap_path(tmp_path, "virtualenv<21.0.0") |
| 180 |
assert path1 == path2 |
| 181 |
assert path1 != path3 |
| 182 |
assert path1.parent == tmp_path / ".virtualenv-bootstrap" |
| 183 |
|
| 184 |
|
| 185 |
def test_has_correct_virtualenv_nonexistent(tmp_path: Path) -> None: |
| 186 |
assert _has_correct_virtualenv(tmp_path / "nonexistent", "virtualenv") is False |
| 187 |
|
| 188 |
|
| 189 |
def test_has_correct_virtualenv_matching(tmp_path: Path, mocker: MockerFixture) -> None: |
| 190 |
python = tmp_path / "python" |
| 191 |
python.touch() |
| 192 |
run_mock = mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run") |
| 193 |
run_mock.return_value = mocker.MagicMock(returncode=0, stdout="20.21.1\n") |
| 194 |
assert _has_correct_virtualenv(python, "virtualenv<20.22.0") is True |
| 195 |
|
| 196 |
|
| 197 |
def test_has_correct_virtualenv_bare_spec(tmp_path: Path, mocker: MockerFixture) -> None: |
| 198 |
python = tmp_path / "python" |
| 199 |
python.touch() |
| 200 |
run_mock = mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run") |
| 201 |
run_mock.return_value = mocker.MagicMock(returncode=0, stdout="20.21.1\n") |
| 202 |
assert _has_correct_virtualenv(python, "virtualenv") is True |
| 203 |
|
| 204 |
|
| 205 |
def test_has_correct_virtualenv_subprocess_fails(tmp_path: Path, mocker: MockerFixture) -> None: |
| 206 |
python = tmp_path / "python" |
| 207 |
python.touch() |
| 208 |
run_mock = mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run") |
| 209 |
run_mock.return_value = mocker.MagicMock(returncode=1) |
| 210 |
assert _has_correct_virtualenv(python, "virtualenv<20.22.0") is False |
| 211 |
|
| 212 |
|
| 213 |
def test_has_correct_virtualenv_subprocess_error(tmp_path: Path, mocker: MockerFixture) -> None: |
| 214 |
python = tmp_path / "python" |
| 215 |
python.touch() |
| 216 |
mocker.patch( |
| 217 |
"tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run", |
| 218 |
side_effect=OSError("boom"), |
| 219 |
) |
| 220 |
assert _has_correct_virtualenv(python, "virtualenv<20.22.0") is False |
| 221 |
|
| 222 |
|
| 223 |
def test_ensure_bootstrap_creates_and_caches(tmp_path: Path, mocker: MockerFixture) -> None: |
| 224 |
venv_create = mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.venv.create") |
| 225 |
run_mock = mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run") |
| 226 |
run_mock.return_value = mocker.MagicMock(returncode=0, stdout="20.21.1\n") |
| 227 |
|
| 228 |
has_correct = mocker.patch( |
| 229 |
"tox.tox_env.python.virtual_env.subprocess_adapter._has_correct_virtualenv", |
| 230 |
side_effect=[False, False, True], |
| 231 |
) |
| 232 |
|
| 233 |
result = ensure_bootstrap(tmp_path, "virtualenv<20.22.0") |
| 234 |
assert result.name == ("python.exe" if sys.platform == "win32" else "python") |
| 235 |
venv_create.assert_called_once() |
| 236 |
run_mock.assert_called_once() |
| 237 |
|
| 238 |
result2 = ensure_bootstrap(tmp_path, "virtualenv<20.22.0") |
| 239 |
assert result == result2 |
| 240 |
assert has_correct.call_count == 3 |
| 241 |
|
| 242 |
|
| 243 |
def test_ensure_bootstrap_race_inside_lock(tmp_path: Path, mocker: MockerFixture) -> None: |
| 244 |
mocker.patch( |
| 245 |
"tox.tox_env.python.virtual_env.subprocess_adapter._has_correct_virtualenv", |
| 246 |
side_effect=[False, True], |
| 247 |
) |
| 248 |
result = ensure_bootstrap(tmp_path, "virtualenv<20.22.0") |
| 249 |
assert result.name == ("python.exe" if sys.platform == "win32" else "python") |
| 250 |
|
| 251 |
|
| 252 |
def test_ensure_bootstrap_removes_stale_base(tmp_path: Path, mocker: MockerFixture) -> None: |
| 253 |
base = _bootstrap_path(tmp_path, "virtualenv<20.22.0") |
| 254 |
base.mkdir(parents=True) |
| 255 |
(base / "stale_file").touch() |
| 256 |
|
| 257 |
mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.venv.create") |
| 258 |
run_mock = mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run") |
| 259 |
run_mock.return_value = mocker.MagicMock(returncode=1, stderr="fail") |
| 260 |
mocker.patch( |
| 261 |
"tox.tox_env.python.virtual_env.subprocess_adapter._has_correct_virtualenv", |
| 262 |
return_value=False, |
| 263 |
) |
| 264 |
|
| 265 |
with pytest.raises(RuntimeError, match="failed to install"): |
| 266 |
ensure_bootstrap(tmp_path, "virtualenv<20.22.0") |
| 267 |
assert not (base / "stale_file").exists() |
| 268 |
|
| 269 |
|
| 270 |
def test_ensure_bootstrap_install_failure(tmp_path: Path, mocker: MockerFixture) -> None: |
| 271 |
mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.venv.create") |
| 272 |
mocker.patch( |
| 273 |
"tox.tox_env.python.virtual_env.subprocess_adapter._has_correct_virtualenv", |
| 274 |
return_value=False, |
| 275 |
) |
| 276 |
run_mock = mocker.patch("tox.tox_env.python.virtual_env.subprocess_adapter.subprocess.run") |
| 277 |
run_mock.return_value = mocker.MagicMock(returncode=1, stderr="no matching distribution") |
| 278 |
|
| 279 |
with pytest.raises(RuntimeError, match="failed to install"): |
| 280 |
ensure_bootstrap(tmp_path, "virtualenv==999.999.999") |
| 281 |
|
| 282 |
|
| 283 |
def test_virtualenv_spec_config_shown(tox_project: ToxProjectCreator) -> None: |
| 284 |
proj = tox_project({ |
| 285 |
"tox.toml": textwrap.dedent("""\ |
| 286 |
[env_run_base] |
| 287 |
package = "skip" |
| 288 |
virtualenv_spec = "virtualenv<20.22.0" |
| 289 |
commands = [["python", "-c", "print('ok')"]] |
| 290 |
"""), |
| 291 |
}) |
| 292 |
result = proj.run("c", "-e", "py", "-k", "virtualenv_spec") |
| 293 |
result.assert_success() |
| 294 |
assert "virtualenv<20.22.0" in result.out |
| 295 |
|
| 296 |
|
| 297 |
def test_virtualenv_spec_cache_includes_spec(tox_project: ToxProjectCreator, mocker: MockerFixture) -> None: |
| 298 |
mocker.patch( |
| 299 |
"tox.tox_env.python.virtual_env.subprocess_adapter.ensure_bootstrap", |
| 300 |
return_value=Path(sys.executable), |
| 301 |
) |
| 302 |
mocker.patch( |
| 303 |
"tox.tox_env.python.virtual_env.subprocess_adapter.probe_python", |
| 304 |
return_value=SubprocessPythonInfo( |
| 305 |
implementation=sys.implementation.name, |
| 306 |
version_info=_VersionInfo(*sys.version_info[:5]), |
| 307 |
version=f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}", |
| 308 |
architecture=64, |
| 309 |
platform=sys.platform, |
| 310 |
system_executable=sys.executable, |
| 311 |
free_threaded=False, |
| 312 |
), |
| 313 |
) |
| 314 |
|
| 315 |
proj = tox_project({ |
| 316 |
"tox.toml": textwrap.dedent("""\ |
| 317 |
[env_run_base] |
| 318 |
package = "skip" |
| 319 |
virtualenv_spec = "virtualenv<20.22.0" |
| 320 |
commands = [["python", "-c", "print('ok')"]] |
| 321 |
"""), |
| 322 |
}) |
| 323 |
result = proj.run("r", "-e", "py") |
| 324 |
result.assert_success() |
| 325 |
|
| 326 |
|
| 327 |
def test_virtualenv_spec_empty_uses_imported(tox_project: ToxProjectCreator) -> None: |
| 328 |
proj = tox_project({ |
| 329 |
"tox.toml": textwrap.dedent("""\ |
| 330 |
[env_run_base] |
| 331 |
package = "skip" |
| 332 |
commands = [["python", "-c", "print('ok')"]] |
| 333 |
"""), |
| 334 |
}) |
| 335 |
result = proj.run("r", "-e", "py") |
| 336 |
result.assert_success() |
| 337 |
assert "ok" in result.out |