mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
430cee7cd2
commit
7db566f9c2
3 changed files with 38 additions and 10 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue