Skip to content

Commit

Permalink
Updated utils to handle revision versions (x.x.x.x) not as a pre-rele…
Browse files Browse the repository at this point in the history
…ase but as a revision.

Signed-off-by: Johannes Tegnér <[email protected]>
  • Loading branch information
Johannestegner committed Oct 21, 2020
1 parent 793a1aa commit 7e6ea9d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
35 changes: 34 additions & 1 deletion pkg/scanner/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@ func MatchVersions(currentVersion *semver.Version, rangeVersions []string) bool
if valid {
return true
}

// In this case, it can either be a patch version or a revision version (c.Metadata()) or just a general error.
if currentVersion.Metadata() != "" {
part := strings.Split(v, "+")
// We create a new constraint to use in case there is a revision in the current constraint.
c2 := c
conRev := 0
if len(part) > 1 {
c2, err = semver.NewConstraint(part[0]) // Set new constraint to only version.
if err != nil {
c2 = c // Just reset and let it fail.
}
conRev, _ = strconv.Atoi(part[1])
}

curPatch := currentVersion.Patch()
curRev, _ := strconv.Atoi(currentVersion.Metadata())
// In case the revision of current is other than the one of the constraint we either + or - on the patch val.
if curRev > conRev {
curPatch++
} else if curRev < conRev {
curPatch--
}

v2, err := semver.NewVersion(fmt.Sprintf("%v.%v.%v", currentVersion.Major(), currentVersion.Minor(), curPatch))
if err == nil {
valid, _ = c2.Validate(v2)
if valid {
return true
}
}
}

for _, m := range msgs {
// re-validate after removing the patch version
if strings.HasSuffix(m.Error(), "is a prerelease version and the constraint is only looking for release versions") {
Expand Down Expand Up @@ -71,7 +104,7 @@ func FormatPatchVersion(version string) string {
part := strings.Split(version, ".")
if len(part) > 3 {
if _, err := strconv.Atoi(part[2]); err == nil {
version = strings.Join(part[:3], ".") + "-" + strings.Join(part[3:], ".")
version = strings.Join(part[:3], ".") + "+" + strings.Join(part[3:], ".")
}
} else {
for i := range part {
Expand Down
45 changes: 40 additions & 5 deletions pkg/scanner/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ func TestMatchVersions(t *testing.T) {
rangeVersion: []string{`>= 1.6.7.1`},
expectedCheck: true,
},
{
name: "expect none-revision to not care about revision number",
currentVersion: "1.2.3",
rangeVersion: []string{"<1.2.3.1"},
expectedCheck: false,
},
{
name: "expect ",
currentVersion: "1.2.3.4",
rangeVersion: []string{"<1.2.3.5", ">1.2.3.3"},
expectedCheck: true,
},
{
name: "expect revision to be higher than same minor but lower than higher minor",
currentVersion: "1.2.3.4",
rangeVersion: []string{">1.2.3", "<1.2.4"},
expectedCheck: true,
},
{
name: "expect revision to not be lower than none-revision",
currentVersion: "1.2.3.4",
rangeVersion: []string{"<1.2.3"},
},
{
name: "expect revision numbers to be tested",
currentVersion: "1.2.3.4",
rangeVersion: []string{">1.2.3.3", "<1.2.3.5"},
expectedCheck: true,
},
{
name: "expect revision to equal same revision",
currentVersion: "1.2.3.4",
rangeVersion: []string{"1.2.3.4"},
expectedCheck: true,
},
{
name: "expect prerelease suffixed in minor version to work",
currentVersion: "4.1a",
Expand Down Expand Up @@ -128,19 +163,19 @@ func TestFormatPatchVersio(t *testing.T) {
expectedVersion: "1.2.3-beta.1",
},
{
name: "patch with dots after integer patch version should append dash and join rest versions parts",
name: "patch with dots after integer patch version should append plus and join rest versions parts",
currentVersion: "1.2.3.4",
expectedVersion: "1.2.3-4",
expectedVersion: "1.2.3+4",
},
{
name: "patch with dots after integer patch version should append dash and join extra versions parts",
name: "patch with dots after integer patch version should append plus and join extra versions parts",
currentVersion: "1.2.3.4.5",
expectedVersion: "1.2.3-4.5",
expectedVersion: "1.2.3+4.5",
},
{
name: "unchanged case",
currentVersion: "1.2.3.4-5",
expectedVersion: "1.2.3-4-5",
expectedVersion: "1.2.3+4-5",
},
{
name: "prerelease suffixed in minor",
Expand Down

0 comments on commit 7e6ea9d

Please sign in to comment.