Repository navigation
Expand file tree
/
Copy pathtest_data_diff.go
More file actions
142 lines (125 loc) · 3.61 KB
/
Copy pathtest_data_diff.go
File metadata and controls
142 lines (125 loc) · 3.61 KB
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
package testutil
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"text/template"
"github.com/go-test/deep"
"github.com/sergi/go-diff/diffmatchpatch"
"gopkg.in/yaml.v2"
)
// TestDataDiffer holds configuration for generating test diff
type TestDataDiffer struct {
PrettyDiff bool
UpdateFixtures bool
RejectPath string
}
// DiffTestYAML compares a YAML structure to a fixture on the filestystem.
func (td *TestDataDiffer) DiffTestYAML(path string, actualYAML string) error {
expectedYAML := ReadTestdata(path)
return td.diffTestYAML(path, actualYAML, expectedYAML)
}
// DiffTestYAMLTemplate compares a YAML structure to a parameterized fixture on the filestystem.
func (td *TestDataDiffer) DiffTestYAMLTemplate(path string, actualYAML string, params any) error {
file := filepath.Join("testdata", path)
t, err := template.New(path).ParseFiles(file)
if err != nil {
return fmt.Errorf("Failed to read YAML template from %s: %w", path, err)
}
var buf bytes.Buffer
err = t.Execute(&buf, params)
if err != nil {
return fmt.Errorf("Failed to build YAML from template %s: %w", path, err)
}
return td.diffTestYAML(path, actualYAML, buf.String())
}
func (td *TestDataDiffer) diffTestYAML(path, actualYAML, expectedYAML string) error {
actual, err := unmarshalYAML([]byte(actualYAML))
if err != nil {
return fmt.Errorf("Failed to unmarshal generated YAML: %w", err)
}
expected, err := unmarshalYAML([]byte(expectedYAML))
if err != nil {
return fmt.Errorf("Failed to unmarshal expected YAML: %w", err)
}
diff := deep.Equal(expected, actual)
if diff == nil {
return nil
}
td.storeActual(path, []byte(actualYAML))
e := fmt.Sprintf("YAML mismatches %s:", path)
for _, d := range diff {
e += fmt.Sprintf("\n %s", d)
}
return errors.New(e)
}
// DiffTestdata generates the diff for actual w.r.the file in path
func (td *TestDataDiffer) DiffTestdata(t *testing.T, path, actual string) {
t.Helper()
expected := ReadTestdata(path)
if actual == expected {
712C
return
}
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expected, actual, true)
diffs = dmp.DiffCleanupSemantic(diffs)
var diff string
if td.PrettyDiff {
diff = dmp.DiffPrettyText(diffs)
} else {
diff = dmp.PatchToText(dmp.PatchMake(diffs))
}
t.Errorf("mismatch: %s\n%s", path, diff)
td.storeActual(path, []byte(actual))
}
func (td *TestDataDiffer) storeActual(path string, actual []byte) {
if td.UpdateFixtures {
writeTestdata(path, actual)
}
if td.RejectPath != "" {
writeRejects(path, actual, td.RejectPath)
}
}
// ReadTestdata reads a file and returns the contents of that file as a string.
func ReadTestdata(fileName string) string {
file, err := os.Open(filepath.Join("testdata", fileName))
if err != nil {
panic(fmt.Sprintf("Failed to open expected output file: %v", err))
}
fixture, err := io.ReadAll(file)
if err != nil {
panic(fmt.Sprintf("Failed to read expected output file: %v", err))
}
return string(fixture)
}
func unmarshalYAML(data []byte) ([]interface{}, error) {
objs := make([]interface{}, 0)
rd := bytes.NewReader(data)
decoder := yaml.NewDecoder(rd)
for {
var obj interface{}
if err := decoder.Decode(&obj); err != nil {
if errors.Is(err, io.EOF) {
return objs, nil
}
return nil, err
}
objs = append(objs, obj)
}
}
func writeTestdata(fileName string, data []byte) {
p := filepath.Join("testdata", fileName)
if err := os.WriteFile(p, data, 0600); err != nil {
panic(err)
}
}
func writeRejects(origFileName string, data []byte, rejectPath string) {
p := filepath.Join(rejectPath, origFileName+".rej")
if err := os.WriteFile(p, data, 0600); err != nil {
panic(err)
}
}