Skip to content

Commit

Permalink
Handling ^ correctly with additional tests
Browse files Browse the repository at this point in the history
^ handling was not dealing with minor and patch versions properly.

Closes #119
  • Loading branch information
mattfarina committed Sep 10, 2019
1 parent 7f96b0d commit 586f3dc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
28 changes: 23 additions & 5 deletions constraints.go
Expand Up @@ -478,19 +478,37 @@ func constraintCaret(v *Version, c *constraint) bool {
return false
}

// This less than handles prereleases
if v.LessThan(c.con) {
return false
}

if (c.con.Major() > 0 || c.minorDirty) && v.Major() == c.con.Major() {
return true
} else if (c.con.Minor() > 0 || c.patchDirty) && v.Minor() == c.con.Minor() {
// ^ when the major > 0 is >=x.y.z < x+1
if c.con.Major() > 0 || c.minorDirty {

// ^ has to be within a major range for > 0. Everything less than was
// filtered out with the LessThan call above. This filters out those
// that greater but not within the same major range.
if v.Major() != c.con.Major() {
return false
}

return true
} else if v.Patch() == c.con.Patch() {
}

// ^ when the major is 0 and minor > 0 is >=0.y.z < 0.y+1
// If the con Minor is > 0 it is not dirty
if c.con.Minor() > 0 || c.patchDirty {
if v.Minor() != c.con.Minor() {
return false
}

return true
}

return false
// At this point the major is 0 and the minor is 0 and not dirty. The patch
// is not dirty so we need to check if they are equal. If they are not equal
return c.con.Patch() == v.Patch()
}

func isX(x string) bool {
Expand Down
8 changes: 8 additions & 0 deletions constraints_test.go
Expand Up @@ -152,6 +152,13 @@ func TestConstraintCheck(t *testing.T) {
{"~1.2.3-beta.2", "1.3.4-beta.2", false},
{"^1.2.3", "1.8.9", true},
{"^1.2.3", "2.8.9", false},
{"^1.2.3", "1.2.1", false},
{"^1.1.0", "2.1.0", false},
{"^1.2.0", "2.2.1", false},
{"^1.2.0", "1.2.1-alpha.1", false},
{"^1.2.0-alpha.0", "1.2.1-alpha.1", true},
{"^1.2.0-alpha.0", "1.2.1-alpha.0", true},
{"^1.2.0-alpha.2", "1.2.0-alpha.1", false},
{"^1.2", "1.8.9", true},
{"^1.2", "2.8.9", false},
{"^1", "1.8.9", true},
Expand All @@ -174,6 +181,7 @@ func TestConstraintCheck(t *testing.T) {
// following semver.
{"^0.2.3-beta.2", "0.2.4-beta.2", true},
{"^0.2.3-beta.2", "0.3.4-beta.2", false},
{"^0.2.3-beta.2", "0.2.3-beta.2", true},
}

for _, tc := range tests {
Expand Down

0 comments on commit 586f3dc

Please sign in to comment.