Skip to content

Commit 3e070a9

Browse files
committed
Support for constraint operators caret (^) and tilde (~)
1 parent 45914be commit 3e070a9

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

constraint.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func init() {
4646
">=": {op: greaterThanEqual, f: constraintGreaterThanEqual},
4747
"<=": {op: lessThanEqual, f: constraintLessThanEqual},
4848
"~>": {op: pessimistic, f: constraintPessimistic},
49+
"^": {op: caret, f: constraintCaret},
50+
"~": {op: tilde, f: constraintTilde},
4951
}
5052

5153
ops := make([]string, 0, len(constraintOperators))
@@ -221,7 +223,9 @@ const (
221223
lessThan operator = '<'
222224
greaterThanEqual operator = '≥'
223225
lessThanEqual operator = '≤'
224-
pessimistic operator = '~'
226+
pessimistic operator = '≳'
227+
caret operator = '^'
228+
tilde operator = '~'
225229
)
226230

227231
func constraintEqual(v, c *Version) bool {
@@ -288,3 +292,31 @@ func constraintPessimistic(v, c *Version) bool {
288292
// If nothing has rejected the version by now, it's valid
289293
return true
290294
}
295+
296+
func constraintCaret(v, c *Version) bool {
297+
if !prereleaseCheck(v, c) || v.LessThan(c) {
298+
return false
299+
}
300+
301+
if v.segments[0] != c.segments[0] {
302+
return false
303+
}
304+
305+
return true
306+
}
307+
308+
func constraintTilde(v, c *Version) bool {
309+
if !prereleaseCheck(v, c) || v.LessThan(c) {
310+
return false
311+
}
312+
313+
if v.segments[0] != c.segments[0] {
314+
return false
315+
}
316+
317+
if v.segments[1] != c.segments[1] && c.si > 1 {
318+
return false
319+
}
320+
321+
return true
322+
}

constraint_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ func TestConstraintCheck(t *testing.T) {
7878
{">= 2.1.0-a", "2.1.1-beta", false},
7979
{">= 2.1.0-a", "2.1.0", true},
8080
{"<= 2.1.0-a", "2.0.0", true},
81+
{"^1.1", "1.1.1", true},
82+
{"^1.1", "1.2.3", true},
83+
{"^1.1", "2.1.0", false},
84+
{"^1.1.2", "1.1.1", false},
85+
{"^1.1.2", "1.1.2", true},
86+
{"^1.1.2", "1.1.2.3", true},
87+
{"~1", "1.3.5", true},
88+
{"~1", "2.1.0", false},
89+
{"~1.1", "1.1.1", true},
90+
{"~1.1", "1.2.3", false},
91+
{"~1.1.2", "1.1.1", false},
92+
{"~1.1.2", "1.1.2", true},
93+
{"~1.1.2", "1.1.2.3", true},
8194
}
8295

8396
for _, tc := range cases {

0 commit comments

Comments
 (0)