Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling ^ correctly with additional tests #120

Merged
merged 1 commit into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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