-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathfmt_test.go
More file actions
129 lines (114 loc) · 3.06 KB
/
fmt_test.go
File metadata and controls
129 lines (114 loc) · 3.06 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
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
func TestFormat(t *testing.T) {
t.Parallel()
for _, tc := range FindTests(t, "testdata", "base") {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
// Parse the config file to determine the engine
configPath := filepath.Join(tc.Path, tc.ConfigName)
configFile, err := os.Open(configPath)
if err != nil {
t.Fatal(err)
}
conf, err := config.ParseConfig(configFile)
configFile.Close()
if err != nil {
t.Fatal(err)
}
// Skip if there are no SQL packages configured
if len(conf.SQL) == 0 {
return
}
// For now, only test PostgreSQL since that's the only engine with Format support
engine := conf.SQL[0].Engine
if engine != config.EnginePostgreSQL {
return
}
// Find query files from config
var queryFiles []string
for _, sql := range conf.SQL {
for _, q := range sql.Queries {
queryPath := filepath.Join(tc.Path, q)
info, err := os.Stat(queryPath)
if err != nil {
continue
}
if info.IsDir() {
// If it's a directory, glob for .sql files
matches, err := filepath.Glob(filepath.Join(queryPath, "*.sql"))
if err != nil {
continue
}
queryFiles = append(queryFiles, matches...)
} else {
queryFiles = append(queryFiles, queryPath)
}
}
}
if len(queryFiles) == 0 {
return
}
parse := postgresql.NewParser()
for _, queryFile := range queryFiles {
if _, err := os.Stat(queryFile); os.IsNotExist(err) {
continue
}
contents, err := os.ReadFile(queryFile)
if err != nil {
t.Fatal(err)
}
// Parse the entire file to get proper statement boundaries
stmts, err := parse.Parse(bytes.NewReader(contents))
if err != nil {
// Skip files with parse errors (e.g., syntax_errors test cases)
return
}
for i, stmt := range stmts {
stmt := stmt
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
// Extract the original query text using statement location and length
start := stmt.Raw.StmtLocation
length := stmt.Raw.StmtLen
if length == 0 {
// If StmtLen is 0, it means the statement goes to the end of the input
length = len(contents) - start
}
query := strings.TrimSpace(string(contents[start : start+length]))
expected, err := postgresql.Fingerprint(query)
if err != nil {
t.Fatal(err)
}
if false {
r, err := postgresql.Parse(query)
debug.Dump(r, err)
}
out := ast.Format(stmt.Raw, parse)
actual, err := postgresql.Fingerprint(out)
if err != nil {
t.Error(err)
}
if expected != actual {
debug.Dump(stmt.Raw)
t.Errorf("- %s", expected)
t.Errorf("- %s", query)
t.Errorf("+ %s", actual)
t.Errorf("+ %s", out)
}
})
}
}
})
}
}