Skip to content

Commit

Permalink
Go: Better preserve original versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg committed May 15, 2024
1 parent e0543d1 commit aac172a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 3 additions & 6 deletions go/extractor/util/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,12 @@ func NewSemVer(version string) SemVer {
version = "v" + version
}

// Convert the remaining version string to a canonical semantic version,
// and check that this was successful.
canonical := semver.Canonical(version)

if canonical == "" {
// Check that the remaining version string is valid.
if !semver.IsValid(version) {
log.Fatalf("%s is not a valid version string\n", version)
}

return semVer(canonical)
return semVer(version)
}

func (ver semVer) Compare(other SemVer) int {
Expand Down
16 changes: 10 additions & 6 deletions go/extractor/util/semver_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package util

import "testing"
import (
"testing"

"golang.org/x/mod/semver"
)

func TestNewSemVer(t *testing.T) {
type TestPair struct {
Expand All @@ -15,10 +19,10 @@ func TestNewSemVer(t *testing.T) {
}

testData := []TestPair{
{"0", "v0.0.0"},
{"1.0", "v1.0.0"},
{"0", "v0"},
{"1.0", "v1.0"},
{"1.0.2", "v1.0.2"},
{"1.20", "v1.20.0"},
{"1.20", "v1.20"},
{"1.22.3", "v1.22.3"},
}

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

expected := pair.Expected
if suffix != "" {
expected += "-rc1"
expected = semver.Canonical(pair.Expected) + "-rc1"
}

if result.String() != expected {
Expand Down

0 comments on commit aac172a

Please sign in to comment.