-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexamples_test.go
More file actions
175 lines (132 loc) · 3.04 KB
/
Copy pathexamples_test.go
File metadata and controls
175 lines (132 loc) · 3.04 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
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
//go:build e2e
package testo
import (
"bufio"
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
)
var ignoreLineOrder = []string{
"02_hooks",
"06_parallel",
}
func TestExamples(t *testing.T) {
examples := collectExamples(t)
for _, path := range examples {
name := filepath.Base(path)
t.Run(name, func(t *testing.T) {
t.Parallel()
wantOutput := readFile(t, filepath.Join(path, "output.golden"))
haveOutput := runTest(t, path)
wantOutput = unifyOutput(wantOutput)
haveOutput = unifyOutput(haveOutput)
requireEqualLines(
t,
string(wantOutput),
string(haveOutput),
slices.Contains(ignoreLineOrder, name),
)
})
}
}
func lines(s string) []string {
// We store test output in files with UNIX line ending.
// But, since windows uses CR-LF line endings, we have to rebuild these strings
// to make line endings the same for further comparison.
scanner := bufio.NewScanner(strings.NewReader(s))
var lines []string
for scanner.Scan() {
lines = append(lines, strings.TrimSpace(scanner.Text()))
}
return lines
}
func requireEqualLines(t *testing.T, a, b string, ignoreOrder bool) {
t.Helper()
aLines, bLines := lines(a), lines(b)
if ignoreOrder {
slices.Sort(aLines)
slices.Sort(bLines)
}
if !slices.Equal(aLines, bLines) {
t.Fatalf(
"lines not equal:\n\n%s\n\n%s",
strings.Join(aLines, "\n"),
strings.Join(bLines, "\n"),
)
}
}
func collectExamples(t *testing.T) []string {
t.Helper()
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
examplesDir := filepath.Join(wd, "examples")
entries, err := os.ReadDir(examplesDir)
if err != nil {
t.Fatal(err)
}
examples := make([]string, 0, len(entries))
namePattern := regexp.MustCompile(`^\d\d_.+$`)
for _, entry := range entries {
if !entry.IsDir() {
continue
}
name := entry.Name()
if !namePattern.MatchString(name) {
continue
}
examples = append(examples, filepath.Join(examplesDir, name))
}
return examples
}
func unifyOutput(b []byte) []byte {
b = replaceDurations(b, []byte("?"))
b = replaceInners(b, []byte("inner_?"))
b = bytes.TrimSpace(b)
return b
}
func replaceInners(src, repl []byte) []byte {
re := regexp.MustCompile(`inner_\d`)
return re.ReplaceAll(src, repl)
}
func replaceDurations(src, repl []byte) []byte {
re := regexp.MustCompile(`[+-]?(?:\d+(?:\.\d+)?(?:ns|us|µs|ms|s|m|h))+`)
return re.ReplaceAll(src, repl)
}
func readFile(t *testing.T, path string) []byte {
t.Helper()
contents, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return contents
}
func runTest(t *testing.T, pkgPath string) []byte {
t.Helper()
cmd := exec.Command(
"go",
"test",
"-tags",
"example",
"-v",
"-count=1", // ignore cache and always run
"-parallel=1", // ensure output remain the same
pkgPath,
)
cmd.Env = os.Environ()
output, err := cmd.CombinedOutput()
if err != nil {
var errExec *exec.ExitError
if !errors.As(err, &errExec) || errExec.ExitCode() != 1 {
t.Fatalf("running go test: %v", err)
}
}
return output
}