Skip to content

Commit 968d66f

Browse files
authored
Merge pull request #190 from immqu/deb-clarification
Raise error for deb comparators << and >>
2 parents 98da6af + 6731aa1 commit 968d66f

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/univers/version_constraint.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ def split(string):
202202
>>> assert VersionConstraint.split("<2.3") == ("<", "2.3",)
203203
>>> assert VersionConstraint.split(">2.3") == (">", "2.3",)
204204
>>> assert VersionConstraint.split("!=2.3") == ("!=", "2.3",)
205+
>>> try:
206+
... VersionConstraint.split("<<2.3")
207+
... raise Exception("ValueError should be raised")
208+
... except ValueError:
209+
... pass
205210
"""
206211
constraint_string = remove_spaces(string)
207212

@@ -212,9 +217,15 @@ def split(string):
212217
for comparator in COMPARATORS:
213218
if constraint_string.startswith(comparator):
214219
# NOTE: we do not report an error if this is not valid
215-
version = constraint_string.lstrip(comparator)
220+
version = constraint_string[len(comparator) :]
216221
if comparator == "*":
217222
version = ""
223+
224+
# Reject malformed repeated comparator prefixes such as
225+
# "<<2.3" and ">>2.3" in VERS constraints which are explicitly not supported in VERS.
226+
elif version and version[0] in "<>!=*":
227+
raise ValueError(f"Unknown comparator in constraint: {constraint_string!r}")
228+
218229
return comparator, version
219230

220231
# default to equality

tests/test_version_constraint.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,11 @@ def test_invert_opertaion(original, inverted):
8787
assert constraint.invert() == inverted_constraint
8888
else:
8989
assert constraint.invert() is None
90+
91+
@pytest.mark.parametrize("spec", ["<<2.3", ">>2.3"])
92+
def test_invalid_vers_comparator_prefixes(spec):
93+
with pytest.raises(ValueError, match="Unknown comparator"):
94+
VersionConstraint.from_string(
95+
string=spec,
96+
version_class=versions.SemverVersion,
97+
)

0 commit comments

Comments
 (0)