-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover_test.go
62 lines (49 loc) · 1.36 KB
/
discover_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package goversion_test
import (
"errors"
"go/build"
"reflect"
"runtime"
"testing"
"github.com/sg0hsmt/goversion"
)
func TestDiscover(t *testing.T) {
var first *goversion.Version
t.Run("first call (get from go command)", func(t *testing.T) {
res, err := goversion.Discover()
if err != nil {
t.Fatalf("discover is failed, %v", err)
}
runver := runtime.Version()
if res.String() != runver {
t.Errorf("unmatch version string, discover %q, runtime %q", res.String(), runver)
}
reltags := build.Default.ReleaseTags
if !reflect.DeepEqual(res.ReleaseTags(), reltags) {
t.Errorf("unmatch release tags, discover %q, runtime %q", res.ReleaseTags(), reltags)
}
first = res
})
testErr := errors.New("test for execution failure")
// replace internal function
goversion.SetExecCmd(func() ([]byte, error) {
return nil, testErr // always error
})
t.Run("second call (get from cache)", func(t *testing.T) {
res, err := goversion.Discover()
if err != nil {
t.Fatalf("discover is failed, %v", err)
}
if !first.Equal(res) {
t.Errorf("second is not equal first, first %#v, second %#v", first, res)
}
})
// clear internal cache
goversion.ResetCache()
t.Run("third call (from go command)", func(t *testing.T) {
_, err := goversion.Discover()
if err != testErr {
t.Errorf("unmatch error, want %v, got %v", testErr, err)
}
})
}