diff --git a/go/extractor/util/semver.go b/go/extractor/util/semver.go index 7cc43541583b..563fbc5a9e34 100644 --- a/go/extractor/util/semver.go +++ b/go/extractor/util/semver.go @@ -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:] - } - - // Add the "v" prefix that is required by the `semver` package. - if !strings.HasPrefix(version, "v") { + var numeric string + prerelease := version[rcIndex:] + + // 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 } diff --git a/go/extractor/util/semver_test.go b/go/extractor/util/semver_test.go index c721b491acf4..30b755b14452 100644 --- a/go/extractor/util/semver_test.go +++ b/go/extractor/util/semver_test.go @@ -22,39 +22,33 @@ func TestNewSemVer(t *testing.T) { {"1.22.3", "v1.22.3"}, } - // Check that we get what we expect for each of the test cases. - for _, pair := range testData { - result := NewSemVer(pair.Input) - - if result.String() != pair.Expected { - t.Errorf("Expected NewSemVer(\"%s\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected, result) - } - } + // prefixes should not affect the result + prefixes := []string{"", "go", "v"} + // suffixes + suffixes := []string{"", "rc1", "-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. + // Check that we get what we expect for each of the test cases. 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) + for _, prefix := range prefixes { + for _, suffix := range suffixes { + // c + input := prefix + pair.Input + suffix + result := NewSemVer(input) + + expected := pair.Expected + if suffix != "" { + expected += "-rc1" + } + + if result.String() != expected { + t.Errorf( + "Expected NewSemVer(\"%s\") to return \"%s\", but got \"%s\".", + input, + expected, + result, + ) + } + } } } }