From 6d097ca3a866d8319434365232fe4c1935c815d0 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Tue, 10 Sep 2019 10:49:13 -0400 Subject: [PATCH] Handling ^ correctly with additional tests ^ handling was not dealing with minor and patch versions properly. Closes #119 --- .travis.yml | 1 + constraints.go | 24 +++++++++++++++++------- constraints_test.go | 8 ++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index e76b439..296b83c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/constraints.go b/constraints.go index 40099db..548e843 100644 --- a/constraints.go +++ b/constraints.go @@ -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 { diff --git a/constraints_test.go b/constraints_test.go index fbf4053..b5bb562 100644 --- a/constraints_test.go +++ b/constraints_test.go @@ -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}, @@ -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 {