-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
runner.go
385 lines (357 loc) · 9.23 KB
/
runner.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
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
package scenarigo
import (
gocontext "context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"github.com/fatih/color"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/errors"
"github.com/zoncoen/scenarigo/internal/filepathutil"
"github.com/zoncoen/scenarigo/plugin"
"github.com/zoncoen/scenarigo/protocol/grpc"
"github.com/zoncoen/scenarigo/protocol/http"
"github.com/zoncoen/scenarigo/reporter"
"github.com/zoncoen/scenarigo/schema"
)
func init() {
http.Register()
grpc.Register()
}
// Runner represents a test runner.
type Runner struct {
vars map[string]any
secrets map[string]any
pluginDir *string
plugins schema.OrderedMap[string, schema.PluginConfig]
scenarioFiles []string
scenarioReaders []io.Reader
enabledColor bool
rootDir string
inputConfig schema.InputConfig
reportConfig schema.ReportConfig
configNode ast.Node
}
// NewRunner returns a new test runner.
func NewRunner(opts ...func(*Runner) error) (*Runner, error) {
r := &Runner{} //nolint:exhaustruct
r.enabledColor = !color.NoColor
for _, opt := range opts {
if err := opt(r); err != nil {
return nil, err
}
}
if r.rootDir == "" {
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get working directory: %w", err)
}
r.rootDir = wd
}
return r, nil
}
// WithConfig returns a option which sets configuration.
func WithConfig(config *schema.Config) func(*Runner) error {
return func(r *Runner) error {
if config == nil {
return nil
}
r.vars = config.Vars
r.secrets = config.Secrets
r.rootDir = config.Root
scenarios := make([]string, len(config.Scenarios))
for i, s := range config.Scenarios {
scenarios[i] = filepath.Join(r.rootDir, s)
}
var opts []func(r *Runner) error
opts = append(opts, WithScenarios(scenarios...))
if config.PluginDirectory != "" {
opts = append(opts, WithPluginDir(filepath.Join(r.rootDir, config.PluginDirectory)))
}
for _, opt := range opts {
if err := opt(r); err != nil {
return err
}
}
r.plugins = config.Plugins
if config.Output.Colored != nil {
r.enabledColor = *config.Output.Colored
}
r.inputConfig = config.Input
r.reportConfig = config.Output.Report
r.configNode = config.Node
return nil
}
}
// WithScenarios returns a option which finds and sets test scenario files.
func WithScenarios(paths ...string) func(*Runner) error {
return func(r *Runner) error {
for i, path := range paths {
abs, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to find test scenarios: %w", err)
}
paths[i] = abs
}
files, err := getAllFiles(paths...)
if err != nil {
return fmt.Errorf("failed to find test scenarios: %w", err)
}
r.scenarioFiles = files
return nil
}
}
// WithPluginDir returns a option which sets plugin root directory.
func WithPluginDir(path string) func(*Runner) error {
return func(r *Runner) error {
abs, err := filepath.Abs(path)
if err != nil {
return errors.Wrapf(err, `failed to set plugin directory "%s"`, path)
}
r.pluginDir = &abs
return nil
}
}
// WithScenariosFromReader returns a option which sets readers to read scenario contents.
func WithScenariosFromReader(readers ...io.Reader) func(*Runner) error {
return func(r *Runner) error {
r.scenarioReaders = readers
return nil
}
}
// WithOptionsFromEnv returns a option which sets flag whether accepts configuration from ENV.
// Currently Available ENV variables are the following.
// - SCENARIGO_COLOR=(1|true|TRUE)
func WithOptionsFromEnv(isEnv bool) func(*Runner) error {
return func(r *Runner) error {
if isEnv {
r.setOptionsFromEnv()
}
return nil
}
}
var yamlPattern = regexp.MustCompile(`(?i)\.ya?ml$`)
func looksLikeYAML(path string) bool {
return yamlPattern.MatchString(path)
}
func getAllFiles(paths ...string) ([]string, error) {
files := []string{}
for _, path := range paths {
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if looksLikeYAML(path) {
files = append(files, path)
}
return nil
}); err != nil {
return nil, err
}
}
return files, nil
}
const (
envScenarigoColor = "SCENARIGO_COLOR"
)
func (r *Runner) setOptionsFromEnv() {
r.setEnabledColor(os.Getenv(envScenarigoColor))
}
func (r *Runner) setEnabledColor(envColor string) {
if envColor == "" {
return
}
result, _ := strconv.ParseBool(envColor)
r.enabledColor = result
}
// ScenarioFiles returns all scenario file paths.
func (r *Runner) ScenarioFiles() []string {
return r.scenarioFiles
}
// Run runs all tests.
func (r *Runner) Run(ctx *context.Context) {
// setup context
if r.vars != nil {
vars, err := ctx.ExecuteTemplate(r.vars)
if err != nil {
ctx.Reporter().Fatalf(
"invalid vars: %s",
errors.WithNodeAndColored(
errors.WithPath(err, "vars"),
r.configNode,
r.enabledColor,
),
)
}
ctx = ctx.WithVars(vars)
}
if r.secrets != nil {
secrets, err := ctx.ExecuteTemplate(r.secrets)
if err != nil {
ctx.Reporter().Fatalf(
"invalid secrets: %s",
errors.WithNodeAndColored(
errors.WithPath(err, "secrets"),
r.configNode,
r.enabledColor,
),
)
}
ctx = ctx.WithSecrets(secrets)
}
if r.pluginDir != nil {
ctx = ctx.WithPluginDir(*r.pluginDir)
}
ctx = ctx.WithEnabledColor(r.enabledColor)
// open plugins
pluginDir := r.rootDir
if dir := ctx.PluginDir(); dir != "" {
pluginDir = dir
}
var setups setupFuncList
for _, item := range r.plugins.ToSlice() {
p, err := plugin.Open(filepath.Join(pluginDir, item.Key))
if err != nil {
setups = append(setups, setupFunc{
name: item.Key,
f: func(ctx *context.Context) (*context.Context, func(*context.Context)) {
ctx.Reporter().Fatalf("failed to open plugin: %s", err)
return nil, nil
},
})
continue
}
if setup := p.GetSetup(); setup != nil {
setups = append(setups, setupFunc{
name: item.Key,
f: setup,
})
}
}
ctx, teardown := setups.setup(ctx)
if ctx.Reporter().Failed() {
teardown(ctx)
return
}
opts := []schema.LoadOption{
schema.WithInputConfig(r.rootDir, r.inputConfig),
}
FILE_LOOP:
for _, f := range r.scenarioFiles {
testName, err := filepath.Rel(r.rootDir, f)
if err != nil {
testName = f
}
for _, exclude := range r.inputConfig.Excludes {
if exclude.MatchString(testName) {
continue FILE_LOOP
}
}
ctx.Run(testName, func(ctx *context.Context) {
scns, err := schema.LoadScenarios(f, opts...)
if err != nil {
ctx.Reporter().Fatalf("failed to load scenarios: %s", err)
}
for _, scn := range scns {
scn := scn
ctx = ctx.WithNode(scn.Node)
ctx.Run(scn.Title, func(ctx *context.Context) {
ctx.Reporter().Parallel()
_ = RunScenario(ctx, scn)
})
}
})
}
for i, reader := range r.scenarioReaders {
ctx.Run(fmt.Sprint(i), func(ctx *context.Context) {
scns, err := schema.LoadScenariosFromReader(reader)
if err != nil {
ctx.Reporter().Fatalf("failed to load scenarios: %s", err)
}
for _, scn := range scns {
scn := scn
ctx = ctx.WithNode(scn.Node)
ctx.Run(scn.Title, func(ctx *context.Context) {
ctx.Reporter().Parallel()
_ = RunScenario(ctx, scn)
})
}
})
}
teardown(ctx)
}
// CreateTestReport creates test reports.
func (r *Runner) CreateTestReport(rptr reporter.Reporter) error {
if r.reportConfig.JSON.Filename == "" && r.reportConfig.JUnit.Filename == "" {
return nil
}
report, err := reporter.GenerateTestReport(rptr)
if err != nil {
return fmt.Errorf("failed to generate test report: %w", err)
}
if r.reportConfig.JSON.Filename != "" {
f, err := os.Create(filepathutil.From(r.rootDir, r.reportConfig.JSON.Filename))
if err != nil {
return fmt.Errorf("failed to write JSON test report: %w", err)
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(report); err != nil {
return fmt.Errorf("failed to write JSON test report: %w", err)
}
}
if r.reportConfig.JUnit.Filename != "" {
f, err := os.Create(filepathutil.From(r.rootDir, r.reportConfig.JUnit.Filename))
if err != nil {
return fmt.Errorf("failed to write JUnit test report: %w", err)
}
defer f.Close()
enc := xml.NewEncoder(f)
enc.Indent("", " ")
if err := enc.Encode(report); err != nil {
return fmt.Errorf("failed to write JUnit test report: %w", err)
}
}
return nil
}
// Dump dumps all test scenarios.
func (r *Runner) Dump(ctx gocontext.Context, w io.Writer) error {
enc := yaml.NewEncoder(w)
defer enc.Close()
opts := []schema.LoadOption{
schema.WithInputConfig(r.rootDir, r.inputConfig),
}
FILE_LOOP:
for _, f := range r.scenarioFiles {
testName, err := filepath.Rel(r.rootDir, f)
if err != nil {
testName = f
}
for _, exclude := range r.inputConfig.Excludes {
if exclude.MatchString(testName) {
continue FILE_LOOP
}
}
scns, err := schema.LoadScenarios(f, opts...)
if err != nil {
return fmt.Errorf("failed to load scenarios: %w", err)
}
for _, scn := range scns {
if err := enc.EncodeContext(ctx, scn); err != nil {
return fmt.Errorf("failed to encode scenarios: %w", err)
}
}
}
return nil
}