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: aboutcode-org#45

Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed May 17, 2022
1 parent 3039839 commit 328ba06
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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
19 changes: 19 additions & 0 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -278,6 +279,24 @@ 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):
version_range = VersionRange(
constraints=(
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")),
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")),
VersionConstraint(comparator="=", version=SemverVersion(string="2.5.0")),
VersionConstraint(comparator="=", version=SemverVersion(string="2.5.0")),
)
)

expected = VersionRange(
constraints=(
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")),
VersionConstraint(comparator="=", version=SemverVersion(string="2.5.0")),
)
)
assert version_range == 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 328ba06

Please sign in to comment.