-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
217 lines (192 loc) · 5.32 KB
/
plugin.go
File metadata and controls
217 lines (192 loc) · 5.32 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
package testo
import (
"cmp"
"slices"
"testing"
"github.com/ozontech/testo/testoplugin"
"github.com/ozontech/testo/testoreflect"
)
// mergeSpecs multiple plugin specs into one.
func mergeSpecs(tb testing.TB, plugins ...testoplugin.Spec) testoplugin.Spec {
tb.Helper()
plans := make([]testoplugin.Plan, 0, len(plugins))
hooks := make([]testoplugin.Hooks, 0, len(plugins))
overrides := make([]testoplugin.Overrides, 0, len(plugins))
for _, p := range plugins {
plans = append(plans, p.Plan)
hooks = append(hooks, p.Hooks)
overrides = append(overrides, p.Overrides)
}
return testoplugin.Spec{
Plan: mergePlans(tb, plans...),
Hooks: mergeHooks(tb, hooks...),
Overrides: mergeOverrides(overrides...),
}
}
func mergePlans(tb testing.TB, plans ...testoplugin.Plan) testoplugin.Plan {
tb.Helper()
return testoplugin.Plan{
Prepare: func(suite testoreflect.SuiteInfo, tests *[]testoplugin.PlannedTest) {
tb.Helper()
// We could've break the loop when len(tests) == 0
// but it may be useful if some plugin would want to throw some warning or error
// when len(tests) == 0. Something like NoEmptySuitesPlugin.
for _, p := range plans {
if p.Prepare != nil {
p.Prepare(suite, tests)
}
}
},
}
}
func mergeHooks(tb testing.TB, hooks ...testoplugin.Hooks) testoplugin.Hooks {
tb.Helper()
beforeAll := make([]testoplugin.Hook, 0, len(hooks))
beforeEach := make([]testoplugin.Hook, 0, len(hooks))
beforeEachSub := make([]testoplugin.Hook, 0, len(hooks))
afterEachSub := make([]testoplugin.Hook, 0, len(hooks))
afterEach := make([]testoplugin.Hook, 0, len(hooks))
afterAll := make([]testoplugin.Hook, 0, len(hooks))
for _, h := range hooks {
if h := h.BeforeAll; h.Func != nil {
beforeAll = append(beforeAll, h)
}
if h := h.BeforeEach; h.Func != nil {
beforeEach = append(beforeEach, h)
}
if h := h.BeforeEachSub; h.Func != nil {
beforeEachSub = append(beforeEachSub, h)
}
if h := h.AfterEachSub; h.Func != nil {
afterEachSub = append(afterEachSub, h)
}
if h := h.AfterEach; h.Func != nil {
afterEach = append(afterEach, h)
}
if h := h.AfterAll; h.Func != nil {
afterAll = append(afterAll, h)
}
}
run := func(hooks []testoplugin.Hook) func() {
tb.Helper()
slices.SortStableFunc(hooks, func(a, b testoplugin.Hook) int {
return cmp.Compare(a.Priority, b.Priority)
})
return func() {
tb.Helper()
for _, h := range hooks {
runHook(tb, h)
}
}
}
return testoplugin.Hooks{
BeforeAll: testoplugin.Hook{Func: run(beforeAll)},
BeforeEach: testoplugin.Hook{Func: run(beforeEach)},
BeforeEachSub: testoplugin.Hook{Func: run(beforeEachSub)},
AfterEachSub: testoplugin.Hook{Func: run(afterEachSub)},
AfterEach: testoplugin.Hook{Func: run(afterEach)},
AfterAll: testoplugin.Hook{Func: run(afterAll)},
}
}
//nolint:funlen // splitting this into subfunctons would make it worse
func mergeOverrides(overrides ...testoplugin.Overrides) testoplugin.Overrides {
return testoplugin.Overrides{
Log: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncLog] {
return o.Log
},
),
Parallel: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncParallel] {
return o.Parallel
},
),
Setenv: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncSetenv] {
return o.Setenv
},
),
TempDir: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncTempDir] {
return o.TempDir
},
),
Deadline: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncDeadline] {
return o.Deadline
},
),
Context: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncContext] {
return o.Context
},
),
Error: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncError] {
return o.Error
},
),
Skip: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncSkip] {
return o.Skip
},
),
SkipNow: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncSkipNow] {
return o.SkipNow
},
),
Skipped: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncSkipped] {
return o.Skipped
},
),
Fail: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncFail] {
return o.Fail
},
),
FailNow: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncFailNow] {
return o.FailNow
},
),
Failed: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncFailed] {
return o.Failed
},
),
Fatal: mergeOverride(
overrides,
func(o testoplugin.Overrides) testoplugin.Override[testoplugin.FuncFatal] {
return o.Fatal
},
),
}
}
func mergeOverride[F any](
overrides []testoplugin.Overrides,
getter func(testoplugin.Overrides) testoplugin.Override[F],
) func(F) F {
return func(f F) F {
for _, o := range overrides {
if override := getter(o); override != nil {
f = override(f)
}
}
return f
}
}