diff --git a/src/univers/version_range.py b/src/univers/version_range.py index e1863c32..15ddcddd 100644 --- a/src/univers/version_range.py +++ b/src/univers/version_range.py @@ -857,7 +857,7 @@ class NginxVersionRange(VersionRange): """ scheme = "nginx" - version_class = versions.SemverVersion + version_class = versions.NginxVersion vers_by_native_comparators = { "==": "=", @@ -919,12 +919,10 @@ def from_native(cls, string): # suffixed version vs = clauses.rstrip("+") version = cls.version_class(vs) - is_stable = is_even(version.value.minor) - - if is_stable: + if version.is_stable: # we have a start and end in stable ranges start_version = cls.version_class(vs) - end_version = cls.version_class(str(start_version.value.next_minor())) + end_version = cls.version_class(str(start_version.next_minor())) vstart = VersionConstraint(comparator=">=", version=start_version) vend = VersionConstraint(comparator="<", version=end_version) constraints.extend([vstart, vend]) @@ -1036,21 +1034,6 @@ def build_range_from_github_advisory_constraint(scheme: str, string: str): return vrc(constraints=constraints) -def is_even(s): - """ - Return True if the string "s" is an even number and False if this is an odd - number. For example: - - >>> is_even(4) - True - >>> is_even(123) - False - >>> is_even(0) - True - """ - return (int(s) % 2) == 0 - - RANGE_CLASS_BY_SCHEMES = { "npm": NpmVersionRange, "deb": DebianVersionRange, diff --git a/src/univers/versions.py b/src/univers/versions.py index 7168fcba..e0f61a8d 100644 --- a/src/univers/versions.py +++ b/src/univers/versions.py @@ -219,6 +219,64 @@ def is_valid(cls, string): except ValueError: return False + @property + def major(self): + return self.value and self.value.major + + @property + def minor(self): + return self.value and self.value.minor + + @property + def patch(self): + return self.value and self.value.patch + + @property + def prerelease(self): + return self.value and self.value.prerelease + + @property + def build(self): + return self.value and self.value.build + + def next_major(self): + return self.value and self.value.next_major() + + def next_minor(self): + return self.value and self.value.next_minor() + + def next_patch(self): + return self.value and self.value.next_patch() + + +def is_even(s): + """ + Return True if the string "s" is an even number and False if this is an odd + number. For example: + + >>> is_even(4) + True + >>> is_even(123) + False + >>> is_even(0) + True + """ + return (int(s) % 2) == 0 + + +@attr.s(frozen=True, order=False, eq=False, hash=True) +class NginxVersion(SemverVersion): + """ + Semver with 3 segments and extra attribute for stable vs. unstable branches + """ + + @property + def is_stable(self): + """ + True if this is a "stable "version + """ + return is_even(self.minor) + @attr.s(frozen=True, order=False, eq=False, hash=True) class RubygemsVersion(Version): diff --git a/tests/test_version_range.py b/tests/test_version_range.py index b2a81048..de2dd87d 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -195,9 +195,9 @@ def test_GemVersionRange_from_native_range_with_pessimistic_operator(self): ) def test_VersionRange_contains_works_for_star_range(self): - from univers.versions import SemverVersion + from univers.versions import NginxVersion - assert SemverVersion("1.0.0") in VersionRange.from_string("vers:nginx/*") + assert NginxVersion("1.0.0") in VersionRange.from_string("vers:nginx/*") def test_NpmVersionRange_from_native_with_compatible_with_version_operator(self): npm_range = "^1.2.9"