Skip to content

Commit 131b99a

Browse files
committed
Support for constraint operators caret (^) and tilde (~)
1 parent d40cf49 commit 131b99a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

constraint.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func init() {
3535
">=": constraintGreaterThanEqual,
3636
"<=": constraintLessThanEqual,
3737
"~>": constraintPessimistic,
38+
"^": constraintCaret,
39+
"~": constraintTilde,
3840
}
3941

4042
ops := make([]string, 0, len(constraintOperators))
@@ -202,3 +204,31 @@ func constraintPessimistic(v, c *Version) bool {
202204
// If nothing has rejected the version by now, it's valid
203205
return true
204206
}
207+
208+
func constraintCaret(v, c *Version) bool {
209+
if !prereleaseCheck(v, c) || v.LessThan(c) {
210+
return false
211+
}
212+
213+
if v.segments[0] != c.segments[0] {
214+
return false
215+
}
216+
217+
return true
218+
}
219+
220+
func constraintTilde(v, c *Version) bool {
221+
if !prereleaseCheck(v, c) || v.LessThan(c) {
222+
return false
223+
}
224+
225+
if v.segments[0] != c.segments[0] {
226+
return false
227+
}
228+
229+
if v.segments[1] != c.segments[1] && c.si > 1 {
230+
return false
231+
}
232+
233+
return true
234+
}

constraint_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ func TestConstraintCheck(t *testing.T) {
7575
{">= 2.1.0-a", "2.1.1-beta", false},
7676
{">= 2.1.0-a", "2.1.0", true},
7777
{"<= 2.1.0-a", "2.0.0", true},
78+
{"^1.1", "1.1.1", true},
79+
{"^1.1", "1.2.3", true},
80+
{"^1.1", "2.1.0", false},
81+
{"^1.1.2", "1.1.1", false},
82+
{"^1.1.2", "1.1.2", true},
83+
{"^1.1.2", "1.1.2.3", true},
84+
{"~1", "1.3.5", true},
85+
{"~1", "2.1.0", false},
86+
{"~1.1", "1.1.1", true},
87+
{"~1.1", "1.2.3", false},
88+
{"~1.1.2", "1.1.1", false},
89+
{"~1.1.2", "1.1.2", true},
90+
{"~1.1.2", "1.1.2.3", true},
7891
}
7992

8093
for _, tc := range cases {

0 commit comments

Comments
 (0)