-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
cupaloy.go
105 lines (88 loc) · 3.25 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
package cupaloy
import (
"fmt"
"os"
"strings"
)
// 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...)
}
// 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...)
}
// Snapshot compares the given value to the it's previous value stored on the filesystem.
// An error containing a diff is returned if the snapshots do not match.
// Snapshot determines the snapshot file automatically from the name of the calling function.
func (c *Config) Snapshot(i ...interface{}) error {
return c.snapshot(getNameOfCaller(), i...)
}
// SnapshotMulti is identical to Snapshot but can be called multiple times from the same function.
// This is done by providing a unique snapshotId for each invocation.
func (c *Config) SnapshotMulti(snapshotID string, i ...interface{}) error {
snapshotName := fmt.Sprintf("%s-%s", getNameOfCaller(), snapshotID)
return c.snapshot(snapshotName, i...)
}
// SnapshotT is identical to Snapshot but gets the snapshot name using
// t.Name() and calls t.Fail() directly if the snapshots do not match.
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)
}
}
// 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, snapshot)
}
return fmt.Errorf("snapshot does not exist for test %s", 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, snapshot)
}
diff := diffSnapshots(prevSnapshot, snapshot)
return fmt.Errorf("snapshot not equal:\n%s", diff)
}