aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.go
diff options
context:
space:
mode:
authorMichał Łowicki <mlowicki@gmail.com>2020-05-04 22:23:28 +0100
committerIan Lance Taylor <iant@golang.org>2020-05-05 22:44:36 +0000
commit7db566f9c26236f852fa0f980e6c4e8cf86890f3 (patch)
tree7e2f81d6eedc815e69b953d68036319b19697c36 /src/testing/testing.go
parent430cee7cd2c2cd4b458fbf2b2dcc4604a3ed8c05 (diff)
downloadgo-7db566f9c26236f852fa0f980e6c4e8cf86890f3.tar.gz
go-7db566f9c26236f852fa0f980e6c4e8cf86890f3.zip
testing: fix reported caller name for funcs passed to Cleanup
Record the caller when Cleanup is called to report it with t.Log instead of unhelpful line in testing.go. Fixes #38800 Change-Id: I3136f5d92a0e5a48f8b32a2e13b2521bc91d72d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/232237 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/testing/testing.go')
-rw-r--r--src/testing/testing.go35
1 files changed, 25 insertions, 10 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index ed88ba51fb..90c15a2cff 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -338,15 +338,17 @@ const maxStackLen = 50
// common holds the elements common between T and B and
// captures common methods such as Errorf.
type common struct {
- mu sync.RWMutex // guards this group of fields
- output []byte // Output generated by test or benchmark.
- w io.Writer // For flushToParent.
- ran bool // Test or benchmark (or one of its subtests) was executed.
- failed bool // Test or benchmark has failed.
- skipped bool // Test of benchmark has been skipped.
- done bool // Test is finished and all subtests have completed.
- helpers map[string]struct{} // functions to be skipped when writing file/line info
- cleanup func() // optional function to be called at the end of the test
+ mu sync.RWMutex // guards this group of fields
+ output []byte // Output generated by test or benchmark.
+ w io.Writer // For flushToParent.
+ ran bool // Test or benchmark (or one of its subtests) was executed.
+ failed bool // Test or benchmark has failed.
+ skipped bool // Test of benchmark has been skipped.
+ done bool // Test is finished and all subtests have completed.
+ helpers map[string]struct{} // functions to be skipped when writing file/line info
+ cleanup func() // optional function to be called at the end of the test
+ cleanupName string // Name of the cleanup function.
+ cleanupPc []uintptr // The stack trace at the point where Cleanup was called.
chatty bool // A copy of the chatty flag.
finished bool // Test function has completed.
@@ -426,6 +428,10 @@ func (c *common) frameSkip(skip int) runtime.Frame {
var firstFrame, prevFrame, frame runtime.Frame
for more := true; more; prevFrame = frame {
frame, more = frames.Next()
+ if frame.Function == c.cleanupName {
+ frames = runtime.CallersFrames(c.cleanupPc)
+ continue
+ }
if firstFrame.PC == 0 {
firstFrame = frame
}
@@ -789,12 +795,21 @@ func (c *common) Cleanup(f func()) {
c.mu.Lock()
defer c.mu.Unlock()
oldCleanup := c.cleanup
+ oldCleanupPc := c.cleanupPc
c.cleanup = func() {
if oldCleanup != nil {
- defer oldCleanup()
+ defer func() {
+ c.cleanupPc = oldCleanupPc
+ oldCleanup()
+ }()
}
+ c.cleanupName = callerName(0)
f()
}
+ var pc [maxStackLen]uintptr
+ // Skip two extra frames to account for this function and runtime.Callers itself.
+ n := runtime.Callers(2, pc[:])
+ c.cleanupPc = pc[:n]
}
var tempDirReplacer struct {