Skip to content

Commit 26308b1

Browse files
authored
Merge pull request #29 from nexB/fix/version_range_crash
Fix VersionRange crash on not in range versions
2 parents c9f8a56 + 0d37220 commit 26308b1

2 files changed

Lines changed: 46 additions & 16 deletions

File tree

src/univers/version_constraint.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -457,20 +457,20 @@ def contains_version(version, constraints):
457457
if len(constraints) == 1:
458458
return version in constraints[0]
459459

460-
# If the "tested version" is equal to the any of the constraint version
461-
# where the constraint comparator is for equality (any of "=", "<=", or ">=")
462-
# then the "tested version" is in the range. Check is finished.
463-
for constraint in constraints:
464-
if "=" in constraint.comparator and version == constraint.version:
465-
return True
466-
467460
# If the "tested version" is equal to the any of the constraint version where
468461
# the constraint comparator is "=!" then the "tested version" is NOT in the
469462
# range. Check is finished.
470463
for constraint in constraints:
471464
if "!=" in constraint.comparator and version == constraint.version:
472465
return False
473466

467+
# If the "tested version" is equal to the any of the constraint version
468+
# where the constraint comparator is for equality (any of "=", "<=", or ">=")
469+
# then the "tested version" is in the range. Check is finished.
470+
for constraint in constraints:
471+
if "=" in constraint.comparator and version == constraint.version:
472+
return True
473+
474474
# Split the constraint list in two sub lists:
475475
# a first list where the comparator is "=" or "!="
476476
# a second list where the comparator is neither "=" nor "!="
@@ -500,13 +500,9 @@ def contains_version(version, constraints):
500500
# and the "tested version" is greater than the current version
501501
# and the "tested version" is less than the next version
502502
# then the "tested version" is IN the range. Check is finished.
503-
if (
504-
cur_comp in (">", ">=")
505-
and nxt_comp in ("<", "<=")
506-
and version > cur_constraint.version
507-
and version < nxt_constraint.version
508-
):
509-
return True
503+
if cur_comp in (">", ">=") and nxt_comp in ("<", "<="):
504+
if version > cur_constraint.version and version < nxt_constraint.version:
505+
return True
510506

511507
# If current comparator is "<" or <=" and next comparator is ">" or >="
512508
# then these versions are out the range. Continue to the next iteration.

tests/test_version_range.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,49 @@ def test_VersionRange_to_string(self):
2727
# note the sorting taking place
2828
assert str(version_range) == "vers:pypi/>=0.0.0|0.0.1|0.0.2|0.0.3|0.0.4|0.0.5|0.0.6"
2929

30-
def test_VersionRange_not_contains(self):
30+
def test_VersionRange_pypi_does_not_contain_basic(self):
3131
vers = "vers:pypi/0.0.2|0.0.6|>=0.0.0|0.0.1|0.0.4|0.0.5|0.0.3"
3232
version_range = VersionRange.from_string(vers)
3333
assert not version_range.contains(PypiVersion("2.0.3"))
3434

35-
def test_VersionRange_contains(self):
35+
def test_VersionRange_does_not_contain_version_after_range(self):
36+
vers = "vers:pypi/>=1.0.0|<=2.0.0"
37+
version_range = VersionRange.from_string(vers)
38+
assert not version_range.contains(PypiVersion("2.0.3"))
39+
40+
def test_VersionRange_does_not_contain_version_before_range(self):
41+
vers = "vers:pypi/>=1.0.0|<=2.0.0"
42+
version_range = VersionRange.from_string(vers)
43+
assert not version_range.contains(PypiVersion("0.0.9"))
44+
45+
def test_VersionRange_does_not_contain_version_in_between(self):
46+
vers = "vers:pypi/<=1.0.0|>=2.0.0"
47+
version_range = VersionRange.from_string(vers)
48+
assert not version_range.contains(PypiVersion("1.5"))
49+
50+
def test_VersionRange_does_not_contain_version_excluded(self):
51+
vers = "vers:pypi/>=3.0.0|!=2.0.3"
52+
version_range = VersionRange.from_string(vers)
53+
assert not version_range.contains(PypiVersion("2.0.3"))
54+
55+
def test_VersionRange_contains_version_after(self):
3656
version_range = VersionRange.from_string("vers:pypi/>0.0.2")
3757
assert PypiVersion("0.0.3") in version_range
3858

59+
def test_VersionRange_contains_version_before(self):
60+
version_range = VersionRange.from_string("vers:pypi/<0.0.2")
61+
assert PypiVersion("0.0.0.1") in version_range
62+
63+
def test_VersionRange_contains_version_included(self):
64+
vers = "vers:pypi/>=3.0.0|2.0.3"
65+
version_range = VersionRange.from_string(vers)
66+
assert version_range.contains(PypiVersion("2.0.3"))
67+
68+
def test_VersionRange_contains_version_in_between(self):
69+
vers = "vers:pypi/>=1.0.0|<=2.0.0"
70+
version_range = VersionRange.from_string(vers)
71+
assert version_range.contains(PypiVersion("1.5"))
72+
3973
def test_VersionRange_from_string_pypi(self):
4074
vers = "vers:pypi/0.0.2|0.0.6|0.0.0|0.0.1|0.0.4|0.0.5|0.0.3"
4175
version_range = VersionRange.from_string(vers)

0 commit comments

Comments
 (0)