Skip to content

Context from substeps is lost #487

@wichert

Description

@wichert

the suite.maybeSubSteps function loops over all substeps and invoked them one by one:

godog/suite.go

Lines 349 to 356 in 5e176da

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

On line 352 new ctx and err variables are created in the scope of the if statement that shadow the ctx from the argument list. As soon as the if scope completes ctx becomes the original context that was passed in to the function again. The effect of this is that the context returned by the subset is discarded.

This can easily be tested by modifying the standard example to add a multistep:

func InitializeScenario(sc *godog.ScenarioContext) {
	sc.StepContext().Before(transformStep)
	sc.Step(`^I eat (\d+)$`, iEat)
	sc.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
	sc.Step(`^I eat (\d+) godogs out of (\d+)`, func(ctx context.Context, a, b int) (context.Context, godog.Steps) {
		return ctx, godog.Steps{
			fmt.Sprintf("there are %d godogs", b),
			fmt.Sprintf("I eat %d", a),
		}
	})
}

with a matching scenario:

    Scenario: Basic multistep
        Given I eat 8 godogs out of 14
        Then there should be 6 remaining

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions