-
Notifications
You must be signed in to change notification settings - Fork 0
/
golden.go
107 lines (98 loc) · 2.19 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
package golden
import (
"bytes"
"errors"
"flag"
"io/ioutil"
"os"
"testing"
)
var update = flag.Bool("update", false, "update golden files")
// Data represents the golden file attributes.
type Data struct {
Path string
Perm os.FileMode
Marsh Marshaller
Diff Differ
Transforms []Transformer
Update bool
}
func (d *Data) Add(t Transformer) *Data {
if t == nil {
return d
}
d.Transforms = append(d.Transforms, t)
return d
}
// File creates a new golden file.
func File(path string, opts ...Option) *Data {
d := &Data{
Marsh: DefaultMarshaller,
Diff: DefaultDiffer,
Transforms: []Transformer{},
Path: path,
Perm: 0644, // -rw-r--r--
Update: *update,
}
for _, opt := range opts {
opt(d)
}
return d
}
// Open opens a golden file. Closing of the file is the callers responsibility.
func (d *Data) Open() (*os.File, error) {
return os.OpenFile(d.Path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, d.Perm)
}
// Possible errors when invoking the Eq func.
var (
ErrNoPath = errors.New("no golden file path")
ErrNoMarshaller = errors.New("no marshaller")
ErrNoDiffer = errors.New("no differ")
ErrNotEqual = errors.New("not equal")
)
// Eq compares the value v to the contents of the golden file.
// If it's not equal, it returns the ErrNotEqual together with a diff string.
func (d *Data) Eq(v interface{}) (string, error) {
if d.Path == "" {
return "", ErrNoPath
}
if d.Marsh == nil {
return "", ErrNoMarshaller
}
if d.Diff == nil {
return "", ErrNoDiffer
}
m, err := d.Marsh(v)
if err != nil {
return "", err
}
if d.Update {
if err = ioutil.WriteFile(d.Path, m, d.Perm); err != nil {
return "", err
}
}
f, err := ioutil.ReadFile(d.Path)
if err != nil {
return "", err
}
for _, t := range d.Transforms {
m, f = t(m, f)
}
if eq := bytes.Equal(m, f); eq {
return "", nil
}
return d.Diff(m, f), ErrNotEqual
}
// Equals compares the value v to the contents of the golden file.
func (d *Data) Equals(t *testing.T, v interface{}) {
diff, err := d.Eq(v)
if err == nil {
return
}
switch err {
case ErrNotEqual:
t.Fatalf("golden file does not match the param\n%s", diff)
default:
t.Fatal(err)
}
}