Debian Codegraph @

goldie_test.go

raw symbols SCIP
1 package goldie
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "os"
7 "path/filepath"
8 "testing"
9 "time"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 )
14
15 func TestGoldenFileName(t *testing.T) {
16 tests := map[string]struct {
17 name string
18 options []Option
19 expected string
20 }{
21 "using defaults": {
22 name: "example",
23 expected: fmt.Sprintf("%s/%s%s", defaultFixtureDir, "example", defaultFileNameSuffix),
24 },
25 "with custom suffix": {
26 name: "example",
27 options: []Option{
28 WithNameSuffix(".txt"),
29 },
30 expected: fmt.Sprintf("%s/%s%s", defaultFixtureDir, "example", ".txt"),
31 },
32 "with custom fixture dir": {
33 name: "example",
34 options: []Option{
35 WithFixtureDir("fixtures"),
36 },
37 expected: fmt.Sprintf("%s/%s%s", "fixtures", "example", defaultFileNameSuffix),
38 },
39 "using test name for dir": {
40 name: "example",
41 options: []Option{
42 WithTestNameForDir(true),
43 },
44 expected: fmt.Sprintf("%s/%s/%s%s", defaultFixtureDir, t.Name(), "example", defaultFileNameSuffix),
45 },
46 "using sub test name for dir": {
47 name: "example",
48 options: []Option{
49 WithSubTestNameForDir(true),
50 },
51 expected: fmt.Sprintf("%s/%s/%s%s", defaultFixtureDir, "using_sub_test_name_for_dir", "example", defaultFileNameSuffix),
52 },
53 }
54
55 for name, test := range tests {
56 t.Run(name, func(t *testing.T) {
57 g := New(t, test.options...)
58 assert.Equal(t, test.expected, g.GoldenFileName(t, test.name))
59 })
60 }
61 }
62
63 func TestEnsureDir(t *testing.T) {
64 tests := map[string]struct {
65 dir string
66 shouldExist bool
67 fileExist bool
68 err interface{}
69 }{
70 "with existing directory": {
71 dir: "example1",
72 shouldExist: true,
73 err: nil,
74 },
75 "without existing directory": {
76 dir: "example2",
77 shouldExist: false,
78 fileExist: false,
79 err: nil,
80 },
81 "with existing deep directory structure": {
82 dir: "now/still/works",
83 shouldExist: true,
84 err: nil,
85 },
86 "error, fixture directory is a file": {
87 dir: "this/will/not",
88 shouldExist: false,
89 fileExist: true,
90 err: newErrFixtureDirectoryIsFile(filepath.Join(os.TempDir(), "this/will/not")),
91 },
92 }
93
94 for name, test := range tests {
95 t.Run(name, func(t *testing.T) {
96 g := New(t)
97 target := filepath.Join(os.TempDir(), test.dir)
98
99 if test.shouldExist {
100 err := os.MkdirAll(target, g.dirPerms)
101 assert.Nil(t, err)
102 }
103
104 if test.fileExist {
105 err := os.MkdirAll(filepath.Dir(target), g.dirPerms)
106 assert.Nil(t, err)
107
108 f, err := os.Create(target)
109 require.NoError(t, err)
110 err = f.Close()
111 assert.Nil(t, err)
112 }
113
114 err := g.ensureDir(target)
115 assert.Equal(t, test.err, err)
116 if err != nil {
117 return
118 }
119
120 s, err := os.Stat(target)
121 assert.Nil(t, err)
122 assert.True(t, s.IsDir())
123 })
124 }
125 }
126
127 // TODO: This test could use a little <3. It should test some more negative
128 // cases.
129 func TestUpdate(t *testing.T) {
130 tests := map[string]struct {
131 name string
132 data []byte
133 err error
134 }{
135 "successful update": {
136 name: "abc",
137 data: []byte("some example data"),
138 err: nil,
139 },
140 }
141
142 for name, test := range tests {
143 t.Run(name, func(t *testing.T) {
144 g := New(t)
145 err := g.Update(t, test.name, test.data)
146 assert.Equal(t, test.err, err)
147
148 data, err := ioutil.ReadFile(g.GoldenFileName(t, test.name))
149 assert.Nil(t, err)
150 assert.Equal(t, test.data, data)
151
152 err = os.RemoveAll(g.fixtureDir)
153 assert.Nil(t, err)
154 })
155 }
156 }
157
158 func TestDiffEngines(t *testing.T) {
159 type engine struct {
160 engine DiffEngine
161 diff string
162 }
163
164 tests := map[string]struct {
165 actual string
166 expected string
167 engine engine
168 }{
169 "simple": {
170 actual: "Lorem ipsum dolor.",
171 expected: "Lorem dolor sit amet.",
172 engine: engine{
173 engine: Simple,
174 diff: `Expected: Lorem dolor sit amet.
175 Got: Lorem ipsum dolor.`},
176 },
177 "classic": {
178 actual: "Lorem ipsum dolor.",
179 expected: "Lorem dolor sit amet.",
180 engine: engine{
181 engine: ClassicDiff,
182 diff: `--- Expected
183 +++ Actual
184 @@ -1 +1 @@
185 -Lorem dolor sit amet.
186 +Lorem ipsum dolor.
187 `},
188 },
189 "colored": {
190 actual: "Lorem ipsum dolor.",
191 expected: "Lorem dolor sit amet.",
192 engine: engine{
193 engine: ColoredDiff,
194 diff: "Lorem \x1b[31mipsum \x1b[0mdolor\x1b[32m sit amet\x1b[0m.",
195 },
196 },
197 }
198
199 for name, test := range tests {
200 t.Run(name, func(t *testing.T) {
201 assert.Equal(
202 t,
203 test.engine.diff,
204 Diff(test.engine.engine, test.actual, test.expected),
205 )
206 })
207 }
208
209 }
210
211 func TestCleanFunction(t *testing.T) {
212 savedCleanState := *clean
213 *clean = false
214 savedUpdateState := *update
215 *update = true
216 ts = time.Now()
217
218 sampleData := []byte("sample data")
219 fixtureDir := "test-fixtures"
220 fixtureSubDirA := fixtureDir + "/a"
221 fixtureSubDirB := fixtureDir + "/b"
222 suffix := ".golden"
223
224 // The first time running go test, with -update, without -clean
225 firstTests := []struct {
226 fixtureDirWithSub string
227 filePrefix string
228 }{
229 {fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a1"},
230 {fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a2"},
231 {fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b1"},
232 {fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b2"},
233 }
234
235 for i, tt := range firstTests {
236 g := New(t,
237 WithFixtureDir(tt.fixtureDirWithSub),
238 WithNameSuffix(suffix),
239 )
240
241 t.Run(fmt.Sprint(i), func(t *testing.T) {
242 g.Assert(t, tt.filePrefix, sampleData)
243 })
244
245 fullPath := fmt.Sprintf("%s%s",
246 filepath.Join(tt.fixtureDirWithSub, tt.filePrefix),
247 suffix,
248 )
249
250 _, err := os.Stat(fullPath)
251 assert.Nil(t, err)
252 }
253
254 *clean = true
255 ts = time.Now()
256
257 // The second time running go test, with -update and -clean
258 secondTests := []struct {
259 fixtureDirWithSub string
260 filePrefix string
261 }{
262 {fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a3"},
263 {fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a4"},
264 {fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b3"},
265 {fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b4"},
266 }
267
268 for i, tt := range secondTests {
269 g := New(t,
270 WithFixtureDir(tt.fixtureDirWithSub),
271 WithNameSuffix(suffix),
272 )
273
274 t.Run(fmt.Sprint(i), func(t *testing.T) {
275 g.Assert(t, tt.filePrefix, sampleData)
276 })
277
278 fullPath := fmt.Sprintf("%s%s",
279 filepath.Join(tt.fixtureDirWithSub, tt.filePrefix),
280 suffix,
281 )
282
283 _, err := os.Stat(fullPath)
284 assert.Nil(t, err)
285 }
286
287 // make sure output files of the first run doesnt exist
288 for _, tt := range firstTests {
289 fullPath := fmt.Sprintf("%s%s",
290 filepath.Join(tt.fixtureDirWithSub, tt.filePrefix),
291 suffix,
292 )
293
294 _, err := os.Stat(fullPath)
295 assert.Error(t, err)
296 assert.True(t, os.IsNotExist(err))
297 }
298
299 err := os.RemoveAll(fixtureDir)
300 assert.Nil(t, err)
301 *clean = savedCleanState
302 *update = savedUpdateState
303 }