-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathodize.go
237 lines (188 loc) · 5.49 KB
/
odize.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
// Package odize lightweight wrapper over the standard testing lib that enables some additional features such as tagging and test lifecycle hooks.
package odize
import (
"fmt"
"slices"
"testing"
"github.com/code-gorilla-au/env"
)
const (
// ODIZE_TAGS is the environment variable that is used to filter tests
ODIZE_TAGS = "ODIZE_TAGS"
// ENV variable declared in pipelines such as Github Actions
ENV_CI = "CI"
)
var (
ErrTestOptionNotAllowedInCI = fmt.Errorf("test option 'Only' not allowed in CI environment")
)
// NewGroup - Create a new test group.
//
// If t he ODIZE_TAGS environment variable is set, then only tests with matching tags will be run.
func NewGroup(t *testing.T, tags *[]string) *TestGroup {
groupTags := tags
if groupTags == nil {
groupTags = &[]string{}
}
tg := &TestGroup{
t: t,
groupTags: *groupTags,
envTags: env.GetAsSlice(ODIZE_TAGS, ","),
registry: []TestRegistryEntry{},
cache: map[string]struct{}{},
isCIEnv: env.GetAsBool(ENV_CI),
}
tg.registerCleanupTasks()
return tg
}
// Test - Add a test to the group
func (tg *TestGroup) Test(name string, testFn TestFn, options ...TestFuncOpts) *TestGroup {
testOpts := TestOpts{}
for _, opt := range options {
opt(&testOpts)
}
if err := tg.registerTest(name, testFn, testOpts); err != nil {
tg.errors.Append(err)
}
return tg
}
// BeforeEach - Run before each test
func (tg *TestGroup) BeforeEach(fn func()) {
tg.beforeEach = fn
}
// BeforeAll - Run before all tests
func (tg *TestGroup) BeforeAll(fn func()) {
tg.beforeAll = fn
}
// AfterEach - Run after each test
func (tg *TestGroup) AfterEach(fn func()) {
tg.afterEach = fn
}
// AfterAll - Run after all tests
func (tg *TestGroup) AfterAll(fn func()) {
tg.afterAll = fn
}
// Run - Run all tests within a group.If the ODIZE_TAGS environment variable is set, then only tests with matching tags will be run.
//
// If errors are encountered, tests will not run.
func (tg *TestGroup) Run() error {
tg.t.Helper()
if tg.errors.Len() > 0 {
tg.complete = true
return &tg.errors
}
if shouldSkipTests(tg.groupTags, tg.envTags) {
tg.skipped = true
tg.t.Skip("Skipping test group ", tg.t.Name())
return nil
}
tg.sanitiseLifecycle()
tg.beforeAll()
entries, err := filterExecutableTests(tg.t, tg.isCIEnv, tg.registry)
if err != nil {
// Stop Run, suite is in an invalid state
tg.complete = true
return fmt.Errorf("Test group \"%s\" error: %w", tg.t.Name(), err)
}
for _, entry := range entries {
tg.beforeEach()
tg.t.Run(entry.name, entry.fn)
tg.afterEach()
}
tg.afterAll()
tg.complete = true
return nil
}
// registerTest registers a test to the group. Do not overwrite existing tests.
func (tg *TestGroup) registerTest(name string, testFn TestFn, options TestOpts) error {
if _, ok := tg.cache[name]; ok {
return fmt.Errorf("test already exists: %s", name)
}
tg.cache[name] = struct{}{}
tg.registry = append(tg.registry, TestRegistryEntry{
name: name,
fn: testFn,
options: options,
})
return nil
}
// registerCleanupTasks registers cleanup tasks to ensure that the test group is run
func (tg *TestGroup) registerCleanupTasks() {
tg.t.Helper()
tg.t.Cleanup(func() {
tg.t.Helper()
if tg.t.Skipped() {
return
}
if !tg.complete && len(tg.registry) > 0 {
tg.t.Fatalf("test group \"%s\" did not run. Make sure you use the .Run() method to execute test group", tg.t.Name())
}
})
}
// sanitiseLifecycle ensures that the lifecycle functions are not nil by adding no op funcs
func (tg *TestGroup) sanitiseLifecycle() {
if tg.beforeAll == nil {
tg.beforeAll = func() {}
}
if tg.beforeEach == nil {
tg.beforeEach = func() {}
}
if tg.afterAll == nil {
tg.afterAll = func() {}
}
if tg.afterEach == nil {
tg.afterEach = func() {}
}
}
// shouldSkipTests checks if the test group should be skipped based on environment tags
func shouldSkipTests(groupTags []string, envTags []string) bool {
if len(groupTags) == 0 && len(envTags) == 0 {
// run all tests
fmt.Println("running test")
return false
}
for _, groupTag := range groupTags {
if slices.Contains(envTags, groupTag) {
return false
}
}
return true
}
// filterExecutableTests filters tests that are executable within the test group
// Note that test option 'Only' is only used for debugging tests, and should not be used in a CI env.
func filterExecutableTests(t *testing.T, isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
filtered, err := filterOnlyAllowedTests(t, isCIEnv, tests)
if err != nil {
return filtered, err
}
if len(filtered) > 0 {
// if there are tests that are marked as only, then return 'only' those tests
return filtered, nil
}
for _, test := range tests {
if test.options.Skip {
filtered = append(filtered, TestRegistryEntry{
name: test.name,
fn: func(t *testing.T) {
t.Skip("skipping test ", test.name)
},
})
continue
}
filtered = append(filtered, test)
}
return filtered, nil
}
// filterOnlyAllowedTests filters tests that are marked as only within a test group
// If the framework detects that the test is running under a CI environment and the group has tests with 'Only', then it will return an error
func filterOnlyAllowedTests(t *testing.T, isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
filtered := []TestRegistryEntry{}
for _, test := range tests {
if test.options.Only && isCIEnv {
return filtered, ErrTestOptionNotAllowedInCI
}
if test.options.Only {
filtered = append(filtered, test)
}
}
return filtered, nil
}