-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcase_test.go
More file actions
90 lines (84 loc) · 1.89 KB
/
case_test.go
File metadata and controls
90 lines (84 loc) · 1.89 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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
type Testcase struct {
Name string
Path string
ConfigName string
Stderr []byte
Exec *Exec
}
type Exec struct {
Command string `json:"command"`
Contexts []string `json:"contexts"`
Process string `json:"process"`
OS []string `json:"os"`
WASM bool `json:"wasm"`
Env map[string]string `json:"env"`
}
func parseStderr(t *testing.T, dir, testctx string) []byte {
t.Helper()
paths := []string{
filepath.Join(dir, "stderr", fmt.Sprintf("%s.txt", testctx)),
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 blob
}
}
return nil
}
func parseExec(t *testing.T, dir string) *Exec {
t.Helper()
path := filepath.Join(dir, "exec.json")
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}
var e Exec
blob, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(blob, &e); err != nil {
t.Fatal(err)
}
if e.Command == "" {
e.Command = "generate"
}
return &e
}
func FindTests(t *testing.T, root, testctx string) []*Testcase {
var tcs []*Testcase
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" || info.Name() == "sqlc.yml" {
dir := filepath.Dir(path)
tcs = append(tcs, &Testcase{
Path: dir,
Name: strings.TrimPrefix(dir, root+string(filepath.Separator)),
ConfigName: info.Name(),
Stderr: parseStderr(t, dir, testctx),
Exec: parseExec(t, dir),
})
return filepath.SkipDir
}
return nil
})
if err != nil {
t.Fatal(err)
}
return tcs
}