Skip to content

Commit

Permalink
goenv: parse patch version, add func Compare to compare two Go versio…
Browse files Browse the repository at this point in the history
…n strings (#4536)

goenv: parse patch version, add func Compare to compare two Go version strings
* Parse tests
* add Compare function to compare two Go version strings
* goenv, builder: parse patch version in Go version string
  • Loading branch information
ydnar authored Oct 21, 2024
1 parent 23d3a31 commit a191326
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 11 deletions.
2 changes: 1 addition & 1 deletion builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewConfig(options *compileopts.Options) (*compileopts.Config, error) {
// compiled with the latest Go version.
// This may be a bit too aggressive: if the newer version doesn't change the
// Go language we will most likely be able to compile it.
buildMajor, buildMinor, err := goenv.Parse(runtime.Version())
buildMajor, buildMinor, _, err := goenv.Parse(runtime.Version())
if err != nil {
return nil, err
}
Expand Down
51 changes: 41 additions & 10 deletions goenv/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,64 @@ func GetGorootVersion() (major, minor int, err error) {
if err != nil {
return 0, 0, err
}
return Parse(s)
major, minor, _, err = Parse(s)
return major, minor, err
}

// Parse parses the Go version (like "go1.3.2") in the parameter and return the
// major and minor version: 1 and 3 in this example. If there is an error, (0,
// 0) and an error will be returned.
func Parse(version string) (major, minor int, err error) {
// major, minor, and patch version: 1, 3, and 2 in this example.
// If there is an error, (0, 0, 0) and an error will be returned.
func Parse(version string) (major, minor, patch int, err error) {
if version == "" || version[:2] != "go" {
return 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix")
return 0, 0, 0, errors.New("could not parse Go version: version does not start with 'go' prefix")
}

parts := strings.Split(version[2:], ".")
if len(parts) < 2 {
return 0, 0, errors.New("could not parse Go version: version has less than two parts")
return 0, 0, 0, errors.New("could not parse Go version: version has less than two parts")
}

// Ignore the errors, we don't really handle errors here anyway.
var trailing string
n, err := fmt.Sscanf(version, "go%d.%d%s", &major, &minor, &trailing)
if n == 2 && err == io.EOF {
n, err := fmt.Sscanf(version, "go%d.%d.%d%s", &major, &minor, &patch, &trailing)
if n == 2 {
n, err = fmt.Sscanf(version, "go%d.%d%s", &major, &minor, &trailing)
}
if n >= 2 && err == io.EOF {
// Means there were no trailing characters (i.e., not an alpha/beta)
err = nil
}
if err != nil {
return 0, 0, fmt.Errorf("failed to parse version: %s", err)
return 0, 0, 0, fmt.Errorf("failed to parse version: %s", err)
}

return major, minor, patch, nil
}

// Compare compares two Go version strings.
// The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
// If either a or b is not a valid Go version, it is treated as "go0.0"
// and compared lexicographically.
// See [Parse] for more information.
func Compare(a, b string) int {
aMajor, aMinor, aPatch, _ := Parse(a)
bMajor, bMinor, bPatch, _ := Parse(b)
switch {
case aMajor < bMajor:
return -1
case aMajor > bMajor:
return +1
case aMinor < bMinor:
return -1
case aMinor > bMinor:
return +1
case aPatch < bPatch:
return -1
case aPatch > bPatch:
return +1
default:
return strings.Compare(a, b)
}
return
}

// GorootVersionString returns the version string as reported by the Go
Expand Down
71 changes: 71 additions & 0 deletions goenv/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package goenv

import "testing"

func TestParse(t *testing.T) {
tests := []struct {
v string
major int
minor int
patch int
wantErr bool
}{
{"", 0, 0, 0, true},
{"go", 0, 0, 0, true},
{"go1", 0, 0, 0, true},
{"go.0", 0, 0, 0, true},
{"go1.0", 1, 0, 0, false},
{"go1.1", 1, 1, 0, false},
{"go1.23", 1, 23, 0, false},
{"go1.23.5", 1, 23, 5, false},
{"go1.23.5-rc6", 1, 23, 5, false},
{"go2.0", 2, 0, 0, false},
{"go2.0.15", 2, 0, 15, false},
}
for _, tt := range tests {
t.Run(tt.v, func(t *testing.T) {
major, minor, patch, err := Parse(tt.v)
if err == nil && tt.wantErr {
t.Errorf("Parse(%q): expected err != nil", tt.v)
}
if err != nil && !tt.wantErr {
t.Errorf("Parse(%q): expected err == nil", tt.v)
}
if major != tt.major || minor != tt.minor || patch != tt.patch {
t.Errorf("Parse(%q): expected %d, %d, %d, nil; got %d, %d, %d, %v",
tt.v, tt.major, tt.minor, tt.patch, major, minor, patch, err)
}
})
}
}

func TestCompare(t *testing.T) {
tests := []struct {
a string
b string
want int
}{
{"", "", 0},
{"go0", "go0", 0},
{"go0", "go1", -1},
{"go1", "go0", 1},
{"go1", "go2", -1},
{"go2", "go1", 1},
{"go1.1", "go1.2", -1},
{"go1.2", "go1.1", 1},
{"go1.1.0", "go1.2.0", -1},
{"go1.2.0", "go1.1.0", 1},
{"go1.2.0", "go2.3.0", -1},
{"go1.23.2", "go1.23.10", -1},
{"go0.1.22", "go1.23.101", -1},
}
for _, tt := range tests {
t.Run(tt.a+" "+tt.b, func(t *testing.T) {
got := Compare(tt.a, tt.b)
if got != tt.want {
t.Errorf("Compare(%q, %q): expected %d; got %d",
tt.a, tt.b, tt.want, got)
}
})
}
}

0 comments on commit a191326

Please sign in to comment.