Skip to content

Commit

Permalink
Merge pull request #2936 from Polber/jkinard/2935
Browse files Browse the repository at this point in the history
Issue 2935 - semantic versioning fails when version is before the given range
  • Loading branch information
linggao authored Nov 2, 2021
2 parents 811a1fe + 8f25659 commit 8db4644
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions semanticversion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 19 additions & 4 deletions semanticversion/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

}
Expand Down Expand Up @@ -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)
}

}
Expand Down Expand Up @@ -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)
}

}
Expand All @@ -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)
}

}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8db4644

Please sign in to comment.