From d2dd19eff9ce58bdf8a7c62dd0e00bf1b6d0ebfd Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Mon, 7 Oct 2024 13:19:35 -0700 Subject: [PATCH] goenv: Parse tests --- goenv/version.go | 13 ++++++++++++- goenv/version_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 goenv/version_test.go diff --git a/goenv/version.go b/goenv/version.go index cdfa278b..81cbf8a0 100644 --- a/goenv/version.go +++ b/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 diff --git a/goenv/version_test.go b/goenv/version_test.go new file mode 100644 index 00000000..cc500ce6 --- /dev/null +++ b/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) + } + }) + } +}