From 4bac91c4d4bf39fc53b2939a684af34490f28eae Mon Sep 17 00:00:00 2001 From: Keshav Priyadarshi Date: Wed, 11 May 2022 19:17:44 +0530 Subject: [PATCH] fix the constraint duplication in VersionRange - add test for constraint duplicaton - closes: https://github.com/nexB/univers/issues/45 Signed-off-by: Keshav Priyadarshi --- src/univers/nuget.py | 4 +++- src/univers/version_range.py | 2 +- tests/test_version_range.py | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/univers/nuget.py b/src/univers/nuget.py index 371ed69a..2855dc0c 100644 --- a/src/univers/nuget.py +++ b/src/univers/nuget.py @@ -14,7 +14,6 @@ import functools import re - import semver _PAD_WIDTH = 8 @@ -164,6 +163,9 @@ def __lt__(self, other): # Revision is the same, so ignore it for comparison purposes. return self._base_semver < other._base_semver + def __hash__(self): + return hash(repr(self)) + @classmethod def from_string(cls, str_version): str_version = coerce(str_version) diff --git a/src/univers/version_range.py b/src/univers/version_range.py index e1863c32..3104862d 100644 --- a/src/univers/version_range.py +++ b/src/univers/version_range.py @@ -51,7 +51,7 @@ class VersionRange: constraints = attr.ib(type=tuple, default=attr.Factory(tuple)) def __attrs_post_init__(self, *args, **kwargs): - constraints = tuple(sorted(self.constraints)) + constraints = tuple(sorted(set(self.constraints))) # Notes: setattr is used because this is an immutable frozen instance. # See https://www.attrs.org/en/stable/init.html?#post-init object.__setattr__(self, "constraints", constraints) diff --git a/tests/test_version_range.py b/tests/test_version_range.py index b2a81048..fd248c94 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -16,6 +16,7 @@ from univers.version_range import RANGE_CLASS_BY_SCHEMES from univers.version_range import NpmVersionRange from univers.version_range import OpensslVersionRange +from univers.version_range import NginxVersionRange from univers.versions import PypiVersion from univers.versions import NugetVersion from univers.versions import RubygemsVersion @@ -276,6 +277,18 @@ def test_nuget_version_range(self): assert version_range == expected assert version_range.to_string() == "vers:nuget/>=1.0.0|<2.0.0" + def test_version_range_constraint_duplication(self): + constraints = NginxVersionRange.from_native("1.5.0+, 1.4.1+, 1.4.0+") + expected = NginxVersionRange( + constraints=( + VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")), + VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.1")), + VersionConstraint(comparator="<", version=SemverVersion(string="1.5.0")), + VersionConstraint(comparator=">=", version=SemverVersion(string="1.5.0")), + ) + ) + assert constraints == expected + VERSION_RANGE_TESTS_BY_SCHEME = { "nginx": ["0.8.40+", "0.7.52-0.8.39", "0.9.10", "1.5.0+, 1.4.1+"],