Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke After Scenario hooks after After Step hooks #444

Merged
merged 3 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt

### Fixed

- After Scenario hook is called before After Step ([444](https://github.com/cucumber/godog/pull/444) - [vearutop])
- `check-go-version` in Makefile to run on WSL. ([443](https://github.com/cucumber/godog/pull/443) - [mxygem])

## [v0.12.2]
Expand Down
2 changes: 1 addition & 1 deletion features/events.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ Feature: suite events

Scenario: two
Then passing step
And failing step
And adding step state to context
And having correct context
And failing step

Scenario Outline: three
Then passing step
Expand Down
22 changes: 10 additions & 12 deletions suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,24 @@ func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, prevS
}
}

defer func() {
// run after step handlers
rctx, err = s.runAfterStepHooks(ctx, step, sr.Status, err)
}()
earlyReturn := prevStepErr != nil || err == ErrUndefined

if prevStepErr != nil {
return
}

if err == ErrUndefined {
return
if !earlyReturn {
sr = models.NewStepResult(pickle.Id, step.Id, match)
}

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 (err == nil && isLast) || err != nil {
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
Expand Down
19 changes: 10 additions & 9 deletions suite_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,12 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
scenarioContext.Before(func(ctx context.Context, pickle *Scenario) (context.Context, error) {
tc.events = append(tc.events, &firedEvent{"BeforeScenario", []interface{}{pickle}})

return context.WithValue(ctx, ctxKey("BeforeScenario"), true), nil
return context.WithValue(ctx, ctxKey("BeforeScenario"), pickle.Name), nil
})

scenarioContext.Before(func(ctx context.Context, sc *Scenario) (context.Context, error) {
if sc.Name == "failing before and after scenario" || sc.Name == "failing before scenario" {
return context.WithValue(ctx, ctxKey("AfterStep"), true), errors.New("failed in before scenario hook")
return context.WithValue(ctx, ctxKey("AfterStep"), sc.Name), errors.New("failed in before scenario hook")
}

return ctx, nil
Expand All @@ -453,7 +453,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
return ctx, errors.New("missing AfterStep in context")
}

return context.WithValue(ctx, ctxKey("AfterScenario"), true), nil
return context.WithValue(ctx, ctxKey("AfterScenario"), pickle.Name), nil
})

scenarioContext.StepContext().Before(func(ctx context.Context, step *Step) (context.Context, error) {
Expand All @@ -463,7 +463,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
return ctx, errors.New("missing BeforeScenario in context")
}

return context.WithValue(ctx, ctxKey("BeforeStep"), true), nil
return context.WithValue(ctx, ctxKey("BeforeStep"), step.Text), nil
})

scenarioContext.StepContext().After(func(ctx context.Context, step *Step, status StepResultStatus, err error) (context.Context, error) {
Expand All @@ -473,6 +473,10 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
return ctx, errors.New("missing BeforeScenario in context")
}

if ctx.Value(ctxKey("AfterScenario")) != nil && status != models.Skipped {
panic("unexpected premature AfterScenario during AfterStep: " + ctx.Value(ctxKey("AfterScenario")).(string))
}

if ctx.Value(ctxKey("BeforeStep")) == nil {
return ctx, errors.New("missing BeforeStep in context")
}
Expand All @@ -485,7 +489,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
return ctx, errors.New("missing Step in context")
}

return context.WithValue(ctx, ctxKey("AfterStep"), true), nil
return context.WithValue(ctx, ctxKey("AfterStep"), step.Text), nil
})

return nil
Expand Down Expand Up @@ -699,10 +703,7 @@ func (tc *godogFeaturesScenario) theRenderOutputWillBe(docstring *DocString) err
actualTrimmed := actual
actual = trimAllLines(actual)

expectedRows := strings.Split(expected, "\n")
actualRows := strings.Split(actual, "\n")

return assertExpectedAndActual(assert.ElementsMatch, expectedRows, actualRows, actualTrimmed)
return assertExpectedAndActual(assert.Equal, expected, actual, actualTrimmed)
}

func (tc *godogFeaturesScenario) theRenderXMLWillBe(docstring *DocString) error {
Expand Down