-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_tb.go
249 lines (196 loc) · 4.54 KB
/
fake_tb.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package faket
import (
"context"
"fmt"
"runtime"
"strings"
"sync"
"testing"
)
var _ testing.TB = (*fakeTB)(nil)
type fakeTB struct {
// embedded to include testing.TB.private, which we can't implement.
// Since this is an interface, unimplemented methods will panic.
testing.TB
mu sync.Mutex // protects all of the below fields.
cleanups []func()
Logs []logEntry
completed chan struct{}
failed bool
skipped bool
panicked bool
// panic metadata
recovered any
recoverCallers []uintptr
}
type logEntry struct {
callers []uintptr // callers[0] is the tb function that logged
entry string
}
// RunTest runs the given test using a fake [testing.TB] and returns
// the result of running the test.
func RunTest(testFn func(t testing.TB)) TestResult {
tb := newRecordingTB()
go func() {
defer tb.checkPanic()
defer tb.postTest()
testFn(tb)
}()
tb.waitForCompleted(context.Background())
return TestResult{tb}
}
func newRecordingTB() *fakeTB {
return &fakeTB{
completed: make(chan struct{}),
}
}
func (tb *fakeTB) checkPanic() {
if r := recover(); r != nil {
tb.panicked = true
tb.recovered = r
tb.recoverCallers = getCallers()
}
}
func (tb *fakeTB) postTest() {
defer close(tb.completed)
for _, f := range tb.cleanups {
defer f()
}
// TODO(prashant): Handle nested Cleanups
}
func (tb *fakeTB) done() bool {
select {
case <-tb.completed:
return true
default:
return false
}
}
func (tb *fakeTB) waitForCompleted(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-tb.completed:
return nil
}
}
func (tb *fakeTB) Cleanup(f func()) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.cleanups = append(tb.cleanups, f)
}
func (tb *fakeTB) logfLocked(callers []uintptr, format string, args ...interface{}) {
formatted := fmt.Sprintf(format, args...)
tb.Logs = append(tb.Logs, logEntry{
callers: callers,
entry: formatted,
})
}
func (tb *fakeTB) logLocked(callers []uintptr, args ...interface{}) {
// Log args are formatted using Sprintln in the testing package
// but we drop the trailing newline as we store an array of lines.
formatted := fmt.Sprintln(args...)
formatted = strings.TrimSuffix(formatted, "\n")
tb.Logs = append(tb.Logs, logEntry{
callers: callers,
entry: formatted,
})
}
func (tb *fakeTB) Error(args ...interface{}) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.logLocked(getCallers(), args...)
tb.failLocked()
}
func (tb *fakeTB) Errorf(format string, args ...interface{}) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.logfLocked(getCallers(), format, args...)
tb.failLocked()
}
func (tb *fakeTB) Fail() {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.failLocked()
}
func (tb *fakeTB) failLocked() {
if tb.done() {
panic("Fail in goroutine after test completed")
}
tb.failed = true
}
func (tb *fakeTB) FailNow() {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.failNowLocked()
}
func (tb *fakeTB) failNowLocked() {
tb.failLocked()
runtime.Goexit()
}
func (tb *fakeTB) Failed() bool {
tb.mu.Lock()
defer tb.mu.Unlock()
return tb.failed || tb.panicked
}
func (tb *fakeTB) Fatal(args ...interface{}) {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.logLocked(getCallers(), args...)
tb.failNowLocked()
}
func (tb *fakeTB) Fatalf(format string, args ...interface{}) {
tb.logfLocked(getCallers(), format, args...)
tb.FailNow()
}
func (tb *fakeTB) Helper() {
// TODO(prashant): Implement Helper, this should result in the helper function frame being skipped
// in any caller file:name resolution.
}
func (tb *fakeTB) Log(args ...interface{}) {
tb.logLocked(getCallers(), args...)
}
func (tb *fakeTB) Logf(format string, args ...interface{}) {
tb.logfLocked(getCallers(), format, args...)
}
func (tb *fakeTB) Name() string {
return "faket-no-name"
}
func (tb *fakeTB) Setenv(key, value string) {
// Set the environment, but clear it on cleanup
}
func (tb *fakeTB) Skip(args ...interface{}) {
tb.logLocked(getCallers(), args...)
tb.skipNowLocked()
}
func (tb *fakeTB) Skipf(format string, args ...interface{}) {
tb.logfLocked(getCallers(), format, args...)
tb.SkipNow()
}
func (tb *fakeTB) SkipNow() {
tb.mu.Lock()
defer tb.mu.Unlock()
tb.skipNowLocked()
}
func (tb *fakeTB) skipNowLocked() {
tb.skipped = true
runtime.Goexit()
}
func (tb *fakeTB) Skipped() bool {
return tb.skipped
}
func (tb *fakeTB) TempDir() string {
return "tmp"
}
func getCallers() []uintptr {
depth := 32
for {
pc := make([]uintptr, depth)
// runtime.Callers returns itself, so skip that, and this function.
n := runtime.Callers(2, pc)
if n < len(pc) {
return pc[:n]
}
depth *= 2
}
}