|
1 | 1 | import pathlib |
| 2 | +import typing |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from packaging import markers |
4 | 6 | from packaging.requirements import Requirement |
5 | 7 | from packaging.version import Version |
6 | 8 |
|
@@ -37,15 +39,67 @@ def test_add_constraint_conflict(): |
37 | 39 | c = constraints.Constraints() |
38 | 40 | c.add_constraint("foo<=1.1") |
39 | 41 | c.add_constraint("flit_core==2.0rc3") |
| 42 | + |
| 43 | + # Exact duplicate should raise error (same package, same marker) |
40 | 44 | with pytest.raises(KeyError): |
41 | 45 | c.add_constraint("foo<=1.1") |
| 46 | + |
| 47 | + # Different version, same marker (no marker) should raise error |
42 | 48 | with pytest.raises(KeyError): |
43 | 49 | c.add_constraint("foo>1.1") |
| 50 | + |
| 51 | + # Different version for flit_core should raise error |
44 | 52 | with pytest.raises(KeyError): |
45 | 53 | c.add_constraint("flit_core>2.0.0") |
| 54 | + |
| 55 | + # Normalized name conflict should raise error |
46 | 56 | with pytest.raises(KeyError): |
47 | 57 | c.add_constraint("flit-core>2.0.0") |
48 | 58 |
|
| 59 | + # Different, but equivalent markers should raise KeyError |
| 60 | + with pytest.raises(KeyError): |
| 61 | + c.add_constraint( |
| 62 | + "bar==1.0; python_version >= '3.11' and platform_machine == 'x86_64'" |
| 63 | + ) |
| 64 | + c.add_constraint( |
| 65 | + "bar==1.1; platform_machine == 'x86_64' and python_version >= '3.11'" |
| 66 | + ) |
| 67 | + c.add_constraint( |
| 68 | + "bar==1.0; python_version >= '3.11' and platform_machine == 'arm64'" |
| 69 | + ) |
| 70 | + c.add_constraint( |
| 71 | + "bar==1.1; platform_machine == 'arm64' and python_version >= '3.11'" |
| 72 | + ) |
| 73 | + |
| 74 | + # Same package with different markers should NOT raise error |
| 75 | + c.add_constraint("baz==1.0; platform_machine != 'ppc64le'") |
| 76 | + c.add_constraint("baz==1.1; platform_machine == 'ppc64le'") |
| 77 | + |
| 78 | + # But same package with same marker should raise error |
| 79 | + with pytest.raises(KeyError): |
| 80 | + c.add_constraint("foo==1.2; platform_machine != 'ppc64le'") |
| 81 | + |
| 82 | + # Verify multiple constraints for same package are stored |
| 83 | + assert len(c._data) == 4 # flit_core, foo, bar, and baz |
| 84 | + |
| 85 | + # Make sure correct constraint is added |
| 86 | + env = typing.cast(dict[str, str], markers.default_environment()) |
| 87 | + constraint = c.get_constraint("bar") |
| 88 | + |
| 89 | + if env.get("platform_machine") == "x86_64" and constraint is not None: |
| 90 | + assert constraint.name == "bar" |
| 91 | + assert constraint.specifier == "==1.0" |
| 92 | + assert constraint.marker == markers.Marker( |
| 93 | + 'python_version >= "3.11" and platform_machine == "x86_64"' |
| 94 | + ) |
| 95 | + |
| 96 | + if env.get("platform_machine") == "arm64" and constraint is not None: |
| 97 | + assert constraint.name == "bar" |
| 98 | + assert constraint.specifier == "==1.0" |
| 99 | + assert constraint.marker == markers.Marker( |
| 100 | + 'python_version >= "3.11" and platform_machine == "arm64"' |
| 101 | + ) |
| 102 | + |
49 | 103 |
|
50 | 104 | def test_allow_prerelease(): |
51 | 105 | c = constraints.Constraints() |
|
0 commit comments