-
Notifications
You must be signed in to change notification settings - Fork 3
/
matcher.go
159 lines (129 loc) · 3.46 KB
/
matcher.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
package goldga
import (
"bytes"
"errors"
"fmt"
"os"
"strconv"
"github.com/onsi/gomega/types"
"github.com/spf13/afero"
)
type Option func(*Matcher)
// WithDescription adds an optional description to the golden file, allowing multiple gold files per test.
func WithDescription(description string) Option {
return func(matcher *Matcher) {
if s, ok := matcher.Storage.(*SuiteStorage); ok {
s.Name = fmt.Sprintf("%s (%s)", s.Name, description)
}
}
}
// WithSerializer overrides the default serializer.
func WithSerializer(serializer Serializer) Option {
return func(matcher *Matcher) {
matcher.Serializer = serializer
}
}
// WithTransformer overrides the default transformer.
func WithTransformer(transformer Transformer) Option {
return func(matcher *Matcher) {
matcher.Transformer = transformer
}
}
// WithStorage overrides the default storage.
func WithStorage(storage Storage) Option {
return func(matcher *Matcher) {
matcher.Storage = storage
}
}
// WithDiffer overrides the default differ.
func WithDiffer(differ Differ) Option {
return func(matcher *Matcher) {
matcher.Differ = differ
}
}
func getUpdateFile() bool {
update, _ := strconv.ParseBool(os.Getenv("UPDATE_GOLDEN"))
return update
}
func Match(options ...Option) *Matcher {
m := &Matcher{
Serializer: DefaultSerializer,
Transformer: DefaultTransformer,
Storage: &SuiteStorage{
Path: getGinkgoPath(),
Name: getGinkgoTestName(),
Fs: defaultFs,
},
Differ: DefaultDiffer,
UpdateFile: getUpdateFile(),
}
for _, option := range options {
option(m)
}
return m
}
var _ types.GomegaMatcher = (*Matcher)(nil)
type Matcher struct {
Serializer Serializer
Transformer Transformer
Storage Storage
Differ Differ
UpdateFile bool
}
func (m *Matcher) Match(actual interface{}) (bool, error) {
actualContent, err := m.getActualContent(actual)
if err != nil {
return false, fmt.Errorf("failed to get actual content: %w", err)
}
expected, err := m.getExpectedContent()
if err != nil {
if !errors.Is(err, afero.ErrFileNotFound) {
return false, fmt.Errorf("failed to get expected content: %w", err)
}
if err := m.Storage.Write(actualContent); err != nil {
return false, fmt.Errorf("faield to write file: %w", err)
}
return true, nil
}
return bytes.Equal(expected, actualContent), nil
}
func (m *Matcher) getMessage(actual interface{}, message string) string {
expectedContent, err := m.getExpectedContent()
if err != nil {
panic(err)
}
actualContent, err := m.getActualContent(actual)
if err != nil {
panic(err)
}
return fmt.Sprintf("Expected %s match the golden file\n%s",
message,
m.Differ.Diff(expectedContent, actualContent))
}
func (m *Matcher) getExpectedContent() ([]byte, error) {
if m.UpdateFile {
return nil, afero.ErrFileNotFound
}
data, err := m.Storage.Read()
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
return data, nil
}
func (m *Matcher) getActualContent(actual interface{}) ([]byte, error) {
var buf bytes.Buffer
transformed, err := m.Transformer.Transform(actual)
if err != nil {
return nil, fmt.Errorf("transform error: %w", err)
}
if err := m.Serializer.Serialize(&buf, transformed); err != nil {
return nil, fmt.Errorf("serialize error: %w", err)
}
return buf.Bytes(), nil
}
func (m *Matcher) FailureMessage(actual interface{}) string {
return m.getMessage(actual, "to")
}
func (m *Matcher) NegatedFailureMessage(actual interface{}) string {
return m.getMessage(actual, "not to")
}