Skip to content

Commit 0873dd6

Browse files
authored
Merge pull request #120 from mattfarina/fix-119
Handling ^ correctly with additional tests
2 parents 7f96b0d + 6d097ca commit 0873dd6

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ language: go
55
go:
66
- 1.11.x
77
- 1.12.x
8+
- 1.13.x
89
- tip
910

1011
# Setting sudo access to false will let Travis CI use containers rather than

constraints.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,29 @@ func constraintCaret(v *Version, c *constraint) bool {
478478
return false
479479
}
480480

481+
// This less than handles prereleases
481482
if v.LessThan(c.con) {
482483
return false
483484
}
484485

485-
if (c.con.Major() > 0 || c.minorDirty) && v.Major() == c.con.Major() {
486-
return true
487-
} else if (c.con.Minor() > 0 || c.patchDirty) && v.Minor() == c.con.Minor() {
488-
return true
489-
} else if v.Patch() == c.con.Patch() {
490-
return true
486+
// ^ when the major > 0 is >=x.y.z < x+1
487+
if c.con.Major() > 0 || c.minorDirty {
488+
489+
// ^ has to be within a major range for > 0. Everything less than was
490+
// filtered out with the LessThan call above. This filters out those
491+
// that greater but not within the same major range.
492+
return v.Major() == c.con.Major()
491493
}
492494

493-
return false
495+
// ^ when the major is 0 and minor > 0 is >=0.y.z < 0.y+1
496+
// If the con Minor is > 0 it is not dirty
497+
if c.con.Minor() > 0 || c.patchDirty {
498+
return v.Minor() == c.con.Minor()
499+
}
500+
501+
// At this point the major is 0 and the minor is 0 and not dirty. The patch
502+
// is not dirty so we need to check if they are equal. If they are not equal
503+
return c.con.Patch() == v.Patch()
494504
}
495505

496506
func isX(x string) bool {

constraints_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ func TestConstraintCheck(t *testing.T) {
152152
{"~1.2.3-beta.2", "1.3.4-beta.2", false},
153153
{"^1.2.3", "1.8.9", true},
154154
{"^1.2.3", "2.8.9", false},
155+
{"^1.2.3", "1.2.1", false},
156+
{"^1.1.0", "2.1.0", false},
157+
{"^1.2.0", "2.2.1", false},
158+
{"^1.2.0", "1.2.1-alpha.1", false},
159+
{"^1.2.0-alpha.0", "1.2.1-alpha.1", true},
160+
{"^1.2.0-alpha.0", "1.2.1-alpha.0", true},
161+
{"^1.2.0-alpha.2", "1.2.0-alpha.1", false},
155162
{"^1.2", "1.8.9", true},
156163
{"^1.2", "2.8.9", false},
157164
{"^1", "1.8.9", true},
@@ -174,6 +181,7 @@ func TestConstraintCheck(t *testing.T) {
174181
// following semver.
175182
{"^0.2.3-beta.2", "0.2.4-beta.2", true},
176183
{"^0.2.3-beta.2", "0.3.4-beta.2", false},
184+
{"^0.2.3-beta.2", "0.2.3-beta.2", true},
177185
}
178186

179187
for _, tc := range tests {

0 commit comments

Comments
 (0)