Repository navigation
Expand file tree
/
Copy pathtestdata.go
More file actions
350 lines (287 loc) · 8.77 KB
/
Copy pathtestdata.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
package got
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"github.com/dominicbarnes/got/codec"
"github.com/fatih/structtag"
"github.com/google/go-cmp/cmp"
)
var updateGolden bool
func init() {
flag.BoolVar(&updateGolden, "update-golden", false, "enable to update golden test files")
}
const tagName = "testdata"
// Load extracts the contents of dir into values which are structs annotated
// with the "testdata" struct tag.
//
// The main parameter of the struct tag will be a path to a file relative to the
// input directory.
//
// Fields with string or []byte as their types will be populated with the raw
// contents of the file.
//
// Struct values will be decoded using the file extension to map to a [Codec].
// For example, ".json" files can be processed using [JSONCodec] if it has been
// registered. Additional codecs (eg: YAML, TOML) can be registered if desired.
//
// Map values can be used to dynamically load the contents of a directory, in
// situations where you don't necessarily know all the files ahead of time.
//
// The defined map type must use string keys, otherwise it will return an error.
// The filename in the struct tag will then be treated as a glob pattern,
// populating the map with a key for each matched file (relative to the input
// directory).
//
// The values in the map can be either string, []byte or structs as described
// above.
func Load(t T, dir string, values ...any) {
t.Helper()
if err := loadDirs(context.TODO(), []string{dir}, values...); err != nil {
t.Fatal(err.Error())
}
}
// LoadDirs is the same as Load but accepts multiple input directories, which
// can be used to set up test cases from a common/shared location while allowing
// an individual test-case to include it's own specific configuration.
func LoadDirs(t T, dirs []string, values ...any) {
t.Helper()
if err := loadDirs(context.TODO(), dirs, values...); err != nil {
t.Fatal(err.Error())
}
}
// Assert ensures that all the fields within the struct values match what is on
// disk, using reflection to Load a fresh copy and then comparing the 2 structs
// using go-cmp to perform the equality check.
//
// When the "test.update-golden" flag is provided, the contents of each value
// struct will be persisted to disk instead. This allows any test to easily
// update their "golden files" and also do the assertion transparently.
func Assert(t T, dir string, values ...any) {
t.Helper()
if err := assert(context.TODO(), dir, values...); err != nil {
t.Fatal(err.Error())
}
}
func assert(ctx context.Context, dir string, values ...any) error {
if len(values) == 0 {
return errors.New("at least 1 value required")
}
for _, actual := range values {
if updateGolden {
if err := saveDir(dir, actual); err != nil {
return err
}
continue
}
expected := reflect.New(reflect.TypeOf(actual).Elem()).Interface()
if err := loadDirs(ctx, []string{dir}, expected); err != nil {
return fmt.Errorf("%T: %w", actual, err)
}
if !cmp.Equal(expected, actual) {
return errors.New(cmp.Diff(expected, actual))
}
}
return nil
}
// loads multiple input dirs into multiple output values
func loadDirs(ctx context.Context, inputs []string, outputs ...any) error {
if len(outputs) == 0 {
return errors.New("at least 1 output required")
}
for _, output := range outputs {
if err := loadDir(ctx, inputs, output); err != nil {
return err
}
}
return nil
}
// loads multiple input dirs into a single output value
func loadDir(_ context.Context, inputs []string, output any) error {
if output == nil {
return errors.New("output cannot be nil")
}
if k := reflect.TypeOf(output).Kind(); k != reflect.Ptr {
return fmt.Errorf("output must be a pointer, instead got %s", k)
}
typ := reflect.TypeOf(output).Elem()
val := reflect.ValueOf(output).Elem()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tags, err := structtag.Parse(string(field.Tag))
if err != nil {
return fmt.Errorf("%s: failed to parse struct tags: %w", field.Name, err)
}
tag, err := tags.Get(tagName)
if err != nil {
continue
} else if tag.Name == "" || tag.Name == "-" {
continue
}
for _, input := range inputs {
file := filepath.Join(input, tag.Name)
if isMap(field.Type) {
matches, err := filepath.Glob(file)
if err != nil {
return fmt.Errorf("%s: failed to list files %s: %w", field.Name, file, err)
}
m := reflect.MakeMap(field.Type)
for _, match := range matches {
rel, err := filepath.Rel(input, match)
if err != nil {
return fmt.Errorf("%s: failed to resolve file %s: %w", field.Name, match, err)
}
key := reflect.ValueOf(rel)
value := reflect.New(m.Type().Elem()).Elem()
if err := loadFile(match, field, value, tag); err != nil {
return err
}
m.SetMapIndex(key, value)
}
val.Field(i).Set(m)
continue
}
if err := loadFile(file, field, val.Field(i), tag); err != nil {
return err
}
}
}
return nil
}
// load a single struct field
func loadFile(file string, field reflect.StructField, value reflect.Value, tag *structtag.Tag) error {
f, err := openTagFile(file)
if err != nil {
return fmt.Errorf("%s: %w", field.Name, err)
} else if f == nil {
return nil
}
data, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("%s: failed to read file %s: %w", field.Name, file, err)
}
// raw types
if isBytes(value.Type()) {
value.SetBytes(data)
return nil
} else if isString(value.Type()) {
value.SetString(string(data))
return nil
}
ext := filepath.Ext(file)
codec, err := codec.Get(ext)
if err != nil {
return fmt.Errorf("%s: failed to get codec for file extension %q", field.Name, ext)
}
p := reflect.New(value.Type())
p.Elem().Set(value) // preserve any prior values
if err := codec.Unmarshal(data, p.Interface()); err != nil {
return fmt.Errorf("%s: failed to unmarshal %s: %w", field.Name, file, err)
}
value.Set(p.Elem()) // overwrite with the updated value
return nil
}
// persist an input struct to dir on disk
func saveDir(dir string, input any) error {
if input == nil {
return errors.New("input cannot be nil")
}
if k := reflect.TypeOf(input).Kind(); k != reflect.Ptr {
return fmt.Errorf("input must be a pointer, instead got %s", k)
}
typ := reflect.TypeOf(input).Elem()
val := reflect.ValueOf(input).Elem()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tags, err := structtag.Parse(string(field.Tag))
if err != nil {
return fmt.Errorf("%s: failed to parse struct tags: %w", field.Name, err)
}
tag, err := tags.Get(tagName)
if err != nil {
continue
} else if tag.Name == "" || tag.Name == "-" {
continue
}
if isMap(field.Type) {
iter := val.Field(i).MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
file := filepath.Join(dir, k.String())
if err := saveFile(file, field, v); err != nil {
return err
}
}
continue
}
file := filepath.Join(dir, tag.Name)
if err := saveFile(file, field, val.Field(i)); err != nil {
return err
}
}
return nil
}
// save a single field to disk
func saveFile(file string, field reflect.StructField, val reflect.Value) error {
data, err := encode(file, field, val)
if err != nil {
return fmt.Errorf("%s: failed to encode file %s: %w", field.Name, file, err)
}
if len(data) == 0 {
if err := os.Remove(file); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("%s: failed to delete file %s: %s", field.Name, file, err)
}
}
} else {
dir := filepath.Dir(file)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("%s: failed to create dir %s: %s", field.Name, dir, err)
}
if err := os.WriteFile(file, data, 0644); err != nil {
return fmt.Errorf("%s: failed to write file %s: %s", field.Name, file, err)
}
}
return nil
}
func encode(file string, field reflect.StructField, val reflect.Value) ([]byte, error) {
if val.IsZero() {
return nil, nil
} else if isBytes(val.Type()) {
return val.Bytes(), nil
} else if isString(val.Type()) {
return []byte(val.String()), nil
}
ext := filepath.Ext(file)
codec, err := codec.Get(ext)
if err != nil {
return nil, fmt.Errorf("%s: failed to get codec for file extension %q", field.Name, ext)
}
return codec.Marshal(val.Interface())
}
// open a file, suppressing "not found" errors
func openTagFile(file string) (*os.File, error) {
f, err := os.Open(file)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return f, nil
}
func isString(targetType reflect.Type) bool {
return targetType.Kind() == reflect.String
}
func isBytes(targetType reflect.Type) bool {
return targetType.Kind() == reflect.Slice && targetType.Elem().Kind() == reflect.Uint8
}
func isMap(targetType reflect.Type) bool {
return targetType.Kind() == reflect.Map && isString(targetType.Key())
}