From 2ef5a29c36c591c5d5010d83ec317ab440edad7b Mon Sep 17 00:00:00 2001 From: gedi Date: Thu, 15 Nov 2018 15:07:03 +0200 Subject: [PATCH] resolve step definition source code line properly with race detector enabled --- fmt.go | 3 +- .../some_scenarions_including_failing.feature | 14 +++++++ .../pretty/single_scenario_with_passing_step | 2 +- .../pretty/some_scenarions_including_failing | 38 +++++++++++++++++++ formatters_print_test.go | 38 +++++++++++++------ 5 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 formatter-tests/features/some_scenarions_including_failing.feature create mode 100644 formatter-tests/pretty/some_scenarions_including_failing diff --git a/fmt.go b/fmt.go index addfe77..6cf23f4 100644 --- a/fmt.go +++ b/fmt.go @@ -388,7 +388,8 @@ func (f *basefmt) Summary() { } if text := f.snippets(); text != "" { - fmt.Fprintln(f.out, yellow("\nYou can implement step definitions for undefined steps with these snippets:")) + fmt.Fprintln(f.out, "") + fmt.Fprintln(f.out, yellow("You can implement step definitions for undefined steps with these snippets:")) fmt.Fprintln(f.out, yellow(text)) } } diff --git a/formatter-tests/features/some_scenarions_including_failing.feature b/formatter-tests/features/some_scenarions_including_failing.feature new file mode 100644 index 0000000..a41f7ef --- /dev/null +++ b/formatter-tests/features/some_scenarions_including_failing.feature @@ -0,0 +1,14 @@ +Feature: some scenarios + + Scenario: failing + Given passing step + When failing step + Then passing step + + Scenario: pending + When pending step + Then passing step + + Scenario: undefined + When undefined + Then passing step diff --git a/formatter-tests/pretty/single_scenario_with_passing_step b/formatter-tests/pretty/single_scenario_with_passing_step index e0dcf32..2e06272 100644 --- a/formatter-tests/pretty/single_scenario_with_passing_step +++ b/formatter-tests/pretty/single_scenario_with_passing_step @@ -4,7 +4,7 @@ feature Scenario: one step passing # formatter-tests/features/single_scenario_with_passing_step.feature:6 - Given a passing step # formatters_print_test.go:35 -> github.com/DATA-DOG/godog.TestPrintingFormatters.func4 + Given a passing step # formatters_print_test.go:64 -> passingStepDef 1 scenarios (1 passed) 1 steps (1 passed) diff --git a/formatter-tests/pretty/some_scenarions_including_failing b/formatter-tests/pretty/some_scenarions_including_failing new file mode 100644 index 0000000..0a19cb6 --- /dev/null +++ b/formatter-tests/pretty/some_scenarions_including_failing @@ -0,0 +1,38 @@ +Feature: some scenarios + + Scenario: failing # formatter-tests/features/some_scenarions_including_failing.feature:3 + Given passing step # formatters_print_test.go:64 -> passingStepDef + When failing step # formatters_print_test.go:80 -> failingStepDef + step failed + Then passing step # formatters_print_test.go:64 -> passingStepDef + + Scenario: pending # formatter-tests/features/some_scenarions_including_failing.feature:8 + When pending step # formatters_print_test.go:78 -> pendingStepDef + TODO: write pending definition + Then passing step # formatters_print_test.go:64 -> passingStepDef + + Scenario: undefined # formatter-tests/features/some_scenarions_including_failing.feature:12 + When undefined + Then passing step # formatters_print_test.go:64 -> passingStepDef + +--- Failed steps: + + Scenario: failing # formatter-tests/features/some_scenarions_including_failing.feature:3 + When failing step # formatter-tests/features/some_scenarions_including_failing.feature:5 + Error: step failed + + +3 scenarios (1 failed, 1 pending, 1 undefined) +7 steps (1 passed, 1 failed, 1 pending, 1 undefined, 3 skipped) +0s + +You can implement step definitions for undefined steps with these snippets: + +func undefined() error { + return godog.ErrPending +} + +func FeatureContext(s *godog.Suite) { + s.Step(`^undefined$`, undefined) +} + diff --git a/formatters_print_test.go b/formatters_print_test.go index 8b4bd51..4c0b7fa 100644 --- a/formatters_print_test.go +++ b/formatters_print_test.go @@ -22,19 +22,14 @@ func TestPrintingFormatters(t *testing.T) { features: features, } - suite.Step(`^(?:a )?failing step`, func() error { - return fmt.Errorf("step failed") - }) - suite.Step(`^this step should fail`, func() error { - return fmt.Errorf("step failed") - }) - suite.Step(`^(?:a )?pending step$`, func() error { - return ErrPending - }) - suite.Step(`^(?:a )?passing step$`, func() error { - return nil - }) + // inlining steps to have same source code line reference + suite.Step(`^(?:a )?failing step`, failingStepDef) + suite.Step(`^(?:a )?pending step$`, pendingStepDef) + suite.Step(`^(?:a )?passing step$`, passingStepDef) + suite.Step(`^is and number$`, oddEvenStepDef) + pkg := os.Getenv("GODOG_TESTED_PACKAGE") + os.Setenv("GODOG_TESTED_PACKAGE", "github.com/DATA-DOG/godog") for _, feat := range features { for name := range AvailableFormatters() { expectOutputPath := strings.Replace(feat.Path, "features", name, 1) @@ -63,4 +58,23 @@ func TestPrintingFormatters(t *testing.T) { } } } + os.Setenv("GODOG_TESTED_PACKAGE", pkg) } + +func passingStepDef() error { return nil } + +func oddEvenStepDef(odd, even int) error { return oddOrEven(odd, even) } + +func oddOrEven(odd, even int) error { + if odd%2 == 0 { + return fmt.Errorf("%d is not odd", odd) + } + if even%2 != 0 { + return fmt.Errorf("%d is not even", even) + } + return nil +} + +func pendingStepDef() error { return ErrPending } + +func failingStepDef() error { return fmt.Errorf("step failed") }