Skip to content

Commit

Permalink
fix the constraint duplication in VersionRange
Browse files Browse the repository at this point in the history
- add test for constraint duplicaton
- closes: #45

Signed-off-by: Keshav Priyadarshi <[email protected]>
  • Loading branch information
keshav-space committed May 11, 2022
1 parent 91f2875 commit 4bac91c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/univers/nuget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import functools
import re

import semver

_PAD_WIDTH = 8
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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+"],
Expand Down

0 comments on commit 4bac91c

Please sign in to comment.