Skip to content

Commit

Permalink
Support for constraint operators caret (^) and tilde (~)
Browse files Browse the repository at this point in the history
  • Loading branch information
beornf committed Mar 27, 2022
1 parent 45914be commit 3e070a9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
34 changes: 33 additions & 1 deletion constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func init() {
">=": {op: greaterThanEqual, f: constraintGreaterThanEqual},
"<=": {op: lessThanEqual, f: constraintLessThanEqual},
"~>": {op: pessimistic, f: constraintPessimistic},
"^": {op: caret, f: constraintCaret},
"~": {op: tilde, f: constraintTilde},
}

ops := make([]string, 0, len(constraintOperators))
Expand Down Expand Up @@ -221,7 +223,9 @@ const (
lessThan operator = '<'
greaterThanEqual operator = '≥'
lessThanEqual operator = '≤'
pessimistic operator = '~'
pessimistic operator = '≳'
caret operator = '^'
tilde operator = '~'
)

func constraintEqual(v, c *Version) bool {
Expand Down Expand Up @@ -288,3 +292,31 @@ func constraintPessimistic(v, c *Version) bool {
// If nothing has rejected the version by now, it's valid
return true
}

func constraintCaret(v, c *Version) bool {
if !prereleaseCheck(v, c) || v.LessThan(c) {
return false
}

if v.segments[0] != c.segments[0] {
return false
}

return true
}

func constraintTilde(v, c *Version) bool {
if !prereleaseCheck(v, c) || v.LessThan(c) {
return false
}

if v.segments[0] != c.segments[0] {
return false
}

if v.segments[1] != c.segments[1] && c.si > 1 {
return false
}

return true
}
13 changes: 13 additions & 0 deletions constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ func TestConstraintCheck(t *testing.T) {
{">= 2.1.0-a", "2.1.1-beta", false},
{">= 2.1.0-a", "2.1.0", true},
{"<= 2.1.0-a", "2.0.0", true},
{"^1.1", "1.1.1", true},
{"^1.1", "1.2.3", true},
{"^1.1", "2.1.0", false},
{"^1.1.2", "1.1.1", false},
{"^1.1.2", "1.1.2", true},
{"^1.1.2", "1.1.2.3", true},
{"~1", "1.3.5", true},
{"~1", "2.1.0", false},
{"~1.1", "1.1.1", true},
{"~1.1", "1.2.3", false},
{"~1.1.2", "1.1.1", false},
{"~1.1.2", "1.1.2", true},
{"~1.1.2", "1.1.2.3", true},
}

for _, tc := range cases {
Expand Down

0 comments on commit 3e070a9

Please sign in to comment.