-
Notifications
You must be signed in to change notification settings - Fork 0
/
bromide.go
131 lines (109 loc) · 2.89 KB
/
bromide.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
// Bromide is a snapshot library, designed to simplify managing snapshot tests.
//
// Snapshot tests are useful if inputs are large or change often.
package bromide
import (
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/cobbinma/bromide/internal"
"github.com/davecgh/go-spew/spew"
)
// Snapshot compares a given input against a reference value.
// The test will fail if the value does not match, or a new snapshot is created.
//
// If the test fails, use bromide to interactively review changes.
// ```
// $ bromide
// ````
func Snapshot[K any](t *testing.T, item K, options ...Option) {
t.Helper()
config := &config{}
for _, option := range options {
option(config)
}
dir := config.snapshotDir
if dir == "" {
wd, err := os.Getwd()
if err != nil {
t.Error("bromide: unable to get working directory")
t.Log(err.Error())
return
}
dir = fmt.Sprintf("%s/snapshots", wd)
}
title := ""
if config.title != "" {
title = "_" + config.title
}
name := internal.Sanitize(t.Name())
acceptedPath := fmt.Sprintf("%s/%s%s%s", dir, name, title, internal.Accepted.Extension())
pendingPath := fmt.Sprintf("%s/%s%s%s", dir, name, title, internal.Pending.Extension())
if err := os.MkdirAll(dir, 0755); err != nil {
t.Error("bromide: unable to create snapshot directory")
t.Log(err.Error())
return
}
incoming := serialize(item)
file, err := os.Open(acceptedPath)
if err != nil {
if !os.IsNotExist(err) {
t.Error("bromide: unable to open accepted snapshot")
t.Log(err.Error())
return
}
// test does not have accepted snapshot
file, err := os.OpenFile(pendingPath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
t.Error("bromide: unable to open pending snapshot")
t.Log(err.Error())
return
}
defer file.Close()
if _, err := file.WriteString(incoming); err != nil {
t.Error("bromide: unable to write pending snapshot")
t.Log(err.Error())
return
}
t.Log("new snapshot 📸")
t.Log("to accept snapshot run `bromide`")
if !config.passNew {
t.Fail()
}
return
}
defer file.Close()
// test has accepted snapshot
existing := new(strings.Builder)
if _, err := io.Copy(existing, file); err != nil {
t.Error("bromide: unable to copy accepted file")
t.Log(err.Error())
return
}
if existing.String() != incoming {
diff := internal.Diff(existing.String(), incoming)
t.Log("snapshot mismatch")
t.Log("\n" + diff)
t.Log("to update snapshot run `bromide`")
if err := os.WriteFile(pendingPath, []byte(incoming), 0644); err != nil {
t.Error("bromide: unable to write pending snapshot")
t.Log(err.Error())
return
}
t.Fail()
return
}
}
func serialize[K any](item K) string {
config := &spew.ConfigState{
Indent: " ",
SortKeys: true,
DisablePointerAddresses: true,
DisableCapacities: true,
SpewKeys: true,
DisableMethods: false,
}
return config.Sdump(item)
}