Skip to content

Commit 6704843

Browse files
qingyunhaianlancetaylor
authored andcommitted
testing: update helperNames just before checking it
parent's helperNames has not been set when frameSkip called, moving helperNames initilazing to frameSkip. Fixes #44887 Change-Id: I5107c5951033e5e47d1ac441eac3ba5344a7bdc0 GitHub-Last-Rev: 44b90b2 GitHub-Pull-Request: #45071 Reviewed-on: https://go-review.googlesource.com/c/go/+/302469 Trust: Cherry Zhang <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent dcc96e4 commit 6704843

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/testing/helper_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ func TestTBHelperParallel(t *T) {
7171
}
7272
}
7373

74+
func TestTBHelperLineNumer(t *T) {
75+
var buf bytes.Buffer
76+
ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
77+
t1 := &T{
78+
common: common{
79+
signal: make(chan bool),
80+
w: &buf,
81+
},
82+
context: ctx,
83+
}
84+
t1.Run("Test", func(t *T) {
85+
helperA := func(t *T) {
86+
t.Helper()
87+
t.Run("subtest", func(t *T) {
88+
t.Helper()
89+
t.Fatal("fatal error message")
90+
})
91+
}
92+
helperA(t)
93+
})
94+
95+
want := "helper_test.go:92: fatal error message"
96+
got := ""
97+
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
98+
if len(lines) > 0 {
99+
got = strings.TrimSpace(lines[len(lines)-1])
100+
}
101+
if got != want {
102+
t.Errorf("got output:\n\n%v\nwant:\n\n%v", got, want)
103+
}
104+
}
105+
74106
type noopWriter int
75107

76108
func (nw *noopWriter) Write(b []byte) (int, error) { return len(b), nil }

src/testing/testing.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,13 @@ func (c *common) frameSkip(skip int) runtime.Frame {
509509
}
510510
return prevFrame
511511
}
512+
// If more helper PCs have been added since we last did the conversion
513+
if c.helperNames == nil {
514+
c.helperNames = make(map[string]struct{})
515+
for pc := range c.helperPCs {
516+
c.helperNames[pcToName(pc)] = struct{}{}
517+
}
518+
}
512519
if _, ok := c.helperNames[frame.Function]; !ok {
513520
// Found a frame that wasn't inside a helper function.
514521
return frame
@@ -521,14 +528,6 @@ func (c *common) frameSkip(skip int) runtime.Frame {
521528
// and inserts the final newline if needed and indentation spaces for formatting.
522529
// This function must be called with c.mu held.
523530
func (c *common) decorate(s string, skip int) string {
524-
// If more helper PCs have been added since we last did the conversion
525-
if c.helperNames == nil {
526-
c.helperNames = make(map[string]struct{})
527-
for pc := range c.helperPCs {
528-
c.helperNames[pcToName(pc)] = struct{}{}
529-
}
530-
}
531-
532531
frame := c.frameSkip(skip)
533532
file := frame.File
534533
line := frame.Line

0 commit comments

Comments
 (0)