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

Print step declaration line instead of handler declaration line #668

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
## Unreleased

- fix(formatter): On concurrent execution, execute formatter at end of Scenario - ([645](https://github.com/cucumber/godog/pull/645) - [tigh-latte](https://github.com/tigh-latte))
- Pretty printing results now prints the line where the step is declared instead of the line where the handler is declared. ([668](https://github.com/cucumber/godog/pull/668) - [spencerc](https://github.com/SpencerC))

## [v0.15.0]

Expand Down
6 changes: 2 additions & 4 deletions internal/formatters/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ func mustConvertStringToInt(s string) int {
func DefinitionID(sd *models.StepDefinition) string {
ptr := sd.HandlerValue.Pointer()
f := runtime.FuncForPC(ptr)
file, line := f.FileLine(ptr)
dir := filepath.Dir(file)

dir := filepath.Dir(sd.File)
fn := strings.Replace(f.Name(), dir, "", -1)
var parts []string
for _, gr := range matchFuncDefRef.FindAllStringSubmatch(fn, -1) {
Expand All @@ -100,7 +98,7 @@ func DefinitionID(sd *models.StepDefinition) string {
fn = strings.Replace(fn, "..", ".", -1)
}

return fmt.Sprintf("%s:%d -> %s", filepath.Base(file), line, fn)
return fmt.Sprintf("%s:%d -> %s", filepath.Base(sd.File), sd.Line, fn)
}

var matchFuncDefRef = regexp.MustCompile(`\(([^\)]+)\)`)
2 changes: 2 additions & 0 deletions internal/models/stepdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type StepDefinition struct {

Args []interface{}
HandlerValue reflect.Value
File string
Line int

// multistep related
Nested bool
Expand Down
4 changes: 4 additions & 0 deletions suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, scena
},
Args: match.Args,
HandlerValue: match.HandlerValue,
File: match.File,
Line: match.Line,
Nested: match.Nested,
Undefined: undef,
}
Expand Down Expand Up @@ -530,6 +532,8 @@ func (s *suite) matchStepTextAndType(text string, stepType messages.PickleStepTy
},
Args: args,
HandlerValue: h.HandlerValue,
File: h.File,
Line: h.Line,
Nested: h.Nested,
}

Expand Down
8 changes: 7 additions & 1 deletion test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"fmt"
"reflect"
"regexp"
"runtime"

messages "github.com/cucumber/messages/go/v21"

"github.com/cucumber/godog/formatters"
"github.com/cucumber/godog/internal/builder"
"github.com/cucumber/godog/internal/flags"
"github.com/cucumber/godog/internal/models"
messages "github.com/cucumber/messages/go/v21"
)

// GherkinDocument represents gherkin document.
Expand Down Expand Up @@ -342,6 +344,10 @@ func (ctx ScenarioContext) stepWithKeyword(expr interface{}, stepFunc interface{
Nested: isNested,
}

// Get the file and line number of the call that created this step with a
// call to one of the Step, Given, When, or Then wrappers.
_, def.File, def.Line, _ = runtime.Caller(2)

// stash the step
ctx.suite.steps = append(ctx.suite.steps, def)
}
Expand Down