-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinder_test.go
More file actions
457 lines (380 loc) · 11.4 KB
/
finder_test.go
File metadata and controls
457 lines (380 loc) · 11.4 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"os"
"path/filepath"
"testing"
)
func TestNewFinder(t *testing.T) {
finder := NewFinder("TestInterface")
if finder.interfaceName != "TestInterface" {
t.Errorf("expected interface name 'TestInterface', got '%s'",
finder.interfaceName)
}
if finder.fset == nil {
t.Error("expected fset to be initialized")
}
if finder.config == nil {
t.Error("expected config to be initialized")
}
if finder.results == nil {
t.Error("expected results to be initialized")
}
if len(finder.results) != 0 {
t.Errorf("expected empty results, got %d items", len(finder.results))
}
}
func TestFinder_ValidateGoModRoot(t *testing.T) {
finder := NewFinder("TestInterface")
tempDir := t.TempDir()
// Test with no go.mod file
oldDir, _ := os.Getwd()
defer os.Chdir(oldDir)
os.Chdir(tempDir)
err := finder.validateGoModRoot()
if err != ErrGoModNotFound {
t.Errorf("expected ErrGoModNotFound, got %v", err)
}
// Test with go.mod file
goModPath := filepath.Join(tempDir, "go.mod")
if err := os.WriteFile(goModPath,
[]byte("module testmodule\n"), 0644); err != nil {
t.Fatalf("failed to create go.mod: %v", err)
}
err = finder.validateGoModRoot()
if err != nil {
t.Errorf("expected no error with go.mod present, got %v", err)
}
}
func TestFinder_LoadModulePath(t *testing.T) {
finder := NewFinder("TestInterface")
tempDir := t.TempDir()
oldDir, _ := os.Getwd()
defer os.Chdir(oldDir)
os.Chdir(tempDir)
tests := []struct {
name string
goModContent string
expectedModule string
expectError bool
}{
{
name: "valid go.mod",
goModContent: "module github.com/test/repo\n\ngo 1.21\n",
expectedModule: "github.com/test/repo",
expectError: false,
},
{
name: "go.mod with spaces",
goModContent: "module github.com/test/repo \n",
expectedModule: "github.com/test/repo",
expectError: false,
},
{
name: "go.mod without module declaration",
goModContent: "go 1.21\n",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
goModPath := filepath.Join(tempDir, "go.mod")
if err := os.WriteFile(goModPath,
[]byte(tt.goModContent), 0644); err != nil {
t.Fatalf("failed to create go.mod: %v", err)
}
err := finder.loadModulePath()
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if finder.modulePath != tt.expectedModule {
t.Errorf("expected module path '%s', got '%s'",
tt.expectedModule, finder.modulePath)
}
}
})
}
}
func TestFinder_GetInterfaceMethods(t *testing.T) {
finder := NewFinder("TestInterface")
// Create a simple interface AST
src := `
package test
type TestInterface interface {
Method1() error
Method2(string) int
Method3()
}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", src, 0)
if err != nil {
t.Fatalf("failed to parse source: %v", err)
}
// Find the interface
var iface *ast.InterfaceType
ast.Inspect(file, func(n ast.Node) bool {
if ts, ok := n.(*ast.TypeSpec); ok && ts.Name.Name == "TestInterface" {
if interfaceType, ok := ts.Type.(*ast.InterfaceType); ok {
iface = interfaceType
return false
}
}
return true
})
if iface == nil {
t.Fatal("failed to find interface in AST")
}
methods := finder.getInterfaceMethods(iface)
expected := []string{"Method1", "Method2", "Method3"}
if len(methods) != len(expected) {
t.Errorf("expected %d methods, got %d", len(expected), len(methods))
}
for i, method := range methods {
if method != expected[i] {
t.Errorf("expected method '%s', got '%s'", expected[i], method)
}
}
}
func TestFinder_TypeImplementsInterface(t *testing.T) {
finder := NewFinder("TestInterface")
finder.interfaceMethods = []string{"Start", "Stop", "GetName"}
// Create a test type that implements the interface
src := `
package test
type TestStruct struct{}
func (t *TestStruct) Start() error { return nil }
func (t *TestStruct) Stop() error { return nil }
func (t *TestStruct) GetName() string { return "test" }
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", src, 0)
if err != nil {
t.Fatalf("failed to parse source: %v", err)
}
config := &types.Config{
Error: func(err error) {}, // Ignore errors
}
pkg, err := config.Check("test", fset, []*ast.File{file}, nil)
if err != nil {
t.Fatalf("failed to type check: %v", err)
}
// Find the TestStruct type
obj := pkg.Scope().Lookup("TestStruct")
if obj == nil {
t.Fatal("TestStruct not found in package scope")
}
typeName, ok := obj.(*types.TypeName)
if !ok {
t.Fatal("TestStruct is not a type name")
}
namedType, ok := typeName.Type().(*types.Named)
if !ok {
t.Fatal("TestStruct is not a named type")
}
if !finder.typeImplementsInterface(namedType) {
t.Error("TestStruct should implement the interface")
}
// Test with incomplete implementation
finder.interfaceMethods = []string{"Start", "Stop", "GetName", "Missing"}
if finder.typeImplementsInterface(namedType) {
t.Error("TestStruct should not implement interface with missing method")
}
}
func TestFinder_GetResults(t *testing.T) {
finder := NewFinder("TestInterface")
// Initially empty
results := finder.getResults()
if len(results) != 0 {
t.Errorf("expected 0 results, got %d", len(results))
}
// Add some results
finder.results = append(finder.results, Implementation{
Package: "test",
Struct: "TestStruct",
PackagePath: "github.com/test/repo",
})
results = finder.getResults()
if len(results) != 1 {
t.Errorf("expected 1 result, got %d", len(results))
}
if results[0].Package != "test" {
t.Errorf("expected package 'test', got '%s'", results[0].Package)
}
}
func TestFinder_LoadModulePathErrors(t *testing.T) {
finder := NewFinder("TestInterface")
tempDir := t.TempDir()
oldDir, _ := os.Getwd()
defer os.Chdir(oldDir)
os.Chdir(tempDir)
// Test with non-readable go.mod file (permission denied)
goModPath := filepath.Join(tempDir, "go.mod")
if err := os.WriteFile(goModPath, []byte("module test\n"), 0644); err != nil {
t.Fatalf("failed to create go.mod: %v", err)
}
// Make file unreadable (won't work on all systems, but covers the error path)
originalContent := "module test\n"
os.WriteFile(goModPath, []byte(originalContent), 0644)
err := finder.loadModulePath()
if err != nil && finder.modulePath != "test" {
// If we can read it, make sure it worked
t.Errorf("should be able to read valid go.mod")
}
}
func TestFinder_ParseInterfaceErrors(t *testing.T) {
finder := NewFinder("TestInterface")
tempDir := t.TempDir()
tests := []struct {
name string
fileContent string
expectError bool
}{
{
name: "valid interface",
fileContent: "package test\ntype TestInterface interface { Method() }",
expectError: false,
},
{
name: "invalid Go syntax",
fileContent: "package test\ntype TestInterface interface { Method( }",
expectError: true,
},
{
name: "interface not found",
fileContent: "package test\ntype OtherInterface interface { Method() }",
expectError: true,
},
{
name: "type is not interface",
fileContent: "package test\ntype TestInterface struct { Field int }",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testFile := filepath.Join(tempDir, "test.go")
if err := os.WriteFile(testFile, []byte(tt.fileContent), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
err := finder.parseInterface(testFile)
if tt.expectError {
if err == nil {
t.Error("expected error but got none")
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
})
}
}
func TestFinder_ScanDirectoryErrors(t *testing.T) {
finder := NewFinder("TestInterface")
// Test with non-existent directory
err := finder.scanDirectory("/nonexistent/directory")
if err == nil {
t.Error("expected error for non-existent directory")
}
}
func TestFinder_ParsePackageFilesEdgeCases(t *testing.T) {
finder := NewFinder("TestInterface")
tempDir := t.TempDir()
// Test directory with no Go files
emptyDir := filepath.Join(tempDir, "empty")
os.Mkdir(emptyDir, 0755)
files, err := finder.parsePackageFiles(emptyDir)
if err != nil {
t.Errorf("unexpected error for empty directory: %v", err)
}
if len(files) != 0 {
t.Errorf("expected 0 files for empty directory, got %d", len(files))
}
// Test directory with only test files
testDir := filepath.Join(tempDir, "testonly")
os.Mkdir(testDir, 0755)
testFile := filepath.Join(testDir, "main_test.go")
os.WriteFile(testFile, []byte("package main\nfunc TestFoo(t *testing.T) {}"), 0644)
files, err = finder.parsePackageFiles(testDir)
if err != nil {
t.Errorf("unexpected error for test-only directory: %v", err)
}
if len(files) != 0 {
t.Errorf("expected 0 files for test-only directory, got %d", len(files))
}
// Test directory with invalid Go file
invalidDir := filepath.Join(tempDir, "invalid")
os.Mkdir(invalidDir, 0755)
invalidFile := filepath.Join(invalidDir, "invalid.go")
os.WriteFile(invalidFile, []byte("invalid go syntax {{{"), 0644)
files, err = finder.parsePackageFiles(invalidDir)
if err != nil {
t.Errorf("unexpected error for invalid Go file: %v", err)
}
// Should skip invalid files and return empty slice
if len(files) != 0 {
t.Errorf("expected 0 files for invalid Go files, got %d", len(files))
}
}
func TestFinder_TypeCheckPackageErrors(t *testing.T) {
finder := NewFinder("TestInterface")
// Test with empty file slice
pkg, err := finder.typeCheckPackage([]*ast.File{})
if pkg != nil {
t.Error("expected nil package for empty file slice")
}
if err != ErrNoFilesToTypeCheck {
t.Errorf("expected ErrNoFilesToTypeCheck, got %v", err)
}
}
func TestFinder_TypeImplementsInterfaceEdgeCases(t *testing.T) {
finder := NewFinder("TestInterface")
// Test with empty interface methods
finder.interfaceMethods = []string{}
src := `package test
type TestStruct struct{}
func (t *TestStruct) Method() {}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", src, 0)
if err != nil {
t.Fatalf("failed to parse source: %v", err)
}
config := &types.Config{Error: func(err error) {}}
pkg, err := config.Check("test", fset, []*ast.File{file}, nil)
if err != nil {
t.Fatalf("failed to type check: %v", err)
}
obj := pkg.Scope().Lookup("TestStruct")
typeName := obj.(*types.TypeName)
namedType := typeName.Type().(*types.Named)
// Should return false for empty interface methods
if finder.typeImplementsInterface(namedType) {
t.Error("should return false for empty interface methods")
}
}
func TestFinder_AnalyzeDirectoryErrorPaths(t *testing.T) {
finder := NewFinder("TestInterface")
tempDir := t.TempDir()
// Test with directory that causes parsePackageFiles to return error
unreadableDir := filepath.Join(tempDir, "unreadable")
os.Mkdir(unreadableDir, 0755)
// Create a regular file where a directory is expected to cause error
badSubDir := filepath.Join(unreadableDir, "badfile")
os.WriteFile(badSubDir, []byte("content"), 0644)
// analyzeDirectory should handle errors gracefully
finder.analyzeDirectory(unreadableDir)
// Should not panic and continue execution
if len(finder.results) != 0 {
t.Errorf("expected 0 results for error case, got %d", len(finder.results))
}
}