-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrunner.go
More file actions
429 lines (358 loc) · 9.37 KB
/
runner.go
File metadata and controls
429 lines (358 loc) · 9.37 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package testo
import (
"fmt"
"path"
"reflect"
"runtime/debug"
"testing"
"github.com/ozontech/testo/internal/reflectutil"
"github.com/ozontech/testo/internal/testnamer"
"github.com/ozontech/testo/testoplugin"
"github.com/ozontech/testo/testoreflect"
)
// parallelWrapperTest is the name of tests which
// wrap multiple (possibly parallel) tests to ensure
// hooks are executed properly.
//
// It should contain some special symbol which identifiers in Go
// cannot include (like exclamation mark), so that it won't collide with suite type name.
const parallelWrapperTest = "testo!"
// Test constructs a new test ready to run as a native [testing] test.
//
// # Examples
//
// func Test(t *testing.T) {
// t.Run("My awesome test", testo.Test(func(t T) {
// // your test goes here
// }))
// }
//
// This is syntactic sugar for a more verbose [RunTest] API:
//
// func Test(t *testing.T) {
// t.Run("My awesome test", func(t *testing.T) {
// testo.RunTest(t, func(t T) {
// // your test goes here
// })
// })
// }
//
// # Options
//
// This function accepts plugin options, see [testoplugin.Option].
// Passed options are treated as test scoped, not suite scoped.
func Test[T CommonT](f func(t T), options ...testoplugin.Option) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
RunTest(t, f, options...)
}
}
// RunTest runs a single test without a suite.
//
// Under the hood it constructs a special singleton suite with one test, named
// as the parent test, and calls [RunSuite].
//
// # Examples
//
// func TestFoo(t *testing.T) {
// testo.RunTest(t, func(t T) {
// t.Log("Hi")
// })
// }
//
// In the example above plugins would see this test as a suite with a single TestFoo method.
//
// See also [Test] as a syntax sugar to run a named test:
//
// func TestFoo(t *testing.T) {
// t.Run("named-test", testo.Test(func(t T) {
// t.Log("Hi")
// }))
// }
//
// # Options
//
// This function accepts plugin options, see [testoplugin.Option].
// Passed options are treated as test scoped, not suite scoped.
//
// # Note
//
// Running this function more than once inside the same test
// means rerunning the same test, not running several different tests.
// If you want to run several suite-less tests from a single test see [Test].
//
// RunTest reports whether f succeeded.
func RunTest[T CommonT](
testingT TestingT,
f func(t T),
options ...testoplugin.Option,
) bool {
testingT.Helper()
s := singleton[T]{
test: f,
name: path.Base(testingT.Name()),
options: options,
}
return RunSuite(testingT, s)
}
// RunSuite runs tests under a suite.
//
// Test is defined as a suite method in the form of "TestXXX" or "Test"
// which accepts a single parameter of the same type as T passed to this function.
//
// It also accepts options for the plugins which can be used to configure those plugins.
// See [testoplugin.Option].
//
// RunSuite reports whether suite succeeded.
func RunSuite[Suite suite[T], T CommonT](
testingT TestingT,
suite Suite,
options ...testoplugin.Option,
) bool {
testingT.Helper()
r := newRunner[Suite](testingT)
return r.runSuite(testingT, suite, nil, options...)
}
// RunSubSuite runs a sub-suite.
//
// This is similar to [RunSuite] but designed to be called from other suites.
//
// RunSubSuite reports whether all sub-suite tests succeeded.
//
// NOTE: this function may cause infinite loop if called within the same suite as passed to it.
func RunSubSuite[Suite suite[Sub], Parent, Sub CommonT](
t Parent,
suite Suite,
options ...testoplugin.Option,
) bool {
t.Helper()
r := newRunner[Suite](t)
return r.runSuite(t.unwrap().testingT, suite, &t.unwrap().reflection.Suite, options...)
}
// Run runs f as a subtest of t called name. It runs f in a separate goroutine
// and blocks until f returns or calls t.Parallel to become a parallel test.
// Run reports whether f succeeded (or at least did not fail before calling t.Parallel).
//
// Run may be called simultaneously from multiple goroutines, but all such calls
// must return before the outer test function for t returns.
//
// WARN: Running this function during t.Cleanup panics.
func Run[T CommonT](
t T,
name string,
f func(t T),
options ...testoplugin.Option,
) bool {
t.Helper()
if f == nil {
f = func(T) {}
}
parentT := t
return parentT.unwrap().testingT.Run(name, func(testingT *testing.T) {
testingT.Helper()
t := construct(
testingT,
&parentT,
func(t *testoT) {
t.testNamer = parentT.unwrap().testNamer
t.reflection.Suite = parentT.unwrap().reflection.Suite
t.reflection.Test = testoreflect.RegularTestInfo{
Name: parentT.unwrap().testNamer.Name(parentT.unwrap().Name(), name),
RawBaseName: name,
Level: t.level(),
IsSubtest: true,
FuncPC: reflect.ValueOf(f).Pointer(),
}
},
options...,
)
defer func() {
if r := recover(); r != nil {
trace := string(debug.Stack())
t.unwrap().reflection.Panic = &testoreflect.PanicInfo{
Value: r,
Trace: trace,
}
t.Fatalf("testo: test %q panicked: %v\n\n%s", t.Name(), r, trace)
}
}()
defer runHook(t, t.unwrap().spec.Hooks.AfterEachSub)
runHook(t, t.unwrap().spec.Hooks.BeforeEachSub)
f(t)
})
}
type runner[Suite suite[T], T CommonT] struct {
caller string
suiteName string
testNamer *testnamer.Namer
}
func newRunner[Suite suite[T], T CommonT](t common) runner[Suite, T] {
suiteName := reflectutil.NameOf[Suite]()
if suiteName == reflectutil.NameOf[singleton[T]]() {
suiteName = ""
}
namer := testnamer.New()
return runner[Suite, T]{
caller: namer.Name(t.Name(), suiteName),
suiteName: suiteName,
testNamer: namer,
}
}
func (r *runner[Suite, T]) collectTests(
t TestingT,
) suiteTests[Suite, T] {
t.Helper()
collector := testsCollector[Suite, T]{
CallerName: r.caller,
TestNamer: r.testNamer,
}
return collector.Collect(t)
}
func (r *runner[Suite, T]) runSuite(
testingT TestingT,
suite Suite,
parentSuite *testoreflect.SuiteInfo,
options ...testoplugin.Option,
) bool {
testingT.Helper()
options = append(getOptions(), options...)
tests := r.collectTests(testingT)
suiteInfo := testoreflect.SuiteInfo{
Parent: parentSuite,
Name: r.suiteName,
Caller: testingT.Name(),
TestingT: testingT,
Value: suite,
}
return testingT.Run(r.suiteName, func(testingT *testing.T) {
testingT.Helper()
t := construct[T](
testingT,
nil,
func(t *testoT) {
t.testNamer = r.testNamer
t.reflection.Suite = suiteInfo
t.reflection.Test = testoreflect.RegularTestInfo{
Name: r.caller,
RawBaseName: r.suiteName,
}
},
options...,
)
t.unwrap().logPlugins()
r.runSuiteTests(t, suite, tests)
})
}
func runHook(t testing.TB, h testoplugin.Hook) {
t.Helper()
if h.Func != nil {
h.Func()
}
}
func (r *runner[Suite, T]) runSuiteTests(t T, s Suite, tests suiteTests[Suite, T]) {
t.Helper()
defer func() {
if !t.Skipped() {
runHook(t, t.unwrap().spec.Hooks.AfterAll)
}
}()
runHook(t, t.unwrap().spec.Hooks.BeforeAll)
defer func() {
if !t.Skipped() {
s.AfterAll(t)
}
}()
s.BeforeAll(t)
suiteInfo := testoreflect.SuiteInfo{
Parent: t.unwrap().reflection.Suite.Parent,
Name: t.unwrap().reflection.Suite.Name,
Caller: t.unwrap().reflection.Suite.Caller,
TestingT: t.unwrap().reflection.Suite.TestingT,
Value: s,
Hooks: t.unwrap().reflection.Suite.Hooks,
}
allTests := r.applyPlan(
t,
suiteInfo,
tests.Collect(s, func(name string) string {
return r.testNamer.Name(r.caller, name)
}),
)
t.unwrap().testingT.Run(parallelWrapperTest, func(testingT *testing.T) {
testingT.Helper()
for _, test := range allTests {
testingT.Run(test.Name, func(testingT *testing.T) {
innerT := construct(
testingT,
&t,
func(t *testoT) {
t.testNamer = r.testNamer
t.reflection.Suite = suiteInfo
t.reflection.Test = test.Info
if test.Configure != nil {
test.Configure(t)
}
},
test.Options...,
)
r.runSuiteTest(
innerT,
s,
test.suiteTest,
)
})
}
})
}
func (r *runner[Suite, T]) runSuiteTest(
t T,
s Suite,
test suiteTest[Suite, T],
) {
t.Helper()
defer func() {
if r := recover(); r != nil {
trace := string(debug.Stack())
t.unwrap().reflection.Panic = &testoreflect.PanicInfo{
Value: r,
Trace: trace,
}
t.Fatalf("testo: test %q panicked: %v\n\n%s", t.Name(), r, trace)
}
}()
defer runHook(t, t.unwrap().spec.Hooks.AfterEach)
runHook(t, t.unwrap().spec.Hooks.BeforeEach)
defer s.AfterEach(t)
s.BeforeEach(t)
test.Run(s, t)
}
func (r *runner[Suite, T]) applyPlan(
t T,
suiteInfo testoreflect.SuiteInfo,
tests []annotatedSuiteTest[Suite, T],
) []annotatedSuiteTest[Suite, T] {
t.Helper()
plannedTests := make([]testoplugin.PlannedTest, 0, len(tests))
for _, t := range tests {
plannedTests = append(plannedTests, plannedSuiteTest[Suite, T]{t})
}
if prepare := t.unwrap().spec.Plan.Prepare; prepare != nil {
prepare(suiteInfo, &plannedTests)
}
testsToReturn := make([]annotatedSuiteTest[Suite, T], 0, len(plannedTests))
for _, t := range plannedTests {
if t == nil {
continue
}
planned, ok := t.(plannedSuiteTest[Suite, T])
if !ok {
// must be unreachable because of "DoNotImplement" directive.
panic(fmt.Sprintf(
"testo: planned test is not of type %q",
reflect.TypeFor[plannedSuiteTest[Suite, T]](),
))
}
testsToReturn = append(testsToReturn, planned.inner)
}
return testsToReturn
}