Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lexicographic versioning scheme #147

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,11 @@ class GolangVersionRange(VersionRange):
}


class LexicographicVersionRange(VersionRange):
scheme = "lexicographic"
version_class = versions.LexicographicVersion


class GenericVersionRange(VersionRange):
scheme = "generic"
version_class = versions.SemverVersion
Expand Down Expand Up @@ -1407,6 +1412,7 @@ def build_range_from_snyk_advisory_string(scheme: str, string: Union[str, List])
"gem": GemVersionRange,
"rpm": RpmVersionRange,
"golang": GolangVersionRange,
"lexicographic": LexicographicVersionRange,
"generic": GenericVersionRange,
"apache": ApacheVersionRange,
"hex": HexVersionRange,
Expand Down
14 changes: 14 additions & 0 deletions src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ def __str__(self):
return str(self.value)


class LexicographicVersion(Version):
@classmethod
def build_value(cls, string):
return str(string)

"""
Create a string, even if, e.g., an integer is given
"""

def normalize(cls, string):
return remove_spaces(str(string))


class GenericVersion(Version):
@classmethod
def is_valid(cls, string):
Expand Down Expand Up @@ -690,6 +703,7 @@ def bump(self, index):
SemverVersion,
GolangVersion,
PypiVersion,
LexicographicVersion,
GenericVersion,
ComposerVersion,
NginxVersion,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from univers.version_range import build_range_from_snyk_advisory_string
from univers.version_range import from_gitlab_native
from univers.versions import InvalidVersion
from univers.versions import LexicographicVersion
from univers.versions import NugetVersion
from univers.versions import OpensslVersion
from univers.versions import PypiVersion
Expand Down Expand Up @@ -546,3 +547,12 @@ def test_version_range_normalize_case3():
nvr = vr.normalize(known_versions=known_versions)

assert str(nvr) == "vers:pypi/>=1.0.0|<=1.3.0|3.0.0"


def test_version_range_lexicographic():
assert LexicographicVersion("1.2.3") in VersionRange.from_string(
"vers:lexicographic/<1.2.4|>0.9"
)
assert LexicographicVersion(-123) in VersionRange.from_string("vers:lexicographic/<~")
assert LexicographicVersion(None) in VersionRange.from_string("vers:lexicographic/*")
assert LexicographicVersion("ABC") in VersionRange.from_string("vers:lexicographic/>abc|<=None")
11 changes: 11 additions & 0 deletions tests/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from univers.versions import EnhancedSemanticVersion
from univers.versions import GentooVersion
from univers.versions import GolangVersion
from univers.versions import LexicographicVersion
from univers.versions import MavenVersion
from univers.versions import NginxVersion
from univers.versions import NugetVersion
Expand Down Expand Up @@ -218,3 +219,13 @@ def test_golang_version():
assert GolangVersion("v0.1.1") >= GolangVersion("v0.1.1")
assert GolangVersion("v0.1.1") <= GolangVersion("v0.1.1")
assert GolangVersion("v0.1.1") <= GolangVersion("v0.1.2")


def test_lexicographic_version():
assert LexicographicVersion("abc") == LexicographicVersion("abc")
assert LexicographicVersion(" abc") == LexicographicVersion("abc")
assert LexicographicVersion("123") == LexicographicVersion(123)
assert LexicographicVersion("abc") > LexicographicVersion(None)
assert LexicographicVersion("Abc") < LexicographicVersion(None)
assert LexicographicVersion("123") < LexicographicVersion("bbc")
assert LexicographicVersion("2.3.4") > LexicographicVersion("1.2.3")