-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79c4946
commit e12833b
Showing
2 changed files
with
62 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |