From 4b3b9e7b7b0c488a812dde8aeadcc2f009c638b9 Mon Sep 17 00:00:00 2001 From: Karn Date: Wed, 8 Jun 2022 19:32:25 +0530 Subject: [PATCH] R - move findFileName from FuncForPC to CallerFrames --- approval_name.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/approval_name.go b/approval_name.go index 5d93c71..dfb9145 100644 --- a/approval_name.go +++ b/approval_name.go @@ -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 {