mirror of
https://github.com/github/codeql.git
synced 2026-04-26 01:05:15 +02:00
Go: Support all permutations of version prefixes and suffixes
This commit is contained in:
@@ -65,11 +65,29 @@ func NewSemVer(version string) SemVer {
|
||||
// which is compatible with the SemVer specification.
|
||||
rcIndex := strings.Index(version, "rc")
|
||||
if rcIndex != -1 {
|
||||
version = semver.Canonical("v"+version[:rcIndex]) + "-" + version[rcIndex:]
|
||||
}
|
||||
var numeric string
|
||||
prerelease := version[rcIndex:]
|
||||
|
||||
// Add the "v" prefix that is required by the `semver` package.
|
||||
if !strings.HasPrefix(version, "v") {
|
||||
// the version string may already contain a "-";
|
||||
// if it does, drop the "-" since we add it back later
|
||||
if version[rcIndex-1] != '-' {
|
||||
numeric = version[:rcIndex]
|
||||
} else {
|
||||
numeric = version[:rcIndex-1]
|
||||
}
|
||||
|
||||
// add a "v" to the numeric part of the version, if it's not already there
|
||||
if !strings.HasPrefix(numeric, "v") {
|
||||
numeric = "v" + numeric
|
||||
}
|
||||
|
||||
// for the semver library to accept a version containing a prerelease,
|
||||
// the numeric part must be canonical; e.g.. "v0-rc1" is not valid and
|
||||
// must be "v0.0.0-rc1" instead.
|
||||
version = semver.Canonical(numeric) + "-" + prerelease
|
||||
} else if !strings.HasPrefix(version, "v") {
|
||||
// Add the "v" prefix that is required by the `semver` package, if
|
||||
// it's not already there.
|
||||
version = "v" + version
|
||||
}
|
||||
|
||||
|
||||
@@ -22,39 +22,33 @@ func TestNewSemVer(t *testing.T) {
|
||||
{"1.22.3", "v1.22.3"},
|
||||
}
|
||||
|
||||
// prefixes should not affect the result
|
||||
prefixes := []string{"", "go", "v"}
|
||||
// suffixes
|
||||
suffixes := []string{"", "rc1", "-rc1"}
|
||||
|
||||
// Check that we get what we expect for each of the test cases.
|
||||
for _, pair := range testData {
|
||||
result := NewSemVer(pair.Input)
|
||||
for _, prefix := range prefixes {
|
||||
for _, suffix := range suffixes {
|
||||
// c
|
||||
input := prefix + pair.Input + suffix
|
||||
result := NewSemVer(input)
|
||||
|
||||
if result.String() != pair.Expected {
|
||||
t.Errorf("Expected NewSemVer(\"%s\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected, result)
|
||||
}
|
||||
}
|
||||
expected := pair.Expected
|
||||
if suffix != "" {
|
||||
expected += "-rc1"
|
||||
}
|
||||
|
||||
// And again, but this time prefixed with "v"
|
||||
for _, pair := range testData {
|
||||
result := NewSemVer("v" + pair.Input)
|
||||
|
||||
if result.String() != pair.Expected {
|
||||
t.Errorf("Expected NewSemVer(\"v%s\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
// And again, but this time prefixed with "go"
|
||||
for _, pair := range testData {
|
||||
result := NewSemVer("go" + pair.Input)
|
||||
|
||||
if result.String() != pair.Expected {
|
||||
t.Errorf("Expected NewSemVer(\"go%s\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
// And again, but this time with an "rc1" suffix.
|
||||
for _, pair := range testData {
|
||||
result := NewSemVer(pair.Input + "rc1")
|
||||
|
||||
if result.String() != pair.Expected+"-rc1" {
|
||||
t.Errorf("Expected NewSemVer(\"%src1\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected+"-rc1", result)
|
||||
if result.String() != expected {
|
||||
t.Errorf(
|
||||
"Expected NewSemVer(\"%s\") to return \"%s\", but got \"%s\".",
|
||||
input,
|
||||
expected,
|
||||
result,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user