|
8 | 8 | import pytest
|
9 | 9 | from packaging.requirements import Requirement
|
10 | 10 |
|
11 |
| -from tox.pytest import CaptureFixture, ToxProjectCreator |
| 11 | +from tox.pytest import CaptureFixture, SubRequest, ToxProject, ToxProjectCreator |
12 | 12 | from tox.tox_env.errors import Fail
|
13 | 13 |
|
14 | 14 |
|
@@ -270,3 +270,139 @@ def test_pip_install_constraint_file_new(tox_project: ToxProjectCreator) -> None
|
270 | 270 | assert "py: recreate env because changed constraint(s) added a" in result_second.out, result_second.out
|
271 | 271 | assert execute_calls.call_count == 1
|
272 | 272 | assert execute_calls.call_args[0][3].cmd == ["python", "-I", "-m", "pip", "install", "a", "-c", "c.txt"]
|
| 273 | + |
| 274 | + |
| 275 | +@pytest.fixture(params=[True, False]) |
| 276 | +def constrain_package_deps(request: SubRequest) -> bool: |
| 277 | + return bool(request.param) |
| 278 | + |
| 279 | + |
| 280 | +@pytest.fixture(params=[True, False]) |
| 281 | +def use_frozen_constraints(request: SubRequest) -> bool: |
| 282 | + return bool(request.param) |
| 283 | + |
| 284 | + |
| 285 | +@pytest.fixture( |
| 286 | + params=[ |
| 287 | + "explicit", |
| 288 | + "requirements", |
| 289 | + "constraints", |
| 290 | + "explicit+requirements", |
| 291 | + "requirements_indirect", |
| 292 | + "requirements_constraints_indirect", |
| 293 | + ], |
| 294 | +) |
| 295 | +def constrained_mock_project( |
| 296 | + request: SubRequest, |
| 297 | + tox_project: ToxProjectCreator, |
| 298 | + demo_pkg_inline: Path, |
| 299 | + constrain_package_deps: bool, |
| 300 | + use_frozen_constraints: bool, |
| 301 | +) -> tuple[ToxProject, list[str]]: |
| 302 | + toml = (demo_pkg_inline / "pyproject.toml").read_text() |
| 303 | + files = { |
| 304 | + "pyproject.toml": toml.replace("requires = []", 'requires = ["setuptools"]') |
| 305 | + + '\n[project]\nname = "demo"\nversion = "0.1"\ndependencies = ["foo > 2"]', |
| 306 | + "build.py": (demo_pkg_inline / "build.py").read_text(), |
| 307 | + } |
| 308 | + exp_constraints: list[str] = [] |
| 309 | + requirement = "foo==1.2.3" |
| 310 | + constraint = "foo<2" |
| 311 | + if request.param == "explicit": |
| 312 | + deps = requirement |
| 313 | + exp_constraints.append(requirement) |
| 314 | + elif request.param == "requirements": |
| 315 | + files["requirements.txt"] = f"--pre\n{requirement}" |
| 316 | + deps = "-rrequirements.txt" |
| 317 | + exp_constraints.append(requirement) |
| 318 | + elif request.param == "constraints": |
| 319 | + files["constraints.txt"] = constraint |
| 320 | + deps = "-cconstraints.txt" |
| 321 | + exp_constraints.append(constraint) |
| 322 | + elif request.param == "explicit+requirements": |
| 323 | + files["requirements.txt"] = f"--pre\n{requirement}" |
| 324 | + deps = "\n\t-rrequirements.txt\n\tfoo" |
| 325 | + exp_constraints.extend(["foo", requirement]) |
| 326 | + elif request.param == "requirements_indirect": |
| 327 | + files["foo.requirements.txt"] = f"--pre\n{requirement}" |
| 328 | + files["requirements.txt"] = "-r foo.requirements.txt" |
| 329 | + deps = "-rrequirements.txt" |
| 330 | + exp_constraints.append(requirement) |
| 331 | + elif request.param == "requirements_constraints_indirect": |
| 332 | + files["foo.requirements.txt"] = f"--pre\n{requirement}" |
| 333 | + files["foo.constraints.txt"] = f"{constraint}" |
| 334 | + files["requirements.txt"] = "-r foo.requirements.txt\n-c foo.constraints.txt" |
| 335 | + deps = "-rrequirements.txt" |
| 336 | + exp_constraints.extend([requirement, constraint]) |
| 337 | + else: # pragma: no cover |
| 338 | + pytest.fail(f"Missing case: {request.param}") |
| 339 | + files["tox.ini"] = ( |
| 340 | + "[testenv]\npackage=wheel\n" |
| 341 | + f"constrain_package_deps = {constrain_package_deps}\n" |
| 342 | + f"use_frozen_constraints = {use_frozen_constraints}\n" |
| 343 | + f"deps = {deps}" |
| 344 | + ) |
| 345 | + return tox_project(files), exp_constraints if constrain_package_deps else [] |
| 346 | + |
| 347 | + |
| 348 | +def test_constrain_package_deps( |
| 349 | + constrained_mock_project: tuple[ToxProject, list[str]], |
| 350 | + constrain_package_deps: bool, |
| 351 | + use_frozen_constraints: bool, |
| 352 | +) -> None: |
| 353 | + proj, exp_constraints = constrained_mock_project |
| 354 | + execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 355 | + result_first = proj.run("r") |
| 356 | + result_first.assert_success() |
| 357 | + exp_run_ids = ["install_deps"] |
| 358 | + if constrain_package_deps and use_frozen_constraints: |
| 359 | + exp_run_ids.append("freeze") |
| 360 | + exp_run_ids.extend( |
| 361 | + [ |
| 362 | + "install_requires", |
| 363 | + "_optional_hooks", |
| 364 | + "get_requires_for_build_wheel", |
| 365 | + "build_wheel", |
| 366 | + "install_package_deps", |
| 367 | + "install_package", |
| 368 | + "_exit", |
| 369 | + ], |
| 370 | + ) |
| 371 | + run_ids = [i[0][3].run_id for i in execute_calls.call_args_list] |
| 372 | + assert run_ids == exp_run_ids |
| 373 | + constraints_file = proj.path / ".tox" / "py" / "constraints.txt" |
| 374 | + if constrain_package_deps: |
| 375 | + constraints = constraints_file.read_text().splitlines() |
| 376 | + for call in execute_calls.call_args_list: |
| 377 | + if call[0][3].run_id == "install_package_deps": |
| 378 | + assert f"-c{constraints_file}" in call[0][3].cmd |
| 379 | + if use_frozen_constraints: |
| 380 | + for c in exp_constraints: |
| 381 | + # when using frozen constraints with this mock, the mock package does NOT |
| 382 | + # actually end up in the constraints, so assert it's not there |
| 383 | + assert c not in constraints |
| 384 | + for c in constraints: |
| 385 | + assert c.partition("==")[0] in ["pip", "setuptools", "wheel"] |
| 386 | + else: |
| 387 | + for c in constraints: |
| 388 | + assert c in exp_constraints |
| 389 | + for c in exp_constraints: |
| 390 | + assert c in constraints |
| 391 | + else: |
| 392 | + assert not constraints_file.exists() |
| 393 | + |
| 394 | + |
| 395 | +@pytest.mark.parametrize("conf_key", ["constrain_package_deps", "use_frozen_constraints"]) |
| 396 | +def test_change_constraint_options_recreates(tox_project: ToxProjectCreator, conf_key: str) -> None: |
| 397 | + tox_ini_content = "[testenv:py]\ndeps=a\nskip_install=true" |
| 398 | + proj = tox_project({"tox.ini": f"{tox_ini_content}\n{conf_key} = true"}) |
| 399 | + proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 400 | + |
| 401 | + result = proj.run("r") |
| 402 | + result.assert_success() |
| 403 | + |
| 404 | + (proj.path / "tox.ini").write_text(f"{tox_ini_content}\n{conf_key} = false") |
| 405 | + result_second = proj.run("r") |
| 406 | + result_second.assert_success() |
| 407 | + assert "recreate env because constraint options changed" in result_second.out |
| 408 | + assert conf_key in result_second.out |
0 commit comments