| 1 |
package approvaltests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"io/ioutil" |
| 8 |
"os" |
| 9 |
"path" |
| 10 |
"runtime" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
type approvalName struct { |
| 15 |
pc uintptr |
| 16 |
fullName string |
| 17 |
name string |
| 18 |
fileName string |
| 19 |
fileLine int |
| 20 |
} |
| 21 |
|
| 22 |
func newApprovalName(pc uintptr, f *runtime.Func) (*approvalName, error) { |
| 23 |
namer := &approvalName{ |
| 24 |
pc: pc, |
| 25 |
fullName: f.Name(), |
| 26 |
} |
| 27 |
|
| 28 |
namer.fileName, namer.fileLine = f.FileLine(pc) |
| 29 |
|
| 30 |
splits := strings.Split(namer.fullName, ".") |
| 31 |
namer.name = splits[len(splits)-1] |
| 32 |
|
| 33 |
return namer, nil |
| 34 |
} |
| 35 |
|
| 36 |
// Walk the call stack, and try to find the test method that was executed. |
| 37 |
// The test method is identified by looking for the test runner, which is |
| 38 |
// *assumed* to be common across all callers. The test runner has a Name() of |
| 39 |
// 'testing.tRunner'. The method immediately previous to this is the test |
| 40 |
// method. |
| 41 |
func getApprovalName() (*approvalName, error) { |
| 42 |
pc := make([]uintptr, 100) |
| 43 |
count := runtime.Callers(0, pc) |
| 44 |
|
| 45 |
i := 0 |
| 46 |
var lastFunc *runtime.Func |
| 47 |
|
| 48 |
for ; i < count; i++ { |
| 49 |
lastFunc = runtime.FuncForPC(pc[i]) |
| 50 |
if isTestRunner(lastFunc) { |
| 51 |
break |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if i == 0 || !isTestRunner(lastFunc) { |
| 56 |
return nil, fmt.Errorf("approvals: could not find the test method") |
| 57 |
} |
| 58 |
|
| 59 |
testMethod := runtime.FuncForPC(pc[i-1]) |
| 60 |
return newApprovalName(pc[i-1], testMethod) |
| 61 |
} |
| 62 |
|
| 63 |
func isTestRunner(f *runtime.Func) bool { |
| 64 |
return f != nil && f.Name() == "testing.tRunner" || f.Name() == "testing.runExample" |
| 65 |
} |
| 66 |
|
| 67 |
func (s *approvalName) compare(approvalFile, receivedFile string, reader io.Reader) error { |
| 68 |
received, err := ioutil.ReadAll(reader) |
| 69 |
if err != nil { |
| 70 |
return err |
| 71 |
} |
| 72 |
|
| 73 |
// Ideally, this should only be written if |
| 74 |
// 1. the approval file does not exist |
| 75 |
// 2. the results differ |
| 76 |
err = s.dumpReceivedTestResult(received, receivedFile) |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
|
| 81 |
fh, err := os.Open(approvalFile) |
| 82 |
if err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
defer fh.Close() |
| 86 |
|
| 87 |
approved, err := ioutil.ReadAll(fh) |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
|
| 92 |
received = s.normalizeLineEndings(received) |
| 93 |
approved = s.normalizeLineEndings(approved) |
| 94 |
|
| 95 |
// The two sides are identical, nothing more to do. |
| 96 |
if bytes.Compare(received, approved) == 0 { |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
return fmt.Errorf("failed to approved %s", s.name) |
| 101 |
} |
| 102 |
|
| 103 |
func (s *approvalName) normalizeLineEndings(bs []byte) []byte { |
| 104 |
return bytes.Replace(bs, []byte("\r\n"), []byte("\n"), -1) |
| 105 |
} |
| 106 |
|
| 107 |
func (s *approvalName) dumpReceivedTestResult(bs []byte, receivedFile string) error { |
| 108 |
err := ioutil.WriteFile(receivedFile, bs, 0644) |
| 109 |
|
| 110 |
return err |
| 111 |
} |
| 112 |
|
| 113 |
func (s *approvalName) getFileName(extWithDot string, suffix string) string { |
| 114 |
if !strings.HasPrefix(extWithDot, ".") { |
| 115 |
extWithDot = fmt.Sprintf(".%s", extWithDot) |
| 116 |
} |
| 117 |
|
| 118 |
baseName := s.fileName |
| 119 |
baseWithoutExt := baseName[:len(baseName)-len(path.Ext(s.fileName))] |
| 120 |
|
| 121 |
return fmt.Sprintf("%s.%s.%s%s", baseWithoutExt, s.name, suffix, extWithDot) |
| 122 |
} |
| 123 |
|
| 124 |
func (s *approvalName) getReceivedFile(extWithDot string) string { |
| 125 |
return s.getFileName(extWithDot, "received") |
| 126 |
} |
| 127 |
|
| 128 |
func (s *approvalName) getApprovalFile(extWithDot string) string { |
| 129 |
return s.getFileName(extWithDot, "approved") |
| 130 |
} |