From 0bd9162b7906033b0759f5fce63342e8ba81392d Mon Sep 17 00:00:00 2001 From: Hritik Vijay Date: Fri, 14 Jan 2022 14:14:47 +0530 Subject: [PATCH 1/2] Fix VersionRange crash on not in range versions Earlier, the check for whether the pairwise version constraints is in a valid sequence and if the given version is in that pairwise range was performed in a single condition. Upon failing the later case, it assumed the failure of earlier as well and crashed. Now, the later check is performed within the former condition. Also, the check for != before looking for any constraint with a "=" avoids matching "!=" in the later case. Fixes: https://github.com/nexB/univers/issues/25 Signed-off-by: Hritik Vijay --- src/univers/version_constraint.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/univers/version_constraint.py b/src/univers/version_constraint.py index 8ae3a3d9..c5089000 100644 --- a/src/univers/version_constraint.py +++ b/src/univers/version_constraint.py @@ -444,13 +444,6 @@ def contains_version(version, constraints): if len(constraints) == 1: return version in constraints[0] - # If the "tested version" is equal to the any of the constraint version - # where the constraint comparator is for equality (any of "=", "<=", or ">=") - # then the "tested version" is in the range. Check is finished. - for constraint in constraints: - if "=" in constraint.comparator and version == constraint.version: - return True - # If the "tested version" is equal to the any of the constraint version where # the constraint comparator is "=!" then the "tested version" is NOT in the # range. Check is finished. @@ -458,6 +451,13 @@ def contains_version(version, constraints): if "!=" in constraint.comparator and version == constraint.version: return False + # If the "tested version" is equal to the any of the constraint version + # where the constraint comparator is for equality (any of "=", "<=", or ">=") + # then the "tested version" is in the range. Check is finished. + for constraint in constraints: + if "=" in constraint.comparator and version == constraint.version: + return True + # Split the constraint list in two sub lists: # a first list where the comparator is "=" or "!=" # a second list where the comparator is neither "=" nor "!=" @@ -487,13 +487,9 @@ def contains_version(version, constraints): # and the "tested version" is greater than the current version # and the "tested version" is less than the next version # then the "tested version" is IN the range. Check is finished. - if ( - cur_comp in (">", ">=") - and nxt_comp in ("<", "<=") - and version > cur_constraint.version - and version < nxt_constraint.version - ): - return True + if cur_comp in (">", ">=") and nxt_comp in ("<", "<="): + if version > cur_constraint.version and version < nxt_constraint.version: + return True # If current comparator is "<" or <=" and next comparator is ">" or >=" # then these versions are out the range. Continue to the next iteration. From 0d3722054f78e5e21f79a7058090898c02ebea52 Mon Sep 17 00:00:00 2001 From: Hritik Vijay Date: Fri, 14 Jan 2022 17:54:11 +0530 Subject: [PATCH 2/2] Add tests for VersionRange contains & not contains Signed-off-by: Hritik Vijay --- tests/test_version_range.py | 38 +++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/test_version_range.py b/tests/test_version_range.py index 06d601d7..6a193806 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -27,15 +27,49 @@ def test_VersionRange_to_string(self): # note the sorting taking place 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" - def test_VersionRange_not_contains(self): + def test_VersionRange_pypi_does_not_contain_basic(self): vers = "vers:pypi/0.0.2|0.0.6|>=0.0.0|0.0.1|0.0.4|0.0.5|0.0.3" version_range = VersionRange.from_string(vers) assert not version_range.contains(PypiVersion("2.0.3")) - def test_VersionRange_contains(self): + def test_VersionRange_does_not_contain_version_after_range(self): + vers = "vers:pypi/>=1.0.0|<=2.0.0" + version_range = VersionRange.from_string(vers) + assert not version_range.contains(PypiVersion("2.0.3")) + + def test_VersionRange_does_not_contain_version_before_range(self): + vers = "vers:pypi/>=1.0.0|<=2.0.0" + version_range = VersionRange.from_string(vers) + assert not version_range.contains(PypiVersion("0.0.9")) + + def test_VersionRange_does_not_contain_version_in_between(self): + vers = "vers:pypi/<=1.0.0|>=2.0.0" + version_range = VersionRange.from_string(vers) + assert not version_range.contains(PypiVersion("1.5")) + + def test_VersionRange_does_not_contain_version_excluded(self): + vers = "vers:pypi/>=3.0.0|!=2.0.3" + version_range = VersionRange.from_string(vers) + assert not version_range.contains(PypiVersion("2.0.3")) + + def test_VersionRange_contains_version_after(self): version_range = VersionRange.from_string("vers:pypi/>0.0.2") assert PypiVersion("0.0.3") in version_range + def test_VersionRange_contains_version_before(self): + version_range = VersionRange.from_string("vers:pypi/<0.0.2") + assert PypiVersion("0.0.0.1") in version_range + + def test_VersionRange_contains_version_included(self): + vers = "vers:pypi/>=3.0.0|2.0.3" + version_range = VersionRange.from_string(vers) + assert version_range.contains(PypiVersion("2.0.3")) + + def test_VersionRange_contains_version_in_between(self): + vers = "vers:pypi/>=1.0.0|<=2.0.0" + version_range = VersionRange.from_string(vers) + assert version_range.contains(PypiVersion("1.5")) + def test_VersionRange_from_string_pypi(self): vers = "vers:pypi/0.0.2|0.0.6|0.0.0|0.0.1|0.0.4|0.0.5|0.0.3" version_range = VersionRange.from_string(vers)