Skip to content

Commit

Permalink
Fixed case when pre-release is in suffix (#565)
Browse files Browse the repository at this point in the history
* Fixed case when pre-release is in suffix

* moved regex to global scope

* removed not needed code

* Update error to debug

* skip constraint when empty
  • Loading branch information
rahul2393 committed Jul 22, 2020
1 parent 6eebed3 commit 0b5d936
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
26 changes: 24 additions & 2 deletions pkg/scanner/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"regexp"
"strconv"
"strings"

Expand All @@ -13,7 +14,8 @@ import (
)

var (
replacer = strings.NewReplacer(".alpha", "-alpha", ".beta", "-beta", ".rc", "-rc")
replacer = strings.NewReplacer(".alpha", "-alpha", ".beta", "-beta", ".rc", "-rc", "==", "=")
preReleaseSplitter = regexp.MustCompile(`(?P<Number>^[0-9]+)(?P<PreRelease>[a-z]*.*)`)
)

func MatchVersions(currentVersion *semver.Version, rangeVersions []string) bool {
Expand All @@ -24,9 +26,12 @@ func MatchVersions(currentVersion *semver.Version, rangeVersions []string) bool
constraintParts[j] = FormatPatchVersion(constraintParts[j])
}
v = strings.Join(constraintParts, ",")
if v == "" {
continue
}
c, err := semver.NewConstraint(v)
if err != nil {
log.Logger.Error("NewConstraint", "error", err)
log.Logger.Debug("NewConstraint", "error", err)
continue
}
// Validate a version against a constraint.
Expand Down Expand Up @@ -64,6 +69,23 @@ func FormatPatchVersion(version string) string {
if _, err := strconv.Atoi(part[2]); err == nil {
version = strings.Join(part[:3], ".") + "-" + strings.Join(part[3:], ".")
}
} else {
for i := range part {
res := preReleaseSplitter.FindStringSubmatch(part[i])
if res == nil {
continue
}
number := res[1]
preRelease := res[2]
if preRelease != "" {
if !strings.HasPrefix(preRelease, "-") {
preRelease = "-" + preRelease
}
part[i] = number + preRelease
break
}
}
version = strings.Join(part, ".")
}
return version
}
Expand Down
36 changes: 35 additions & 1 deletion pkg/scanner/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,35 @@ func TestMatchVersions(t *testing.T) {
rangeVersion: []string{`>= 1.6.7.1`},
expectedCheck: true,
},
{
name: "expect prerelease suffixed in minor version to work",
currentVersion: "4.1a",
rangeVersion: []string{`< 4.2b1`},
expectedCheck: true,
},
{
name: "expect prerelease suffixed in patch version to work",
currentVersion: "4.1.2a",
rangeVersion: []string{`< 4.2b1`},
expectedCheck: true,
},
{
name: "expect prerelease suffixed in patch version to work in failing case",
currentVersion: "4.1.2c",
rangeVersion: []string{`<= 4.1.2b`},
expectedCheck: false,
},
{
name: "expect double equal to work",
currentVersion: "1.7",
rangeVersion: []string{`==1.7`},
expectedCheck: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
v, err := semver.NewVersion(tc.currentVersion)
v, err := semver.NewVersion(FormatPatchVersion(tc.currentVersion))
require.NoError(t, err)
match := MatchVersions(v, tc.rangeVersion)
assert.Equal(t, tc.expectedCheck, match)
Expand Down Expand Up @@ -118,6 +142,16 @@ func TestFormatPatchVersio(t *testing.T) {
currentVersion: "1.2.3.4-5",
expectedVersion: "1.2.3-4-5",
},
{
name: "prerelease suffixed in minor",
currentVersion: "1.11a",
expectedVersion: "1.11-a",
},
{
name: "prerelease suffixed in patch",
currentVersion: "1.11.5rc",
expectedVersion: "1.11.5-rc",
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 0b5d936

Please sign in to comment.