-
Notifications
You must be signed in to change notification settings - Fork 1
/
maintest_test.go
159 lines (137 loc) · 3.07 KB
/
maintest_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package fixenv
import (
"errors"
"runtime"
"sync"
"testing"
)
func TestCreateMainTestEnv(t *testing.T) {
t.Run("simple", func(t *testing.T) {
e, cancel := CreateMainTestEnv(nil)
e.T().Logf("env created")
requireEquals(t, packageScopeName, e.t.Name())
requireNotNil(t, globalScopeInfo[packageScopeName])
cancel()
requireNil(t, globalScopeInfo[packageScopeName])
})
t.Run("fatal_as_panic", func(t *testing.T) {
e, cancel := CreateMainTestEnv(nil)
defer cancel()
requirePanic(t, func() {
e.T().Fatalf("asd")
})
})
t.Run("opt_fatal", func(t *testing.T) {
var fFormat string
var fArgs []interface{}
e, cancel := CreateMainTestEnv(&CreateMainTestEnvOpts{Fatalf: func(format string, args ...interface{}) {
fFormat = format
fArgs = args
}})
defer cancel()
e.T().Fatalf("asd", 1, 2, 3)
requireEquals(t, "asd", fFormat)
requireEquals(t, []interface{}{1, 2, 3}, fArgs)
})
t.Run("skip_now", func(t *testing.T) {
t.Run("default", func(t *testing.T) {
e, cancel := CreateMainTestEnv(nil)
defer cancel()
requirePanic(t, func() {
e.T().SkipNow()
})
requireTrue(t, e.T().Skipped())
})
t.Run("opt", func(t *testing.T) {
skipCalled := 0
e, cancel := CreateMainTestEnv(&CreateMainTestEnvOpts{SkipNow: func() {
skipCalled++
runtime.Goexit()
}})
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
e.T().SkipNow()
}()
wg.Wait()
requireEquals(t, 1, skipCalled)
requireTrue(t, e.T().Skipped())
})
})
}
func TestRunTests(t *testing.T) {
expectedReturnCode := 123
checkInitialized := func(t *testing.T) {
t.Helper()
globalMutex.Lock()
defer globalMutex.Unlock()
if _, ok := globalScopeInfo[packageScopeName]; !ok {
t.Fatal()
}
}
cleanGlobalState := func() {
globalMutex.Lock()
defer globalMutex.Unlock()
delete(globalScopeInfo, packageScopeName)
}
t.Run("without options", func(t *testing.T) {
m := &mTestsMock{
returnCode: expectedReturnCode,
run: func() {
checkInitialized(t)
},
}
if res := RunTests(m); res != expectedReturnCode {
t.Fatalf("%v != %v", res, expectedReturnCode)
}
cleanGlobalState()
})
t.Run("with options", func(t *testing.T) {
m := &mTestsMock{
returnCode: expectedReturnCode,
run: func() {
checkInitialized(t)
lastPackageLevelVirtualTest.SkipNow()
},
}
called := false
RunTests(m, CreateMainTestEnvOpts{SkipNow: func() {
called = true
}})
if !called {
t.Fatal()
}
cleanGlobalState()
})
t.Run("with two options", func(t *testing.T) {
defer func() {
cleanGlobalState()
rec := recover()
if !errors.Is(rec.(error), errTooManyOptionalArgs) {
t.Fatal(rec)
}
}()
m := &mTestsMock{
run: func() {
checkInitialized(t)
},
}
RunTests(m, CreateMainTestEnvOpts{}, CreateMainTestEnvOpts{})
})
}
type mTestsMock struct {
runCalled bool
returnCode int
run func()
}
func (r *mTestsMock) Run() (code int) {
r.runCalled = true
if r.run != nil {
r.run()
}
return r.returnCode
}
// check interface implementation
var _ RunTestsI = &mTestsMock{}