From 8f2565944ef0dc508907af2299dfd9767ccc8784 Mon Sep 17 00:00:00 2001 From: Jeff Kinard Date: Tue, 2 Nov 2021 11:11:34 -0400 Subject: [PATCH] Issue 2935 - semantic versioning fails when version is before the given range Signed-off-by: Jeff Kinard --- semanticversion/version.go | 6 +++--- semanticversion/version_test.go | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/semanticversion/version.go b/semanticversion/version.go index 1a8e00f61..8913bb350 100644 --- a/semanticversion/version.go +++ b/semanticversion/version.go @@ -195,7 +195,7 @@ func (self *Version_Expression) Is_within_range(expr string) (bool, error) { if c, err := CompareVersions(self.start, normalizedExpr); err != nil { return false, err - } else if c > 1 { + } else if c > 0 { return false, nil } @@ -207,7 +207,7 @@ func (self *Version_Expression) Is_within_range(expr string) (bool, error) { if c, err := CompareVersions(self.end, normalizedExpr); err != nil { return false, err - } else if c < 1 { + } else if c < 0 { return false, nil } @@ -330,7 +330,7 @@ func multipleVersions(expr string) bool { } // Return true if the input version string is a valid version according to the version string schema above. -// A number with leading 0's, for example 1.02.1, is not a valid version string. +// A number with leading 0's, for example 1.02.1, is a valid version string. func IsVersionString(expr string) bool { if len(expr) == 0 { diff --git a/semanticversion/version_test.go b/semanticversion/version_test.go index 981a68958..c23948523 100644 --- a/semanticversion/version_test.go +++ b/semanticversion/version_test.go @@ -111,6 +111,10 @@ func TestPositive(t *testing.T) { t.Errorf("Factory returned nil, but should not. Error: %v \n", err) } else if c, err := Version_Expression_Factory("(1.1-a-b,2]"); c == nil { t.Errorf("Factory returned nil, but should not. Error: %v \n", err) + } else if c, err := Version_Expression_Factory("(1.1-a-b,2.01.2]"); c == nil { + t.Errorf("Factory returned nil, but should not. Error: %v \n", err) + } else if c, err := Version_Expression_Factory("(1.02,2]"); c == nil { + t.Errorf("Factory returned nil, but should not. Error: %v \n", err) } } @@ -146,6 +150,8 @@ func TestRanges1(t *testing.T) { t.Errorf("Input is in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("1.1.01"); err != nil || !inrange { t.Errorf("Input is in range. Error: %v \n", err) + } else if inrange, err := c.Is_within_range("1.09.0"); err != nil || !inrange { + t.Errorf("Input is in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("2.1"); err != nil || inrange { t.Errorf("Input is NOT in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("2.01"); err != nil || inrange { @@ -188,12 +194,16 @@ func TestRanges2(t *testing.T) { t.Errorf("Input is in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("1.1.1"); err != nil || !inrange { t.Errorf("Input is in range. Error: %v \n", err) + } else if inrange, err := c.Is_within_range("1.01.90-z"); err != nil || !inrange { + t.Errorf("Input is in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("1.1.1-custom-tag"); err != nil || !inrange { t.Errorf("Input is in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("2.1"); err != nil || inrange { t.Errorf("Input is NOT in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("2.1-a"); err != nil || inrange { t.Errorf("Input is NOT in range. Error: %v \n", err) + } else if inrange, err := c.Is_within_range("0.0.1-beta"); err != nil || inrange { + t.Errorf("Input is NOT in range. Error: %v \n", err) } } @@ -235,6 +245,8 @@ func TestRanges3(t *testing.T) { t.Errorf("Input is NOT in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("2.1-tag"); err != nil || inrange { t.Errorf("Input is NOT in range. Error: %v \n", err) + } else if inrange, err := c.Is_within_range("0.0.01"); err != nil || inrange { + t.Errorf("Input is NOT in range. Error: %v \n", err) } } @@ -275,6 +287,8 @@ func TestRanges4(t *testing.T) { t.Errorf("Input is in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("2.1"); err != nil || inrange { t.Errorf("Input is NOT in range. Error: %v \n", err) + } else if inrange, err := c.Is_within_range("0.0.1"); err != nil || inrange { + t.Errorf("Input is NOT in range. Error: %v \n", err) } } @@ -292,8 +306,10 @@ func TestRanges5(t *testing.T) { t.Errorf("Input is in range. Error: %v \n", err) } else if inrange, err := c.Is_within_range("1.5.1"); err != nil || !inrange { t.Errorf("Input is in range. Error: %v \n", err) - } else if inrange, err := c.Is_within_range("1.46.71-alpha"); err != nil || !inrange { + } else if inrange, err := c.Is_within_range("1.46.71-alpha"); err != nil || !inrange { t.Errorf("Input is in range. Error: %v \n", err) + } else if inrange, err := c.Is_within_range("0.03.1"); err != nil || inrange { + t.Errorf("Input is NOT in range. Error: %v \n", err) } } @@ -351,17 +367,16 @@ func TestVersionExpressionFailure(t *testing.T) { t.Errorf("Input is NOT a version expression\n") } } - // This test tests if the version string is a valide string. func TestIsVersionString(t *testing.T) { - v_good := []string{"1.0", "1.2", "1.234.567", "3.0.0", "234", "1.2.3-abc", "1.0-abc", "1-a", "1.2.0-testing", "1-a", "1.0.0-1-a"} + v_good := []string{"1.0", "1.2", "1.234.567", "3.0.0", "234", "1.2.03", "1.02.3", "1.2.3-abc", "1.0-abc", "1-a", "1.2.0-testing", "1-a", "1.0.0-1-a"} for _, v := range v_good { if !IsVersionString(v) { t.Errorf("Version string %v is valid, however the IsVersionString function returned false.\n", v) } } - v_bad := []string{"1.0.0.1", "1.2.3a", "[1.2, 1.3]", "1.2.03", "1.2.a", "1.-a", "1.0.1-d*s"} + v_bad := []string{"1.0.0.1", "1.2.3a", "[1.2, 1.3]", "1.2.a", "1.-a", "1.0.1-d*s"} for _, v := range v_bad { if IsVersionString(v) { t.Errorf("Version string %v is invalid, however the IsVersionString function returned true.\n", v)