-
Notifications
You must be signed in to change notification settings - Fork 3
/
suite.go
183 lines (156 loc) · 5.38 KB
/
suite.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
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package suitetest
import (
"fmt"
"github.com/cisco-open/go-lanai/pkg/utils/order"
"github.com/cisco-open/go-lanai/test"
"os"
"sort"
"testing"
)
const (
HookOrderPackage int = - 0xffff
)
type PackageHook interface {
Setup() error
Teardown() error
}
type PackageOptions func(opt *pkg)
type pkg struct {
PackageHooks []PackageHook
TestOptions []test.Options
}
func RunTests(m *testing.M, opts ...PackageOptions) {
s := pkg{
PackageHooks: []PackageHook{},
TestOptions: []test.Options{},
}
for _, fn := range opts {
fn(&s)
}
sort.SliceStable(s.PackageHooks, func(i, j int) bool {
return order.OrderedFirstCompare(s.PackageHooks[i], s.PackageHooks[j])
})
// run setup TestHooks
for _, h := range s.PackageHooks {
if e := h.Setup(); e != nil {
panic(fmt.Errorf("error when setup test pkg: %v", e))
}
}
// register DefaultTestHook
test.InternalOptions = s.TestOptions
// run tests
code := m.Run()
// run teardown TestHooks in reversed order
for i := len(s.PackageHooks) - 1; i >= 0; i-- {
if e := s.PackageHooks[i].Teardown(); e != nil {
panic(fmt.Errorf("error when teardown test pkg: %v", e))
}
}
os.Exit(code)
}
/****************************
Suite Options
****************************/
// SetupFunc is package level setup function that run once per package
type SetupFunc func() error
// TeardownFunc is package level teardown function that run once per package
type TeardownFunc func() error
// orderedSuiteHook implements PackageHook and order.Ordered
type orderedSuiteHook struct {
order int
setupFunc SetupFunc
teardownFunc TeardownFunc
}
func (h *orderedSuiteHook) Order() int {
return h.order
}
func (h *orderedSuiteHook) Setup() error {
if h.setupFunc == nil {
return nil
}
return h.setupFunc()
}
func (h *orderedSuiteHook) Teardown() error {
if h.teardownFunc == nil {
return nil
}
return h.teardownFunc()
}
// WithOptions group multiple PackageOptions into one, typically used for other test utilities to provide
// single entry point of certain feature.
// Not recommended for test implementers to use directly
func WithOptions(opts ...PackageOptions) PackageOptions {
return func(opt *pkg) {
for _, fn := range opts {
fn(opt)
}
}
}
// Setup register the given setup function to run at order 0, higher(smaller) order runs first
// package setup runs once per test package, and should only be registered in TestMain(m *testing.M)
func Setup(fn SetupFunc) PackageOptions {
return SetupWithOrder(0, fn)
}
// SetupWithOrder register the given setup function to run at given order, higher(smaller) order runs first
// package setup runs once per test package, and should only be registered in TestMain(m *testing.M)
func SetupWithOrder(order int, fn SetupFunc) PackageOptions {
return func(opt *pkg) {
opt.PackageHooks = append(opt.PackageHooks, &orderedSuiteHook{
order: order,
setupFunc: fn,
})
}
}
// Teardown register the given teardown function to run at order 0, higher(smaller) order runs LAST
// package teardown runs once per test package, and should only be registered in TestMain(m *testing.M)
func Teardown(fn TeardownFunc) PackageOptions {
return TeardownWithOrder(0, fn)
}
// TeardownWithOrder register the given teardown function to run at given order, higher(smaller) order runs LAST
// package teardown runs once per test package, and should only be registered in TestMain(m *testing.M)
func TeardownWithOrder(order int, fn TeardownFunc) PackageOptions {
return func(opt *pkg) {
opt.PackageHooks = append(opt.PackageHooks, &orderedSuiteHook{
order: order,
teardownFunc: fn,
})
}
}
// TestOptions register per-test options at package level: only declared once in TestMain(m *testing.M)
// All test.Options are applied once per Test*()
func TestOptions(opts ...test.Options) PackageOptions {
return func(opt *pkg) {
opt.TestOptions = append(opt.TestOptions, opts...)
}
}
// TestSetup is a convenient function equivalent to TestOptions(test.Setup(fn))
func TestSetup(fn test.SetupFunc) PackageOptions {
return TestSetupWithOrder(HookOrderPackage, fn)
}
// TestSetupWithOrder is a convenient function equivalent to TestOptions(test.Hooks(test.NewSetupHook(order, fn)))
func TestSetupWithOrder(order int, fn test.SetupFunc) PackageOptions {
return TestOptions(test.Hooks(test.NewSetupHook(order, fn)))
}
// TestTeardown is a convenient function equivalent to TestOptions(test.Teardown(fn))
func TestTeardown(fn test.TeardownFunc) PackageOptions {
return TestTeardownWithOrder(HookOrderPackage, fn)
}
// TestTeardownWithOrder is a convenient function equivalent to TestOptions(test.Hooks(test.NewTeardownHook(order, fn)))
func TestTeardownWithOrder(order int, fn test.TeardownFunc) PackageOptions {
return TestOptions(test.Hooks(test.NewTeardownHook(order, fn)))
}