Skip to content

Commit e0543d1

Browse files
committed
Go: Support all permutations of version prefixes and suffixes
1 parent 6652685 commit e0543d1

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

go/extractor/util/semver.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,29 @@ func NewSemVer(version string) SemVer {
6565
// which is compatible with the SemVer specification.
6666
rcIndex := strings.Index(version, "rc")
6767
if rcIndex != -1 {
68-
version = semver.Canonical("v"+version[:rcIndex]) + "-" + version[rcIndex:]
69-
}
70-
71-
// Add the "v" prefix that is required by the `semver` package.
72-
if !strings.HasPrefix(version, "v") {
68+
var numeric string
69+
prerelease := version[rcIndex:]
70+
71+
// the version string may already contain a "-";
72+
// if it does, drop the "-" since we add it back later
73+
if version[rcIndex-1] != '-' {
74+
numeric = version[:rcIndex]
75+
} else {
76+
numeric = version[:rcIndex-1]
77+
}
78+
79+
// add a "v" to the numeric part of the version, if it's not already there
80+
if !strings.HasPrefix(numeric, "v") {
81+
numeric = "v" + numeric
82+
}
83+
84+
// for the semver library to accept a version containing a prerelease,
85+
// the numeric part must be canonical; e.g.. "v0-rc1" is not valid and
86+
// must be "v0.0.0-rc1" instead.
87+
version = semver.Canonical(numeric) + "-" + prerelease
88+
} else if !strings.HasPrefix(version, "v") {
89+
// Add the "v" prefix that is required by the `semver` package, if
90+
// it's not already there.
7391
version = "v" + version
7492
}
7593

go/extractor/util/semver_test.go

+25-31
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,33 @@ func TestNewSemVer(t *testing.T) {
2222
{"1.22.3", "v1.22.3"},
2323
}
2424

25-
// Check that we get what we expect for each of the test cases.
26-
for _, pair := range testData {
27-
result := NewSemVer(pair.Input)
28-
29-
if result.String() != pair.Expected {
30-
t.Errorf("Expected NewSemVer(\"%s\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected, result)
31-
}
32-
}
25+
// prefixes should not affect the result
26+
prefixes := []string{"", "go", "v"}
27+
// suffixes
28+
suffixes := []string{"", "rc1", "-rc1"}
3329

34-
// And again, but this time prefixed with "v"
35-
for _, pair := range testData {
36-
result := NewSemVer("v" + pair.Input)
37-
38-
if result.String() != pair.Expected {
39-
t.Errorf("Expected NewSemVer(\"v%s\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected, result)
40-
}
41-
}
42-
43-
// And again, but this time prefixed with "go"
44-
for _, pair := range testData {
45-
result := NewSemVer("go" + pair.Input)
46-
47-
if result.String() != pair.Expected {
48-
t.Errorf("Expected NewSemVer(\"go%s\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected, result)
49-
}
50-
}
51-
52-
// And again, but this time with an "rc1" suffix.
30+
// Check that we get what we expect for each of the test cases.
5331
for _, pair := range testData {
54-
result := NewSemVer(pair.Input + "rc1")
55-
56-
if result.String() != pair.Expected+"-rc1" {
57-
t.Errorf("Expected NewSemVer(\"%src1\") to return \"%s\", but got \"%s\".", pair.Input, pair.Expected+"-rc1", result)
32+
for _, prefix := range prefixes {
33+
for _, suffix := range suffixes {
34+
// c
35+
input := prefix + pair.Input + suffix
36+
result := NewSemVer(input)
37+
38+
expected := pair.Expected
39+
if suffix != "" {
40+
expected += "-rc1"
41+
}
42+
43+
if result.String() != expected {
44+
t.Errorf(
45+
"Expected NewSemVer(\"%s\") to return \"%s\", but got \"%s\".",
46+
input,
47+
expected,
48+
result,
49+
)
50+
}
51+
}
5852
}
5953
}
6054
}

0 commit comments

Comments
 (0)