Skip to content

Commit

Permalink
Go: Support all permutations of version prefixes and suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg committed May 15, 2024
1 parent 6652685 commit e0543d1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
28 changes: 23 additions & 5 deletions go/extractor/util/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
56 changes: 25 additions & 31 deletions go/extractor/util/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
}
}
}

0 comments on commit e0543d1

Please sign in to comment.