-
-
Notifications
You must be signed in to change notification settings - Fork 19
Added Swift Native Version Range Support and Associated Unit Tests #156
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about using
Suggested change
|
||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||
|
||||||||
@staticmethod | ||||||||
def parse(expression): | ||||||||
Comment on lines
+18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn’t correct, you need to implement |
||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import unittest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
There was a problem hiding this comment.
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
.