forked from hygraph/cupaloy-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cupaloy.go
198 lines (170 loc) · 6.02 KB
/
cupaloy.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
package cupaloy
import (
"fmt"
"os"
"strings"
"github.com/GraphCMS/cupaloy-json/v2/internal"
)
// New constructs a new, configured instance of cupaloy using the given
// Configurators applied to the default config.
func New(configurators ...Configurator) *Config {
return NewDefaultConfig().WithOptions(configurators...)
}
// Snapshot calls Snapshotter.Snapshot with the global config.
func Snapshot(i ...interface{}) error {
return Global.snapshot(getNameOfCaller(), i...)
}
// SnapshotJSON calls Snapshotter.SnapshotJSON with the global config.
func SnapshotJSON(i ...interface{}) error {
return Global.snapshotJSON(getNameOfCaller(), i...)
}
// SnapshotMulti calls Snapshotter.SnapshotMulti with the global config.
func SnapshotMulti(snapshotID string, i ...interface{}) error {
snapshotName := fmt.Sprintf("%s-%s", getNameOfCaller(), snapshotID)
return Global.snapshot(snapshotName, i...)
}
// SnapshotT calls Snapshotter.SnapshotT with the global config.
func SnapshotT(t TestingT, i ...interface{}) {
t.Helper()
Global.SnapshotT(t, i...)
}
// SnapshotJSONT calls Snapshotter.SnapshotJSONT with the global config.
func SnapshotJSONT(t TestingT, i ...interface{}) {
t.Helper()
Global.SnapshotJSONT(t, i...)
}
// Snapshot compares the given variable to its previous value stored on the filesystem.
// An error containing a diff is returned if the snapshots do not match, or if a new
// snapshot was created.
//
// Snapshot determines the snapshot file automatically from the name of the calling function.
// As a result it can be called at most once per function. If you want to call Snapshot
// multiple times in a function, if possible, instead collect the values and call Snapshot
// with all values at once. Otherwise see SnapshotMulti.
//
// If using snapshots in tests, prefer the SnapshotT function which fails the test
// directly, rather than requiring your to remember to check the error.
func (c *Config) Snapshot(i ...interface{}) error {
return c.snapshot(getNameOfCaller(), i...)
}
// SnapshotMulti is similar to Snapshot but can be called multiple times from the
// same function. This is possible by providing a unique id for each snapshot which is
// appended to the function name to form the snapshot name.
func (c *Config) SnapshotMulti(snapshotID string, i ...interface{}) error {
snapshotName := fmt.Sprintf("%s-%s", getNameOfCaller(), snapshotID)
return c.snapshot(snapshotName, i...)
}
// SnapshotT compares the given variable to the its previous value stored on the filesystem.
// The current test is failed (with error containing a diff) if the values do not match, or
// if a new snapshot was created.
//
// SnapshotT determines the snapshot file automatically from the name of the test (using
// the t.Name() function). As a result, SnapshotT can be called at most once per test.
// If you want to call SnapshotT multiple times in a test, if possible, instead collect the
// values and call SnapshotT with all values at once. Alternatively, use sub-tests and call
// SnapshotT once in each.
//
// If using snapshots in tests, SnapshotT is preferred over Snapshot and SnapshotMulti.
func (c *Config) SnapshotT(t TestingT, i ...interface{}) {
t.Helper()
if t.Failed() {
return
}
snapshotName := strings.Replace(t.Name(), "/", "-", -1)
err := c.snapshot(snapshotName, i...)
if err != nil {
if c.fatalOnMismatch {
t.Fatal(err)
return
}
t.Error(err)
}
}
// SnapshotJSONT is the same as SnapshotT but with better JSON comparison support
func (c *Config) SnapshotJSONT(t TestingT, i ...interface{}) {
t.Helper()
if t.Failed() {
return
}
snapshotName := strings.Replace(t.Name(), "/", "-", -1)
err := c.snapshotJSON(snapshotName, i...)
if err != nil {
if c.fatalOnMismatch {
t.Fatal(err)
return
}
t.Error(err)
}
}
// WithOptions returns a copy of an existing Config with additional Configurators applied.
// This can be used to apply a different option for a single call e.g.
// snapshotter.WithOptions(cupaloy.SnapshotSubdirectory("testdata")).SnapshotT(t, result)
// Or to modify the Global Config e.g.
// cupaloy.Global = cupaloy.Global.WithOptions(cupaloy.SnapshotSubdirectory("testdata"))
func (c *Config) WithOptions(configurators ...Configurator) *Config {
clonedConfig := c.clone()
for _, configurator := range configurators {
configurator(clonedConfig)
}
return clonedConfig
}
func (c *Config) snapshot(snapshotName string, i ...interface{}) error {
snapshot := takeSnapshot(i...)
prevSnapshot, err := c.readSnapshot(snapshotName)
if os.IsNotExist(err) {
if c.createNewAutomatically {
return c.updateSnapshot(snapshotName, prevSnapshot, snapshot)
}
return internal.ErrNoSnapshot{Name: snapshotName}
}
if err != nil {
return err
}
if snapshot == prevSnapshot || takeV1Snapshot(i...) == prevSnapshot {
// previous snapshot matches current value
return nil
}
if c.shouldUpdate() {
// updates snapshot to current value and upgrades snapshot format
return c.updateSnapshot(snapshotName, prevSnapshot, snapshot)
}
return internal.ErrSnapshotMismatch{
Diff: diffSnapshots(prevSnapshot, snapshot),
}
}
// Same as snapshot but optimized for deep comparing JSON data
func (c *Config) snapshotJSON(snapshotName string, i ...interface{}) error {
snapshot := takeSnapshot(i...)
prevSnapshot, err := c.readSnapshot(snapshotName)
if os.IsNotExist(err) {
if c.createNewAutomatically {
return c.updateSnapshot(snapshotName, prevSnapshot, snapshot)
}
return internal.ErrNoSnapshot{Name: snapshotName}
}
if err != nil {
return err
}
if snapshot == prevSnapshot || takeV1Snapshot(i...) == prevSnapshot {
// previous snapshot matches current value
return nil
}
if c.shouldUpdate() {
// updates snapshot to current value and upgrades snapshot format
return c.updateSnapshot(snapshotName, prevSnapshot, snapshot)
}
snap1, err := Unmarshal([]byte(prevSnapshot))
if err != nil {
return err
}
snap2, err := Unmarshal([]byte(snapshot))
if err != nil {
return err
}
if Equal(snap1, snap2) {
return nil
}
return internal.ErrSnapshotMismatch{
Diff: diffSnapshots(prevSnapshot, snapshot),
}
}