-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathendtoend_test.go
More file actions
174 lines (146 loc) · 3.46 KB
/
endtoend_test.go
File metadata and controls
174 lines (146 loc) · 3.46 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
// Package mysql contains end-to-end tests for the MySQL engine.
package mysql
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/sqlc-dev/sqlc/internal/cmd"
"github.com/sqlc-dev/sqlc/internal/enginetest/testcases"
)
func TestEndToEnd(t *testing.T) {
t.Parallel()
ctx := context.Background()
testdataDir, err := filepath.Abs("testdata")
if err != nil {
t.Fatal(err)
}
// Walk through all test directories
err = filepath.Walk(testdataDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Look for sqlc config files
if info.Name() != "sqlc.yaml" && info.Name() != "sqlc.json" {
return nil
}
dir := filepath.Dir(path)
testName := strings.TrimPrefix(dir, testdataDir+string(filepath.Separator))
t.Run(testName, func(t *testing.T) {
t.Parallel()
runTest(ctx, t, dir)
})
return filepath.SkipDir
})
if err != nil {
t.Fatal(err)
}
}
func runTest(ctx context.Context, t *testing.T, dir string) {
t.Helper()
var stderr bytes.Buffer
opts := &cmd.Options{
Env: cmd.Env{},
Stderr: &stderr,
}
// Check for expected stderr
expectedStderr := readExpectedStderr(t, dir)
output, err := cmd.Generate(ctx, dir, "", opts)
// If we expect an error, check stderr matches
if len(expectedStderr) > 0 {
if err == nil {
t.Fatalf("expected error but got none")
}
diff := cmp.Diff(
strings.TrimSpace(expectedStderr),
strings.TrimSpace(stderr.String()),
stderrTransformer(),
)
if diff != "" {
t.Fatalf("stderr differed (-want +got):\n%s", diff)
}
return
}
if err != nil {
t.Fatalf("sqlc generate failed: %s", stderr.String())
}
cmpDirectory(t, dir, output)
}
func readExpectedStderr(t *testing.T, dir string) string {
t.Helper()
paths := []string{
filepath.Join(dir, "stderr.txt"),
}
for _, path := range paths {
if _, err := os.Stat(path); !os.IsNotExist(err) {
blob, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return string(blob)
}
}
return ""
}
func stderrTransformer() cmp.Option {
return cmp.Transformer("Stderr", func(in string) string {
s := strings.Replace(in, "\r", "", -1)
return strings.Replace(s, "\\", "/", -1)
})
}
func lineEndings() cmp.Option {
return cmp.Transformer("LineEndings", func(in string) string {
return strings.Replace(in, "\r\n", "\n", -1)
})
}
func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
t.Helper()
expected := map[string]string{}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
if strings.HasSuffix(path, "_test.go") {
return nil
}
blob, err := os.ReadFile(path)
if err != nil {
return err
}
expected[path] = string(blob)
return nil
})
if err != nil {
t.Fatal(err)
}
opts := []cmp.Option{
cmpopts.EquateEmpty(),
lineEndings(),
}
if !cmp.Equal(expected, actual, opts...) {
t.Errorf("%s contents differ", dir)
for name, contents := range expected {
if actual[name] == "" {
t.Errorf("%s is empty", name)
continue
}
if diff := cmp.Diff(contents, actual[name], opts...); diff != "" {
t.Errorf("%s differed (-want +got):\n%s", name, diff)
}
}
}
}
// Engine returns the engine type for this package
func Engine() testcases.Engine {
return testcases.EngineMySQL
}