From 2ed2b81d7b7d9261d23145b6aa922fb86ed2cb4b Mon Sep 17 00:00:00 2001 From: Tao Qingyun Date: Wed, 17 Mar 2021 09:30:43 +0800 Subject: [PATCH 1/2] testing: update helperNames just before checking it parent's helperNames has not been set when frameSkip called, moving helperNames initilazing to frameSkip. Fixes #44887 --- src/testing/helper_test.go | 32 ++++++++++++++++++++++++++++++++ src/testing/testing.go | 14 ++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/testing/helper_test.go b/src/testing/helper_test.go index 8858196cf08c9e..b27fd62ee8f718 100644 --- a/src/testing/helper_test.go +++ b/src/testing/helper_test.go @@ -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 } diff --git a/src/testing/testing.go b/src/testing/testing.go index 466dd96981acca..3e0cd509326018 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -509,6 +509,12 @@ func (c *common) frameSkip(skip int) runtime.Frame { } return prevFrame } + 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 @@ -521,14 +527,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 From 44b90b2e2eeca8e2bb4a2084ec6fdd279c88f76d Mon Sep 17 00:00:00 2001 From: Tao Qingyun Date: Wed, 17 Mar 2021 09:30:43 +0800 Subject: [PATCH 2/2] testing: update helperNames just before checking it parent's helperNames has not been set when frameSkip called, moving helperNames initilazing to frameSkip. Fixes #44887 --- src/testing/testing.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/testing/testing.go b/src/testing/testing.go index 3e0cd509326018..8cdfb0f10f9873 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -509,6 +509,7 @@ 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 {