Repository navigation
Expand file tree
/
Copy pathexample_compile_test.go
More file actions
270 lines (226 loc) · 6.47 KB
/
Copy pathexample_compile_test.go
File metadata and controls
270 lines (226 loc) · 6.47 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
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
package str
import (
"bytes"
"encoding/json"
"fmt"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
)
// TestExampleModuleUsesLocalLibrary prevents examples from silently testing a published major instead of this checkout.
func TestExampleModuleUsesLocalLibrary(t *testing.T) {
t.Parallel()
root, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
modulePath, err := readModulePath(root)
if err != nil {
t.Fatal(err)
}
command := exec.Command("go", "list", "-m", "-json", modulePath)
command.Dir = filepath.Join(root, "examples")
command.Env = append(os.Environ(), "GOWORK=off")
output, err := command.Output()
if err != nil {
t.Fatalf("resolve example dependency: %v", err)
}
var module struct {
Path string
Dir string
}
if err := json.Unmarshal(output, &module); err != nil {
t.Fatal(err)
}
if module.Path != modulePath || filepath.Clean(module.Dir) != filepath.Clean(root) {
t.Fatalf("examples resolved %q at %q, want %q at %q", module.Path, module.Dir, modulePath, root)
}
}
// TestGeneratedExamplesMatchDocumentedOutput guards its covered contract against regressions.
func TestGeneratedExamplesMatchDocumentedOutput(t *testing.T) {
t.Parallel()
root, err := os.Getwd()
if err != nil {
t.Fatalf("cannot determine module root: %v", err)
}
modulePath, err := readModulePath(root)
if err != nil {
t.Fatal(err)
}
examplesDir := "examples"
entries, err := os.ReadDir(filepath.Join(root, examplesDir))
if err != nil {
t.Fatalf("cannot read examples directory: %v", err)
}
exampleCount := 0
for _, entry := range entries {
if !entry.IsDir() {
continue
}
exampleCount++
name := entry.Name()
path := filepath.Join(examplesDir, name)
t.Run(name, func(t *testing.T) {
t.Parallel()
sourcePath := filepath.Join(root, path, "main.go")
source, err := os.ReadFile(sourcePath)
if err != nil {
t.Fatalf("read generated example: %v", err)
}
if err := requireLocalModuleImport(sourcePath, source, modulePath); err != nil {
t.Fatal(err)
}
expected, err := documentedOutput(source)
if err != nil {
t.Fatalf("parse documented output: %v", err)
}
actual, err := runExample(root, name)
if err != nil {
t.Fatalf("example %q failed to execute:\n%v", name, err)
}
if !bytes.Equal(actual, expected) {
t.Errorf(
"example %q output mismatch:\nexpected: %q\nactual: %q",
name,
expected,
actual,
)
}
})
}
if exampleCount == 0 {
t.Fatal("no generated example directories found")
}
}
// TestDocumentedOutput guards its covered contract against regressions.
func TestDocumentedOutput(t *testing.T) {
t.Parallel()
source := []byte(`package main
func main() {
println(42, true)
// #int 42
// #bool true
println("first\nsecond")
// #string first\nsecond
println(" padded ")
` + "\t// #string padded \n}\n")
got, err := documentedOutput(source)
if err != nil {
t.Fatal(err)
}
want := []byte("42 true\nfirst\nsecond\n padded \n")
if !bytes.Equal(got, want) {
t.Errorf("documented output mismatch:\nexpected: %q\nactual: %q", want, got)
}
}
// readModulePath returns the module path declared by the root go.mod file.
func readModulePath(root string) (string, error) {
data, err := os.ReadFile(filepath.Join(root, "go.mod"))
if err != nil {
return "", fmt.Errorf("read go.mod: %w", err)
}
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 || fields[0] != "module" {
continue
}
if strings.HasPrefix(fields[1], `"`) {
path, err := strconv.Unquote(fields[1])
if err != nil {
return "", fmt.Errorf("parse module path: %w", err)
}
return path, nil
}
return fields[1], nil
}
return "", fmt.Errorf("module path not found in go.mod")
}
// requireLocalModuleImport verifies that the example imports the module under test.
func requireLocalModuleImport(filename string, src []byte, modulePath string) error {
file, err := parser.ParseFile(token.NewFileSet(), filename, src, parser.ImportsOnly)
if err != nil {
return fmt.Errorf("parse generated example imports: %w", err)
}
for _, spec := range file.Imports {
path, err := strconv.Unquote(spec.Path.Value)
if err != nil {
return fmt.Errorf("parse generated example import %s: %w", spec.Path.Value, err)
}
if path == modulePath {
return nil
}
}
return fmt.Errorf("generated example does not import local module %q", modulePath)
}
// documentedOutput converts contiguous // #type value comments into output lines.
func documentedOutput(src []byte) ([]byte, error) {
normalized := strings.ReplaceAll(string(src), "\r\n", "\n")
var outputLines []string
var values []string
flush := func() {
if len(values) == 0 {
return
}
outputLines = append(outputLines, strings.Join(values, " "))
values = nil
}
for lineNumber, line := range strings.Split(normalized, "\n") {
comment := strings.TrimLeft(line, " \t")
if !strings.HasPrefix(comment, "//") {
flush()
continue
}
comment = strings.TrimLeft(strings.TrimPrefix(comment, "//"), " \t")
if !strings.HasPrefix(comment, "#") {
flush()
continue
}
separator := strings.IndexAny(comment, " \t")
if separator < 2 {
return nil, fmt.Errorf("line %d has malformed expected output %q", lineNumber+1, comment)
}
value, err := unescapeExpectedValue(comment[separator+1:])
if err != nil {
return nil, fmt.Errorf("line %d: %w", lineNumber+1, err)
}
values = append(values, value)
}
flush()
if len(outputLines) == 0 {
return nil, fmt.Errorf("no // #type expected output lines found")
}
return []byte(strings.Join(outputLines, "\n") + "\n"), nil
}
// unescapeExpectedValue decodes Go escape sequences used to document multiline and Unicode output.
func unescapeExpectedValue(value string) (string, error) {
var decoded strings.Builder
for value != "" {
if value[0] == '"' {
decoded.WriteByte(value[0])
value = value[1:]
continue
}
r, _, tail, err := strconv.UnquoteChar(value, '"')
if err != nil {
return "", fmt.Errorf("decode expected output %q: %w", value, err)
}
decoded.WriteRune(r)
value = tail
}
return decoded.String(), nil
}
// runExample executes a generated command from the nested examples module.
func runExample(root, exampleName string) ([]byte, error) {
cmd := exec.Command("go", "run", "./"+exampleName)
cmd.Dir = filepath.Join(root, "examples")
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("go run: %w\n%s", err, output)
}
return output, nil
}