Skip to content

Commit 6955566

Browse files
authored
Merge pull request #160 from immqu/lexicographic-versioning-scheme
Add lexicographic versioning scheme
2 parents eef8d41 + 9064438 commit 6955566

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/univers/version_range.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,11 @@ class NoneVersionRange(VersionRange):
12011201
version_class = versions.NoneVersion
12021202

12031203

1204+
class LexicographicVersionRange(VersionRange):
1205+
scheme = "lexicographic"
1206+
version_class = versions.LexicographicVersion
1207+
1208+
12041209
def from_gitlab_native(gitlab_scheme, string):
12051210
purl_scheme = gitlab_scheme
12061211
if gitlab_scheme not in PURL_TYPE_BY_GITLAB_SCHEME.values():
@@ -1446,6 +1451,7 @@ def build_range_from_snyk_advisory_string(scheme: str, string: Union[str, List])
14461451
"all": AllVersionRange,
14471452
"none": NoneVersionRange,
14481453
"intdot": IntdotVersionRange,
1454+
"lexicographic": LexicographicVersionRange,
14491455
}
14501456

14511457
PURL_TYPE_BY_GITLAB_SCHEME = {

src/univers/versions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,29 @@ def is_valid(cls, string):
146146
return string == "vers:none/*"
147147

148148

149+
class LexicographicVersion(Version):
150+
@classmethod
151+
def build_value(cls, string):
152+
return str(string)
153+
154+
"""
155+
Create a string, even if, e.g., an integer is given
156+
"""
157+
158+
@classmethod
159+
def normalize(cls, string):
160+
return remove_spaces(str(string))
161+
162+
def __lt__(self, other):
163+
return self.value.encode("utf-8") < other.value.encode("utf-8")
164+
165+
def __gt__(self, other):
166+
return self.value.encode("utf-8") > other.value.encode("utf-8")
167+
168+
def __eq__(self, other):
169+
return self.value.encode("utf-8") == other.value.encode("utf-8")
170+
171+
149172
class IntdotVersion(Version):
150173
@classmethod
151174
def build_value(cls, string):
@@ -726,4 +749,5 @@ def bump(self, index):
726749
LegacyOpensslVersion,
727750
AlpineLinuxVersion,
728751
IntdotVersion,
752+
LexicographicVersion,
729753
]

tests/test_version_range.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from univers.version_range import build_range_from_snyk_advisory_string
2323
from univers.version_range import from_gitlab_native
2424
from univers.versions import IntdotVersion
25+
from univers.versions import LexicographicVersion
2526
from univers.versions import OpensslVersion
2627
from univers.versions import PypiVersion
2728
from univers.versions import SemverVersion
@@ -366,3 +367,12 @@ def test_version_range_intdot():
366367
assert IntdotVersion("1.3.3alpha") in intdot_range
367368
assert IntdotVersion("1.2.2.pre") not in intdot_range
368369
assert IntdotVersion("1010.23.234203.0") in IntdotVersionRange.from_string("vers:intdot/*")
370+
371+
372+
def test_version_range_lexicographic():
373+
assert LexicographicVersion("1.2.3") in VersionRange.from_string(
374+
"vers:lexicographic/<1.2.4|>0.9"
375+
)
376+
assert LexicographicVersion(-123) in VersionRange.from_string("vers:lexicographic/<~")
377+
assert LexicographicVersion(None) in VersionRange.from_string("vers:lexicographic/*")
378+
assert LexicographicVersion("ABC") in VersionRange.from_string("vers:lexicographic/>abc|<=None")

tests/test_versions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from univers.versions import GentooVersion
1414
from univers.versions import GolangVersion
1515
from univers.versions import IntdotVersion
16+
from univers.versions import LexicographicVersion
1617
from univers.versions import MavenVersion
1718
from univers.versions import NginxVersion
1819
from univers.versions import NugetVersion
@@ -230,3 +231,13 @@ def test_intdot_version():
230231
assert IntdotVersion("1.2.3.4.6-pre") <= IntdotVersion("2.2.3.4.5.pre")
231232
assert IntdotVersion("1.2.3.4.6-pre") <= IntdotVersion("2.2.3.4.5-10")
232233
assert IntdotVersion("1.2.3.4.6-pre") <= IntdotVersion("2.2.3.4.5-10")
234+
235+
236+
def test_lexicographic_version():
237+
assert LexicographicVersion("abc") == LexicographicVersion("abc")
238+
assert LexicographicVersion(" abc") == LexicographicVersion("abc")
239+
assert LexicographicVersion("123") == LexicographicVersion(123)
240+
assert LexicographicVersion("abc") > LexicographicVersion(None)
241+
assert LexicographicVersion("Abc") < LexicographicVersion(None)
242+
assert LexicographicVersion("123") < LexicographicVersion("bbc")
243+
assert LexicographicVersion("2.3.4") > LexicographicVersion("1.2.3")

0 commit comments

Comments
 (0)