| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
from pathlib import Path |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
from unittest.mock import MagicMock, PropertyMock |
| 8 |
|
| 9 |
import pytest |
| 10 |
from virtualenv import __version__ as virtualenv_version |
| 11 |
from virtualenv import session_via_cli |
| 12 |
from virtualenv.config.cli.parser import VirtualEnvOptions |
| 13 |
|
| 14 |
from tox.tox_env.python.virtual_env.api import VirtualEnv |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from pytest_mock import MockerFixture |
| 18 |
|
| 19 |
from tox.execute import ExecuteRequest |
| 20 |
from tox.pytest import MonkeyPatch, ToxProject, ToxProjectCreator |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture |
| 24 |
def virtualenv_opt(monkeypatch: MonkeyPatch, mocker: MockerFixture) -> VirtualEnvOptions: |
| 25 |
for key in os.environ: |
| 26 |
if key.startswith("VIRTUALENV_"): # pragma: no cover |
| 27 |
monkeypatch.delenv(key) # pragma: no cover |
| 28 |
opts = VirtualEnvOptions() |
| 29 |
mocker.patch( |
| 30 |
"tox.tox_env.python.virtual_env.api.session_via_cli", |
| 31 |
side_effect=lambda args, options, setup_logging, env: session_via_cli( # noqa: ARG005 |
| 32 |
args, |
| 33 |
opts, |
| 34 |
setup_logging, |
| 35 |
env, |
| 36 |
), |
| 37 |
) |
| 38 |
return opts |
| 39 |
|
| 40 |
|
| 41 |
def test_virtualenv_default_settings(tox_project: ToxProjectCreator, virtualenv_opt: VirtualEnvOptions) -> None: |
| 42 |
proj = tox_project({"tox.ini": "[testenv]\npackage=skip"}) |
| 43 |
result = proj.run("r", "-e", "py3", "--discover", sys.executable, str(proj.path / "a")) |
| 44 |
result.assert_success() |
| 45 |
|
| 46 |
conf = result.env_conf("py3") |
| 47 |
assert conf["system_site_packages"] is False |
| 48 |
assert conf["always_copy"] is False |
| 49 |
assert conf["download"] is False |
| 50 |
|
| 51 |
assert virtualenv_opt.clear is False |
| 52 |
assert virtualenv_opt.system_site is False |
| 53 |
assert virtualenv_opt.download is False |
| 54 |
if hasattr(virtualenv_opt, "copies"): |
| 55 |
assert virtualenv_opt.copies is False |
| 56 |
assert virtualenv_opt.no_periodic_update is True |
| 57 |
assert virtualenv_opt.python == ["py3"] |
| 58 |
assert virtualenv_opt.try_first_with == [str(sys.executable), str(proj.path / "a")] |
| 59 |
|
| 60 |
|
| 61 |
def test_virtualenv_flipped_settings( |
| 62 |
tox_project: ToxProjectCreator, |
| 63 |
virtualenv_opt: VirtualEnvOptions, |
| 64 |
monkeypatch: MonkeyPatch, |
| 65 |
) -> None: |
| 66 |
proj = tox_project( |
| 67 |
{"tox.ini": "[testenv]\npackage=skip\nsystem_site_packages=True\nalways_copy=True\ndownload=True"}, |
| 68 |
) |
| 69 |
monkeypatch.setenv("VIRTUALENV_CLEAR", "0") |
| 70 |
|
| 71 |
result = proj.run("r", "-e", "py3") |
| 72 |
result.assert_success() |
| 73 |
|
| 74 |
conf = result.env_conf("py3") |
| 75 |
assert conf["system_site_packages"] is True |
| 76 |
assert conf["always_copy"] is True |
| 77 |
assert conf["download"] is True |
| 78 |
|
| 79 |
assert virtualenv_opt.clear is False |
| 80 |
assert virtualenv_opt.system_site is True |
| 81 |
assert virtualenv_opt.download is True |
| 82 |
if hasattr(virtualenv_opt, "copies"): |
| 83 |
assert virtualenv_opt.copies is True |
| 84 |
assert virtualenv_opt.python == ["py3"] |
| 85 |
|
| 86 |
|
| 87 |
def test_virtualenv_env_ignored_if_set( |
| 88 |
tox_project: ToxProjectCreator, |
| 89 |
virtualenv_opt: VirtualEnvOptions, |
| 90 |
monkeypatch: MonkeyPatch, |
| 91 |
) -> None: |
| 92 |
ini = "[testenv]\npackage=skip\nsystem_site_packages=True\nalways_copy=True\ndownload=True" |
| 93 |
proj = tox_project({"tox.ini": ini}) |
| 94 |
monkeypatch.setenv("VIRTUALENV_COPIES", "0") |
| 95 |
monkeypatch.setenv("VIRTUALENV_DOWNLOAD", "0") |
| 96 |
monkeypatch.setenv("VIRTUALENV_SYSTEM_SITE_PACKAGES", "0") |
| 97 |
run_and_check_set(proj, virtualenv_opt) |
| 98 |
|
| 99 |
|
| 100 |
def test_virtualenv_env_used_if_not_set( |
| 101 |
tox_project: ToxProjectCreator, |
| 102 |
virtualenv_opt: VirtualEnvOptions, |
| 103 |
monkeypatch: MonkeyPatch, |
| 104 |
) -> None: |
| 105 |
proj = tox_project({"tox.ini": "[testenv]\npackage=skip"}) |
| 106 |
monkeypatch.setenv("VIRTUALENV_COPIES", "1") |
| 107 |
monkeypatch.setenv("VIRTUALENV_DOWNLOAD", "1") |
| 108 |
monkeypatch.setenv("VIRTUALENV_SYSTEM_SITE_PACKAGES", "1") |
| 109 |
run_and_check_set(proj, virtualenv_opt) |
| 110 |
|
| 111 |
|
| 112 |
def run_and_check_set(proj: ToxProject, virtualenv_opt: VirtualEnvOptions) -> None: |
| 113 |
result = proj.run("r", "-e", "py") |
| 114 |
result.assert_success() |
| 115 |
conf = result.env_conf("py") |
| 116 |
assert conf["system_site_packages"] is True |
| 117 |
assert conf["always_copy"] is True |
| 118 |
assert conf["download"] is True |
| 119 |
assert virtualenv_opt.system_site is True |
| 120 |
assert virtualenv_opt.download is True |
| 121 |
if hasattr(virtualenv_opt, "copies"): |
| 122 |
assert virtualenv_opt.copies is True |
| 123 |
|
| 124 |
|
| 125 |
def test_honor_set_env_for_clear_periodic_update( |
| 126 |
tox_project: ToxProjectCreator, |
| 127 |
virtualenv_opt: VirtualEnvOptions, |
| 128 |
) -> None: |
| 129 |
ini = "[testenv]\npackage=skip\nset_env=\n VIRTUALENV_CLEAR=0\n VIRTUALENV_NO_PERIODIC_UPDATE=0" |
| 130 |
proj = tox_project({"tox.ini": ini}) |
| 131 |
result = proj.run("r", "-e", "py") |
| 132 |
result.assert_success() |
| 133 |
|
| 134 |
assert virtualenv_opt.clear is False |
| 135 |
assert virtualenv_opt.no_periodic_update is False |
| 136 |
|
| 137 |
|
| 138 |
def test_recreate_when_virtualenv_changes(tox_project: ToxProjectCreator, mocker: MockerFixture) -> None: |
| 139 |
proj = tox_project({"tox.ini": "[testenv]\npackage=skip"}) |
| 140 |
proj.run("r") |
| 141 |
|
| 142 |
from tox.tox_env.python.virtual_env import api # noqa: PLC0415 |
| 143 |
|
| 144 |
mocker.patch.object(api, "virtualenv_version", "1.0") |
| 145 |
result = proj.run("r") |
| 146 |
assert f"recreate env because python changed virtualenv version='{virtualenv_version}'->'1.0'" in result.out |
| 147 |
assert "remove tox env folder" in result.out |
| 148 |
|
| 149 |
|
| 150 |
@pytest.mark.parametrize("on", [True, False]) |
| 151 |
def test_pip_pre(tox_project: ToxProjectCreator, on: bool) -> None: |
| 152 |
proj = tox_project({"tox.ini": f"[testenv]\npackage=skip\npip_pre={on}\ndeps=magic"}) |
| 153 |
execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 154 |
result = proj.run("r", "-e", "py") |
| 155 |
result.assert_success() |
| 156 |
if on: |
| 157 |
assert "--pre" in execute_calls.call_args[0][3].cmd |
| 158 |
else: |
| 159 |
assert "--pre" not in execute_calls.call_args[0][3].cmd |
| 160 |
|
| 161 |
|
| 162 |
def test_install_command_no_packages(tox_project: ToxProjectCreator, disable_pip_pypi_access: tuple[str, str]) -> None: |
| 163 |
install_cmd = "python -m pip install -i {env:PIP_INDEX_URL}" |
| 164 |
proj = tox_project({"tox.ini": f"[testenv]\npackage=skip\ninstall_command={install_cmd}\npip_pre=true\ndeps=magic"}) |
| 165 |
execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 166 |
result = proj.run("r") |
| 167 |
result.assert_success() |
| 168 |
request: ExecuteRequest = execute_calls.call_args[0][3] |
| 169 |
found_cmd = request.cmd |
| 170 |
assert found_cmd == ["python", "-m", "pip", "install", "-i", disable_pip_pypi_access[0], "--pre", "magic"] |
| 171 |
|
| 172 |
|
| 173 |
@pytest.mark.slow |
| 174 |
def test_list_dependencies_command(tox_project: ToxProjectCreator) -> None: |
| 175 |
install_cmd = "python -m pip freeze" |
| 176 |
proj = tox_project({"tox.ini": f"[testenv]\npackage=skip\nlist_dependencies_command={install_cmd}"}) |
| 177 |
execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 178 |
result = proj.run("r", "--result-json", str(proj.path / "out.json")) |
| 179 |
result.assert_success() |
| 180 |
request: ExecuteRequest = execute_calls.call_args[0][3] |
| 181 |
assert request.cmd == ["python", "-m", "pip", "freeze"] |
| 182 |
|
| 183 |
|
| 184 |
def test_posargs_colon_in_inactive_env_does_not_crash(tox_project: ToxProjectCreator) -> None: |
| 185 |
project = tox_project({ |
| 186 |
"tox.toml": """ |
| 187 |
env_list = ["hello"] |
| 188 |
|
| 189 |
[env.hello] |
| 190 |
package = "skip" |
| 191 |
commands = [["python", "-c", "print('ok')"]] |
| 192 |
|
| 193 |
[env.dev] |
| 194 |
env_dir = "{posargs:venv}" |
| 195 |
package = "editable" |
| 196 |
commands = [["python", "-c", "print('dev')"]] |
| 197 |
""", |
| 198 |
}) |
| 199 |
outcome = project.run("r", "-e", "hello", "--", "x:y") |
| 200 |
outcome.assert_success() |
| 201 |
assert "ok" in outcome.out |
| 202 |
|
| 203 |
|
| 204 |
def test_env_site_packages_dir_plat(tox_project: ToxProjectCreator) -> None: |
| 205 |
toml = """\ |
| 206 |
[env_run_base] |
| 207 |
package = "skip" |
| 208 |
commands = [["python", "-c", "print('ok')"]] |
| 209 |
""" |
| 210 |
result = tox_project({"tox.toml": toml}).run("c", "-e", "py", "-k", "env_site_packages_dir_plat") |
| 211 |
result.assert_success() |
| 212 |
assert "site-packages" in result.out |
| 213 |
|
| 214 |
|
| 215 |
def test_pip_user_disabled(tox_project: ToxProjectCreator) -> None: |
| 216 |
proj = tox_project( |
| 217 |
{ |
| 218 |
"tox.toml": """ |
| 219 |
[env_run_base] |
| 220 |
package = "skip" |
| 221 |
commands = [ |
| 222 |
["python", "-c", "import os; print('PIP_USER=' + os.environ.get('PIP_USER', 'NOT_SET'))"] |
| 223 |
] |
| 224 |
""", |
| 225 |
}, |
| 226 |
) |
| 227 |
result = proj.run("r", "-e", "py") |
| 228 |
result.assert_success() |
| 229 |
assert "PIP_USER=0" in result.out |
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.parametrize( |
| 233 |
("var", "substitution", "expected_fragment"), |
| 234 |
[ |
| 235 |
pytest.param("COVERAGE_SRC", "{env_site_packages_dir}", "site-packages", id="env_site_packages_dir"), |
| 236 |
pytest.param("MY_BIN", "{env_bin_dir}", "Scripts" if sys.platform == "win32" else "bin", id="env_bin_dir"), |
| 237 |
pytest.param("MY_PYTHON", "{env_python}", "python", id="env_python"), |
| 238 |
], |
| 239 |
) |
| 240 |
def test_set_env_lazy_constant_no_circular_dependency( |
| 241 |
tox_project: ToxProjectCreator, |
| 242 |
var: str, |
| 243 |
substitution: str, |
| 244 |
expected_fragment: str, |
| 245 |
) -> None: |
| 246 |
proj = tox_project( |
| 247 |
{ |
| 248 |
"tox.toml": f"""\ |
| 249 |
[env_run_base] |
| 250 |
package = "skip" |
| 251 |
set_env.{var} = "{substitution}" |
| 252 |
commands = [["python", "-c", "print('ok')"]] |
| 253 |
""", |
| 254 |
}, |
| 255 |
) |
| 256 |
result = proj.run("c", "-e", "py", "-k", "set_env") |
| 257 |
result.assert_success() |
| 258 |
assert f"{var}=" in result.out |
| 259 |
assert expected_fragment in result.out |
| 260 |
|
| 261 |
|
| 262 |
def test_get_python_returns_none_when_system_executable_missing( |
| 263 |
tox_project: ToxProjectCreator, |
| 264 |
mocker: MockerFixture, |
| 265 |
) -> None: |
| 266 |
mocker.patch.object( |
| 267 |
VirtualEnv, |
| 268 |
"creator", |
| 269 |
new_callable=PropertyMock, |
| 270 |
return_value=MagicMock(interpreter=MagicMock(system_executable=None)), |
| 271 |
) |
| 272 |
proj = tox_project({"tox.ini": "[testenv]\npackage=skip\nbase_python=missing-interp"}) |
| 273 |
result = proj.run("r") |
| 274 |
result.assert_failed() |
| 275 |
assert "could not find python interpreter" in result.out |
| 276 |
|
| 277 |
|
| 278 |
def test_get_virtualenv_py_info_raises_on_none(mocker: MockerFixture) -> None: |
| 279 |
mocker.patch("virtualenv.discovery.cached_py_info.from_exe", return_value=None) |
| 280 |
with pytest.raises(RuntimeError, match="could not query python information for"): |
| 281 |
VirtualEnv.get_virtualenv_py_info(Path("/no/such/python")) |