Skip to content

Commit 4d06225

Browse files
committed
Close to all tests passing
1 parent 2767264 commit 4d06225

29 files changed

Lines changed: 401 additions & 96 deletions

internal/endtoend/endtoend.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
)
10+
11+
type Testcase struct {
12+
Name string
13+
Path string
14+
ConfigName string
15+
Stderr []byte
16+
Exec *Exec
17+
}
18+
19+
type Exec struct {
20+
Command string `json:"command"`
21+
Process string `json:"process"`
22+
Env map[string]string `json:"env"`
23+
}
24+
25+
func parseStderr(t *testing.T, dir string) []byte {
26+
t.Helper()
27+
path := filepath.Join(dir, "stderr.txt")
28+
if _, err := os.Stat(path); os.IsNotExist(err) {
29+
return nil
30+
}
31+
blob, err := os.ReadFile(path)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
return blob
36+
}
37+
38+
func parseExec(t *testing.T, dir string) *Exec {
39+
t.Helper()
40+
path := filepath.Join(dir, "exec.json")
41+
if _, err := os.Stat(path); os.IsNotExist(err) {
42+
return nil
43+
}
44+
var e Exec
45+
blob, err := os.ReadFile(path)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
if err := json.Unmarshal(blob, &e); err != nil {
50+
t.Fatal(err)
51+
}
52+
if e.Command == "" {
53+
e.Command = "generate"
54+
}
55+
return &e
56+
}
57+
58+
func FindTests(t *testing.T, root string) []*Testcase {
59+
var tcs []*Testcase
60+
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
61+
if err != nil {
62+
return err
63+
}
64+
if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" {
65+
dir := filepath.Dir(path)
66+
tcs = append(tcs, &Testcase{
67+
Path: dir,
68+
Name: strings.TrimPrefix(dir, root+string(filepath.Separator)),
69+
ConfigName: info.Name(),
70+
Stderr: parseStderr(t, dir),
71+
Exec: parseExec(t, dir),
72+
})
73+
return filepath.SkipDir
74+
}
75+
return nil
76+
})
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
return tcs
81+
}

internal/endtoend/endtoend_test.go

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"os"
87
osexec "os/exec"
98
"path/filepath"
@@ -81,32 +80,21 @@ func TestReplay(t *testing.T) {
8180

8281
t.Parallel()
8382
ctx := context.Background()
84-
var dirs []string
85-
err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
86-
if err != nil {
87-
return err
88-
}
89-
if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" || info.Name() == "sqlc.yml" {
90-
dirs = append(dirs, filepath.Dir(path))
91-
return filepath.SkipDir
92-
}
93-
return nil
94-
})
95-
if err != nil {
96-
t.Fatal(err)
97-
}
98-
for _, replay := range dirs {
83+
for _, replay := range FindTests(t, "testdata") {
9984
tc := replay
100-
t.Run(tc, func(t *testing.T) {
85+
t.Run(tc.Name, func(t *testing.T) {
10186
t.Parallel()
10287

10388
var stderr bytes.Buffer
10489
var output map[string]string
10590
var err error
10691

107-
path, _ := filepath.Abs(tc)
108-
args := parseExec(t, path)
109-
expected := expectedStderr(t, path)
92+
path, _ := filepath.Abs(tc.Path)
93+
args := tc.Exec
94+
if args == nil {
95+
args = &Exec{Command: "generate"}
96+
}
97+
expected := string(tc.Stderr)
11098

11199
if args.Process != "" {
112100
_, err := osexec.LookPath(args.Process)
@@ -206,44 +194,6 @@ func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
206194
}
207195
}
208196

209-
func expectedStderr(t *testing.T, dir string) string {
210-
t.Helper()
211-
path := filepath.Join(dir, "stderr.txt")
212-
if _, err := os.Stat(path); !os.IsNotExist(err) {
213-
blob, err := os.ReadFile(path)
214-
if err != nil {
215-
t.Fatal(err)
216-
}
217-
return string(blob)
218-
}
219-
return ""
220-
}
221-
222-
type exec struct {
223-
Command string `json:"command"`
224-
Process string `json:"process"`
225-
Env map[string]string `json:"env"`
226-
}
227-
228-
func parseExec(t *testing.T, dir string) exec {
229-
t.Helper()
230-
var e exec
231-
path := filepath.Join(dir, "exec.json")
232-
if _, err := os.Stat(path); !os.IsNotExist(err) {
233-
blob, err := os.ReadFile(path)
234-
if err != nil {
235-
t.Fatal(err)
236-
}
237-
if err := json.Unmarshal(blob, &e); err != nil {
238-
t.Fatal(err)
239-
}
240-
}
241-
if e.Command == "" {
242-
e.Command = "generate"
243-
}
244-
return e
245-
}
246-
247197
func BenchmarkReplay(b *testing.B) {
248198
ctx := context.Background()
249199
var dirs []string

internal/endtoend/fmt_test.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,20 @@ import (
1616

1717
func TestFormat(t *testing.T) {
1818
t.Parallel()
19-
var queries []string
20-
err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
21-
if err != nil {
22-
return err
23-
}
24-
if !strings.Contains(path, filepath.Join("pgx/v5")) {
25-
return nil
19+
parse := postgresql.NewParser()
20+
for _, tc := range FindTests(t, "testdata") {
21+
tc := tc
22+
23+
if !strings.Contains(tc.Path, filepath.Join("pgx/v5")) {
24+
continue
2625
}
27-
if info.Name() == "query.sql" {
28-
queries = append(queries, path)
29-
return filepath.SkipDir
26+
27+
q := filepath.Join(tc.Path, "query.sql")
28+
if _, err := os.Stat(q); os.IsNotExist(err) {
29+
continue
3030
}
31-
return nil
32-
})
33-
if err != nil {
34-
t.Fatal(err)
35-
}
36-
parse := postgresql.NewParser()
37-
for _, q := range queries {
38-
q := q
39-
t.Run(filepath.Dir(q), func(t *testing.T) {
31+
32+
t.Run(tc.Name, func(t *testing.T) {
4033
contents, err := os.ReadFile(q)
4134
if err != nil {
4235
t.Fatal(err)
@@ -58,6 +51,11 @@ func TestFormat(t *testing.T) {
5851
if len(stmts) != 1 {
5952
t.Fatal("expected one statement")
6053
}
54+
if false {
55+
r, err := pg_query.Parse(string(query))
56+
debug.Dump(r, err)
57+
}
58+
6159
out := ast.Format(stmts[0].Raw)
6260
actual, err := pg_query.Fingerprint(out)
6361
if err != nil {

internal/sql/ast/a_expr.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ func (n *A_Expr) Format(buf *TrackedBuffer) {
1818
}
1919
buf.astFormat(n.Lexpr)
2020
buf.WriteString(" ")
21-
buf.astFormat(n.Name)
22-
buf.WriteString(" ")
23-
buf.astFormat(n.Rexpr)
21+
if n.Kind == A_Expr_Kind_IN {
22+
buf.WriteString(" IN (")
23+
buf.astFormat(n.Rexpr)
24+
buf.WriteString(")")
25+
} else {
26+
buf.astFormat(n.Name)
27+
buf.WriteString(" ")
28+
buf.astFormat(n.Rexpr)
29+
}
2430
}

internal/sql/ast/a_expr_kind.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package ast
22

33
type A_Expr_Kind uint
44

5+
const (
6+
A_Expr_Kind_IN A_Expr_Kind = 7
7+
)
8+
59
func (n *A_Expr_Kind) Pos() int {
610
return 0
711
}

internal/sql/ast/alias.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ func (n *Alias) Format(buf *TrackedBuffer) {
1616
if n.Aliasname != nil {
1717
buf.WriteString(*n.Aliasname)
1818
}
19+
if items(n.Colnames) {
20+
buf.WriteString("(")
21+
buf.astFormat((n.Colnames))
22+
buf.WriteString(")")
23+
}
1924
}

internal/sql/ast/bool_expr.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ func (n *BoolExpr) Format(buf *TrackedBuffer) {
1515
if n == nil {
1616
return
1717
}
18+
buf.WriteString("(")
1819
if items(n.Args) {
1920
switch n.Boolop {
2021
case BoolExprTypeAnd:
2122
buf.join(n.Args, " AND ")
2223
case BoolExprTypeOr:
2324
buf.join(n.Args, " OR ")
2425
case BoolExprTypeNot:
25-
buf.join(n.Args, " NOT ")
26+
buf.WriteString(" NOT ")
27+
buf.astFormat(n.Args)
2628
}
2729
}
30+
buf.WriteString(")")
2831
}

internal/sql/ast/call_stmt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ func (n *CallStmt) Pos() int {
1010
}
1111
return n.FuncCall.Pos()
1212
}
13+
14+
func (n *CallStmt) Format(buf *TrackedBuffer) {
15+
buf.WriteString("CALL ")
16+
buf.astFormat(n.FuncCall)
17+
}

internal/sql/ast/coalesce_expr.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ type CoalesceExpr struct {
1111
func (n *CoalesceExpr) Pos() int {
1212
return n.Location
1313
}
14+
15+
func (n *CoalesceExpr) Format(buf *TrackedBuffer) {
16+
if n == nil {
17+
return
18+
}
19+
buf.WriteString("COALESCE(")
20+
buf.astFormat(n.Args)
21+
buf.WriteString(")")
22+
}

internal/sql/ast/column_def.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ type ColumnDef struct {
3030
func (n *ColumnDef) Pos() int {
3131
return n.Location
3232
}
33+
34+
func (n *ColumnDef) Format(buf *TrackedBuffer) {
35+
if n == nil {
36+
return
37+
}
38+
buf.WriteString(n.Colname)
39+
buf.WriteString(" ")
40+
buf.astFormat(n.TypeName)
41+
if n.IsNotNull {
42+
buf.WriteString(" NOT NULL")
43+
}
44+
}

0 commit comments

Comments
 (0)