Skip to content

Commit

Permalink
Merge branch 'master' into mauclair-mock-match-sprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeauclair committed Jun 25, 2024
2 parents 2b53603 + 7ecde95 commit 6147cf0
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/stretchr/testify/assert/yaml"
)

const stackFrameBufferSize = 10

//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_format.go.tmpl"

// TestingT is an interface wrapper around *testing.T
Expand Down Expand Up @@ -212,19 +214,26 @@ the problem actually occurred in calling code.*/
func CallerInfo() []string {

var pc uintptr
var ok bool
var file string
var line int
var name string

callers := []string{}
for i := 0; ; i++ {
pc, file, line, ok = runtime.Caller(i)
if !ok {
// The breaks below failed to terminate the loop, and we ran off the
// end of the call stack.
break
}
pcs := make([]uintptr, stackFrameBufferSize)
offset := 1
n := runtime.Callers(offset, pcs)
maybeMore := n == stackFrameBufferSize

if n == 0 {
return []string{}
}
frames := runtime.CallersFrames(pcs[:n])

for {
frame, more := frames.Next()
pc = frame.PC
file = frame.File
line = frame.Line

// This is a huge edge case, but it will panic if this is the case, see #180
if file == "<autogenerated>" {
Expand Down Expand Up @@ -263,6 +272,23 @@ func CallerInfo() []string {
isTest(name, "Example") {
break
}

if !more {
// We know we already have less than a buffer's worth of frames
if !maybeMore {
break
}
offset += stackFrameBufferSize
n = runtime.Callers(offset, pcs)
if n == 0 {
break
}

maybeMore = n == stackFrameBufferSize

frames = runtime.CallersFrames(pcs[:n])
}

}

return callers
Expand Down

0 comments on commit 6147cf0

Please sign in to comment.