-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer_test.go
455 lines (412 loc) · 10.2 KB
/
analyzer_test.go
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
package readgo
import (
"context"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
func TestAnalyzeProject(t *testing.T) {
tests := []struct {
name string
path string
wantErr bool
}{
{
name: "Valid project",
path: ".",
wantErr: false,
},
{
name: "Non-existent project",
path: "nonexistent",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
analyzer := NewAnalyzer(WithWorkDir("testdata/basic"))
result, err := analyzer.AnalyzeProject(context.Background(), tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("AnalyzeProject() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && result != nil {
if result.Name != "main" {
t.Errorf("Expected project name to be 'main', got %q", result.Name)
}
if len(result.Types) == 0 {
t.Error("Expected types to be found")
}
if len(result.Functions) == 0 {
t.Error("Expected functions to be found")
}
}
})
}
}
func TestAnalyzeProjectWithDirectories(t *testing.T) {
// 创建临时测试目录结构
tmpDir := t.TempDir()
// 创建嵌套的目录结构
dirs := []string{
filepath.Join(tmpDir, "src"),
filepath.Join(tmpDir, "src", "models"),
filepath.Join(tmpDir, "src", "handlers"),
}
for _, dir := range dirs {
err := os.MkdirAll(dir, 0755)
assertNoError(t, err)
assertDirExists(t, dir)
}
// 在目录中创建一些 Go 文件,确保包名和内容格式正确
files := map[string]string{
filepath.Join(tmpDir, "src", "models", "user.go"): `// Package models contains data models
package models
// User represents a user in the system
type User struct {
ID int
Name string
}`,
filepath.Join(tmpDir, "src", "handlers", "handler.go"): `// Package handlers contains HTTP handlers
package handlers
// Handler represents an HTTP handler
type Handler struct {
Path string
}`,
filepath.Join(tmpDir, "go.mod"): `module testproject
go 1.16
`,
}
for path, content := range files {
err := os.WriteFile(path, []byte(content), 0644)
assertNoError(t, err)
assertFileExists(t, path)
}
// 使用 Analyzer 分析项目,使用正确的相对路径
analyzer := NewAnalyzer(WithWorkDir(tmpDir))
result, err := analyzer.AnalyzeProject(context.Background(), "src")
assertNoError(t, err)
if result == nil {
t.Fatal("Expected analysis result, got nil")
}
// 验证是否找到了所有类型
foundTypes := make(map[string]bool)
for _, typ := range result.Types {
foundTypes[typ.Name] = true
}
expectedTypes := []string{"User", "Handler"}
for _, typeName := range expectedTypes {
if !foundTypes[typeName] {
t.Errorf("Expected to find type %s in analysis results", typeName)
}
}
// 验证包名,添加调试信息
packages := make(map[string]bool)
for _, typ := range result.Types {
t.Logf("Found type %s in package %s", typ.Name, typ.Package)
// 从完整包路径中提取最后一个部分作为包名
pkgParts := strings.Split(typ.Package, "/")
pkgName := pkgParts[len(pkgParts)-1]
packages[pkgName] = true
}
expectedPackages := []string{"models", "handlers"}
for _, pkgName := range expectedPackages {
if !packages[pkgName] {
t.Errorf("Expected to find package %s in analysis results", pkgName)
}
}
}
func TestAnalyzeFile(t *testing.T) {
tmpDir := t.TempDir()
setupTestFiles(t, tmpDir)
// 创建一个超大文件
largePath := filepath.Join(tmpDir, "large.go")
largeContent := make([]byte, maxFileSize+1)
err := os.WriteFile(largePath, largeContent, 0600)
assertNoError(t, err)
tests := []struct {
name string
filePath string
wantErr bool
}{
{
name: "Valid file",
filePath: filepath.Join("testdata", "basic", "main.go"),
wantErr: false,
},
{
name: "Non-existent file",
filePath: "nonexistent.go",
wantErr: true,
},
{
name: "File too large",
filePath: largePath,
wantErr: true,
},
{
name: "Invalid extension",
filePath: "test.txt",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
analyzer := NewAnalyzer(
WithWorkDir(tmpDir),
WithCacheTTL(time.Minute),
)
result, err := analyzer.AnalyzeFile(context.Background(), tt.filePath)
if (err != nil) != tt.wantErr {
t.Errorf("AnalyzeFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
assertNoError(t, err)
assertFileExists(t, filepath.Join(tmpDir, tt.filePath))
// Check if we found the expected types
var foundUser, foundInterface bool
for _, typ := range result.Types {
switch typ.Name {
case "User":
foundUser = true
if typ.Type != "struct{ID int; Name string}" {
t.Errorf("User type has wrong structure: %s", typ.Type)
}
case "ComplexInterface":
foundInterface = true
if !strings.Contains(typ.Type, "interface") {
t.Errorf("ComplexInterface is not an interface type: %s", typ.Type)
}
}
}
if !foundUser {
t.Error("User type not found in analysis results")
}
if !foundInterface {
t.Error("ComplexInterface not found in analysis results")
}
} else {
assertError(t, err)
}
})
}
}
func TestFindType(t *testing.T) {
tmpDir := t.TempDir()
setupTestFiles(t, tmpDir)
tests := []struct {
name string
pkgPath string
typeName string
wantErr bool
}{
{
name: "Valid type",
pkgPath: "./testdata/basic",
typeName: "User",
wantErr: false,
},
{
name: "Non-existent type",
pkgPath: "./testdata/basic",
typeName: "NonExistentType",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
analyzer := NewAnalyzer(
WithWorkDir(tmpDir),
WithCacheTTL(time.Minute),
)
result, err := analyzer.FindType(context.Background(), tt.pkgPath, tt.typeName)
if (err != nil) != tt.wantErr {
t.Errorf("FindType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
assertNoError(t, err)
if result.Name != tt.typeName {
t.Errorf("FindType() got type name %q, want %q", result.Name, tt.typeName)
}
} else {
assertError(t, err)
}
})
}
}
func TestFindInterface(t *testing.T) {
tmpDir := t.TempDir()
setupTestFiles(t, tmpDir)
tests := []struct {
name string
pkgPath string
interfaceName string
wantErr bool
}{
{
name: "Valid interface",
pkgPath: "./testdata/basic",
interfaceName: "ComplexInterface",
wantErr: false,
},
{
name: "Non-existent interface",
pkgPath: "./testdata/basic",
interfaceName: "NonExistentInterface",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
analyzer := NewAnalyzer(
WithWorkDir(tmpDir),
WithCacheTTL(time.Minute),
)
result, err := analyzer.FindInterface(context.Background(), tt.pkgPath, tt.interfaceName)
if (err != nil) != tt.wantErr {
t.Errorf("FindInterface() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
assertNoError(t, err)
if result.Name != tt.interfaceName {
t.Errorf("FindInterface() got interface name %q, want %q", result.Name, tt.interfaceName)
}
} else {
assertError(t, err)
}
})
}
}
func TestCacheEffectiveness(t *testing.T) {
analyzer := NewAnalyzer(
WithWorkDir("testdata/basic"),
WithCacheTTL(time.Minute),
)
// First call
start := time.Now()
result1, err := analyzer.FindType(context.Background(), ".", "TestType")
if err != nil {
t.Fatalf("First FindType() failed: %v", err)
}
firstDuration := time.Since(start)
// Second call (should be cached)
start = time.Now()
result2, err := analyzer.FindType(context.Background(), ".", "TestType")
if err != nil {
t.Fatalf("Second FindType() failed: %v", err)
}
secondDuration := time.Since(start)
// Verify results are the same
if !reflect.DeepEqual(result1, result2) {
t.Error("Cache returned different results")
}
// Check cache stats
stats := analyzer.GetCacheStats()
if hits, ok := stats["hits"].(int64); !ok || hits == 0 {
t.Error("Expected cache hits > 0")
}
// The second call should be significantly faster
if secondDuration > firstDuration/2 {
t.Logf("First call: %v", firstDuration)
t.Logf("Second call: %v", secondDuration)
t.Skip("Cache performance test skipped - results may vary on different machines")
}
}
// Helper functions for assertions
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func assertError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Error("Expected error, got nil")
}
}
func assertDirExists(t *testing.T, path string) {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Errorf("Directory %s does not exist: %v", path, err)
return
}
if !info.IsDir() {
t.Errorf("Path %s exists but is not a directory", path)
}
}
func assertFileExists(t *testing.T, path string) {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Errorf("File %s does not exist: %v", path, err)
return
}
if info.IsDir() {
t.Errorf("Path %s exists but is a directory", path)
}
}
func TestIsGeneratedFile(t *testing.T) {
tests := []struct {
name string
content string
expected bool
}{
{
name: "Generated file",
content: "// Code generated by protoc-gen-go. DO NOT EDIT.\npackage main",
expected: true,
},
{
name: "Normal file",
content: "package main\n\nfunc main() {}",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isGeneratedFile([]byte(tt.content))
if result != tt.expected {
t.Errorf("isGeneratedFile() = %v, want %v", result, tt.expected)
}
})
}
}
func TestIsAllowedExtension(t *testing.T) {
tests := []struct {
name string
ext string
expected bool
}{
{
name: "Go file",
ext: ".go",
expected: true,
},
{
name: "Text file",
ext: ".txt",
expected: false,
},
{
name: "No extension",
ext: "",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isAllowedExtension(tt.ext)
if result != tt.expected {
t.Errorf("isAllowedExtension() = %v, want %v", result, tt.expected)
}
})
}
}