Skip to content

Commit

Permalink
- r move stack trace naming
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen committed Feb 12, 2025
1 parent 79c4946 commit e12833b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 57 deletions.
57 changes: 0 additions & 57 deletions approval_name.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,9 @@
package approvals

import (
"fmt"
"runtime"
"strings"

"github.com/approvals/go-approval-tests/core"
)

func getApprovalNameCreator() core.ApprovalNamerCreator {
return CreateTemplatedCustomNamerCreator("{TestSourceDirectory}/{ApprovalsSubdirectory}/{TestFileName}.{TestCaseName}.{ApprovedOrReceived}.{FileExtension}")
}

func getApprovalName(t core.Failable) (string, string) {
fileName, err := findFileName()
if err != nil {
t.Fatalf("approvals: could not find the test filename or approved files location")
return "", ""
}

var name = t.Name()
name = strings.ReplaceAll(name, "/", ".")

return name, fileName
}

// Walk the call stack, and try to find the test method that was executed.
// The test method is identified by looking for the test runner, which is
// *assumed* to be common across all callers. The test runner has a Name() of
// 'testing.tRunner'. The method immediately previous to this is the test
// method.
func findFileName() (string, error) {
pc := make([]uintptr, 100)
count := runtime.Callers(0, pc)
frames := runtime.CallersFrames(pc[:count])

var lastFrame, testFrame *runtime.Frame

for {
frame, more := frames.Next()
if !more {
break
}

if isTestRunner(&frame) {
testFrame = &frame
break
}
lastFrame = &frame
}

if !isTestRunner(testFrame) {
return "", fmt.Errorf("approvals: could not find the test method")
}

if lastFrame == nil {
return "", fmt.Errorf("approvals: could not find the last frame")
}

return lastFrame.File, nil
}

func isTestRunner(f *runtime.Frame) bool {
return f != nil && f.Function == "testing.tRunner" || f.Function == "testing.runExample"
}
62 changes: 62 additions & 0 deletions stack_trace_namer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package approvals

import (
"fmt"
"runtime"
"strings"

"github.com/approvals/go-approval-tests/core"
)

func getApprovalName(t core.Failable) (name string, fileName string) {
fileName, err := findFileName()
if err != nil {
t.Fatalf("approvals: could not find the test filename or approved files location")
return "", ""
}

name = t.Name()
name = strings.ReplaceAll(name, "/", ".")

return name, fileName
}

// Walk the call stack, and try to find the test method that was executed.
// The test method is identified by looking for the test runner, which is
// *assumed* to be common across all callers. The test runner has a Name() of
// 'testing.tRunner'. The method immediately previous to this is the test
// method.
func findFileName() (string, error) {
pc := make([]uintptr, 100)
count := runtime.Callers(0, pc)
frames := runtime.CallersFrames(pc[:count])

var lastFrame, testFrame *runtime.Frame

for {
frame, more := frames.Next()
if !more {
break
}

if isTestRunner(&frame) {
testFrame = &frame
break
}
lastFrame = &frame
}

if !isTestRunner(testFrame) {
return "", fmt.Errorf("approvals: could not find the test method")
}

if lastFrame == nil {
return "", fmt.Errorf("approvals: could not find the last frame")
}

return lastFrame.File, nil
}

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

0 comments on commit e12833b

Please sign in to comment.