-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunusedsqlc_test.go
More file actions
291 lines (260 loc) · 7.26 KB
/
Copy pathunusedsqlc_test.go
File metadata and controls
291 lines (260 loc) · 7.26 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package unusedsqlc_test
import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"strings"
"testing"
"github.com/malpou/unusedsqlc"
)
func TestUnusedSQLc(t *testing.T) {
tests := []struct {
name string
dir string
expected []string
}{
{
name: "basic - should find UnusedQuery",
dir: "fixtures/basic",
expected: []string{
"UnusedQuery",
},
},
{
name: "crosspackage - should find DeleteProduct",
dir: "fixtures/crosspackage",
expected: []string{
"DeleteProduct",
},
},
{
name: "noissues - all queries used",
dir: "fixtures/noissues",
expected: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
messages, err := runLinter(tt.dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Check that we found the expected unused queries
foundQueries := make(map[string]bool)
for _, msg := range messages {
// Extract query name from message
if idx := strings.Index(msg.Message, `unused sqlc query "`); idx >= 0 {
start := idx + len(`unused sqlc query "`)
end := strings.Index(msg.Message[start:], `"`)
if end > 0 {
queryName := msg.Message[start : start+end]
foundQueries[queryName] = true
}
}
}
// Verify expected queries were found
for _, expectedQuery := range tt.expected {
if !foundQueries[expectedQuery] {
t.Errorf("expected to find unused query %q, but didn't", expectedQuery)
}
delete(foundQueries, expectedQuery)
}
// Check for unexpected queries
for query := range foundQueries {
t.Errorf("found unexpected unused query %q", query)
}
})
}
}
func TestQueryParser(t *testing.T) {
tests := []struct {
name string
dir string
expectedCount int
expectedNames []string
}{
{
name: "basic queries",
dir: "fixtures/basic",
expectedCount: 6,
expectedNames: []string{"GetUser", "ListUsers", "CreateUser", "UpdateUser", "DeleteUser", "UnusedQuery"},
},
{
name: "crosspackage queries",
dir: "fixtures/crosspackage",
expectedCount: 3,
expectedNames: []string{"GetProduct", "ListProducts", "DeleteProduct"},
},
{
name: "noissues queries",
dir: "fixtures/noissues",
expectedCount: 2,
expectedNames: []string{"GetAllUsers", "CountUsers"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := unusedsqlc.NewQueryParser(nil)
queries, err := parser.ParseDirectory(tt.dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(queries) != tt.expectedCount {
t.Errorf("expected %d queries, got %d", tt.expectedCount, len(queries))
}
queryMap := make(map[string]bool)
for _, q := range queries {
queryMap[q.Name] = true
}
for _, name := range tt.expectedNames {
if !queryMap[name] {
t.Errorf("expected to find query %q", name)
}
}
})
}
}
func TestGolangciLintMode(t *testing.T) {
tests := []struct {
name string
dir string
golangciLintMode bool
expectedQueries []string
checkPositions bool
}{
{
name: "basic - standalone mode",
dir: "fixtures/basic",
golangciLintMode: false,
expectedQueries: []string{"UnusedQuery"},
checkPositions: true,
},
{
name: "basic - golangci-lint mode",
dir: "fixtures/basic",
golangciLintMode: true,
expectedQueries: []string{"UnusedQuery"},
checkPositions: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fset := token.NewFileSet()
// Parse Go files in the directory
pkgs, err := parser.ParseDir(fset, tt.dir, nil, parser.ParseComments)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var files []*ast.File
for _, pkg := range pkgs {
for _, file := range pkg.Files {
files = append(files, file)
}
}
// Create linter with specified mode
config := unusedsqlc.DefaultConfig()
config.GolangciLintMode = tt.golangciLintMode
linter := unusedsqlc.New(config)
absDir, _ := filepath.Abs(tt.dir)
messages, err := linter.Run(files, fset, absDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Check that we found the expected unused queries
foundQueries := make(map[string]bool)
for _, msg := range messages {
// Extract query name from message
if idx := strings.Index(msg.Message, `unused sqlc query "`); idx >= 0 {
start := idx + len(`unused sqlc query "`)
end := strings.Index(msg.Message[start:], `"`)
if end > 0 {
queryName := msg.Message[start : start+end]
foundQueries[queryName] = true
// Check position based on mode
if tt.checkPositions {
if tt.golangciLintMode {
// In golangci-lint mode, position should be in a Go file
if strings.HasSuffix(msg.Pos.Filename, ".sql") {
t.Errorf("In golangci-lint mode, position should not be in SQL file, got: %s",
msg.Pos.Filename)
}
} else {
// In standalone mode, position should be in SQL file
if !strings.HasSuffix(msg.Pos.Filename, ".sql") {
t.Errorf("In standalone mode, position should be in SQL file, got: %s", msg.Pos.Filename)
}
}
}
}
}
}
// Verify expected queries were found
for _, expectedQuery := range tt.expectedQueries {
if !foundQueries[expectedQuery] {
t.Errorf("expected to find unused query %q, but didn't", expectedQuery)
}
}
})
}
}
func TestConfig(t *testing.T) {
t.Run("default config", func(t *testing.T) {
config := unusedsqlc.DefaultConfig()
if !config.IgnoreGenerated {
t.Error("expected IgnoreGenerated to be true by default")
}
if len(config.ExcludeQueries) != 0 {
t.Error("expected no excluded queries by default")
}
if config.GolangciLintMode {
t.Error("expected GolangciLintMode to be false by default")
}
})
t.Run("exclude queries", func(t *testing.T) {
config := unusedsqlc.DefaultConfig()
config.ExcludeQueries = []string{"TestQuery1", "TestQuery2"}
if !config.IsQueryExcluded("TestQuery1") {
t.Error("expected TestQuery1 to be excluded")
}
if config.IsQueryExcluded("TestQuery3") {
t.Error("expected TestQuery3 not to be excluded")
}
})
t.Run("exclude patterns", func(t *testing.T) {
config := unusedsqlc.DefaultConfig()
config.ExcludePatterns = []string{"Test*", "*Internal", "*_test"}
testCases := []struct {
query string
excluded bool
}{
{"TestQuery", true},
{"TestAnything", true},
{"QueryInternal", true},
{"query_test", true},
{"NormalQuery", false},
}
for _, tc := range testCases {
if config.IsQueryExcluded(tc.query) != tc.excluded {
t.Errorf("expected IsQueryExcluded(%q) to be %v", tc.query, tc.excluded)
}
}
})
}
func runLinter(dir string) ([]unusedsqlc.Message, error) {
fset := token.NewFileSet()
// Parse Go files in the directory
pkgs, err := parser.ParseDir(fset, dir, nil, parser.ParseComments)
if err != nil {
return nil, err
}
var files []*ast.File
for _, pkg := range pkgs {
for _, file := range pkg.Files {
files = append(files, file)
}
}
// Run the linter using the exported Run function
absDir, _ := filepath.Abs(dir)
return unusedsqlc.Run(files, fset, absDir)
}