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

Do not discard context from substeps #488

Merged
merged 2 commits into from Jul 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
- README example is updated with `context.Context` and `go test` usage. ([477](https://github.com/cucumber/godog/pull/477) - [vearutop](https://github.com/vearutop))

### Fixed
- Fixed a bug which would ignore the context returned from a substep.([488](https://github.com/cucumber/godog/pull/488) - [wichert](https://github.com/wichert))
- Fixed a bug which would cause a panic when using the pretty formatter with a feature that contained a rule. ([480](https://github.com/cucumber/godog/pull/480) - [dumpsterfireproject](https://github.com/dumpsterfireproject))

## [v0.12.5]
Expand Down
23 changes: 23 additions & 0 deletions features/multistep.feature
Expand Up @@ -138,3 +138,26 @@ Feature: run features with nested steps
"""
I should have 1 scenario registered
"""

Scenario: context passed between steps
Given a feature "normal.feature" file:
"""
Feature: normal feature

Scenario: run passing multistep
Given I return a context from a step
Then I should see the context in the next step
"""
When I run feature suite
Then the suite should have passed

Scenario: context passed between steps
Given a feature "normal.feature" file:
"""
Feature: normal feature

Scenario: run passing multistep
Given I can see contexts passed in multisteps
"""
When I run feature suite
Then the suite should have passed
12 changes: 6 additions & 6 deletions run_test.go
Expand Up @@ -432,11 +432,11 @@ func Test_AllFeaturesRun(t *testing.T) {
...................................................................... 140
...................................................................... 210
...................................................................... 280
................................................. 329
....................................................... 335


86 scenarios (86 passed)
329 steps (329 passed)
88 scenarios (88 passed)
335 steps (335 passed)
0s
`

Expand All @@ -459,11 +459,11 @@ func Test_AllFeaturesRunAsSubtests(t *testing.T) {
...................................................................... 140
...................................................................... 210
...................................................................... 280
................................................. 329
....................................................... 335


86 scenarios (86 passed)
329 steps (329 passed)
88 scenarios (88 passed)
335 steps (335 passed)
0s
`

Expand Down
4 changes: 3 additions & 1 deletion suite.go
Expand Up @@ -346,10 +346,12 @@ func (s *suite) maybeSubSteps(ctx context.Context, result interface{}) (context.
return ctx, fmt.Errorf("unexpected error, should have been []string: %T - %+v", result, result)
}

var err error

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 {
} else if ctx, err = s.maybeSubSteps(def.Run(ctx)); err != nil {
return ctx, fmt.Errorf("%s: %+v", text, err)
}
}
Expand Down
26 changes: 26 additions & 0 deletions suite_context_test.go
Expand Up @@ -132,6 +132,15 @@ func InitializeScenario(ctx *ScenarioContext) {
return context.WithValue(ctx, ctxKey("StepState"), true)
})

ctx.Step(`^I return a context from a step$`, tc.iReturnAContextFromAStep)
ctx.Step(`^I should see the context in the next step$`, tc.iShouldSeeTheContextInTheNextStep)
ctx.Step(`^I can see contexts passed in multisteps$`, func() Steps {
return Steps{
"I return a context from a step",
"I should see the context in the next step",
}
})

ctx.StepContext().Before(tc.inject)
}

Expand Down Expand Up @@ -339,6 +348,23 @@ func (tc *godogFeaturesScenario) theUndefinedStepSnippetsShouldBe(body *DocStrin
return nil
}

type multiContextKey struct{}

func (tc *godogFeaturesScenario) iReturnAContextFromAStep(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, multiContextKey{}, "value"), nil
}

func (tc *godogFeaturesScenario) iShouldSeeTheContextInTheNextStep(ctx context.Context) error {
value, ok := ctx.Value(multiContextKey{}).(string)
if !ok {
return errors.New("context does not contain our key")
}
if value != "value" {
return errors.New("context has the wrong value for our key")
}
return nil
}

func (tc *godogFeaturesScenario) followingStepsShouldHave(status string, steps *DocString) error {
var expected = strings.Split(steps.Content, "\n")
var actual, unmatched, matched []string
Expand Down