/
snapshot.go
294 lines (247 loc) · 6.29 KB
/
snapshot.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
package snaps
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"github.com/gkampitakis/go-snaps/internal/colors"
)
var (
testsRegistry = newRegistry()
_m = sync.RWMutex{}
endSequenceByteSlice = []byte(endSequence)
)
var (
addedMsg = colors.Sprint(colors.Green, updateSymbol+"Snapshot added")
updatedMsg = colors.Sprint(colors.Green, updateSymbol+"Snapshot updated")
)
type config struct {
filename string
snapsDir string
update *bool
}
// Update determines whether to update snapshots or not
//
// It respects if running on CI.
func Update(u bool) func(*config) {
return func(c *config) {
c.update = &u
}
}
// Specify folder name where snapshots are stored
//
// default: __snapshots__
//
// this doesn't change the file extension
func Filename(name string) func(*config) {
return func(c *config) {
c.filename = name
}
}
// Specify folder name where snapshots are stored
//
// default: __snapshots__
//
// Accepts absolute paths
func Dir(dir string) func(*config) {
return func(c *config) {
c.snapsDir = dir
}
}
// Create snaps with configuration
//
// e.g snaps.WithConfig(snaps.Filename("my_test")).MatchSnapshot(t, "hello world")
func WithConfig(args ...func(*config)) *config {
s := defaultConfig
for _, arg := range args {
arg(&s)
}
return &s
}
func handleError(t testingT, err any) {
t.Helper()
t.Error(err)
testEvents.register(erred)
}
/*
We track occurrence as in the same test we can run multiple snapshots
This also helps with keeping track with obsolete snaps
map[snap path]: map[testname]: <number of snapshots>
*/
type syncRegistry struct {
running map[string]map[string]int
cleanup map[string]map[string]int
sync.Mutex
}
// Returns the id of the test in the snapshot
// Form [<test-name> - <occurrence>]
func (s *syncRegistry) getTestID(snapPath, testName string) string {
s.Lock()
if _, exists := s.running[snapPath]; !exists {
s.running[snapPath] = make(map[string]int)
s.cleanup[snapPath] = make(map[string]int)
}
s.running[snapPath][testName]++
s.cleanup[snapPath][testName]++
c := s.running[snapPath][testName]
s.Unlock()
return fmt.Sprintf("[%s - %d]", testName, c)
}
// reset sets only the number of running registry for the given test to 0.
func (s *syncRegistry) reset(snapPath, testName string) {
s.Lock()
s.running[snapPath][testName] = 0
s.Unlock()
}
func newRegistry() *syncRegistry {
return &syncRegistry{
running: make(map[string]map[string]int),
cleanup: make(map[string]map[string]int),
Mutex: sync.Mutex{},
}
}
// getPrevSnapshot scans file searching for a snapshot matching the given testID and returns
// the snapshot with the line where is located inside the file.
//
// If not found returns errSnapNotFound error.
func getPrevSnapshot(testID, snapPath string) (string, int, error) {
_m.RLock()
defer _m.RUnlock()
f, err := os.ReadFile(snapPath)
if err != nil {
return "", -1, errSnapNotFound
}
lineNumber := 1
tid := []byte(testID)
s := snapshotScanner(bytes.NewReader(f))
for s.Scan() {
l := s.Bytes()
if !bytes.Equal(l, tid) {
lineNumber++
continue
}
var snapshot strings.Builder
for s.Scan() {
line := s.Bytes()
if bytes.Equal(line, endSequenceByteSlice) {
return strings.TrimSuffix(snapshot.String(), "\n"), lineNumber, nil
}
snapshot.Write(line)
snapshot.WriteByte('\n')
}
}
if err := s.Err(); err != nil {
return "", -1, err
}
return "", -1, errSnapNotFound
}
func addNewSnapshot(testID, snapshot, snapPath string) error {
if err := os.MkdirAll(filepath.Dir(snapPath), os.ModePerm); err != nil {
return err
}
f, err := os.OpenFile(snapPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
defer f.Close()
_, err = fmt.Fprintf(f, "\n%s\n%s\n---\n", testID, snapshot)
return err
}
func updateSnapshot(testID, snapshot, snapPath string) error {
// When t.Parallel a test can override another snapshot as we dump
// all snapshots
_m.Lock()
defer _m.Unlock()
f, err := os.OpenFile(snapPath, os.O_RDWR, os.ModePerm)
if err != nil {
return err
}
defer f.Close()
tid := []byte(testID)
var updatedSnapFile bytes.Buffer
i, err := f.Stat()
if err == nil {
updatedSnapFile.Grow(int(i.Size()))
}
s := snapshotScanner(f)
for s.Scan() {
b := s.Bytes()
updatedSnapFile.Write(b)
updatedSnapFile.WriteByte('\n')
if !bytes.Equal(b, tid) {
continue
}
removeSnapshot(s)
// add new snapshot
updatedSnapFile.WriteString(snapshot)
updatedSnapFile.WriteByte('\n')
updatedSnapFile.Write(endSequenceByteSlice)
updatedSnapFile.WriteByte('\n')
}
if err := s.Err(); err != nil {
return err
}
return overwriteFile(f, updatedSnapFile.Bytes())
}
func overwriteFile(f *os.File, b []byte) error {
f.Truncate(0)
f.Seek(0, io.SeekStart)
_, err := f.Write(b)
return err
}
func removeSnapshot(s *bufio.Scanner) {
for s.Scan() {
// skip until ---
if bytes.Equal(s.Bytes(), endSequenceByteSlice) {
break
}
}
}
/*
Returns the path for snapshots
- if no config provided returns the directory where tests are running
- if snapsDir is relative path just gets appended to directory where tests are running
- if snapsDir is absolute path then we are returning this path
and for the filename
- if no config provided we use the test file name with `.snap` extension
- if filename provided we return the filename with `.snap` extension
Returns the relative path of the caller and the snapshot path.
*/
func snapshotPath(c *config) (string, string) {
// skips current func, the wrapper match* and the exported Match* func
callerPath := baseCaller(3)
dir := c.snapsDir
if !filepath.IsAbs(dir) {
dir = filepath.Join(filepath.Dir(callerPath), c.snapsDir)
}
filename := c.filename
if filename == "" {
base := filepath.Base(callerPath)
filename = strings.TrimSuffix(base, filepath.Ext(base))
}
snapPath := filepath.Join(dir, filename+snapsExt)
snapPathRel, _ := filepath.Rel(callerPath, snapPath)
return snapPath, snapPathRel
}
func unescapeEndChars(s string) string {
ss := strings.Split(s, "\n")
for idx, s := range ss {
if s == "/-/-/-/" {
ss[idx] = endSequence
}
}
return strings.Join(ss, "\n")
}
func escapeEndChars(s string) string {
ss := strings.Split(s, "\n")
for idx, s := range ss {
if s == endSequence {
ss[idx] = "/-/-/-/"
}
}
return strings.Join(ss, "\n")
}