Skip to content

Commit

Permalink
Merge pull request #120 from mattfarina/fix-119
Browse files Browse the repository at this point in the history
Handling ^ correctly with additional tests
  • Loading branch information
mattfarina committed Sep 10, 2019
2 parents 7f96b0d + 6d097ca commit 0873dd6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -5,6 +5,7 @@ language: go
go:
- 1.11.x
- 1.12.x
- 1.13.x
- tip

# Setting sudo access to false will let Travis CI use containers rather than
Expand Down
24 changes: 17 additions & 7 deletions constraints.go
Expand Up @@ -478,19 +478,29 @@ 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() {
return true
} else if v.Patch() == c.con.Patch() {
return true
// ^ 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.
return v.Major() == c.con.Major()
}

return false
// ^ 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 {
return v.Minor() == c.con.Minor()
}

// 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 0873dd6

Please sign in to comment.