/
matcher_snapshot.go
77 lines (59 loc) · 1.48 KB
/
matcher_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
package testing
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/txtar"
)
var updateSnapshots string
func init() {
updateSnapshots = os.Getenv("UPDATE_SNAPSHOTS")
}
func WithSnapshotUpdate() func(m *snapshotMatcher) {
return func(m *snapshotMatcher) {
m.update = true
}
}
func MatchSnapshot(name string, optionFuncs ...func(m *snapshotMatcher)) Matcher[*txtar.Archive] {
// testdata/__snapshots__/<name>.txtar
snapshotFilename := fmt.Sprintf("testdata/__snapshots__/%s.txtar", name)
snapshot, _ := os.ReadFile(snapshotFilename)
m := &snapshotMatcher{
filename: snapshotFilename,
expected: snapshot,
update: updateSnapshots == "ALL" || strings.Contains(updateSnapshots, name),
}
for _, fn := range optionFuncs {
fn(m)
}
return m
}
type snapshotMatcher struct {
filename string
expected []byte
update bool
}
func (s snapshotMatcher) Name() string {
return "MatchSnapshot"
}
func (s snapshotMatcher) Negative() bool {
return false
}
func (s snapshotMatcher) FormatExpected() string {
return string(s.expected)
}
func (s snapshotMatcher) FormatActual(a *txtar.Archive) string {
return string(txtar.Format(a))
}
func (s snapshotMatcher) Match(a *txtar.Archive) bool {
data := txtar.Format(a)
if s.update || len(s.expected) == 0 {
_ = os.MkdirAll(filepath.Dir(s.filename), os.ModePerm)
_ = os.WriteFile(s.filename, data, 0o644)
return true
}
return bytes.Equal(data, s.expected)
}
var _ ExpectedFormatter = &snapshotMatcher{}