Browse Source

goenv: Parse tests

pull/4501/head
Randy Reddig 1 month ago
parent
commit
d2dd19eff9
Failed to extract signature
  1. 13
      goenv/version.go
  2. 38
      goenv/version_test.go

13
goenv/version.go

@ -60,7 +60,18 @@ func Parse(version string) (major, minor int, err error) {
if err != nil {
return 0, 0, fmt.Errorf("failed to parse version: %s", err)
}
return
return major, minor, nil
}
// WantGoVersion returns true if Go version s is >= major and minor.
// Returns false if s is not a valid Go version string. See [Parse] for more information.
func WantGoVersion(s string, major, minor int) bool {
ma, mi, err := Parse(s)
if err != nil {
return false
}
return ma >= major && mi >= minor
}
// GorootVersionString returns the version string as reported by the Go

38
goenv/version_test.go

@ -0,0 +1,38 @@
package goenv
import "testing"
func TestParse(t *testing.T) {
tests := []struct {
v string
major int
minor int
wantErr bool
}{
{"", 0, 0, true},
{"go", 0, 0, true},
{"go1", 0, 0, true},
{"go.0", 0, 0, true},
{"go1.0", 1, 0, false},
{"go1.1", 1, 1, false},
{"go1.23", 1, 23, false},
{"go1.23.5", 1, 23, false},
{"go1.23.5-rc6", 1, 23, false},
{"go2.0", 2, 0, false},
}
for _, tt := range tests {
t.Run(tt.v, func(t *testing.T) {
major, minor, 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 {
t.Errorf("Parse(%q): expected %d, %d, nil; got %d, %d, %v",
tt.v, tt.major, tt.minor, major, minor, err)
}
})
}
}
Loading…
Cancel
Save