Skip to content

Commit fabd7a9

Browse files
committed
Go: Better preserve original versions
1 parent e0543d1 commit fabd7a9

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

go/extractor/util/BUILD.bazel

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/util/semver.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,12 @@ func NewSemVer(version string) SemVer {
9191
version = "v" + version
9292
}
9393

94-
// Convert the remaining version string to a canonical semantic version,
95-
// and check that this was successful.
96-
canonical := semver.Canonical(version)
97-
98-
if canonical == "" {
94+
// Check that the remaining version string is valid.
95+
if !semver.IsValid(version) {
9996
log.Fatalf("%s is not a valid version string\n", version)
10097
}
10198

102-
return semVer(canonical)
99+
return semVer(version)
103100
}
104101

105102
func (ver semVer) Compare(other SemVer) int {

go/extractor/util/semver_test.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package util
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"golang.org/x/mod/semver"
7+
)
48

59
func TestNewSemVer(t *testing.T) {
610
type TestPair struct {
@@ -15,10 +19,10 @@ func TestNewSemVer(t *testing.T) {
1519
}
1620

1721
testData := []TestPair{
18-
{"0", "v0.0.0"},
19-
{"1.0", "v1.0.0"},
22+
{"0", "v0"},
23+
{"1.0", "v1.0"},
2024
{"1.0.2", "v1.0.2"},
21-
{"1.20", "v1.20.0"},
25+
{"1.20", "v1.20"},
2226
{"1.22.3", "v1.22.3"},
2327
}
2428

@@ -31,13 +35,13 @@ func TestNewSemVer(t *testing.T) {
3135
for _, pair := range testData {
3236
for _, prefix := range prefixes {
3337
for _, suffix := range suffixes {
34-
// c
38+
// combine the input string with the current prefix and suffix
3539
input := prefix + pair.Input + suffix
3640
result := NewSemVer(input)
3741

3842
expected := pair.Expected
3943
if suffix != "" {
40-
expected += "-rc1"
44+
expected = semver.Canonical(pair.Expected) + "-rc1"
4145
}
4246

4347
if result.String() != expected {

0 commit comments

Comments
 (0)