Skip to content

Commit df34260

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 b72e697 commit df34260

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from univers.version_range import RANGE_CLASS_BY_SCHEMES
1919
from univers.version_range import NpmVersionRange
2020
from univers.version_range import OpensslVersionRange
21+
from univers.version_range import NginxVersionRange
2122
from univers.versions import PypiVersion
2223
from univers.versions import NugetVersion
2324
from univers.versions import RubygemsVersion
@@ -278,6 +279,24 @@ def test_nuget_version_range(self):
278279
assert version_range == expected
279280
assert version_range.to_string() == "vers:nuget/>=1.0.0|<2.0.0"
280281

282+
def test_version_range_constraint_duplication(self):
283+
version_range = VersionRange(
284+
constraints=(
285+
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")),
286+
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")),
287+
VersionConstraint(comparator="=", version=SemverVersion(string="2.5.0")),
288+
VersionConstraint(comparator="=", version=SemverVersion(string="2.5.0")),
289+
)
290+
)
291+
292+
expected = VersionRange(
293+
constraints=(
294+
VersionConstraint(comparator=">=", version=SemverVersion(string="1.4.0")),
295+
VersionConstraint(comparator="=", version=SemverVersion(string="2.5.0")),
296+
)
297+
)
298+
assert version_range == expected
299+
281300

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

0 commit comments

Comments
 (0)