Skip to content

Commit

Permalink
Merge pull request #1 from karngyan/caller-frames
Browse files Browse the repository at this point in the history
R - move findFileName from FuncForPC to CallerFrames
  • Loading branch information
aleclerc-cio authored Mar 26, 2024
2 parents 32d5677 + 4b3b9e7 commit 312e543
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions approval_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,32 @@ func NewApprovalName(name string, fileName string) ApprovalName {
func findFileName() (*string, error) {
pc := make([]uintptr, 100)
count := runtime.Callers(0, pc)
frames := runtime.CallersFrames(pc[:count])

i := 0
var lastFunc *runtime.Func
var lastFrame, testFrame *runtime.Frame

for ; i < count; i++ {
lastFunc = runtime.FuncForPC(pc[i])
if isTestRunner(lastFunc) {
for {
frame, more := frames.Next()
if !more {
break
}

if isTestRunner(&frame) {
testFrame = &frame
break
}
lastFrame = &frame
}
testMethodPtr := pc[i-1]
testMethod := runtime.FuncForPC(testMethodPtr)
var fileName, _ = testMethod.FileLine(testMethodPtr)

if i == 0 || !isTestRunner(lastFunc) {
if !isTestRunner(testFrame) {
return nil, fmt.Errorf("approvals: could not find the test method")
}
return &fileName, nil

return &lastFrame.File, nil
}

func isTestRunner(f *runtime.Func) bool {
return f != nil && f.Name() == "testing.tRunner" || f.Name() == "testing.runExample"
func isTestRunner(f *runtime.Frame) bool {
return f != nil && f.Function == "testing.tRunner" || f.Function == "testing.runExample"
}

func (s *ApprovalName) compare(approvalFile, receivedFile string, reader io.Reader) error {
Expand Down

0 comments on commit 312e543

Please sign in to comment.