Skip to content

Commit 4bac91c

Browse files
committed
fix the constraint duplication in VersionRange
- add test for constraint duplicaton - closes: #45 Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 91f2875 commit 4bac91c

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/univers/nuget.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import functools
1616
import re
17-
1817
import semver
1918

2019
_PAD_WIDTH = 8
@@ -164,6 +163,9 @@ def __lt__(self, other):
164163
# Revision is the same, so ignore it for comparison purposes.
165164
return self._base_semver < other._base_semver
166165

166+
def __hash__(self):
167+
return hash(repr(self))
168+
167169
@classmethod
168170
def from_string(cls, str_version):
169171
str_version = coerce(str_version)

src/univers/version_range.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class VersionRange:
5151
constraints = attr.ib(type=tuple, default=attr.Factory(tuple))
5252

5353
def __attrs_post_init__(self, *args, **kwargs):
54-
constraints = tuple(sorted(self.constraints))
54+
constraints = tuple(sorted(set(self.constraints)))
5555
# Notes: setattr is used because this is an immutable frozen instance.
5656
# See https://www.attrs.org/en/stable/init.html?#post-init
5757
object.__setattr__(self, "constraints", constraints)

tests/test_version_range.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from univers.version_range import RANGE_CLASS_BY_SCHEMES
1717
from univers.version_range import NpmVersionRange
1818
from univers.version_range import OpensslVersionRange
19+
from univers.version_range import NginxVersionRange
1920
from univers.versions import PypiVersion
2021
from univers.versions import NugetVersion
2122
from univers.versions import RubygemsVersion
@@ -276,6 +277,18 @@ def test_nuget_version_range(self):
276277
assert version_range == expected
277278
assert version_range.to_string() == "vers:nuget/>=1.0.0|<2.0.0"
278279

280+
def test_version_range_constraint_duplication(self):
281+
constraints = NginxVersionRange.from_native("1.5.0+, 1.4.1+, 1.4.0+")
282+
expected = NginxVersionRange(
283+
constraints=(
284+
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")),
285+
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.1")),
286+
VersionConstraint(comparator="<", version=SemverVersion(string="1.5.0")),
287+
VersionConstraint(comparator=">=", version=SemverVersion(string="1.5.0")),
288+
)
289+
)
290+
assert constraints == expected
291+
279292

280293
VERSION_RANGE_TESTS_BY_SCHEME = {
281294
"nginx": ["0.8.40+", "0.7.52-0.8.39", "0.9.10", "1.5.0+, 1.4.1+"],

0 commit comments

Comments
 (0)