Skip to content

Commit 005cfc2

Browse files
authored
Print step declaration line instead of handler declaration line (#668)
* Print step declaration line instead of handler declaration line. * Update changelog.
1 parent 6ba3a7e commit 005cfc2

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
1717
### Fixed
1818
- fix(errors): fix(errors): Fix expected Step argument count for steps with `context.Context` ([679](https://github.com/cucumber/godog/pull/679) - [tigh-latte](https://github.com/tigh-latte))
1919
- 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))
20+
- 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))
2021

2122
## [v0.15.0]
2223

internal/formatters/fmt.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ func mustConvertStringToInt(s string) int {
7878
func DefinitionID(sd *models.StepDefinition) string {
7979
ptr := sd.HandlerValue.Pointer()
8080
f := runtime.FuncForPC(ptr)
81-
file, line := f.FileLine(ptr)
82-
dir := filepath.Dir(file)
83-
81+
dir := filepath.Dir(sd.File)
8482
fn := strings.Replace(f.Name(), dir, "", -1)
8583
var parts []string
8684
for _, gr := range matchFuncDefRef.FindAllStringSubmatch(fn, -1) {
@@ -100,7 +98,7 @@ func DefinitionID(sd *models.StepDefinition) string {
10098
fn = strings.Replace(fn, "..", ".", -1)
10199
}
102100

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

106104
var matchFuncDefRef = regexp.MustCompile(`\(([^\)]+)\)`)

internal/models/stepdef.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type StepDefinition struct {
2727

2828
Args []interface{}
2929
HandlerValue reflect.Value
30+
File string
31+
Line int
3032

3133
// multistep related
3234
Nested bool

suite.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, scena
260260
},
261261
Args: match.Args,
262262
HandlerValue: match.HandlerValue,
263+
File: match.File,
264+
Line: match.Line,
263265
Nested: match.Nested,
264266
Undefined: undef,
265267
}
@@ -532,6 +534,8 @@ func (s *suite) matchStepTextAndType(text string, stepType messages.PickleStepTy
532534
},
533535
Args: args,
534536
HandlerValue: h.HandlerValue,
537+
File: h.File,
538+
Line: h.Line,
535539
Nested: h.Nested,
536540
}
537541

test_context.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"fmt"
66
"reflect"
77
"regexp"
8+
"runtime"
9+
10+
messages "github.com/cucumber/messages/go/v21"
811

912
"github.com/cucumber/godog/formatters"
1013
"github.com/cucumber/godog/internal/builder"
1114
"github.com/cucumber/godog/internal/flags"
1215
"github.com/cucumber/godog/internal/models"
13-
messages "github.com/cucumber/messages/go/v21"
1416
)
1517

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

347+
// Get the file and line number of the call that created this step with a
348+
// call to one of the Step, Given, When, or Then wrappers.
349+
_, def.File, def.Line, _ = runtime.Caller(2)
350+
345351
// stash the step
346352
ctx.suite.steps = append(ctx.suite.steps, def)
347353
}

0 commit comments

Comments
 (0)