-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgolden.go
394 lines (338 loc) · 10.4 KB
/
golden.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright (c) 2019-2024 Vasiliy Vasilyuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package golden
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"
"github.com/stretchr/testify/assert"
)
// TestingTB is the interface common to T and B.
type TestingTB interface {
Name() string
Logf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
FailNow()
Fail()
}
type testingHelper interface {
Helper()
}
// Tool implements the basic logic of working with golden files.
// All functionality is implemented through a non-mutating state
// machine, which at a certain point in time can perform an action
// on the basis of the state or change any parameter by creating
// a new copy of the state.
type Tool struct {
test TestingTB
dir string
fileMode os.FileMode
modeDir os.FileMode
target target
flag *bool
prefix string
extension string
// want it stores manually set expected data, if it is nil, then the
// data will be read from the files, otherwise the value from this
// field will be taken.
want []byte
mkdirAll func(path string, perm os.FileMode) error
readFile func(filename string) ([]byte, error)
remove func(name string) error
stat func(name string) (os.FileInfo, error)
writeFile func(filename string, data []byte, perm os.FileMode) error
}
// tool object with predefined parameters intended for use in global
// functions that provide a simplified api for convenient interaction
// with the functionality of the package.
var _golden = Tool{
// dir testdata is the directory for test data already accepted
// in the standard library, which is also ignored by standard
// go tools and should not change in your tests.
dir: "testdata",
fileMode: 0644,
modeDir: 0755,
target: Golden,
mkdirAll: os.MkdirAll,
readFile: ioutil.ReadFile,
remove: os.Remove,
stat: os.Stat,
writeFile: ioutil.WriteFile,
}
const updateEnvName = "GOLDEN_UPDATE"
func getUpdateEnv() bool {
env := os.Getenv(updateEnvName)
if env == "" {
env = strconv.FormatBool(false)
}
is, err := strconv.ParseBool(env)
if err != nil {
const msg = "cannot parse flag %q, error: %v"
panic(fmt.Sprintf(msg, updateEnvName, err))
}
return is
}
func init() {
_golden.flag = flag.Bool("getUpdateEnv", getUpdateEnv(), "update test golden files")
}
// Assert is a tool to compare the actual value obtained in the test and
// the value from the golden file. Also, built-in functionality for
// updating golden files using the command line flag.
func Assert(t TestingTB, got []byte) {
if h, ok := t.(testingHelper); ok {
h.Helper()
}
SetTest(t).Assert(got)
}
// Equal is a tool to compare the actual value obtained in the test and
// the value from the golden file. Also, built-in functionality for
// updating golden files using the command line flag.
func Equal(t TestingTB, got []byte) Conclusion {
if h, ok := t.(testingHelper); ok {
h.Helper()
}
return SetTest(t).Equal(got)
}
// Read is a functional for reading both input and golden files using
// the appropriate target.
func Read(t TestingTB) []byte {
return SetTest(t).SetTarget(Input).Read()
}
// Run is a functional that automates the process of reading the input file
// of the test bytes and the execution of the input function of testing and
// checking the results.
func Run(t TestingTB, do func(input []byte) (outcome []byte, err error)) {
SetTest(t).Run(do)
}
// SetTest is a mechanism to create a new copy of the base Tool object for
// advanced use. This method replaces the constructor for the Tool structure.
func SetTest(t TestingTB) Tool {
return _golden.SetTest(t)
}
// SetWant a place to set the expected want manually.
// want value it stores manually set expected data, if it is nil,
// then the data will be read from the files, otherwise the value
// from this field will be taken.
func SetWant(t TestingTB, bs []byte) Tool {
return _golden.SetTest(t).SetWant(bs)
}
// Assert is a tool to compare the actual value obtained in the test and
// the value from the golden file. Also, built-in functionality for
// updating golden files using the command line flag.
func (t Tool) Assert(got []byte) {
t.Update(got)
if h, ok := t.test.(testingHelper); ok {
h.Helper()
}
t.Equal(got).FailNow()
}
// Equal is a tool to compare the actual value obtained in the test and
// the value from the golden file. Also, built-in functionality for
// updating golden files using the command line flag.
func (t Tool) Equal(got []byte) Conclusion {
t.Update(got)
if h, ok := t.test.(testingHelper); ok {
h.Helper()
}
want := t.SetTarget(Golden).Read()
if want == nil {
want = []byte(fmt.Sprintf("%#v", want))
}
if got == nil {
got = []byte(fmt.Sprintf("%#v", got))
}
i := new(interceptor)
c := newConclusion(t.test)
c.successful = assert.Equal(i, string(want), string(got))
c.diff = i
return c
}
// JSONEq is a tool to compare the actual JSON value obtained in the test and
// the value from the golden file. Also, built-in functionality for
// updating golden files using the command line flag.
func (t Tool) JSONEq(got string) Conclusion {
if h, ok := t.test.(testingHelper); ok {
h.Helper()
}
return t.jsonEqual(got)
}
func (t Tool) jsonEqual(got string) conclusion {
t.setExtension("json").update(func() []byte {
return []byte(jsonFormatter(t.test, got))
})
want := t.setExtension("json").SetTarget(Golden).Read()
i := new(interceptor)
c := newConclusion(t.test)
c.successful = assert.JSONEq(i, string(want), string(got))
c.diff = i
return c
}
// JSONEq is a tool to compare the actual JSON value obtained in the test and
// the value from the golden file. Also, built-in functionality for
// updating golden files using the command line flag.
func JSONEq(tb TestingTB, got string) Conclusion {
if h, ok := tb.(testingHelper); ok {
h.Helper()
}
return _golden.SetTest(tb).jsonEqual(got)
}
// Read is a functional for reading both input and golden files using
// the appropriate target.
func (t Tool) Read() (bs []byte) {
if t.want != nil {
t.test.Logf("golden: read the value from the want field")
bs = make([]byte, len(t.want))
copy(bs, t.want)
return bs
}
bs, err := t.readFile(t.path())
if os.IsNotExist(err) {
const f = "golden: read the value of nil since it is not found file: %s"
t.test.Logf(f, t.path())
return nil
} else if err != nil {
t.test.Fatalf("golden: %s", err)
}
return bs
}
// Run is a functional that automates the process of reading the input file
// of the test bytes and the execution of the input function of testing and
// checking the results.
func (t Tool) Run(do func(input []byte) (got []byte, err error)) {
bs, err := do(t.SetTarget(Input).Read())
t.noError(err)
t.Assert(bs)
}
// SetPrefix a prefix value setter.
func (t Tool) SetPrefix(prefix string) Tool {
t.prefix = rewrite(prefix)
return t
}
// SetTarget a target value setter.
func (t Tool) SetTarget(tar target) Tool {
t.target = tar
return t
}
// SetTest a test value setter in the call chain must be used first
// to prevent abnormal situations when using other methods.
func (t Tool) SetTest(tb TestingTB) Tool {
t.test = tb
return t
}
// SetWant a place to set the expected want manually.
// want value it stores manually set expected data, if it is nil,
// then the data will be read from the files, otherwise the value
// from this field will be taken.
func (t Tool) SetWant(bs []byte) Tool {
t.want = nil
if bs != nil {
t.want = make([]byte, len(bs))
copy(t.want, bs)
}
return t
}
// Update functional reviewer is the need to update the golden files
// and doing it.
func (t Tool) Update(bs []byte) {
t.update(func() []byte { return bs })
}
// write is a functional for writing both input and golden files using
// the appropriate target.
func (t Tool) write(bs []byte) {
path := t.path()
t.mkdir(filepath.Dir(path))
t.test.Logf("golden: start write to file: %s", path)
if bs == nil {
t.test.Logf("golden: nil value will not be written")
fileInfo, err := t.stat(path)
if err == nil && !fileInfo.IsDir() {
t.test.Logf("golden: current test bytes file will be deleted")
t.noError(t.remove(path))
}
if !os.IsNotExist(err) {
t.noError(err)
}
} else {
t.noError(t.writeFile(path, bs, t.fileMode))
}
}
// mkdir the mechanism to create the directory.
func (t Tool) mkdir(loc string) {
fileInfo, err := t.stat(loc)
switch {
case err != nil && os.IsNotExist(err):
t.test.Logf("golden: trying to create a directory: %q", loc)
err = t.mkdirAll(loc, t.modeDir)
case err == nil && !fileInfo.IsDir():
t.test.Errorf("golden: test dir is a file: %s", loc)
}
t.noError(err)
}
// noError fails the test if an err is not nil.
func (t Tool) noError(err error) {
if err != nil {
t.test.Fatalf("golden: %s", err)
}
}
// path is getter to get the path to the file containing the test data.
func (t Tool) path() (path string) {
format := "%s"
args := []interface{}{t.test.Name()}
if t.prefix != "" {
args = append(args, t.prefix)
}
if t.extension != "" {
args = append(args, t.extension)
}
// Add a target expansion. Always added last.
args = append(args, t.target.String())
// We add placeholders for the number of parameters excluding the name
// of the test to print all the parameters.
format += strings.Repeat(".%s", len(args)-1)
return filepath.Join(t.dir, fmt.Sprintf(format, args...))
}
func (t Tool) update(f func() []byte) {
if t.flag != nil && *t.flag && t.want == nil {
t.test.Logf("golden: updating file: %s", t.path())
t.write(f())
}
}
func (t Tool) setExtension(ext string) Tool {
t.extension = ext
return t
}
// rewrite rewrites a subname to having only printable characters and no white
// space.
func rewrite(str string) string {
bs := make([]byte, 0, len(str))
for _, b := range str {
switch {
case unicode.IsSpace(b):
bs = append(bs, '_')
case !strconv.IsPrint(b):
s := strconv.QuoteRune(b)
bs = append(bs, s[1:len(s)-1]...)
default:
bs = append(bs, string(b)...)
}
}
return string(bs)
}
func jsonFormatter(t TestingTB, str string) string {
var value interface{}
if err := json.Unmarshal([]byte(str), &value); err != nil {
const format = "Data (%q) needs to be valid json.\nJSON parsing error: %q"
assert.FailNow(t, fmt.Sprintf(format, str, err))
}
bs, err := json.MarshalIndent(value, "", "\t")
assert.NoError(t, err)
return string(bs)
}