forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
460 lines (373 loc) · 10.6 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package godog
import (
"context"
"fmt"
"reflect"
"strings"
"testing"
"github.com/cucumber/messages-go/v16"
"github.com/cucumber/godog/formatters"
"github.com/cucumber/godog/internal/models"
"github.com/cucumber/godog/internal/storage"
"github.com/cucumber/godog/internal/utils"
)
var (
errorInterface = reflect.TypeOf((*error)(nil)).Elem()
contextInterface = reflect.TypeOf((*context.Context)(nil)).Elem()
)
// ErrUndefined is returned in case if step definition was not found
var ErrUndefined = fmt.Errorf("step is undefined")
// ErrPending should be returned by step definition if
// step implementation is pending
var ErrPending = fmt.Errorf("step implementation is pending")
// StepResultStatus describes step result.
type StepResultStatus = models.StepResultStatus
const (
// StepPassed indicates step that passed.
StepPassed StepResultStatus = models.Passed
// StepFailed indicates step that failed.
StepFailed = models.Failed
// StepSkipped indicates step that was skipped.
StepSkipped = models.Skipped
// StepUndefined indicates undefined step.
StepUndefined = models.Undefined
// StepPending indicates step with pending implementation.
StepPending = models.Pending
)
type suite struct {
steps []*models.StepDefinition
fmt Formatter
storage *storage.Storage
failed bool
randomSeed int64
stopOnFailure bool
strict bool
defaultContext context.Context
testingT *testing.T
// suite event handlers
beforeScenarioHandlers []BeforeScenarioHook
beforeStepHandlers []BeforeStepHook
afterStepHandlers []AfterStepHook
afterScenarioHandlers []AfterScenarioHook
}
func (s *suite) matchStep(step *messages.PickleStep) *models.StepDefinition {
def := s.matchStepText(step.Text)
if def != nil && step.Argument != nil {
def.Args = append(def.Args, step.Argument)
}
return def
}
func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, prevStepErr error, isFirst, isLast bool) (rctx context.Context, err error) {
var (
match *models.StepDefinition
sr = models.PickleStepResult{Status: models.Undefined}
)
rctx = ctx
// user multistep definitions may panic
defer func() {
if e := recover(); e != nil {
err = &traceError{
msg: fmt.Sprintf("%v", e),
stack: callStack(),
}
}
earlyReturn := prevStepErr != nil || err == ErrUndefined
if !earlyReturn {
sr = models.NewStepResult(pickle.Id, step.Id, match)
}
// Run after step handlers.
rctx, err = s.runAfterStepHooks(ctx, step, sr.Status, err)
// Trigger after scenario on failing or last step to attach possible hook error to step.
if sr.Status != StepSkipped && ((err == nil && isLast) || err != nil) {
rctx, err = s.runAfterScenarioHooks(rctx, pickle, err)
}
if earlyReturn {
return
}
switch err {
case nil:
sr.Status = models.Passed
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Passed(pickle, step, match.GetInternalStepDefinition())
case ErrPending:
sr.Status = models.Pending
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Pending(pickle, step, match.GetInternalStepDefinition())
default:
sr.Status = models.Failed
sr.Err = err
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Failed(pickle, step, match.GetInternalStepDefinition(), err)
}
}()
// run before scenario handlers
if isFirst {
ctx, err = s.runBeforeScenarioHooks(ctx, pickle)
}
// run before step handlers
ctx, err = s.runBeforeStepHooks(ctx, step, err)
match = s.matchStep(step)
s.storage.MustInsertStepDefintionMatch(step.AstNodeIds[0], match)
s.fmt.Defined(pickle, step, match.GetInternalStepDefinition())
if err != nil {
sr = models.NewStepResult(pickle.Id, step.Id, match)
sr.Status = models.Failed
s.storage.MustInsertPickleStepResult(sr)
return ctx, err
}
if ctx, undef, err := s.maybeUndefined(ctx, step.Text, step.Argument); err != nil {
return ctx, err
} else if len(undef) > 0 {
if match != nil {
match = &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Expr: match.Expr,
Handler: match.Handler,
},
Args: match.Args,
HandlerValue: match.HandlerValue,
Nested: match.Nested,
Undefined: undef,
}
}
sr = models.NewStepResult(pickle.Id, step.Id, match)
sr.Status = models.Undefined
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Undefined(pickle, step, match.GetInternalStepDefinition())
return ctx, ErrUndefined
}
if prevStepErr != nil {
sr = models.NewStepResult(pickle.Id, step.Id, match)
sr.Status = models.Skipped
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Skipped(pickle, step, match.GetInternalStepDefinition())
return ctx, nil
}
ctx, err = s.maybeSubSteps(match.Run(ctx))
return ctx, err
}
func (s *suite) runBeforeStepHooks(ctx context.Context, step *Step, err error) (context.Context, error) {
hooksFailed := false
for _, f := range s.beforeStepHandlers {
hctx, herr := f(ctx, step)
if herr != nil {
hooksFailed = true
if err == nil {
err = herr
} else {
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
if hooksFailed {
err = fmt.Errorf("before step hook failed: %w", err)
}
return ctx, err
}
func (s *suite) runAfterStepHooks(ctx context.Context, step *Step, status StepResultStatus, err error) (context.Context, error) {
for _, f := range s.afterStepHandlers {
hctx, herr := f(ctx, step, status, err)
// Adding hook error to resulting error without breaking hooks loop.
if herr != nil {
if err == nil {
err = herr
} else {
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
return ctx, err
}
func (s *suite) runBeforeScenarioHooks(ctx context.Context, pickle *messages.Pickle) (context.Context, error) {
var err error
// run before scenario handlers
for _, f := range s.beforeScenarioHandlers {
hctx, herr := f(ctx, pickle)
if herr != nil {
if err == nil {
err = herr
} else {
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
if err != nil {
err = fmt.Errorf("before scenario hook failed: %w", err)
}
return ctx, err
}
func (s *suite) runAfterScenarioHooks(ctx context.Context, pickle *messages.Pickle, lastStepErr error) (context.Context, error) {
err := lastStepErr
hooksFailed := false
isStepErr := true
// run after scenario handlers
for _, f := range s.afterScenarioHandlers {
hctx, herr := f(ctx, pickle, err)
// Adding hook error to resulting error without breaking hooks loop.
if herr != nil {
hooksFailed = true
if err == nil {
isStepErr = false
err = herr
} else {
if isStepErr {
err = fmt.Errorf("step error: %w", err)
isStepErr = false
}
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
if hooksFailed {
err = fmt.Errorf("after scenario hook failed: %w", err)
}
return ctx, err
}
func (s *suite) maybeUndefined(ctx context.Context, text string, arg interface{}) (context.Context, []string, error) {
step := s.matchStepText(text)
if nil == step {
return ctx, []string{text}, nil
}
var undefined []string
if !step.Nested {
return ctx, undefined, nil
}
if arg != nil {
step.Args = append(step.Args, arg)
}
ctx, steps := step.Run(ctx)
for _, next := range steps.(Steps) {
lines := strings.Split(next, "\n")
// @TODO: we cannot currently parse table or content body from nested steps
if len(lines) > 1 {
return ctx, undefined, fmt.Errorf("nested steps cannot be multiline and have table or content body argument")
}
if len(lines[0]) > 0 && lines[0][len(lines[0])-1] == ':' {
return ctx, undefined, fmt.Errorf("nested steps cannot be multiline and have table or content body argument")
}
ctx, undef, err := s.maybeUndefined(ctx, next, nil)
if err != nil {
return ctx, undefined, err
}
undefined = append(undefined, undef...)
}
return ctx, undefined, nil
}
func (s *suite) maybeSubSteps(ctx context.Context, result interface{}) (context.Context, error) {
if nil == result {
return ctx, nil
}
if err, ok := result.(error); ok {
return ctx, err
}
steps, ok := result.(Steps)
if !ok {
return ctx, fmt.Errorf("unexpected error, should have been []string: %T - %+v", result, result)
}
for _, text := range steps {
if def := s.matchStepText(text); def == nil {
return ctx, ErrUndefined
} else if ctx, err := s.maybeSubSteps(def.Run(ctx)); err != nil {
return ctx, fmt.Errorf("%s: %+v", text, err)
}
}
return ctx, nil
}
func (s *suite) matchStepText(text string) *models.StepDefinition {
for _, h := range s.steps {
if m := h.Expr.FindStringSubmatch(text); len(m) > 0 {
var args []interface{}
for _, m := range m[1:] {
args = append(args, m)
}
// since we need to assign arguments
// better to copy the step definition
return &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Expr: h.Expr,
Handler: h.Handler,
},
Args: args,
HandlerValue: h.HandlerValue,
Nested: h.Nested,
}
}
}
return nil
}
func (s *suite) runSteps(ctx context.Context, pickle *Scenario, steps []*Step) (context.Context, error) {
var (
stepErr, err error
)
for i, step := range steps {
isLast := i == len(steps)-1
isFirst := i == 0
ctx, stepErr = s.runStep(ctx, pickle, step, err, isFirst, isLast)
switch stepErr {
case ErrUndefined:
// do not overwrite failed error
if err == ErrUndefined || err == nil {
err = stepErr
}
case ErrPending:
err = stepErr
case nil:
default:
err = stepErr
}
}
return ctx, err
}
func (s *suite) shouldFail(err error) bool {
if err == nil {
return false
}
if err == ErrUndefined || err == ErrPending {
return s.strict
}
return true
}
func (s *suite) runPickle(pickle *messages.Pickle) (err error) {
ctx := s.defaultContext
if ctx == nil {
ctx = context.Background()
}
if len(pickle.Steps) == 0 {
pr := models.PickleResult{PickleID: pickle.Id, StartedAt: utils.TimeNowFunc()}
s.storage.MustInsertPickleResult(pr)
s.fmt.Pickle(pickle)
return ErrUndefined
}
// Before scenario hooks are called in context of first evaluated step
// so that error from handler can be added to step.
pr := models.PickleResult{PickleID: pickle.Id, StartedAt: utils.TimeNowFunc()}
s.storage.MustInsertPickleResult(pr)
s.fmt.Pickle(pickle)
// scenario
if s.testingT != nil {
// Running scenario as a subtest.
s.testingT.Run(pickle.Name, func(t *testing.T) {
ctx, err = s.runSteps(ctx, pickle, pickle.Steps)
if err != nil {
t.Error(err)
}
})
} else {
ctx, err = s.runSteps(ctx, pickle, pickle.Steps)
}
// After scenario handlers are called in context of last evaluated step
// so that error from handler can be added to step.
return err
}