-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscan_test.go
265 lines (247 loc) · 5.62 KB
/
scan_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
package main
import (
"os"
"path/filepath"
"regexp"
"testing"
)
func TestScanDir(t *testing.T) {
tempDir := t.TempDir()
testFiles := map[string]int{
"file1.txt": 100,
"file2.txt": 100,
"file3.txt": 200,
"ignored_file.txt": 300,
"subdir/file4.txt": 400,
"subdir/file5.txt": 400,
"subdir/ignored_file.txt": 500,
"regex_ignore/file6.txt": 600,
"another_dir/file7.txt": 700,
"symlink_target.txt": 800,
"local.tmp": 0,
}
dirs := []string{
filepath.Join(tempDir, "subdir"),
filepath.Join(tempDir, "regex_ignore"),
filepath.Join(tempDir, "another_dir"),
filepath.Join(tempDir, "empty_dir"),
filepath.Join(tempDir, "ignore_dir"),
}
for _, dir := range dirs {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("Failed to create directory %s: %v", dir, err)
}
}
for filePath, size := range testFiles {
fullPath := filepath.Join(tempDir, filePath)
file, err := os.Create(fullPath)
if err != nil {
t.Fatalf("Failed to create file %s: %v", fullPath, err)
}
if size > 0 {
data := make([]byte, size)
if _, err := file.Write(data); err != nil {
file.Close()
t.Fatalf("Failed to write to file %s: %v", fullPath, err)
}
}
file.Close()
}
symlinkPath := filepath.Join(tempDir, "symlink.txt")
symlinkTarget := filepath.Join(tempDir, "symlink_target.txt")
if err := os.Symlink(symlinkTarget, symlinkPath); err != nil {
// On Windows, creating symlinks may require admin privileges or developer mode
// Skip this part if it fails
t.Logf("Skipping symlink creation: %v", err)
}
tests := []struct {
name string
root string
ignoreNames map[string]struct{}
ignoreRegex *regexp.Regexp
expectedSizes map[int64]int
expectError bool
}{
{
name: "Scan all files",
root: tempDir,
ignoreNames: nil,
ignoreRegex: nil,
expectedSizes: map[int64]int{
0: 1,
100: 2, // two files of size 100
200: 1,
300: 1,
400: 2,
500: 1,
600: 1,
700: 1,
800: 1,
},
expectError: false,
},
{
name: "Ignore specific filename",
root: tempDir,
ignoreNames: map[string]struct{}{"ignored_file.txt": {}},
ignoreRegex: nil,
expectedSizes: map[int64]int{
0: 1,
100: 2,
200: 1,
400: 2,
600: 1,
700: 1,
800: 1,
},
expectError: false,
},
{
name: "Ignore by regex",
root: tempDir,
ignoreNames: nil,
ignoreRegex: regexp.MustCompile(`regex_ignore`),
expectedSizes: map[int64]int{
0: 1,
100: 2,
200: 1,
300: 1,
400: 2,
500: 1,
700: 1,
800: 1,
},
expectError: false,
},
{
name: "Ignore both by name and regex",
root: tempDir,
ignoreNames: map[string]struct{}{"ignored_file.txt": {}},
ignoreRegex: regexp.MustCompile(`regex_ignore`),
expectedSizes: map[int64]int{
0: 1,
100: 2,
200: 1,
400: 2,
700: 1,
800: 1,
},
expectError: false,
},
{
name: "Non-existent directory",
root: filepath.Join(tempDir, "non_existent_dir"),
ignoreNames: nil,
ignoreRegex: nil,
expectedSizes: nil,
expectError: true,
},
{
name: "Scan subdirectory only",
root: filepath.Join(tempDir, "subdir"),
ignoreNames: nil,
ignoreRegex: nil,
expectedSizes: map[int64]int{
400: 2,
500: 1,
},
expectError: false,
},
{
name: "Empty directory",
root: filepath.Join(tempDir, "empty_dir"),
ignoreNames: nil,
ignoreRegex: nil,
expectedSizes: map[int64]int{},
expectError: false,
},
{
name: "Ignore directory",
root: tempDir,
ignoreNames: map[string]struct{}{"ignore_dir": struct{}{}},
ignoreRegex: nil,
expectedSizes: map[int64]int{
0: 1,
100: 2,
200: 1,
300: 1,
400: 2,
500: 1,
600: 1,
700: 1,
800: 1,
},
expectError: false,
},
{
name: "Ignore file by regex",
root: tempDir,
ignoreNames: nil,
ignoreRegex: regexp.MustCompile(`.*\.tmp$`),
expectedSizes: map[int64]int{
100: 2,
200: 1,
300: 1,
400: 2,
500: 1,
600: 1,
700: 1,
800: 1,
},
expectError: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
filesBySize, err := scanDir(tc.root, tc.ignoreNames, tc.ignoreRegex)
if tc.expectError {
if err == nil {
t.Errorf("an error is expected, got: %v", err)
}
return
}
if err != nil {
t.Error(err)
}
sizeCounts := make(map[int64]int)
for size, files := range filesBySize {
sizeCounts[size] = len(files)
}
if len(tc.expectedSizes) != len(sizeCounts) {
t.Error("File size counts don't match expected values")
}
for k, v := range tc.expectedSizes {
if vv, ok := sizeCounts[k]; !ok || v != vv {
t.Error("File size counts don't match expected values")
}
}
for size, files := range filesBySize {
if len(files) > 1 {
for _, path := range files {
stat, err := os.Stat(path)
if err != nil {
t.Errorf("error: %v", err)
}
if size != stat.Size() {
t.Errorf("File %s has wrong size", path)
}
}
for _, path := range files {
_, err := os.Stat(path)
if err != nil {
t.Errorf("File path %s is not valid", path)
}
}
}
}
for _, files := range filesBySize {
for _, file := range files {
fileInfo, err := os.Lstat(file)
if err == nil && fileInfo.Mode()&os.ModeSymlink != 0 {
t.Errorf("Symlink found in results: %s", file)
}
}
}
})
}
}