1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
from __future__ import annotations
import os
import pytest
from dep_logic.markers import parse_marker
@pytest.mark.parametrize(
("marker_string", "environment", "expected"),
[
(f"os_name == '{os.name}'", None, True),
("os_name == 'foo'", {"os_name": "foo"}, True),
("os_name == 'foo'", {"os_name": "bar"}, False),
("'2.7' in python_version", {"python_version": "2.7.5"}, True),
("'2.7' not in python_version", {"python_version": "2.7"}, False),
(
"os_name == 'foo' and python_version ~= '2.7.0'",
{"os_name": "foo", "python_version": "2.7.6"},
True,
),
(
"python_version ~= '2.7.0' and (os_name == 'foo' or os_name == 'bar')",
{"os_name": "foo", "python_version": "2.7.4"},
True,
),
(
"python_version ~= '2.7.0' and (os_name == 'foo' or os_name == 'bar')",
{"os_name": "bar", "python_version": "2.7.4"},
True,
),
(
"python_version ~= '2.7.0' and (os_name == 'foo' or os_name == 'bar')",
{"os_name": "other", "python_version": "2.7.4"},
False,
),
("extra == 'security'", {"extra": "quux"}, False),
("extra == 'security'", {"extra": "security"}, True),
("extra == 'SECURITY'", {"extra": "security"}, True),
("extra == 'security'", {"extra": "SECURITY"}, True),
("extra == 'pep-685-norm'", {"extra": "PEP_685...norm"}, True),
(
"extra == 'Different.punctuation..is...equal'",
{"extra": "different__punctuation_is_EQUAL"},
True,
),
],
)
def test_evaluates(
marker_string: str, environment: dict[str, str | set[str]], expected: bool
) -> None:
assert parse_marker(marker_string).evaluate(environment) == expected
@pytest.mark.parametrize(
("marker_string", "environment", "expected"),
[
(f"os.name == '{os.name}'", None, True),
("sys.platform == 'win32'", {"sys_platform": "linux2"}, False),
("platform.version in 'Ubuntu'", {"platform_version": "#39"}, False),
("platform.machine=='x86_64'", {"platform_machine": "x86_64"}, True),
(
"platform.python_implementation=='Jython'",
{"platform_python_implementation": "CPython"},
False,
),
(
"python_version == '2.5' and platform.python_implementation!= 'Jython'",
{"python_version": "2.7"},
False,
),
(
(
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE"
" amd64 AMD64 win32 WIN32'"
),
{"platform_machine": "foo"},
False,
),
(
(
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE"
" amd64 AMD64 win32 WIN32'"
),
{"platform_machine": "x86_64"},
True,
),
(
(
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE"
" amd64 AMD64 win32 WIN32'"
),
{"platform_machine": "foo"},
True,
),
(
(
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE"
" amd64 AMD64 win32 WIN32'"
),
{"platform_machine": "x86_64"},
False,
),
("platform_release >= '6'", {"platform_release": "6.1-foobar"}, True),
# extras
# single extra
("extra != 'security'", {"extra": "quux"}, True),
("extra != 'security'", {"extra": "security"}, False),
("extra != 'security'", {}, True),
("extra != 'security'", {"platform_machine": "x86_64"}, True),
# normalization
("extra == 'Security.1'", {"extra": "security-1"}, True),
("extra == 'a'", {}, False),
("extra != 'a'", {}, True),
("extra == 'a' and extra == 'b'", {}, False),
("extra == 'a' or extra == 'b'", {}, False),
("extra != 'a' and extra != 'b'", {}, True),
("extra != 'a' or extra != 'b'", {}, True),
("extra != 'a' and extra == 'b'", {}, False),
("extra != 'a' or extra == 'b'", {}, True),
# multiple extras
("extra == 'a'", {"extra": ("a", "b")}, True),
("extra == 'a'", {"extra": ("b", "c")}, False),
("extra != 'a'", {"extra": ("a", "b")}, False),
("extra != 'a'", {"extra": ("b", "c")}, True),
("extra == 'a' and extra == 'b'", {"extra": ("a", "b", "c")}, True),
("extra == 'a' and extra == 'b'", {"extra": ("a", "c")}, False),
("extra == 'a' or extra == 'b'", {"extra": ("a", "c")}, True),
("extra == 'a' or extra == 'b'", {"extra": ("b", "c")}, True),
("extra == 'a' or extra == 'b'", {"extra": ("c", "d")}, False),
("extra != 'a' and extra != 'b'", {"extra": ("a", "c")}, False),
("extra != 'a' and extra != 'b'", {"extra": ("b", "c")}, False),
("extra != 'a' and extra != 'b'", {"extra": ("c", "d")}, True),
("extra != 'a' or extra != 'b'", {"extra": ("a", "b", "c")}, False),
("extra != 'a' or extra != 'b'", {"extra": ("a", "c")}, True),
("extra != 'a' or extra != 'b'", {"extra": ("b", "c")}, True),
("extra != 'a' and extra == 'b'", {"extra": ("a", "b")}, False),
("extra != 'a' and extra == 'b'", {"extra": ("b", "c")}, True),
("extra != 'a' and extra == 'b'", {"extra": ("c", "d")}, False),
("extra != 'a' or extra == 'b'", {"extra": ("a", "b")}, True),
("extra != 'a' or extra == 'b'", {"extra": ("c", "d")}, True),
("extra != 'a' or extra == 'b'", {"extra": ("a", "c")}, False),
],
)
def test_evaluate_extra(
marker_string: str, environment: dict[str, str | set[str]] | None, expected: bool
) -> None:
m = parse_marker(marker_string)
assert m.evaluate(environment) is expected
@pytest.mark.parametrize(
"marker, env",
[
(
'platform_release >= "9.0" and platform_release < "11.0"',
{"platform_release": "10.0"},
)
],
)
def test_parse_version_like_markers(
marker: str, env: dict[str, str | set[str]]
) -> None:
m = parse_marker(marker)
assert m.evaluate(env)
@pytest.mark.parametrize("variable", ["extras", "dependency_groups"])
@pytest.mark.parametrize(
"expression,result",
[
pytest.param('"foo" in {0}', True, id="value-in-foo"),
pytest.param('"bar" in {0}', True, id="value-in-bar"),
pytest.param('"baz" in {0}', False, id="value-not-in"),
pytest.param('"baz" not in {0}', True, id="value-not-in-negated"),
pytest.param('"foo" in {0} and "bar" in {0}', True, id="and-in"),
pytest.param('"foo" in {0} or "bar" in {0}', True, id="or-in"),
pytest.param('"baz" in {0} and "foo" in {0}', False, id="short-circuit-and"),
pytest.param('"foo" in {0} or "baz" in {0}', True, id="short-circuit-or"),
pytest.param('"Foo" in {0}', True, id="case-sensitive"),
],
)
def test_extras_and_dependency_groups(
variable: str, expression: str, result: bool
) -> None:
environment: dict[str, str | set[str]] = {variable: {"foo", "bar"}}
assert parse_marker(expression.format(variable)).evaluate(environment) == result
@pytest.mark.parametrize("variable", ["extras", "dependency_groups"])
def test_extras_and_dependency_groups_disallowed(variable: str) -> None:
marker = parse_marker(f'"foo" in {variable}')
assert not marker.evaluate(context="lock_file")
with pytest.raises(KeyError):
marker.evaluate()
with pytest.raises(KeyError):
marker.evaluate(context="requirement")
|