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

testing: update helperNames just before checking it #45071

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 32 additions & 0 deletions src/testing/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,38 @@ func TestTBHelperParallel(t *T) {
}
}

func TestTBHelperLineNumer(t *T) {
var buf bytes.Buffer
ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
t1 := &T{
common: common{
signal: make(chan bool),
w: &buf,
},
context: ctx,
}
t1.Run("Test", func(t *T) {
helperA := func(t *T) {
t.Helper()
t.Run("subtest", func(t *T) {
t.Helper()
t.Fatal("fatal error message")
})
}
helperA(t)
})

want := "helper_test.go:92: fatal error message"
got := ""
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(lines) > 0 {
got = strings.TrimSpace(lines[len(lines)-1])
}
if got != want {
t.Errorf("got output:\n\n%v\nwant:\n\n%v", got, want)
}
}

type noopWriter int

func (nw *noopWriter) Write(b []byte) (int, error) { return len(b), nil }
Expand Down
15 changes: 7 additions & 8 deletions src/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ func (c *common) frameSkip(skip int) runtime.Frame {
}
return prevFrame
}
// If more helper PCs have been added since we last did the conversion
if c.helperNames == nil {
c.helperNames = make(map[string]struct{})
for pc := range c.helperPCs {
c.helperNames[pcToName(pc)] = struct{}{}
}
}
if _, ok := c.helperNames[frame.Function]; !ok {
// Found a frame that wasn't inside a helper function.
return frame
Expand All @@ -521,14 +528,6 @@ func (c *common) frameSkip(skip int) runtime.Frame {
// and inserts the final newline if needed and indentation spaces for formatting.
// This function must be called with c.mu held.
func (c *common) decorate(s string, skip int) string {
// If more helper PCs have been added since we last did the conversion
if c.helperNames == nil {
c.helperNames = make(map[string]struct{})
for pc := range c.helperPCs {
c.helperNames[pcToName(pc)] = struct{}{}
}
}

frame := c.frameSkip(skip)
file := frame.File
line := frame.Line
Expand Down