Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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