Skip to content
Open
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
57 changes: 57 additions & 0 deletions src/univers/swift_version_range.py
Copy link
Member

Choose a reason for hiding this comment

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

This should not be here, it belongs in version_range.py.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import attr

from univers.version_constraint import VersionConstraint
from univers.version_range import VersionRange
from univers.versions import SwiftVersion


@attr.s(auto_attribs=True, frozen=True)
class SwiftVersionRange(VersionRange):
expression: str
scheme: str = "swift"
version_class: type = SwiftVersion
Copy link
Member

Choose a reason for hiding this comment

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

How about using SemverVersion, if that's what the Swift version is?

Suggested change
version_class: type = SwiftVersion
version_class: type = versions.SemverVersion

constraints: list = attr.ib(init=False)

def __attrs_post_init__(self):
object.__setattr__(self, "constraints", self.parse(self.expression))
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? Aren’t we already taking care of it in the base VersionRange class?


@staticmethod
def parse(expression):
Comment on lines +18 to +19
Copy link
Member

Choose a reason for hiding this comment

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

This isn’t correct, you need to implement from_native.

constraints = []
# Remove quotes for parsing.
expression = expression.replace('"', "").strip()

# Handle different range types.
if "..<" in expression:
parts = expression.split("..<")
if len(parts) == 2:
lower = SwiftVersion(parts[0].strip())
upper = SwiftVersion(parts[1].strip())
constraints.append(VersionConstraint(comparator=">=", version=lower))
constraints.append(VersionConstraint(comparator="<", version=upper))
elif "..." in expression:
parts = expression.split("...")
if len(parts) == 2:
lower = SwiftVersion(parts[0].strip())
upper = SwiftVersion(parts[1].strip())
constraints.append(VersionConstraint(comparator=">=", version=lower))
constraints.append(VersionConstraint(comparator="<=", version=upper))
else:
# Handle other cases such as 'exact:' and 'from:' prefixes.
if "exact:" in expression:
version = SwiftVersion(expression.split("exact:")[1].strip())
constraints.append(VersionConstraint(comparator="=", version=version))
elif "from:" in expression:
version = SwiftVersion(expression.split("from:")[1].strip())
next_major_version = version.next_major()
constraints.append(VersionConstraint(comparator=">=", version=version))
constraints.append(VersionConstraint(comparator="<", version=next_major_version))
else:
# Handle a single version without any prefix.
version = SwiftVersion(expression)
constraints.append(VersionConstraint(comparator="=", version=version))

return constraints

def __str__(self):
return f"vers:swift/{'|'.join([c.to_string() for c in self.constraints])}"
Comment on lines +56 to +57
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason why we need to override the base str for swift?

18 changes: 18 additions & 0 deletions src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,21 @@ def bump(self, index):
LegacyOpensslVersion,
AlpineLinuxVersion,
]


class SwiftVersion(Version):
@classmethod
def build_value(cls, string):
return semantic_version.Version(string)

@classmethod
def is_valid(cls, string):
try:
cls.build_value(string)
return True
except ValueError:
return False

def next_major(self):
"""Increase the major version and reset minor and patch."""
return SwiftVersion(str(self.value.next_major()))
38 changes: 38 additions & 0 deletions tests/test_swift_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest
Copy link
Member

Choose a reason for hiding this comment

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

Do you links to actual example of these version ranges seen in the wild?


from univers.swift_version_range import SwiftVersionRange
from univers.versions import SwiftVersion


class TestSwiftVersionRange(unittest.TestCase):
def test_exact_version(self):
vr = SwiftVersionRange('exact: "1.2.3"')
v = SwiftVersion("1.2.3")
self.assertTrue(v in vr)

def test_from_version(self):
vr = SwiftVersionRange('from: "1.2.3"')
v1 = SwiftVersion("1.2.3")
v2 = SwiftVersion("2.0.0")
self.assertTrue(v1 in vr)
self.assertFalse(v2 in vr)

def test_range_with_upper_bound(self):
vr = SwiftVersionRange('"1.2.3"..<"1.2.6"')
v1 = SwiftVersion("1.2.3")
v2 = SwiftVersion("1.2.6")
self.assertTrue(v1 in vr)
self.assertFalse(v2 in vr)

def test_range_with_both_bounds(self):
vr = SwiftVersionRange('"1.2.3"..."1.2.6"')
v1 = SwiftVersion("1.2.3")
v2 = SwiftVersion("1.2.6")
v3 = SwiftVersion("1.2.7")
self.assertTrue(v1 in vr)
self.assertTrue(v2 in vr)
self.assertFalse(v3 in vr)


if __name__ == "__main__":
unittest.main()