Unverified Commit 66499213 authored by Jeremy Morrell's avatar Jeremy Morrell Committed by GitHub

Work around missing library functionality (#657)

parent 97410698
......@@ -11,6 +11,7 @@ import (
"regexp"
"runtime"
"sort"
"strings"
"time"
"github.com/Masterminds/semver"
......@@ -165,7 +166,8 @@ func resolveYarn(objects []s3Object, versionRequirement string) (matchResult, er
}
func matchReleaseSemver(releases []release, versionRequirement string) (matchResult, error) {
constraints, err := semver.NewConstraint(versionRequirement)
rewrittenRequirement := rewriteRange(versionRequirement)
constraints, err := semver.NewConstraint(rewrittenRequirement)
if err != nil {
return matchResult{}, err
}
......@@ -310,3 +312,39 @@ func listS3Objects(bucketName string, prefix string) ([]s3Object, error) {
return out, nil
}
// regex matching the semver version definitions
// Ex:
// v1.0.0
// 9
// 8.x
const cvRegex string = `v?([0-9|x|X|\*]+)(\.[0-9|x|X|\*]+)?(\.[0-9|x|X|\*]+)?` +
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
// regex matching the semver operators
const ops string = `=<|~>|!=|>|<|>=|=>|<=|\^|=|~`
// Masterminds/semver does not support constraints like: `>1 <2`, preferring
// `>1, <2` with a comma separator. This catches this particular case and
// rewrites it
func rewriteRange(c string) string {
constraintRangeRegex := regexp.MustCompile(fmt.Sprintf(
`^\s*(%s)(\s*%s)\s*(%s)(\s*%s)$`,
ops, cvRegex, ops, cvRegex,
))
ors := strings.Split(c, "||")
out := make([]string, len(ors))
for i, v := range ors {
m := constraintRangeRegex.FindStringSubmatch(v)
if m != nil {
out[i] = fmt.Sprintf("%s%s, %s%s", m[1], m[2], m[12], m[13])
} else {
out[i] = v
}
}
return strings.Join(out, `||`)
}
......@@ -107,8 +107,8 @@ func TestMatchReleaseSemver(t *testing.T) {
Case{input: ">= 6.0.0", output: "11.14.0"},
Case{input: "^6.9.0 || ^8.9.0 || ^10.13.0", output: "10.15.3"},
Case{input: "6.* || 8.* || >= 10.*", output: "11.14.0"},
// TODO: these fail to parse with the library
// Case{input: ">= 6.11.1 <= 10", output: "8.16.0"},
Case{input: ">= 6.11.1 <= 10", output: "10.15.3"},
// TODO: Masterminds/semver interprets this as `< 11.x`
// Case{input: ">=8.10 <11", output: "10.15.3"},
}
......@@ -166,8 +166,7 @@ func TestResolveYarn(t *testing.T) {
Case{input: "1.*.*", output: "1.15.2"},
Case{input: "^v1.0.1", output: "1.15.2"},
Case{input: "1.13 - 1.16", output: "1.15.2"},
// TODO: these fail to parse with the library
// Case{input: ">=1.9.4 <2.0.0", output: "1.15.2"},
Case{input: ">=1.9.4 <2.0.0", output: "1.15.2"},
}
for _, c := range cases {
......@@ -232,23 +231,25 @@ func TestResolveNode(t *testing.T) {
Case{input: ">= 6.0.0", output: "11.14.0"},
Case{input: "^6.9.0 || ^8.9.0 || ^10.13.0", output: "10.15.3"},
Case{input: "6.* || 8.* || >= 10.*", output: "11.14.0"},
// TODO: these fail to parse with the library
// Case{input: ">= 6.11.1 <= 10", output: "8.16.0"},
Case{input: ">= 6.11.1 <= 10", output: "10.15.3"},
// TODO: Masterminds/semver interprets this as `< 11.x`
// Case{input: ">=8.10 <11", output: "10.15.3"},
}
for _, c := range cases {
result, err := resolveNode(objects, "linux-x64", c.input)
assert.Nil(t, err)
assert.True(t, result.matched)
assert.Equal(t, result.release.version.String(), c.output)
if assert.Nil(t, err) {
assert.True(t, result.matched)
assert.Equal(t, result.release.version.String(), c.output)
}
}
for _, c := range cases {
result, err := resolveNode(objects, "darwin-x64", c.input)
assert.Nil(t, err)
assert.False(t, result.matched)
assert.Equal(t, result.versionRequirement, c.input)
if assert.Nil(t, err) {
assert.False(t, result.matched)
assert.Equal(t, result.versionRequirement, c.input)
}
}
}
......@@ -305,3 +306,26 @@ func TestResolveNodeStaging(t *testing.T) {
}
}
}
func TestRewriteRange(t *testing.T) {
cases := []Case{
Case{input: "10.x", output: "10.x"},
Case{input: "10.*", output: "10.*"},
Case{input: "10", output: "10"},
Case{input: "8.x", output: "8.x"},
Case{input: "^8.11.3", output: "^8.11.3"},
Case{input: "~8.11.3", output: "~8.11.3"},
Case{input: ">= 6.0.0", output: ">= 6.0.0"},
Case{input: "^6.9.0 || ^8.9.0 || ^10.13.0", output: "^6.9.0 || ^8.9.0 || ^10.13.0"},
Case{input: "6.* || 8.* || >= 10.*", output: "6.* || 8.* || >= 10.*"},
Case{input: ">= 6.11.1 <= 10", output: ">= 6.11.1, <= 10"},
Case{input: ">=8.10 <11", output: ">=8.10, <11"},
Case{input: ">1<2", output: ">1, <2"},
Case{input: ">1<", output: ">1<"},
}
for _, c := range cases {
out := rewriteRange(c.input)
assert.Equal(t, c.output, out)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment