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

Subclass semver for nginx #63

Merged
merged 3 commits into from
May 12, 2022
Merged
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
23 changes: 3 additions & 20 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ class NginxVersionRange(VersionRange):
"""

scheme = "nginx"
version_class = versions.SemverVersion
version_class = versions.NginxVersion

vers_by_native_comparators = {
"==": "=",
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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,
Expand Down
58 changes: 58 additions & 0 deletions src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,64 @@ def is_valid(cls, string):
except ValueError:
return False

@property
def major(self):
return self.value and self.value.major
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick !

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's more of an idiom to me! Though in earnest I am always split in abusing this shortcutting of "and" and "or" and their returning of a value as used here ... it can become hard to read unless used for very simple checks like this one.


@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):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down