-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnilcheck_test.go
More file actions
150 lines (137 loc) · 3.79 KB
/
nilcheck_test.go
File metadata and controls
150 lines (137 loc) · 3.79 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
package main
import (
"os"
"path/filepath"
"testing"
"time"
"golang.org/x/tools/go/analysis/analysistest"
)
func TestNilCheckAnalyzer(t *testing.T) {
// Use absolute path to testdata for robustness
dir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
testdata := filepath.Join(dir, "testdata")
os.Remove(filepath.Join(testdata, ".nilcheck_cache.json")) // Clean cache
tests := []struct {
name string
patterns []string
}{
{"BasicNilDeref", []string{"./basic"}},
// Skip problematic tests for now
{"SliceOfPointers", []string{"./slice"}},
{"Caching", []string{"./cache"}},
{"MultiPackage", []string{"./multi/mypkg"}}, // Only testing library part to avoid import issues
{"ChainedCalls", []string{"./chained"}},
{"MethodSafety", []string{"./method"}},
{"EdgeCases", []string{"./edge"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, pattern := range tt.patterns {
analysistest.Run(t, testdata, Analyzer, pattern)
}
})
}
}
type mockFSChecker struct {
files map[string]struct {
isDir bool
err error
}
}
func (m mockFSChecker) Stat(name string) (os.FileInfo, error) {
if info, ok := m.files[name]; ok {
if info.err != nil {
return nil, info.err
}
return &mockFileInfo{isDir: info.isDir}, nil
}
return nil, os.ErrNotExist
}
type mockFileInfo struct {
isDir bool
}
func (m *mockFileInfo) IsDir() bool { return m.isDir }
func (m *mockFileInfo) Name() string { return "" }
func (m *mockFileInfo) Size() int64 { return 0 }
func (m *mockFileInfo) Mode() os.FileMode { return 0 }
func (m *mockFileInfo) ModTime() time.Time { return time.Time{} }
func (m *mockFileInfo) Sys() interface{} { return nil }
func TestValidateFlags(t *testing.T) {
tests := []struct {
name string
pkgs string
funcs string
cache string
fsChecker FSChecker
wantErr string
}{
{"ValidAll", "main,mypkg", "main.main,mypkg.GetUsers", "cache.json", mockFSChecker{
files: map[string]struct {
isDir bool
err error
}{
".": {isDir: true},
"cache.json": {isDir: false},
},
}, ""},
{"InvalidPkg", "main,invalid@pkg", "", "cache.json", mockFSChecker{
files: map[string]struct {
isDir bool
err error
}{
".": {isDir: true},
"cache.json": {isDir: false},
},
}, "invalid package name \"invalid@pkg\" in -pkgs flag"},
{"InvalidFunc", "", "main.invalid@func", "cache.json", mockFSChecker{
files: map[string]struct {
isDir bool
err error
}{
".": {isDir: true},
"cache.json": {isDir: false},
},
}, "invalid function name \"main.invalid@func\" in -funcs flag (use pkg.Func or Func)"},
{"EmptyCache", "", "", "", mockFSChecker{}, "-cache flag cannot be empty"},
{"InaccessibleCacheDir", "", "", "/nonexistent/cache.json", mockFSChecker{
files: map[string]struct {
isDir bool
err error
}{},
}, "cache file directory \"/nonexistent\" is not accessible: file does not exist"},
{"CacheIsDir", "", "", "dir/cache.json", mockFSChecker{
files: map[string]struct {
isDir bool
err error
}{
"dir": {isDir: true},
"dir/cache.json": {isDir: true},
},
}, "cache path \"dir/cache.json\" is a directory, not a file"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
*pkgFilter = tt.pkgs
*funcFilter = tt.funcs
*cacheFlag = tt.cache
fsChecker = tt.fsChecker
err := validateFlags()
if tt.wantErr == "" {
if err != nil {
t.Errorf("validateFlags() error = %v, want nil", err)
}
} else {
if err == nil || err.Error() != tt.wantErr {
t.Errorf("validateFlags() error = %v, want %q", err, tt.wantErr)
}
}
// Reset for next test
pkgSet = nil
funcSet = nil
cachePath = ""
})
}
}