Debian Codegraph @

v2/assertions.go

raw symbols SCIP
1 package goldie
2
3 import (
4 "bytes"
5 "encoding/json"
6 "encoding/xml"
7 "errors"
8 "fmt"
9 "io/ioutil"
10 "os"
11 "testing"
12 "text/template"
13 )
14
15 // Assert compares the actual data received with the expected data in the
16 // golden files. If the update flag is set, it will also update the golden
17 // file.
18 //
19 // `name` refers to the name of the test and it should typically be unique
20 // within the package. Also it should be a valid file name (so keeping to
21 // `a-z0-9\-\_` is a good idea).
22 func (g *Goldie) Assert(t *testing.T, name string, actualData []byte) {
23 t.Helper()
24 if *update {
25 err := g.Update(t, name, actualData)
26 if err != nil {
27 t.Error(err)
28 t.FailNow()
29 }
30 }
31
32 err := g.compare(t, name, actualData)
33 if err != nil {
34 {
35 var e *errFixtureNotFound
36 if errors.As(err, &e) {
37 t.Error(err)
38 t.FailNow()
39 return
40 }
41 }
42
43 {
44 var e *errFixtureMismatch
45 if errors.As(err, &e) {
46 t.Error(err)
47 return
48 }
49 }
50
51 t.Error(err)
52 }
53 }
54
55 // AssertJson compares the actual json data received with expected data in the
56 // golden files. If the update flag is set, it will also update the golden
57 // file.
58 //
59 // `name` refers to the name of the test and it should typically be unique
60 // within the package. Also it should be a valid file name (so keeping to
61 // `a-z0-9\-\_` is a good idea).
62 func (g *Goldie) AssertJson(t *testing.T, name string, actualJsonData interface{}) {
63 t.Helper()
64 js, err := json.MarshalIndent(actualJsonData, "", " ")
65
66 if err != nil {
67 t.Error(err)
68 t.FailNow()
69 }
70
71 g.Assert(t, name, normalizeLF(js))
72 }
73
74 // AssertXml compares the actual xml data received with expected data in the
75 // golden files. If the update flag is set, it will also update the golden
76 // file.
77 //
78 // `name` refers to the name of the test and it should typically be unique
79 // within the package. Also it should be a valid file name (so keeping to
80 // `a-z0-9\-\_` is a good idea).
81 func (g *Goldie) AssertXml(t *testing.T, name string, actualXmlData interface{}) {
82 t.Helper()
83 x, err := xml.MarshalIndent(actualXmlData, "", " ")
84
85 if err != nil {
86 t.Error(err)
87 t.FailNow()
88 }
89
90 g.Assert(t, name, normalizeLF(x))
91 }
92
93 // normalizeLF normalizes line feed character set across os (es)
94 // \r\n (windows) & \r (mac) into \n (unix)
95 func normalizeLF(d []byte) []byte {
96 // if empty / nil return as is
97 if len(d) == 0 {
98 return d
99 }
100 // replace CR LF \r\n (windows) with LF \n (unix)
101 d = bytes.Replace(d, []byte{13, 10}, []byte{10}, -1)
102 // replace CF \r (mac) with LF \n (unix)
103 d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
104 return d
105 }
106
107 // Assert compares the actual data received with the expected data in the
108 // golden files after executing it as a template with data parameter. If the
109 // update flag is set, it will also update the golden file. `name` refers to
110 // the name of the test and it should typically be unique within the package.
111 // Also it should be a valid file name (so keeping to `a-z0-9\-\_` is a good
112 // idea).
113 func (g *Goldie) AssertWithTemplate(t *testing.T, name string, data interface{}, actualData []byte) {
114 t.Helper()
115 if *update {
116 err := g.Update(t, name, actualData)
117 if err != nil {
118 t.Error(err)
119 t.FailNow()
120 }
121 }
122
123 err := g.compareTemplate(t, name, data, actualData)
124 if err != nil {
125 {
126 var e *errFixtureNotFound
127 if errors.As(err, &e) {
128 t.Error(err)
129 t.FailNow()
130 return
131 }
132 }
133
134 {
135 var e *errFixtureMismatch
136 if errors.As(err, &e) {
137 t.Error(err)
138 return
139 }
140 }
141
142 t.Error(err)
143 }
144 }
145
146 // compare is reading the golden fixture file and compare the stored data with
147 // the actual data.
148 func (g *Goldie) compare(t *testing.T, name string, actualData []byte) error {
149 expectedData, err := ioutil.ReadFile(g.GoldenFileName(t, name))
150
151 if err != nil {
152 if os.IsNotExist(err) {
153 return newErrFixtureNotFound()
154 }
155
156 return fmt.Errorf("expected %s to be nil", err.Error())
157 }
158
159 if !bytes.Equal(actualData, expectedData) {
160 msg := "Result did not match the golden fixture. Diff is below:\n\n"
161 actual := string(actualData)
162 expected := string(expectedData)
163
164 if g.diffFn != nil {
165 msg += g.diffFn(actual, expected)
166 } else {
167 msg += Diff(g.diffEngine, actual, expected)
168 }
169
170 return newErrFixtureMismatch(msg)
171 }
172
173 return nil
174 }
175
176 // compareTemplate is reading the golden fixture file and compare the stored
177 // data with the actual data.
178 func (g *Goldie) compareTemplate(t *testing.T, name string, data interface{}, actualData []byte) error {
179 expectedDataTmpl, err := ioutil.ReadFile(g.GoldenFileName(t, name))
180
181 if err != nil {
182 if os.IsNotExist(err) {
183 return newErrFixtureNotFound()
184 }
185
186 return fmt.Errorf("expected %s to be nil", err.Error())
187 }
188
189 missingKey := "error"
190 if g.ignoreTemplateErrors {
191 missingKey = "default"
192 }
193
194 tmpl, err := template.New("test").Option("missingkey=" + missingKey).Parse(string(expectedDataTmpl))
195 if err != nil {
196 return fmt.Errorf("expected %s to be nil", err.Error())
197 }
198
199 var expectedData bytes.Buffer
200 err = tmpl.Execute(&expectedData, data)
201 if err != nil {
202 return newErrMissingKey(fmt.Sprintf("Template error: %s", err.Error()))
203 }
204
205 if !bytes.Equal(actualData, expectedData.Bytes()) {
206 msg := "Result did not match the golden fixture. Diff is below:\n\n"
207 actual := string(actualData)
208 expected := expectedData.String()
209
210 if g.diffFn != nil {
211 msg += g.diffFn(actual, expected)
212 } else {
213 msg += Diff(g.diffEngine, actual, expected)
214 }
215
216 return newErrFixtureMismatch(msg)
217 }
218
219 return nil
220 }