diff --git a/find_step.go b/find_step.go index 361c664..bf09d24 100644 --- a/find_step.go +++ b/find_step.go @@ -9,11 +9,11 @@ import ( func (r *Runner) findStep(t *testing.T, step *messages.PickleStep) *stepDef { t.Helper() - for _, def := range r.stepDefs { - matches := def.regex.FindSubmatch([]byte(step.Text)) - if len(matches) != 0 { - return def - } + r.stepDefsMutex.RLock() + def := findSubmatch(t, step.Text, r.stepDefs) + r.stepDefsMutex.RUnlock() + if def != nil { + return def } sig := guessMethodSig(step) @@ -30,3 +30,16 @@ func (r *Runner) findStep(t *testing.T, step *messages.PickleStep) *stepDef { return nil } + +func findSubmatch(t *testing.T, stepText string, stepDefs []*stepDef) *stepDef { + t.Helper() + + for _, def := range stepDefs { + matches := def.regex.FindSubmatch([]byte(stepText)) + if len(matches) != 0 { + return def + } + } + + return nil +} \ No newline at end of file diff --git a/runner.go b/runner.go index 2795328..4a848d9 100644 --- a/runner.go +++ b/runner.go @@ -2,6 +2,7 @@ package gocuke import ( "reflect" + "sync" "testing" messages "github.com/cucumber/messages/go/v22" @@ -17,6 +18,7 @@ type Runner struct { paths []string parallel bool stepDefs []*stepDef + stepDefsMutex *sync.RWMutex haveSuggestion map[string]bool suggestions []methodSig supportedSpecialArgs map[reflect.Type]specialArgGetter @@ -84,6 +86,7 @@ func NewRunner(t *testing.T, suiteType interface{}) *Runner { }, }, suiteUsesRapid: false, + stepDefsMutex: &sync.RWMutex{}, } r.registerSuite(suiteType) diff --git a/simple_test.go b/simple_test.go index 97b0b31..35123b8 100644 --- a/simple_test.go +++ b/simple_test.go @@ -34,7 +34,6 @@ func TestSimpleNonPointer(t *testing.T) { gocuke. NewRunner(t, simpleSuiteNP{}). Path("examples/simple/simple.feature"). - NonParallel(). Run() } diff --git a/step_def.go b/step_def.go index a3446a1..e6e1df5 100644 --- a/step_def.go +++ b/step_def.go @@ -54,7 +54,9 @@ func (r *Runner) Step(step interface{}, definition interface{}) *Runner { func (r *Runner) addStepDef(t *testing.T, exp *regexp.Regexp, definition reflect.Value) *stepDef { t.Helper() + defer r.stepDefsMutex.Unlock() + r.stepDefsMutex.Lock() def := r.newStepDefOrHook(t, exp, definition) r.stepDefs = append(r.stepDefs, def) return def