From adf96c49754d7a3ae8f1196da6d192764c29b80a Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Mon, 2 Apr 2018 11:03:25 -0500 Subject: [PATCH 1/2] Require a space around the range separator 2-3 could be interpreted as 2.0.0 - 3.0.0 OR 2.0.0-3 where 3 is the pre-release segment. In order to disambiguate between the two, require that ranges have a space around the range separator. 2-3 is interpreted as 2.0.0-3 2 - 3 is interpreted as 2.0.0 - 3.0.0 --- constraints.go | 2 +- constraints_test.go | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/constraints.go b/constraints.go index 164c011..4379a56 100644 --- a/constraints.go +++ b/constraints.go @@ -42,7 +42,7 @@ func init() { cvRegex)) constraintRangeRegex = regexp.MustCompile(fmt.Sprintf( - `\s*(%s)\s*-\s*(%s)\s*`, + `\s*(%s)\s* - \s*(%s)\s*`, cvRegex, cvRegex)) } diff --git a/constraints_test.go b/constraints_test.go index a45714d..0333075 100644 --- a/constraints_test.go +++ b/constraints_test.go @@ -251,7 +251,7 @@ func TestNewConstraint(t *testing.T) { includeMin: true, }, }, false}, - {"3-4 || => 1.0, < 2", Union( + {"3 - 4 || => 1.0, < 2", Union( rangeConstraint{ min: newV(3, 0, 0), max: newV(4, 0, 0), @@ -265,7 +265,7 @@ func TestNewConstraint(t *testing.T) { }, ), false}, // demonstrates union compression - {"3-4 || => 3.0, < 4", rangeConstraint{ + {"3 - 4 || => 3.0, < 4", rangeConstraint{ min: newV(3, 0, 0), max: newV(4, 0, 0), includeMin: true, @@ -545,9 +545,30 @@ func TestRewriteRange(t *testing.T) { c string nc string }{ - {"2-3", ">= 2, <= 3"}, - {"2-3, 2-3", ">= 2, <= 3,>= 2, <= 3"}, - {"2-3, 4.0.0-5.1", ">= 2, <= 3,>= 4.0.0, <= 5.1"}, + {"2 - 3", ">= 2, <= 3"}, + {"2 - 3, 2 - 3", ">= 2, <= 3,>= 2, <= 3"}, + {"2 - 3, 4.0.0 - 5.1", ">= 2, <= 3,>= 4.0.0, <= 5.1"}, + } + + for _, tc := range tests { + o := rewriteRange(tc.c) + + if o != tc.nc { + t.Errorf("Range %s rewritten incorrectly as '%s'", tc.c, o) + } + } +} + +func TestRewriteRange_InvalidRange(t *testing.T) { + // Ranges require a space to be recognized + + tests := []struct { + c string + nc string + }{ + {"2-3", "2-3"}, + {"2-3, 2 - 3","2-3,>= 2, <= 3"}, + {"2-3, 4.0.0 - 5.1", "2-3,>= 4.0.0, <= 5.1"}, } for _, tc := range tests { From 97599fc8ba7ef3d1f66400427079e3ae4f9df5b4 Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Mon, 2 Apr 2018 11:19:19 -0500 Subject: [PATCH 2/2] Test versions with a dash in the prerelease tag Add tests to verify that versions with a dash in the prerelease tag, e.g. 1.0.0-12-abc123 isn't converted to a range --- constraints_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/constraints_test.go b/constraints_test.go index 0333075..5cc7b5f 100644 --- a/constraints_test.go +++ b/constraints_test.go @@ -70,7 +70,7 @@ func TestParseConstraint(t *testing.T) { } if !constraintEq(tc.c, c) { - t.Errorf("Incorrect version found on %s", tc.in) + t.Errorf("%q produced constraint %q, but expected %q", tc.in, c, tc.c) } } } @@ -292,6 +292,17 @@ func TestNewConstraint(t *testing.T) { newV(1, 4, 0), }, }, false}, + {"1.1.0 - 12-abc123", rangeConstraint{ + min: newV(1,1,0), + max: Version{major: 12, minor: 0, patch: 0, pre: "abc123"}, + includeMin: true, + includeMax: true, + }, false}, + {"1.1.0-12-abc123", Version{ + major: 1, + minor: 1, + patch: 0, + pre: "12-abc123"}, false}, } for _, tc := range tests {