Skip to content

Commit 7b4fca8

Browse files
committed
Add version range support for maven
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 269d8c0 commit 7b4fca8

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

src/univers/version_range.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from semantic_version.base import AnyOf
1313

1414
from univers import gem
15+
from univers import maven
1516
from univers import versions
1617
from univers.utils import remove_spaces
1718
from univers.version_constraint import VersionConstraint
@@ -590,6 +591,56 @@ class MavenVersionRange(VersionRange):
590591
scheme = "maven"
591592
version_class = versions.MavenVersion
592593

594+
@classmethod
595+
def from_native(cls, string):
596+
"""
597+
Return a VersionRange built from a Maven version specifiers ``string``.
598+
"""
599+
600+
maven_version_range_restrictions = maven.Restriction(string)
601+
constraints = []
602+
lower_bound = maven_version_range_restrictions.lower_bound
603+
upper_bound = maven_version_range_restrictions.upper_bound
604+
lower_inclusive = maven_version_range_restrictions.lower_bound_inclusive
605+
upper_inclusive = maven_version_range_restrictions.upper_bound_inclusive
606+
607+
if lower_bound == upper_bound:
608+
constraints.append(
609+
VersionConstraint(comparator="=", version=cls.version_class(str(lower_bound)))
610+
)
611+
return cls(constraints=constraints)
612+
613+
if lower_bound:
614+
comparator = ">"
615+
if lower_inclusive:
616+
comparator = ">="
617+
constraints.append(
618+
VersionConstraint(
619+
comparator=comparator, version=cls.version_class(str(lower_bound))
620+
)
621+
)
622+
623+
if upper_bound:
624+
comparator = "<"
625+
if upper_inclusive:
626+
comparator = "<="
627+
constraints.append(
628+
VersionConstraint(
629+
comparator=comparator, version=cls.version_class(str(upper_bound))
630+
)
631+
)
632+
633+
return cls(constraints=constraints)
634+
635+
@classmethod
636+
def from_natives(cls, strings):
637+
if isinstance(strings, str):
638+
return cls.from_native(strings)
639+
constraints = []
640+
for rel in strings:
641+
constraints.extend(cls.from_native(rel).constraints)
642+
return cls(constraints=constraints)
643+
593644

594645
class NugetVersionRange(VersionRange):
595646
"""

src/univers/versions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ def is_valid(cls, string):
274274
return debian.Version.is_valid(string)
275275

276276

277+
@total_ordering
277278
@attr.s(frozen=True, order=False, eq=False, hash=True)
278279
class MavenVersion(Version):
279280
# See https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html
@@ -283,6 +284,16 @@ class MavenVersion(Version):
283284
def build_value(cls, string):
284285
return maven.Version(string)
285286

287+
def __eq__(self, other):
288+
if not isinstance(other, self.__class__):
289+
return NotImplemented
290+
return maven.Version(self.string) == maven.Version(other.string)
291+
292+
def __lt__(self, other):
293+
if not isinstance(other, self.__class__):
294+
return NotImplemented
295+
return maven.Version(self.string) < maven.Version(other.string)
296+
286297

287298
@attr.s(frozen=True, order=False, eq=False, hash=True)
288299
class NugetVersion(SemverVersion):

tests/test_maven_version_range.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# Copyright (c) nexB Inc. and others.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Visit https://aboutcode.org and https://github.com/nexB/univers for support and download.
6+
7+
from univers.version_range import MavenVersionRange
8+
from univers.version_constraint import VersionConstraint
9+
from univers.versions import MavenVersion
10+
11+
12+
def test_maven_version_range_from_native_with_lower_inclusive():
13+
version_range = MavenVersionRange.from_native("[1.0.0,2.0.0)")
14+
assert version_range == MavenVersionRange(
15+
constraints=(
16+
VersionConstraint(comparator=">=", version=MavenVersion(string="1.0.0")),
17+
VersionConstraint(comparator="<", version=MavenVersion(string="2.0.0")),
18+
)
19+
)
20+
21+
22+
def test_maven_version_range_from_native_with_nothing_inclusive():
23+
version_range = MavenVersionRange.from_native("(1.0.0,2.1.4)")
24+
assert version_range == MavenVersionRange(
25+
constraints=(
26+
VersionConstraint(comparator=">", version=MavenVersion(string="1.0.0")),
27+
VersionConstraint(comparator="<", version=MavenVersion(string="2.1.4")),
28+
)
29+
)
30+
31+
32+
def test_maven_version_range_from_native_for_pinned_version():
33+
version_range = MavenVersionRange.from_native("[1.0.0]")
34+
assert version_range == MavenVersionRange(
35+
constraints=(VersionConstraint(comparator="=", version=MavenVersion(string="1.0.0")),)
36+
)
37+
38+
39+
def test_maven_version_range_from_native_for_illformed_version():
40+
try:
41+
MavenVersionRange.from_native("(1.0.0]")
42+
assert False, "should have raised an exception"
43+
except ValueError:
44+
assert True
45+
46+
47+
def test_maven_version_range_from_native_str_representation():
48+
version_range = MavenVersionRange.from_native("[1.0.0,5.1)")
49+
assert str(version_range) == "vers:maven/>=1.0.0|<5.1"

0 commit comments

Comments
 (0)