-
Notifications
You must be signed in to change notification settings - Fork 252
/
suite.go
645 lines (524 loc) · 15.9 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
package godog
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"testing"
messages "github.com/cucumber/messages/go/v21"
"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()
)
// more than one regex matched the step text
var ErrAmbiguous = fmt.Errorf("ambiguous step definition")
// 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")
// ErrSkip should be returned by step definition or a hook if scenario and further steps are to be skipped.
var ErrSkip = fmt.Errorf("skipped")
// 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
// StepAmbiguous indicates step text matches more than one step def
StepAmbiguous = models.Ambiguous
)
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
}
type Attachment struct {
Body []byte
FileName string
MediaType string
}
type attachmentKey struct{}
func Attach(ctx context.Context, attachments ...Attachment) context.Context {
existing := Attachments(ctx)
updated := append(existing, attachments...)
return context.WithValue(ctx, attachmentKey{}, updated)
}
func Attachments(ctx context.Context) []Attachment {
v := ctx.Value(attachmentKey{})
if v == nil {
return []Attachment{}
}
return v.([]Attachment)
}
func clearAttach(ctx context.Context) context.Context {
return context.WithValue(ctx, attachmentKey{}, nil)
}
func pickleAttachments(ctx context.Context) []models.PickleAttachment {
pickledAttachments := []models.PickleAttachment{}
attachments := Attachments(ctx)
for _, a := range attachments {
pickledAttachments = append(pickledAttachments, models.PickleAttachment{
Name: a.FileName,
Data: a.Body,
MimeType: a.MediaType,
})
}
return pickledAttachments
}
func (s *suite) matchStep(step *messages.PickleStep) (*models.StepDefinition, error) {
def, err := s.matchStepTextAndType(step.Text, step.Type)
if err != nil {
return nil, err
}
if def != nil && step.Argument != nil {
def.Args = append(def.Args, step.Argument)
}
return def, nil
}
func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, scenarioErr error, isFirst, isLast bool) (rctx context.Context, err error) {
var match *models.StepDefinition
rctx = ctx
// user multistep definitions may panic
defer func() {
if e := recover(); e != nil {
pe, isErr := e.(error)
switch {
case isErr && errors.Is(pe, errStopNow):
// FailNow or SkipNow called on dogTestingT, so clear the error to let the normal
// below getTestingT(ctx).isFailed() call handle the reasons.
err = nil
case err != nil:
err = &traceError{
msg: fmt.Sprintf("%s: %v", err.Error(), e),
stack: callStack(),
}
default:
err = &traceError{
msg: fmt.Sprintf("%v", e),
stack: callStack(),
}
}
}
earlyReturn := scenarioErr != nil || errors.Is(err, ErrUndefined)
// Check for any calls to Fail on dogT
if err == nil {
err = getTestingT(ctx).isFailed()
}
status := StepUndefined
switch {
case errors.Is(err, ErrAmbiguous):
status = StepAmbiguous
case errors.Is(err, ErrPending):
status = StepPending
case errors.Is(err, ErrSkip), err == nil && scenarioErr != nil:
status = StepSkipped
case errors.Is(err, ErrUndefined):
status = StepUndefined
case err != nil:
status = StepFailed
case err == nil && scenarioErr == nil:
status = StepPassed
}
// Run after step handlers.
rctx, err = s.runAfterStepHooks(ctx, step, status, err)
// Trigger after scenario on failing or last step to attach possible hook error to step.
if !s.shouldFail(scenarioErr) && (isLast || s.shouldFail(err)) {
rctx, err = s.runAfterScenarioHooks(rctx, pickle, err)
}
// extract any accumulated attachments and clear them
pickledAttachments := pickleAttachments(rctx)
rctx = clearAttach(rctx)
if earlyReturn {
return
}
switch {
case err == nil:
sr := models.NewStepResult(models.Passed, pickle.Id, step.Id, match, pickledAttachments, nil)
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Passed(pickle, step, match.GetInternalStepDefinition())
case errors.Is(err, ErrPending):
sr := models.NewStepResult(models.Pending, pickle.Id, step.Id, match, pickledAttachments, nil)
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Pending(pickle, step, match.GetInternalStepDefinition())
case errors.Is(err, ErrSkip):
sr := models.NewStepResult(models.Skipped, pickle.Id, step.Id, match, pickledAttachments, nil)
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Skipped(pickle, step, match.GetInternalStepDefinition())
case errors.Is(err, ErrAmbiguous):
sr := models.NewStepResult(models.Ambiguous, pickle.Id, step.Id, match, pickledAttachments, err)
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Ambiguous(pickle, step, match.GetInternalStepDefinition(), err)
default:
sr := models.NewStepResult(models.Failed, pickle.Id, step.Id, match, pickledAttachments, 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)
var matchError error
match, matchError = s.matchStep(step)
s.storage.MustInsertStepDefintionMatch(step.AstNodeIds[0], match)
s.fmt.Defined(pickle, step, match.GetInternalStepDefinition())
if err != nil {
pickledAttachments := pickleAttachments(ctx)
ctx = clearAttach(ctx)
sr := models.NewStepResult(models.Failed, pickle.Id, step.Id, match, pickledAttachments, nil)
s.storage.MustInsertPickleStepResult(sr)
return ctx, err
}
if matchError != nil {
return ctx, matchError
}
if ctx, undef, err := s.maybeUndefined(ctx, step.Text, step.Argument, step.Type); 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,
Keyword: match.Keyword,
},
Args: match.Args,
HandlerValue: match.HandlerValue,
Nested: match.Nested,
Undefined: undef,
}
}
pickledAttachments := pickleAttachments(ctx)
ctx = clearAttach(ctx)
sr := models.NewStepResult(models.Undefined, pickle.Id, step.Id, match, pickledAttachments, nil)
s.storage.MustInsertPickleStepResult(sr)
s.fmt.Undefined(pickle, step, match.GetInternalStepDefinition())
return ctx, ErrUndefined
}
if scenarioErr != nil {
pickledAttachments := pickleAttachments(ctx)
ctx = clearAttach(ctx)
sr := models.NewStepResult(models.Skipped, pickle.Id, step.Id, match, pickledAttachments, nil)
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{}, stepType messages.PickleStepType) (context.Context, []string, error) {
var undefined []string
step, err := s.matchStepTextAndType(text, stepType)
if err != nil {
return ctx, undefined, err
}
if nil == step {
return ctx, []string{text}, nil
}
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, messages.PickleStepType_UNKNOWN)
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 godog.Steps: %T - %+v", result, result)
}
for _, text := range steps {
def, err := s.matchStepTextAndType(text, messages.PickleStepType_UNKNOWN)
if err != nil {
return ctx, err
}
if def == nil {
return ctx, ErrUndefined
} else {
ctx, err = s.runSubStep(ctx, text, def)
if err != nil {
return ctx, err
}
}
}
return ctx, nil
}
func (s *suite) runSubStep(ctx context.Context, text string, def *models.StepDefinition) (_ context.Context, err error) {
st := &Step{}
st.Text = text
st.Type = messages.PickleStepType_ACTION
defer func() {
status := StepPassed
switch {
case errors.Is(err, ErrUndefined):
status = StepUndefined
case errors.Is(err, ErrPending):
status = StepPending
case err != nil:
status = StepFailed
}
ctx, err = s.runAfterStepHooks(ctx, st, status, err)
}()
ctx, err = s.runBeforeStepHooks(ctx, st, nil)
if err != nil {
return ctx, fmt.Errorf("%s: %+v", text, err)
}
if ctx, err = s.maybeSubSteps(def.Run(ctx)); err != nil {
return ctx, fmt.Errorf("%s: %+v", text, err)
}
return ctx, nil
}
func (s *suite) matchStepTextAndType(text string, stepType messages.PickleStepType) (*models.StepDefinition, error) {
var first *models.StepDefinition
matchingExpressions := make([]string, 0)
for _, h := range s.steps {
if m := h.Expr.FindStringSubmatch(text); len(m) > 0 {
if !keywordMatches(h.Keyword, stepType) {
continue
}
var args []interface{}
for _, m := range m[1:] {
args = append(args, m)
}
matchingExpressions = append(matchingExpressions, h.Expr.String())
// since we need to assign arguments
// better to copy the step definition
match := &models.StepDefinition{
StepDefinition: formatters.StepDefinition{
Expr: h.Expr,
Handler: h.Handler,
Keyword: h.Keyword,
},
Args: args,
HandlerValue: h.HandlerValue,
Nested: h.Nested,
}
if first == nil {
first = match
}
}
}
if s.strict {
if len(matchingExpressions) > 1 {
errs := "\n " + strings.Join(matchingExpressions, "\n ")
return nil, fmt.Errorf("%w, step text: %s\n matches:%s", ErrAmbiguous, text, errs)
}
}
return first, nil
}
func keywordMatches(k formatters.Keyword, stepType messages.PickleStepType) bool {
if k == formatters.None {
return true
}
switch stepType {
case messages.PickleStepType_CONTEXT:
return k == formatters.Given
case messages.PickleStepType_ACTION:
return k == formatters.When
case messages.PickleStepType_OUTCOME:
return k == formatters.Then
default:
return true
}
}
func (s *suite) runSteps(ctx context.Context, pickle *Scenario, steps []*Step) (context.Context, error) {
var (
stepErr, scenarioErr error
)
for i, step := range steps {
isLast := i == len(steps)-1
isFirst := i == 0
ctx, stepErr = s.runStep(ctx, pickle, step, scenarioErr, isFirst, isLast)
if scenarioErr == nil || s.shouldFail(stepErr) {
scenarioErr = stepErr
}
}
return ctx, scenarioErr
}
func (s *suite) shouldFail(err error) bool {
if err == nil || errors.Is(err, ErrSkip) {
return false
}
if errors.Is(err, ErrUndefined) || errors.Is(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()
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
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)
dt := &testingT{
name: pickle.Name,
}
ctx = setContextTestingT(ctx, dt)
// scenario
if s.testingT != nil {
// Running scenario as a subtest.
s.testingT.Run(pickle.Name, func(t *testing.T) {
dt.t = t
ctx, err = s.runSteps(ctx, pickle, pickle.Steps)
if s.shouldFail(err) {
t.Errorf("%+v", 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
}