Skip to content

Commit 545db80

Browse files
authored
Merge pull request #1351 from minrk/fix-semver-tilde
fix julia semver tilde upper bounds
2 parents 6014649 + f466cdd commit 545db80

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

repo2docker/semver.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,32 @@ def create_semver_matcher(constraint_str):
9595
else:
9696
return VersionRange(constraint, (major(constraint) + 1,), True)
9797

98-
# '~' matching (only allowed to bump the last present number by one)
98+
# '~' matching, bumps minor version unless only major specified or leading 0.0.
99+
# https://pkgdocs.julialang.org/v1/compatibility/#Tilde-specifiers
99100
if comparison_symbol == "~":
100-
return VersionRange(
101-
constraint, constraint[:-1] + (constraint[-1] + 1,), exclusive=False
102-
)
101+
if len(constraint) == 1:
102+
# only major specified
103+
# ~x -> [x-x+1)
104+
return VersionRange(constraint, (constraint[0] + 1,), exclusive=True)
105+
elif len(constraint) == 2:
106+
# ~x.y -> [x.y-x.y+1)
107+
return VersionRange(
108+
constraint,
109+
(
110+
major(constraint),
111+
minor(constraint) + 1,
112+
),
113+
exclusive=True,
114+
)
115+
else:
116+
# 3 parts, different depending on if starts with 0.0
117+
if major(constraint) == 0 and minor(constraint) == 0:
118+
# ~0.0.3 = [0.0.3-0.0.4)
119+
upper = (0, 0, patch(constraint) + 1)
120+
else:
121+
# ~0.2.3 = [0.2.3-0.3)
122+
upper = (major(constraint), minor(constraint) + 1)
123+
return VersionRange(constraint, upper, exclusive=True)
103124

104125
# Use semver package's comparisons for everything else:
105126

tests/unit/test_semver.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ def test_major_minor_patch():
2929
("^0.0.3", "[0.0.3-0.0.4)"),
3030
("^0.0", "[0.0-0.1)"),
3131
("^0", "[0-1)"),
32-
# This one seems wrong: `~1.2.3 = [1.2.3, 1.2.4)`
33-
# but ~ is special in Julia from
34-
# https://docs.julialang.org/en/latest/stdlib/Pkg/#Tilde-specifiers-1
35-
("~1.2.3", "[1.2.3-1.2.4]"),
36-
("~1.3.5", "[1.3.5-1.3.6]"),
37-
("~1.2", "[1.2-1.3]"),
38-
("~1", "[1-2]"),
32+
# https://pkgdocs.julialang.org/v1/compatibility/#Tilde-specifiers
33+
("~1.2.3", "[1.2.3-1.3)"),
34+
("~1.3.5", "[1.3.5-1.4)"),
35+
("~1.2", "[1.2-1.3)"),
36+
("~1", "[1-2)"),
37+
("~0.0.3", "[0.0.3-0.0.4)"),
38+
("~0.0", "[0.0-0.1)"),
39+
("~0", "[0-1)"),
3940
],
4041
)
4142
def test_simple_matches(test_input, expected):
@@ -65,6 +66,9 @@ def test_range_matches(test_input, expected):
6566
("1.2.3", "1.2.3"),
6667
("~1.2.3", "1.2.4"),
6768
("~1.2.3", "1.2.3"),
69+
("~1.2", "1.2.10"),
70+
("~1", "1.99"),
71+
("~0.0.3", "0.0.3"),
6872
("1.2", "1.2.0"),
6973
("1.2", "1.9.9"),
7074
("0.2.3", "0.2.3"),
@@ -86,6 +90,9 @@ def test_match_particular_version_expected_true(test_input, expected):
8690
("1.2.3", "2.0.0"),
8791
("1.2.3", "1.2.2"),
8892
("~1.2.3", "1.3"),
93+
("~1.2", "1.3"),
94+
("~1", "2"),
95+
("~0.0.3", "0.0.4"),
8996
("1.2", "2.0.0"),
9097
("1.2", "1.1.9"),
9198
("0.2.3", "0.3.0"),

0 commit comments

Comments
 (0)