| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from argparse import Namespace |
| 4 |
from typing import TYPE_CHECKING |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from tox.tox_env.python.pip.req_file import PythonConstraints, PythonDeps |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from pathlib import Path |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.parametrize("legacy_flag", ["-r", "-c"]) |
| 15 |
def test_legacy_requirement_file(tmp_path: Path, legacy_flag: str) -> None: |
| 16 |
python_deps = PythonDeps(f"{legacy_flag}a.txt", tmp_path) |
| 17 |
(tmp_path / "a.txt").write_text("b") |
| 18 |
assert python_deps.as_root_args == [legacy_flag, "a.txt"] |
| 19 |
assert vars(python_deps.options) == {} |
| 20 |
assert [str(i) for i in python_deps.requirements] == ["b" if legacy_flag == "-r" else "-c b"] |
| 21 |
|
| 22 |
|
| 23 |
def test_deps_with_hash(tmp_path: Path) -> None: |
| 24 |
"""deps with --hash should raise an exception.""" |
| 25 |
python_deps = PythonDeps( |
| 26 |
raw="foo==1 --hash sha256:97a702083b0d906517b79672d8501eee470d60ae55df0fa9d4cfba56c7f65a82", |
| 27 |
root=tmp_path, |
| 28 |
) |
| 29 |
with pytest.raises(ValueError, match="Cannot use --hash in deps list"): |
| 30 |
_ = python_deps.requirements |
| 31 |
|
| 32 |
|
| 33 |
def test_deps_with_requirements_with_hash(tmp_path: Path) -> None: |
| 34 |
"""deps can point to a requirements file that has --hash.""" |
| 35 |
exp_hash = "sha256:97a702083b0d906517b79672d8501eee470d60ae55df0fa9d4cfba56c7f65a82" |
| 36 |
requirements = tmp_path / "requirements.txt" |
| 37 |
requirements.write_text(f"foo==1 --hash {exp_hash}") |
| 38 |
python_deps = PythonDeps(raw="-r requirements.txt", root=tmp_path) |
| 39 |
assert len(python_deps.requirements) == 1 |
| 40 |
parsed_req = python_deps.requirements[0] |
| 41 |
assert str(parsed_req.requirement) == "foo==1" |
| 42 |
assert parsed_req.options == {"hash": [exp_hash]} |
| 43 |
assert parsed_req.from_file == str(requirements) |
| 44 |
|
| 45 |
|
| 46 |
def test_deps_with_no_deps(tmp_path: Path) -> None: |
| 47 |
(tmp_path / "r.txt").write_text("urrlib3") |
| 48 |
python_deps = PythonDeps(raw="-rr.txt\n--no-deps", root=tmp_path) |
| 49 |
|
| 50 |
assert len(python_deps.requirements) == 1 |
| 51 |
parsed_req = python_deps.requirements[0] |
| 52 |
assert str(parsed_req.requirement) == "urrlib3" |
| 53 |
|
| 54 |
assert python_deps.options.no_deps is True |
| 55 |
assert python_deps.as_root_args == ["-r", "r.txt", "--no-deps"] |
| 56 |
|
| 57 |
|
| 58 |
def test_req_with_no_deps(tmp_path: Path) -> None: |
| 59 |
(tmp_path / "r.txt").write_text("--no-deps") |
| 60 |
python_deps = PythonDeps(raw="-rr.txt", root=tmp_path) |
| 61 |
with pytest.raises(ValueError, match="unrecognized arguments: --no-deps"): |
| 62 |
python_deps.requirements # noqa: B018 |
| 63 |
|
| 64 |
|
| 65 |
def test_opt_only_req_file(tmp_path: Path) -> None: |
| 66 |
(tmp_path / "r.txt").write_text("--use-feature fast-deps") |
| 67 |
python_deps = PythonDeps(raw="-rr.txt", root=tmp_path) |
| 68 |
assert not python_deps.requirements |
| 69 |
assert python_deps.options == Namespace(features_enabled=["fast-deps"]) |
| 70 |
|
| 71 |
|
| 72 |
def test_req_iadd(tmp_path: Path) -> None: |
| 73 |
a = PythonDeps(raw="foo", root=tmp_path) |
| 74 |
b = PythonDeps(raw="bar", root=tmp_path) |
| 75 |
a += b |
| 76 |
assert a.lines() == ["foo", "bar"] |
| 77 |
|
| 78 |
|
| 79 |
def test_deps_factory_invalid_list(tmp_path: Path) -> None: |
| 80 |
with pytest.raises(TypeError, match="deps expected str, list\\[str\\], or list\\[Requirement\\]") as exc_info: |
| 81 |
PythonDeps.factory(tmp_path, ["ok", 42, "also-ok"]) |
| 82 |
assert "got list with invalid items: [1] int" in str(exc_info.value) |
| 83 |
|
| 84 |
|
| 85 |
def test_deps_factory_invalid_type(tmp_path: Path) -> None: |
| 86 |
with pytest.raises(TypeError, match="deps expected str, list\\[str\\], or list\\[Requirement\\]") as exc_info: |
| 87 |
PythonDeps.factory(tmp_path, 123) |
| 88 |
assert "got int: 123" in str(exc_info.value) |
| 89 |
|
| 90 |
|
| 91 |
def test_constraints_factory_invalid_list(tmp_path: Path) -> None: |
| 92 |
with pytest.raises( |
| 93 |
TypeError, match="constraints expected str, list\\[str\\], or list\\[Requirement\\]" |
| 94 |
) as exc_info: |
| 95 |
PythonConstraints.factory(tmp_path, [["nested"]]) |
| 96 |
assert "got list with invalid items: [0] list" in str(exc_info.value) |
| 97 |
|
| 98 |
|
| 99 |
def test_constraints_factory_invalid_type(tmp_path: Path) -> None: |
| 100 |
with pytest.raises( |
| 101 |
TypeError, match="constraints expected str, list\\[str\\], or list\\[Requirement\\]" |
| 102 |
) as exc_info: |
| 103 |
PythonConstraints.factory(tmp_path, {"key": "val"}) |
| 104 |
assert "got dict: {'key': 'val'}" in str(exc_info.value) |