| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
import sysconfig |
| 5 |
from pathlib import Path |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
from unittest.mock import MagicMock |
| 8 |
|
| 9 |
import pytest |
| 10 |
from packaging.requirements import Requirement |
| 11 |
|
| 12 |
from tox.journal import EnvJournal |
| 13 |
from tox.tox_env.package import PathPackage |
| 14 |
from tox.tox_env.python.extras import resolve_extras_static |
| 15 |
from tox.tox_env.python.runner import PythonRun |
| 16 |
from tox.tox_env.python.virtual_env.package.cmd_builder import VenvCmdBuilder |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from tox.pytest import ToxProjectCreator |
| 20 |
|
| 21 |
|
| 22 |
def test_deps_config_path_req(tox_project: ToxProjectCreator) -> None: |
| 23 |
project = tox_project( |
| 24 |
{ |
| 25 |
"tox.ini": "[testenv:py]\ndeps =-rpath.txt\n -r {toxinidir}{/}path2.txt\n pytest", |
| 26 |
"path.txt": "alpha", |
| 27 |
"path2.txt": "beta", |
| 28 |
}, |
| 29 |
) |
| 30 |
result = project.run("c", "-e", "py") |
| 31 |
result.assert_success() |
| 32 |
deps = result.state.conf.get_env("py")["deps"] |
| 33 |
assert deps.unroll() == ([], ["alpha", "beta", "pytest"]) |
| 34 |
assert deps.as_root_args == ["pytest", "-r", "path.txt", "-r", str(project.path / "path2.txt")] |
| 35 |
assert str(deps) == f"-r {project.path / 'tox.ini'}" |
| 36 |
|
| 37 |
|
| 38 |
def test_journal_package_empty() -> None: |
| 39 |
journal = EnvJournal(enabled=True, name="a") |
| 40 |
|
| 41 |
PythonRun._handle_journal_package(journal, []) # noqa: SLF001 |
| 42 |
|
| 43 |
content = journal.content |
| 44 |
assert content == {} |
| 45 |
|
| 46 |
|
| 47 |
def test_journal_one_wheel_file(tmp_path: Path) -> None: |
| 48 |
wheel = tmp_path / "a.whl" |
| 49 |
wheel.write_bytes(b"magical") |
| 50 |
journal = EnvJournal(enabled=True, name="a") |
| 51 |
|
| 52 |
PythonRun._handle_journal_package(journal, [PathPackage(wheel)]) # noqa: SLF001 |
| 53 |
|
| 54 |
content = journal.content |
| 55 |
assert content == { |
| 56 |
"installpkg": { |
| 57 |
"basename": "a.whl", |
| 58 |
"sha256": "0ce2d4c7087733c06b1087b28db95e114d7caeb515b841c6cdec8960cf884654", |
| 59 |
"type": "file", |
| 60 |
}, |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
def test_journal_multiple_wheel_file(tmp_path: Path) -> None: |
| 65 |
wheel_1 = tmp_path / "a.whl" |
| 66 |
wheel_1.write_bytes(b"magical") |
| 67 |
wheel_2 = tmp_path / "b.whl" |
| 68 |
wheel_2.write_bytes(b"magic") |
| 69 |
journal = EnvJournal(enabled=True, name="a") |
| 70 |
|
| 71 |
PythonRun._handle_journal_package(journal, [PathPackage(wheel_1), PathPackage(wheel_2)]) # noqa: SLF001 |
| 72 |
|
| 73 |
content = journal.content |
| 74 |
assert content == { |
| 75 |
"installpkg": [ |
| 76 |
{ |
| 77 |
"basename": "a.whl", |
| 78 |
"sha256": "0ce2d4c7087733c06b1087b28db95e114d7caeb515b841c6cdec8960cf884654", |
| 79 |
"type": "file", |
| 80 |
}, |
| 81 |
{ |
| 82 |
"basename": "b.whl", |
| 83 |
"sha256": "3be7a505483c0050243c5cbad4700da13925aa4137a55e9e33efd8bc4d05850f", |
| 84 |
"type": "file", |
| 85 |
}, |
| 86 |
], |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
def test_journal_package_dir(tmp_path: Path) -> None: |
| 91 |
journal = EnvJournal(enabled=True, name="a") |
| 92 |
|
| 93 |
PythonRun._handle_journal_package(journal, [PathPackage(tmp_path)]) # noqa: SLF001 |
| 94 |
|
| 95 |
content = journal.content |
| 96 |
assert content == { |
| 97 |
"installpkg": { |
| 98 |
"basename": tmp_path.name, |
| 99 |
"type": "dir", |
| 100 |
}, |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
def test_package_temp_dir_view(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None: |
| 105 |
project = tox_project({"tox.ini": "[testenv]\npackage=wheel"}) |
| 106 |
result = project.run( |
| 107 |
"r", |
| 108 |
"-vv", |
| 109 |
"-e", |
| 110 |
"py", |
| 111 |
"--root", |
| 112 |
str(demo_pkg_inline), |
| 113 |
"--workdir", |
| 114 |
str(project.path / ".tox"), |
| 115 |
) |
| 116 |
result.assert_success() |
| 117 |
wheel_name = "demo_pkg_inline-1.0.0-py3-none-any.whl" |
| 118 |
session_path = Path(".tmp") / "package" / "1" / wheel_name |
| 119 |
msg = f" D package {session_path} copied to {Path('.pkg') / 'dist' / wheel_name} ({project.path / '.tox'}) " |
| 120 |
assert msg in result.out |
| 121 |
assert f" D delete package {project.path / '.tox' / session_path}" in result.out |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.parametrize( |
| 125 |
("extra", "used_extra"), |
| 126 |
[ |
| 127 |
("d_oc", "d-oc"), |
| 128 |
("d-oc", "d-oc"), |
| 129 |
("d.oc", "d-oc"), |
| 130 |
], |
| 131 |
) |
| 132 |
def test_extras_are_normalized( |
| 133 |
tox_project: ToxProjectCreator, |
| 134 |
demo_pkg_inline: Path, |
| 135 |
extra: str, |
| 136 |
used_extra: str, |
| 137 |
) -> None: |
| 138 |
project = tox_project({"tox.ini": f"[testenv]\nextras={extra}"}) |
| 139 |
result = project.run( |
| 140 |
"c", |
| 141 |
"-e", |
| 142 |
"py", |
| 143 |
"--root", |
| 144 |
str(demo_pkg_inline), |
| 145 |
"--workdir", |
| 146 |
str(project.path / ".tox"), |
| 147 |
"-k", |
| 148 |
"extras", |
| 149 |
) |
| 150 |
result.assert_success() |
| 151 |
assert result.out == f"[testenv:py]\nextras = {used_extra}\n" |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.parametrize( |
| 155 |
("config", "cli", "expected"), |
| 156 |
[("false", "true", True), ("true", "false", False), ("false", "config", False), ("true", "config", True)], |
| 157 |
) |
| 158 |
def test_config_skip_missing_interpreters( |
| 159 |
tox_project: ToxProjectCreator, |
| 160 |
config: str, |
| 161 |
cli: str, |
| 162 |
expected: bool, |
| 163 |
) -> None: |
| 164 |
py_ver = ".".join(str(i) for i in sys.version_info[0:2]) |
| 165 |
project = tox_project({"tox.ini": f"[tox]\nenvlist=py4,py{py_ver}\nskip_missing_interpreters={config}"}) |
| 166 |
result = project.run(f"--skip-missing-interpreters={cli}") |
| 167 |
assert result.code == (0 if expected else 1) |
| 168 |
|
| 169 |
|
| 170 |
SYS_PY_VER = "".join(str(i) for i in sys.version_info[0:2]) + ( |
| 171 |
"t" if sysconfig.get_config_var("Py_GIL_DISABLED") == 1 else "" |
| 172 |
) |
| 173 |
|
| 174 |
|
| 175 |
@pytest.mark.parametrize( |
| 176 |
("skip", "env", "retcode"), |
| 177 |
[ |
| 178 |
("true", f"py{SYS_PY_VER}", 0), |
| 179 |
("false", f"py{SYS_PY_VER}", 0), |
| 180 |
("true", "py31", 1), |
| 181 |
("false", "py31", 1), |
| 182 |
("true", None, 0), |
| 183 |
("false", None, 1), |
| 184 |
], |
| 185 |
) |
| 186 |
def test_skip_missing_interpreters_specified_env( |
| 187 |
tox_project: ToxProjectCreator, |
| 188 |
skip: str, |
| 189 |
env: str | None, |
| 190 |
retcode: int, |
| 191 |
) -> None: |
| 192 |
project = tox_project({"tox.ini": f"[tox]\nenvlist=py31,py{SYS_PY_VER}\n[testenv]\nusedevelop=true"}) |
| 193 |
args = [f"--skip-missing-interpreters={skip}"] |
| 194 |
if env: |
| 195 |
args += ["-e", env] |
| 196 |
result = project.run(*args) |
| 197 |
assert result.code == retcode |
| 198 |
|
| 199 |
|
| 200 |
def test_per_env_skip_missing_interpreters_override_global_false(tox_project: ToxProjectCreator) -> None: |
| 201 |
py_ver = ".".join(str(i) for i in sys.version_info[0:2]) |
| 202 |
project = tox_project({ |
| 203 |
"tox.ini": ( |
| 204 |
f"[tox]\nenvlist=py31,py{py_ver}\nskip_missing_interpreters=false\n" |
| 205 |
"[testenv:py31]\nskip_missing_interpreters=true\n" |
| 206 |
), |
| 207 |
}) |
| 208 |
result = project.run() |
| 209 |
assert result.code == 0 |
| 210 |
|
| 211 |
|
| 212 |
def test_per_env_skip_missing_interpreters_override_global_true(tox_project: ToxProjectCreator) -> None: |
| 213 |
project = tox_project({ |
| 214 |
"tox.ini": ( |
| 215 |
"[tox]\nenvlist=py31\nskip_missing_interpreters=true\n[testenv:py31]\nskip_missing_interpreters=false\n" |
| 216 |
), |
| 217 |
}) |
| 218 |
result = project.run() |
| 219 |
assert result.code == 1 |
| 220 |
|
| 221 |
|
| 222 |
def test_per_env_skip_missing_interpreters_cli_overrides_env(tox_project: ToxProjectCreator) -> None: |
| 223 |
project = tox_project({ |
| 224 |
"tox.ini": ( |
| 225 |
"[tox]\nenvlist=py31\nskip_missing_interpreters=false\n[testenv:py31]\nskip_missing_interpreters=false\n" |
| 226 |
), |
| 227 |
}) |
| 228 |
result = project.run("--skip-missing-interpreters=true") |
| 229 |
assert result.code == 1 |
| 230 |
|
| 231 |
|
| 232 |
def test_per_env_skip_missing_interpreters_unset_falls_to_global(tox_project: ToxProjectCreator) -> None: |
| 233 |
py_ver = ".".join(str(i) for i in sys.version_info[0:2]) |
| 234 |
project = tox_project({ |
| 235 |
"tox.ini": f"[tox]\nenvlist=py31,py{py_ver}\nskip_missing_interpreters=true\n", |
| 236 |
}) |
| 237 |
result = project.run() |
| 238 |
assert result.code == 0 |
| 239 |
|
| 240 |
|
| 241 |
def test_per_env_skip_missing_interpreters_toml(tox_project: ToxProjectCreator) -> None: |
| 242 |
project = tox_project({ |
| 243 |
"tox.toml": """ |
| 244 |
env_list = ["py31"] |
| 245 |
skip_missing_interpreters = true |
| 246 |
[env.py31] |
| 247 |
skip_missing_interpreters = false |
| 248 |
""", |
| 249 |
}) |
| 250 |
result = project.run() |
| 251 |
assert result.code == 1 |
| 252 |
|
| 253 |
|
| 254 |
def test_dependency_groups_single(tox_project: ToxProjectCreator) -> None: |
| 255 |
project = tox_project( |
| 256 |
{ |
| 257 |
"tox.toml": """ |
| 258 |
[env_run_base] |
| 259 |
skip_install = true |
| 260 |
dependency_groups = ["test"] |
| 261 |
""", |
| 262 |
"pyproject.toml": """ |
| 263 |
[dependency-groups] |
| 264 |
test = [ |
| 265 |
"furo>=2024.8.6", |
| 266 |
"sphinx>=8.0.2", |
| 267 |
] |
| 268 |
""", |
| 269 |
}, |
| 270 |
) |
| 271 |
execute_calls = project.patch_execute() |
| 272 |
result = project.run("r", "-e", "py") |
| 273 |
|
| 274 |
result.assert_success() |
| 275 |
|
| 276 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 277 |
assert found_calls == [ |
| 278 |
("py", "install_dependency-groups", ["python", "-I", "-m", "pip", "install", "furo>=2024.8.6", "sphinx>=8.0.2"]) |
| 279 |
] |
| 280 |
|
| 281 |
|
| 282 |
def test_dependency_groups_extras(tox_project: ToxProjectCreator) -> None: |
| 283 |
project = tox_project( |
| 284 |
{ |
| 285 |
"tox.toml": """ |
| 286 |
[env_run_base] |
| 287 |
skip_install = true |
| 288 |
dependency_groups = ["test"] |
| 289 |
""", |
| 290 |
"pyproject.toml": """ |
| 291 |
[project] |
| 292 |
name = "demo_pkg" |
| 293 |
|
| 294 |
[project.optional-dependencies] |
| 295 |
extra1 = ["extra_pkg>=1.0"] |
| 296 |
[dependency-groups] |
| 297 |
test = [ |
| 298 |
"furo>=2024.8.6", |
| 299 |
"sphinx>=8.0.2", |
| 300 |
"demo_pkg[extra1]", |
| 301 |
] |
| 302 |
""", |
| 303 |
}, |
| 304 |
) |
| 305 |
execute_calls = project.patch_execute() |
| 306 |
result = project.run("r", "-e", "py") |
| 307 |
|
| 308 |
result.assert_success() |
| 309 |
|
| 310 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 311 |
assert found_calls == [ |
| 312 |
( |
| 313 |
"py", |
| 314 |
"install_dependency-groups", |
| 315 |
["python", "-I", "-m", "pip", "install", "extra_pkg>=1.0", "furo>=2024.8.6", "sphinx>=8.0.2"], |
| 316 |
) |
| 317 |
] |
| 318 |
|
| 319 |
|
| 320 |
def test_dependency_groups_nested_extras(tox_project: ToxProjectCreator) -> None: |
| 321 |
project = tox_project( |
| 322 |
{ |
| 323 |
"tox.toml": """ |
| 324 |
[env_run_base] |
| 325 |
skip_install = true |
| 326 |
dependency_groups = ["test"] |
| 327 |
""", |
| 328 |
"pyproject.toml": """ |
| 329 |
[project] |
| 330 |
name = "demo_pkg" |
| 331 |
|
| 332 |
[project.optional-dependencies] |
| 333 |
extra1 = ["extra_pkg>=1.0"] |
| 334 |
extra2 = ["demo_pkg[extra1]"] |
| 335 |
[dependency-groups] |
| 336 |
test = [ |
| 337 |
"furo>=2024.8.6", |
| 338 |
"sphinx>=8.0.2", |
| 339 |
"demo_pkg[extra2]", |
| 340 |
] |
| 341 |
""", |
| 342 |
}, |
| 343 |
) |
| 344 |
execute_calls = project.patch_execute() |
| 345 |
result = project.run("r", "-e", "py") |
| 346 |
|
| 347 |
result.assert_success() |
| 348 |
|
| 349 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 350 |
assert found_calls == [ |
| 351 |
( |
| 352 |
"py", |
| 353 |
"install_dependency-groups", |
| 354 |
["python", "-I", "-m", "pip", "install", "extra_pkg>=1.0", "furo>=2024.8.6", "sphinx>=8.0.2"], |
| 355 |
) |
| 356 |
] |
| 357 |
|
| 358 |
|
| 359 |
def test_dependency_groups_double_extras(tox_project: ToxProjectCreator) -> None: |
| 360 |
project = tox_project( |
| 361 |
{ |
| 362 |
"tox.toml": """ |
| 363 |
[env_run_base] |
| 364 |
skip_install = true |
| 365 |
dependency_groups = ["test"] |
| 366 |
""", |
| 367 |
"pyproject.toml": """ |
| 368 |
[project] |
| 369 |
name = "demo_pkg" |
| 370 |
|
| 371 |
[project.optional-dependencies] |
| 372 |
extra1 = ["extra_pkg>=1.0"] |
| 373 |
extra2 = ["extra_pkg2>=1.0"] |
| 374 |
[dependency-groups] |
| 375 |
test = [ |
| 376 |
"furo>=2024.8.6", |
| 377 |
"sphinx>=8.0.2", |
| 378 |
"demo_pkg[extra1,extra2]", |
| 379 |
] |
| 380 |
""", |
| 381 |
}, |
| 382 |
) |
| 383 |
execute_calls = project.patch_execute() |
| 384 |
result = project.run("r", "-e", "py") |
| 385 |
|
| 386 |
result.assert_success() |
| 387 |
|
| 388 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 389 |
assert found_calls == [ |
| 390 |
( |
| 391 |
"py", |
| 392 |
"install_dependency-groups", |
| 393 |
[ |
| 394 |
"python", |
| 395 |
"-I", |
| 396 |
"-m", |
| 397 |
"pip", |
| 398 |
"install", |
| 399 |
"extra_pkg2>=1.0", |
| 400 |
"extra_pkg>=1.0", |
| 401 |
"furo>=2024.8.6", |
| 402 |
"sphinx>=8.0.2", |
| 403 |
], |
| 404 |
) |
| 405 |
] |
| 406 |
|
| 407 |
|
| 408 |
def test_dependency_groups_duplicate_extras(tox_project: ToxProjectCreator) -> None: |
| 409 |
project = tox_project( |
| 410 |
{ |
| 411 |
"tox.toml": """ |
| 412 |
[env_run_base] |
| 413 |
skip_install = true |
| 414 |
dependency_groups = ["test"] |
| 415 |
""", |
| 416 |
"pyproject.toml": """ |
| 417 |
[project] |
| 418 |
name = "demo_pkg" |
| 419 |
|
| 420 |
[project.optional-dependencies] |
| 421 |
extra1 = ["extra_pkg>=1.0"] |
| 422 |
extra2 = ["extra_pkg2>=1.0", "demo_pkg[extra1]"] |
| 423 |
[dependency-groups] |
| 424 |
test = [ |
| 425 |
"furo>=2024.8.6", |
| 426 |
"sphinx>=8.0.2", |
| 427 |
"demo_pkg[extra1,extra2]", |
| 428 |
] |
| 429 |
""", |
| 430 |
}, |
| 431 |
) |
| 432 |
execute_calls = project.patch_execute() |
| 433 |
result = project.run("r", "-e", "py") |
| 434 |
|
| 435 |
result.assert_success() |
| 436 |
|
| 437 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 438 |
assert found_calls == [ |
| 439 |
( |
| 440 |
"py", |
| 441 |
"install_dependency-groups", |
| 442 |
[ |
| 443 |
"python", |
| 444 |
"-I", |
| 445 |
"-m", |
| 446 |
"pip", |
| 447 |
"install", |
| 448 |
"extra_pkg2>=1.0", |
| 449 |
"extra_pkg>=1.0", |
| 450 |
"furo>=2024.8.6", |
| 451 |
"sphinx>=8.0.2", |
| 452 |
], |
| 453 |
) |
| 454 |
] |
| 455 |
|
| 456 |
|
| 457 |
def test_dependency_groups_multiple(tox_project: ToxProjectCreator) -> None: |
| 458 |
project = tox_project( |
| 459 |
{ |
| 460 |
"tox.toml": """ |
| 461 |
[env_run_base] |
| 462 |
skip_install = true |
| 463 |
dependency_groups = ["test", "type"] |
| 464 |
""", |
| 465 |
"pyproject.toml": """ |
| 466 |
[dependency-groups] |
| 467 |
test = [ |
| 468 |
"furo>=2024.8.6", |
| 469 |
"sphinx>=8.0.2", |
| 470 |
] |
| 471 |
type = [ |
| 472 |
"furo>=2024.8.6", |
| 473 |
"mypy>=1", |
| 474 |
] |
| 475 |
""", |
| 476 |
}, |
| 477 |
) |
| 478 |
execute_calls = project.patch_execute() |
| 479 |
result = project.run("r", "-e", "py") |
| 480 |
|
| 481 |
result.assert_success() |
| 482 |
|
| 483 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 484 |
assert found_calls == [ |
| 485 |
( |
| 486 |
"py", |
| 487 |
"install_dependency-groups", |
| 488 |
["python", "-I", "-m", "pip", "install", "furo>=2024.8.6", "mypy>=1", "sphinx>=8.0.2"], |
| 489 |
) |
| 490 |
] |
| 491 |
|
| 492 |
|
| 493 |
def test_dependency_groups_include(tox_project: ToxProjectCreator) -> None: |
| 494 |
project = tox_project( |
| 495 |
{ |
| 496 |
"tox.toml": """ |
| 497 |
[env_run_base] |
| 498 |
skip_install = true |
| 499 |
dependency_groups = ["test", "type"] |
| 500 |
""", |
| 501 |
"pyproject.toml": """ |
| 502 |
[dependency-groups] |
| 503 |
test = [ |
| 504 |
"furo>=2024.8.6", |
| 505 |
"sphinx>=8.0.2", |
| 506 |
] |
| 507 |
"friendly.Bard" = [ |
| 508 |
"bard-song", |
| 509 |
] |
| 510 |
type = [ |
| 511 |
{include-group = "test"}, |
| 512 |
{include-group = "FrIeNdLy-._.-bArD"}, |
| 513 |
"mypy>=1", |
| 514 |
] |
| 515 |
""", |
| 516 |
}, |
| 517 |
) |
| 518 |
execute_calls = project.patch_execute() |
| 519 |
result = project.run("r", "-e", "py") |
| 520 |
|
| 521 |
result.assert_success() |
| 522 |
|
| 523 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 524 |
assert found_calls == [ |
| 525 |
( |
| 526 |
"py", |
| 527 |
"install_dependency-groups", |
| 528 |
["python", "-I", "-m", "pip", "install", "bard-song", "furo>=2024.8.6", "mypy>=1", "sphinx>=8.0.2"], |
| 529 |
) |
| 530 |
] |
| 531 |
|
| 532 |
|
| 533 |
def test_dependency_groups_not_table(tox_project: ToxProjectCreator) -> None: |
| 534 |
project = tox_project( |
| 535 |
{ |
| 536 |
"tox.toml": """ |
| 537 |
[env_run_base] |
| 538 |
skip_install = true |
| 539 |
dependency_groups = ["test"] |
| 540 |
""", |
| 541 |
"pyproject.toml": """ |
| 542 |
dependency-groups = 1 |
| 543 |
""", |
| 544 |
}, |
| 545 |
) |
| 546 |
result = project.run("r", "-e", "py") |
| 547 |
|
| 548 |
result.assert_failed() |
| 549 |
assert "py: failed with dependency-groups is int instead of table\n" in result.out |
| 550 |
|
| 551 |
|
| 552 |
def test_dependency_groups_missing(tox_project: ToxProjectCreator) -> None: |
| 553 |
project = tox_project( |
| 554 |
{ |
| 555 |
"tox.toml": """ |
| 556 |
[env_run_base] |
| 557 |
skip_install = true |
| 558 |
dependency_groups = ["type"] |
| 559 |
""", |
| 560 |
"pyproject.toml": """ |
| 561 |
[dependency-groups] |
| 562 |
test = [ |
| 563 |
"furo>=2024.8.6", |
| 564 |
] |
| 565 |
""", |
| 566 |
}, |
| 567 |
) |
| 568 |
result = project.run("r", "-e", "py") |
| 569 |
|
| 570 |
result.assert_failed() |
| 571 |
assert "py: failed with dependency group 'type' not found\n" in result.out |
| 572 |
|
| 573 |
|
| 574 |
def test_dependency_groups_not_list(tox_project: ToxProjectCreator) -> None: |
| 575 |
project = tox_project( |
| 576 |
{ |
| 577 |
"tox.toml": """ |
| 578 |
[env_run_base] |
| 579 |
skip_install = true |
| 580 |
dependency_groups = ["tEst"] |
| 581 |
""", |
| 582 |
"pyproject.toml": """ |
| 583 |
[dependency-groups] |
| 584 |
teSt = 1 |
| 585 |
""", |
| 586 |
}, |
| 587 |
) |
| 588 |
result = project.run("r", "-e", "py") |
| 589 |
|
| 590 |
result.assert_failed() |
| 591 |
assert "py: failed with dependency group 'teSt' is not a list\n" in result.out |
| 592 |
|
| 593 |
|
| 594 |
def test_dependency_groups_bad_requirement(tox_project: ToxProjectCreator) -> None: |
| 595 |
project = tox_project( |
| 596 |
{ |
| 597 |
"tox.toml": """ |
| 598 |
[env_run_base] |
| 599 |
skip_install = true |
| 600 |
dependency_groups = ["test"] |
| 601 |
""", |
| 602 |
"pyproject.toml": """ |
| 603 |
[dependency-groups] |
| 604 |
test = [ "whatever --" ] |
| 605 |
""", |
| 606 |
}, |
| 607 |
) |
| 608 |
result = project.run("r", "-e", "py") |
| 609 |
|
| 610 |
result.assert_failed() |
| 611 |
assert ( |
| 612 |
"py: failed with 'whatever --' is not valid requirement due to " |
| 613 |
"Expected semicolon (after name with no version specifier) or end\n whatever --\n ^\n" |
| 614 |
in result.out |
| 615 |
) |
| 616 |
|
| 617 |
|
| 618 |
def test_dependency_groups_bad_entry(tox_project: ToxProjectCreator) -> None: |
| 619 |
project = tox_project( |
| 620 |
{ |
| 621 |
"tox.toml": """ |
| 622 |
[env_run_base] |
| 623 |
skip_install = true |
| 624 |
dependency_groups = ["test"] |
| 625 |
""", |
| 626 |
"pyproject.toml": """ |
| 627 |
[dependency-groups] |
| 628 |
test = [ { magic = "ok" } ] |
| 629 |
""", |
| 630 |
}, |
| 631 |
) |
| 632 |
result = project.run("r", "-e", "py") |
| 633 |
|
| 634 |
result.assert_failed() |
| 635 |
assert "py: failed with invalid dependency group item: {'magic': 'ok'}\n" in result.out |
| 636 |
|
| 637 |
|
| 638 |
def test_dependency_groups_cyclic(tox_project: ToxProjectCreator) -> None: |
| 639 |
project = tox_project( |
| 640 |
{ |
| 641 |
"tox.toml": """ |
| 642 |
[env_run_base] |
| 643 |
skip_install = true |
| 644 |
dependency_groups = ["test"] |
| 645 |
""", |
| 646 |
"pyproject.toml": """ |
| 647 |
[dependency-groups] |
| 648 |
teSt = [ { include-group = "type" } ] |
| 649 |
tyPe = [ { include-group = "test" } ] |
| 650 |
""", |
| 651 |
}, |
| 652 |
) |
| 653 |
result = project.run("r", "-e", "py") |
| 654 |
|
| 655 |
result.assert_failed() |
| 656 |
assert "py: failed with Cyclic dependency group include: 'teSt' -> ('teSt', 'tyPe')\n" in result.out |
| 657 |
|
| 658 |
|
| 659 |
def test_deps_only_static(tox_project: ToxProjectCreator) -> None: |
| 660 |
project = tox_project( |
| 661 |
{ |
| 662 |
"tox.toml": """ |
| 663 |
[env_run_base] |
| 664 |
package = "deps-only" |
| 665 |
""", |
| 666 |
"pyproject.toml": """ |
| 667 |
[project] |
| 668 |
name = "demo" |
| 669 |
version = "1.0" |
| 670 |
dependencies = ["httpx>=0.27", "rich>=13"] |
| 671 |
""", |
| 672 |
}, |
| 673 |
) |
| 674 |
execute_calls = project.patch_execute() |
| 675 |
result = project.run("r", "-e", "py") |
| 676 |
|
| 677 |
result.assert_success() |
| 678 |
|
| 679 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 680 |
assert found_calls == [ |
| 681 |
("py", "install_package_deps", ["python", "-I", "-m", "pip", "install", "httpx>=0.27", "rich>=13"]) |
| 682 |
] |
| 683 |
|
| 684 |
|
| 685 |
def test_deps_only_with_extras(tox_project: ToxProjectCreator) -> None: |
| 686 |
project = tox_project( |
| 687 |
{ |
| 688 |
"tox.toml": """ |
| 689 |
[env_run_base] |
| 690 |
package = "deps-only" |
| 691 |
extras = ["docs"] |
| 692 |
""", |
| 693 |
"pyproject.toml": """ |
| 694 |
[project] |
| 695 |
name = "demo" |
| 696 |
version = "1.0" |
| 697 |
dependencies = ["httpx>=0.27"] |
| 698 |
[project.optional-dependencies] |
| 699 |
docs = ["sphinx>=7", "furo"] |
| 700 |
""", |
| 701 |
}, |
| 702 |
) |
| 703 |
execute_calls = project.patch_execute() |
| 704 |
result = project.run("r", "-e", "py") |
| 705 |
|
| 706 |
result.assert_success() |
| 707 |
|
| 708 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 709 |
assert found_calls == [ |
| 710 |
( |
| 711 |
"py", |
| 712 |
"install_package_deps", |
| 713 |
["python", "-I", "-m", "pip", "install", "furo", "httpx>=0.27", "sphinx>=7"], |
| 714 |
) |
| 715 |
] |
| 716 |
|
| 717 |
|
| 718 |
def test_deps_only_multiple_extras(tox_project: ToxProjectCreator) -> None: |
| 719 |
project = tox_project( |
| 720 |
{ |
| 721 |
"tox.toml": """ |
| 722 |
[env_run_base] |
| 723 |
package = "deps-only" |
| 724 |
extras = ["docs", "testing"] |
| 725 |
""", |
| 726 |
"pyproject.toml": """ |
| 727 |
[project] |
| 728 |
name = "demo" |
| 729 |
version = "1.0" |
| 730 |
dependencies = ["httpx>=0.27"] |
| 731 |
[project.optional-dependencies] |
| 732 |
docs = ["sphinx>=7"] |
| 733 |
testing = ["pytest>=8"] |
| 734 |
""", |
| 735 |
}, |
| 736 |
) |
| 737 |
execute_calls = project.patch_execute() |
| 738 |
result = project.run("r", "-e", "py") |
| 739 |
|
| 740 |
result.assert_success() |
| 741 |
|
| 742 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 743 |
assert found_calls == [ |
| 744 |
( |
| 745 |
"py", |
| 746 |
"install_package_deps", |
| 747 |
["python", "-I", "-m", "pip", "install", "httpx>=0.27", "pytest>=8", "sphinx>=7"], |
| 748 |
) |
| 749 |
] |
| 750 |
|
| 751 |
|
| 752 |
def test_deps_only_with_deps(tox_project: ToxProjectCreator) -> None: |
| 753 |
project = tox_project( |
| 754 |
{ |
| 755 |
"tox.toml": """ |
| 756 |
[env_run_base] |
| 757 |
package = "deps-only" |
| 758 |
deps = ["coverage[toml]"] |
| 759 |
""", |
| 760 |
"pyproject.toml": """ |
| 761 |
[project] |
| 762 |
name = "demo" |
| 763 |
version = "1.0" |
| 764 |
dependencies = ["httpx>=0.27"] |
| 765 |
""", |
| 766 |
}, |
| 767 |
) |
| 768 |
execute_calls = project.patch_execute() |
| 769 |
result = project.run("r", "-e", "py") |
| 770 |
|
| 771 |
result.assert_success() |
| 772 |
|
| 773 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 774 |
assert found_calls == [ |
| 775 |
("py", "install_deps", ["python", "-I", "-m", "pip", "install", "coverage[toml]"]), |
| 776 |
("py", "install_package_deps", ["python", "-I", "-m", "pip", "install", "httpx>=0.27"]), |
| 777 |
] |
| 778 |
|
| 779 |
|
| 780 |
def test_deps_only_with_dependency_groups(tox_project: ToxProjectCreator) -> None: |
| 781 |
project = tox_project( |
| 782 |
{ |
| 783 |
"tox.toml": """ |
| 784 |
[env_run_base] |
| 785 |
package = "deps-only" |
| 786 |
dependency_groups = ["test"] |
| 787 |
""", |
| 788 |
"pyproject.toml": """ |
| 789 |
[project] |
| 790 |
name = "demo" |
| 791 |
version = "1.0" |
| 792 |
dependencies = ["httpx>=0.27"] |
| 793 |
[dependency-groups] |
| 794 |
test = ["pytest>=8"] |
| 795 |
""", |
| 796 |
}, |
| 797 |
) |
| 798 |
execute_calls = project.patch_execute() |
| 799 |
result = project.run("r", "-e", "py") |
| 800 |
|
| 801 |
result.assert_success() |
| 802 |
|
| 803 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 804 |
assert found_calls == [ |
| 805 |
("py", "install_dependency-groups", ["python", "-I", "-m", "pip", "install", "pytest>=8"]), |
| 806 |
("py", "install_package_deps", ["python", "-I", "-m", "pip", "install", "httpx>=0.27"]), |
| 807 |
] |
| 808 |
|
| 809 |
|
| 810 |
def test_deps_only_no_extras(tox_project: ToxProjectCreator) -> None: |
| 811 |
project = tox_project( |
| 812 |
{ |
| 813 |
"tox.toml": """ |
| 814 |
[env_run_base] |
| 815 |
package = "deps-only" |
| 816 |
""", |
| 817 |
"pyproject.toml": """ |
| 818 |
[project] |
| 819 |
name = "demo" |
| 820 |
version = "1.0" |
| 821 |
dependencies = ["httpx>=0.27"] |
| 822 |
[project.optional-dependencies] |
| 823 |
docs = ["sphinx>=7"] |
| 824 |
""", |
| 825 |
}, |
| 826 |
) |
| 827 |
execute_calls = project.patch_execute() |
| 828 |
result = project.run("r", "-e", "py") |
| 829 |
|
| 830 |
result.assert_success() |
| 831 |
|
| 832 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id, i[0][3].cmd) for i in execute_calls.call_args_list] |
| 833 |
assert found_calls == [("py", "install_package_deps", ["python", "-I", "-m", "pip", "install", "httpx>=0.27"])] |
| 834 |
|
| 835 |
|
| 836 |
def test_deps_only_unknown_extra(tox_project: ToxProjectCreator) -> None: |
| 837 |
project = tox_project( |
| 838 |
{ |
| 839 |
"tox.toml": """ |
| 840 |
[env_run_base] |
| 841 |
package = "deps-only" |
| 842 |
extras = ["nonexistent"] |
| 843 |
""", |
| 844 |
"pyproject.toml": """ |
| 845 |
[project] |
| 846 |
name = "demo" |
| 847 |
version = "1.0" |
| 848 |
dependencies = ["httpx>=0.27"] |
| 849 |
[project.optional-dependencies] |
| 850 |
docs = ["sphinx>=7"] |
| 851 |
""", |
| 852 |
}, |
| 853 |
) |
| 854 |
result = project.run("r", "-e", "py") |
| 855 |
|
| 856 |
result.assert_failed() |
| 857 |
assert "extras not found for package demo: nonexistent (available: docs)" in result.out |
| 858 |
|
| 859 |
|
| 860 |
def test_deps_only_fallback_no_project_table(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None: |
| 861 |
project = tox_project({"tox.toml": '[env_run_base]\npackage = "deps-only"\n'}) |
| 862 |
execute_calls = project.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 863 |
result = project.run("r", "-e", "py", "--root", str(demo_pkg_inline), "--workdir", str(project.path / ".tox")) |
| 864 |
|
| 865 |
result.assert_success() |
| 866 |
|
| 867 |
found_calls = [(i[0][0].conf.name, i[0][3].run_id) for i in execute_calls.call_args_list] |
| 868 |
pkg_calls = [c for c in found_calls if c[0] == "py" and "package_deps" in c[1]] |
| 869 |
assert pkg_calls == [] |
| 870 |
|
| 871 |
|
| 872 |
def test_resolve_extras_static_no_pyproject(tmp_path: Path) -> None: |
| 873 |
assert resolve_extras_static(tmp_path, set()) is None |
| 874 |
|
| 875 |
|
| 876 |
def test_resolve_extras_static_no_project_table(tmp_path: Path) -> None: |
| 877 |
(tmp_path / "pyproject.toml").write_text('[build-system]\nrequires = ["setuptools"]') |
| 878 |
assert resolve_extras_static(tmp_path, set()) is None |
| 879 |
|
| 880 |
|
| 881 |
def test_resolve_extras_static_dynamic_deps(tmp_path: Path) -> None: |
| 882 |
(tmp_path / "pyproject.toml").write_text('[project]\nname = "demo"\nversion = "1.0"\ndynamic = ["dependencies"]') |
| 883 |
assert resolve_extras_static(tmp_path, set()) is None |
| 884 |
|
| 885 |
|
| 886 |
def test_resolve_extras_static_dynamic_optional_deps(tmp_path: Path) -> None: |
| 887 |
(tmp_path / "pyproject.toml").write_text( |
| 888 |
'[project]\nname = "demo"\nversion = "1.0"\ndynamic = ["optional-dependencies"]' |
| 889 |
) |
| 890 |
assert resolve_extras_static(tmp_path, {"docs"}) is None |
| 891 |
|
| 892 |
|
| 893 |
def test_cmd_builder_load_deps_for_env() -> None: |
| 894 |
builder = MagicMock(spec=VenvCmdBuilder) |
| 895 |
mock_meta_env = MagicMock() |
| 896 |
mock_meta_env.load_deps_for_env.return_value = [Requirement("requests>=2")] |
| 897 |
builder._sdist_meta_tox_env = mock_meta_env # noqa: SLF001 |
| 898 |
mock_conf = MagicMock() |
| 899 |
|
| 900 |
result = VenvCmdBuilder.load_deps_for_env(builder, mock_conf) |
| 901 |
|
| 902 |
assert result == [Requirement("requests>=2")] |
| 903 |
mock_meta_env.load_deps_for_env.assert_called_once_with(mock_conf) |