-
Notifications
You must be signed in to change notification settings - Fork 22
/
approval_name.go
140 lines (113 loc) · 3.3 KB
/
approval_name.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package approvals
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
)
// ApprovalName struct.
type ApprovalName struct {
name string
fileName string
}
func getApprovalName(t Failable) *ApprovalName {
fileName, err := findFileName()
if err != nil {
t.Fatalf("approvals: could not find the test filename or approved files location")
return nil
}
var name = t.Name()
name = strings.ReplaceAll(name, "/", ".")
namer := NewApprovalName(name, *fileName)
return &namer
}
// NewApprovalName returns a new ApprovalName object.
func NewApprovalName(name string, fileName string) ApprovalName {
var namer = ApprovalName{
name: name,
fileName: fileName,
}
return namer
}
// 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)
i := 0
var lastFunc *runtime.Func
for ; i < count; i++ {
lastFunc = runtime.FuncForPC(pc[i])
if isTestRunner(lastFunc) {
break
}
}
testMethodPtr := pc[i-1]
testMethod := runtime.FuncForPC(testMethodPtr)
var fileName, _ = testMethod.FileLine(testMethodPtr)
if i == 0 || !isTestRunner(lastFunc) {
return nil, fmt.Errorf("approvals: could not find the test method")
}
return &fileName, nil
}
func isTestRunner(f *runtime.Func) bool {
return f != nil && f.Name() == "testing.tRunner" || f.Name() == "testing.runExample"
}
func (s *ApprovalName) compare(approvalFile, receivedFile string, reader io.Reader) error {
received, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
// Ideally, this should only be written if
// 1. the approval file does not exist
// 2. the results differ
err = s.dumpReceivedTestResult(received, receivedFile)
if err != nil {
return err
}
fh, err := os.Open(approvalFile)
if err != nil {
return err
}
defer fh.Close()
approved, err := ioutil.ReadAll(fh)
if err != nil {
return err
}
received = s.normalizeLineEndings(received)
approved = s.normalizeLineEndings(approved)
// The two sides are identical, nothing more to do.
if bytes.Equal(received, approved) {
return nil
}
return fmt.Errorf("failed to approved %s", s.name)
}
func (s *ApprovalName) normalizeLineEndings(bs []byte) []byte {
return bytes.Replace(bs, []byte("\r\n"), []byte("\n"), -1)
}
func (s *ApprovalName) dumpReceivedTestResult(bs []byte, receivedFile string) error {
err := ioutil.WriteFile(receivedFile, bs, 0644)
return err
}
func (s *ApprovalName) getFileName(extWithDot string, suffix string) string {
if !strings.HasPrefix(extWithDot, ".") {
extWithDot = fmt.Sprintf(".%s", extWithDot)
}
_, baseName := path.Split(s.fileName)
baseWithoutExt := baseName[:len(baseName)-len(path.Ext(s.fileName))]
filename := fmt.Sprintf("%s.%s.%s%s", baseWithoutExt, s.name, suffix, extWithDot)
return path.Join(defaultFolder, filename)
}
func (s *ApprovalName) getReceivedFile(extWithDot string) string {
return s.getFileName(extWithDot, "received")
}
func (s *ApprovalName) getApprovalFile(extWithDot string) string {
return s.getFileName(extWithDot, "approved")
}