Skip to content

Commit b2672bb

Browse files
authored
Do not discard context from substeps (#488)
1 parent 5e176da commit b2672bb

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
1212
- README example is updated with `context.Context` and `go test` usage. ([477](https://github.com/cucumber/godog/pull/477) - [vearutop](https://github.com/vearutop))
1313

1414
### Fixed
15+
- 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))
1516
- 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))
1617

1718
## [v0.12.5]

features/multistep.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,26 @@ Feature: run features with nested steps
138138
"""
139139
I should have 1 scenario registered
140140
"""
141+
142+
Scenario: context passed between steps
143+
Given a feature "normal.feature" file:
144+
"""
145+
Feature: normal feature
146+
147+
Scenario: run passing multistep
148+
Given I return a context from a step
149+
Then I should see the context in the next step
150+
"""
151+
When I run feature suite
152+
Then the suite should have passed
153+
154+
Scenario: context passed between steps
155+
Given a feature "normal.feature" file:
156+
"""
157+
Feature: normal feature
158+
159+
Scenario: run passing multistep
160+
Given I can see contexts passed in multisteps
161+
"""
162+
When I run feature suite
163+
Then the suite should have passed

run_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ func Test_AllFeaturesRun(t *testing.T) {
432432
...................................................................... 140
433433
...................................................................... 210
434434
...................................................................... 280
435-
................................................. 329
435+
....................................................... 335
436436
437437
438-
86 scenarios (86 passed)
439-
329 steps (329 passed)
438+
88 scenarios (88 passed)
439+
335 steps (335 passed)
440440
0s
441441
`
442442

@@ -459,11 +459,11 @@ func Test_AllFeaturesRunAsSubtests(t *testing.T) {
459459
...................................................................... 140
460460
...................................................................... 210
461461
...................................................................... 280
462-
................................................. 329
462+
....................................................... 335
463463
464464
465-
86 scenarios (86 passed)
466-
329 steps (329 passed)
465+
88 scenarios (88 passed)
466+
335 steps (335 passed)
467467
0s
468468
`
469469

suite.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,12 @@ func (s *suite) maybeSubSteps(ctx context.Context, result interface{}) (context.
346346
return ctx, fmt.Errorf("unexpected error, should have been []string: %T - %+v", result, result)
347347
}
348348

349+
var err error
350+
349351
for _, text := range steps {
350352
if def := s.matchStepText(text); def == nil {
351353
return ctx, ErrUndefined
352-
} else if ctx, err := s.maybeSubSteps(def.Run(ctx)); err != nil {
354+
} else if ctx, err = s.maybeSubSteps(def.Run(ctx)); err != nil {
353355
return ctx, fmt.Errorf("%s: %+v", text, err)
354356
}
355357
}

suite_context_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ func InitializeScenario(ctx *ScenarioContext) {
132132
return context.WithValue(ctx, ctxKey("StepState"), true)
133133
})
134134

135+
ctx.Step(`^I return a context from a step$`, tc.iReturnAContextFromAStep)
136+
ctx.Step(`^I should see the context in the next step$`, tc.iShouldSeeTheContextInTheNextStep)
137+
ctx.Step(`^I can see contexts passed in multisteps$`, func() Steps {
138+
return Steps{
139+
"I return a context from a step",
140+
"I should see the context in the next step",
141+
}
142+
})
143+
135144
ctx.StepContext().Before(tc.inject)
136145
}
137146

@@ -339,6 +348,23 @@ func (tc *godogFeaturesScenario) theUndefinedStepSnippetsShouldBe(body *DocStrin
339348
return nil
340349
}
341350

351+
type multiContextKey struct{}
352+
353+
func (tc *godogFeaturesScenario) iReturnAContextFromAStep(ctx context.Context) (context.Context, error) {
354+
return context.WithValue(ctx, multiContextKey{}, "value"), nil
355+
}
356+
357+
func (tc *godogFeaturesScenario) iShouldSeeTheContextInTheNextStep(ctx context.Context) error {
358+
value, ok := ctx.Value(multiContextKey{}).(string)
359+
if !ok {
360+
return errors.New("context does not contain our key")
361+
}
362+
if value != "value" {
363+
return errors.New("context has the wrong value for our key")
364+
}
365+
return nil
366+
}
367+
342368
func (tc *godogFeaturesScenario) followingStepsShouldHave(status string, steps *DocString) error {
343369
var expected = strings.Split(steps.Content, "\n")
344370
var actual, unmatched, matched []string

0 commit comments

Comments
 (0)