-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner_test.go
More file actions
373 lines (329 loc) · 9.81 KB
/
scanner_test.go
File metadata and controls
373 lines (329 loc) · 9.81 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
package main
import (
"bytes"
"errors"
"flag"
"io"
"io/fs"
"os"
"strings"
"testing"
"time"
)
type testDirEntry struct {
name string
isDir bool
fileMode fs.FileMode
fileInfo fs.FileInfo
err error
}
func (tde *testDirEntry) Name() string { return tde.name }
func (tde *testDirEntry) IsDir() bool { return tde.isDir }
func (tde *testDirEntry) Type() fs.FileMode { return tde.fileMode }
func (tde *testDirEntry) Info() (fs.FileInfo, error) { return tde.fileInfo, tde.err }
type testFileInfo struct {
name string
size int64
mode fs.FileMode
modTime time.Time
isDir bool
sys any
}
func (tfi *testFileInfo) Name() string { return tfi.name }
func (tfi *testFileInfo) Size() int64 { return tfi.size }
func (tfi *testFileInfo) Mode() fs.FileMode { return tfi.mode }
func (tfi *testFileInfo) ModTime() time.Time { return tfi.modTime }
func (tfi *testFileInfo) IsDir() bool { return tfi.isDir }
func (tfi *testFileInfo) Sys() any { return tfi.sys }
type testDir struct {
err error
dirents []fs.DirEntry
}
func (td *testDir) readDir() ([]fs.DirEntry, error) {
return td.dirents, td.err
}
func testScannerSetup(cfg *config, stderr io.Writer, ccCount int) (*scanner, *candidates, error) {
cfg.setInternalDefaults()
err := cfg.compile()
if err != nil {
return nil, nil, err
}
cc := newConcurrencyController(ccCount)
now := time.Now()
can := newCandidates(20, age{})
scn := newScanner(cfg, cc, can, now, stderr)
return scn, can, err
}
func TestStats(t *testing.T) {
var s1 stats
if s1.sum() != 0 {
t.Error("Initial state should be zero, not", s1.sum())
}
s1.errorCount++
if s1.sum() != 1 {
t.Error("error++ should be 1, not", s1.sum())
}
s1.zero()
if s1.sum() != 0 {
t.Error("zero() did not zero", s1.sum(), s1)
}
}
// Test for readDir error return
func TestScannerReaddir(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
scn, _, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
td := testDir{err: errors.New("Error One")}
scn.rdf = func(name string) ([]fs.DirEntry, error) { return td.readDir() }
scn.scan(0, "testdata")
scn.wait()
if scn.stats.errorCount != 1 || scn.stats.ignoreCount != 1 || scn.stats.sum() != 2 {
t.Error("Expected error,ignore,sum == '1 1 2' not",
scn.stats.errorCount, scn.stats.ignoreCount, scn.stats.sum(), scn.stats)
}
got := stderr.String()
exp := "Error One\n"
if !strings.Contains(got, exp) {
t.Errorf("Expected stderr to contain '%s' got '%s'\n", exp, got)
}
}
// Test for empty directory
func TestScannerEmpty(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
scn, _, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
td := testDir{}
scn.rdf = func(name string) ([]fs.DirEntry, error) { return td.readDir() }
scn.scan(0, "testdata")
scn.wait()
if scn.stats.ignoreCount != 1 || scn.stats.sum() != 1 {
t.Error("Empty dir should only set ignoreCount", scn.stats)
}
}
// Test for dirents.Info() error return
func TestScannerDirents(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
scn, _, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
td := testDir{}
td.dirents = append(td.dirents, &testDirEntry{err: errors.New("td error one")})
scn.rdf = func(name string) ([]fs.DirEntry, error) { return td.readDir() }
scn.scan(0, "testdata")
scn.wait()
exp := stderr.String()
if scn.stats.errorCount != 1 || !strings.Contains(exp, "error one") {
t.Error("dirents.Info should have returned error", scn.stats, exp)
}
}
// Test for ignoring irregular
func TestScannerIrregular(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
scn, _, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
td := testDir{}
tde := &testDirEntry{fileMode: fs.ModeIrregular,
fileInfo: &testFileInfo{name: "testfile", mode: fs.ModeIrregular}}
td.dirents = append(td.dirents, tde)
scn.rdf = func(name string) ([]fs.DirEntry, error) { return td.readDir() }
scn.scan(0, "testdata")
scn.wait()
if scn.stats.errorCount != 0 || scn.stats.otherCount != 1 {
t.Error("scan did not detect irregular", scn.stats)
}
}
// Test for ignoring candidate
func TestScannerCandidate(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
cfg.ignoreBases.v = "testfileIgnore"
cfg.printIgnored.v = true
scn, _, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
td := testDir{}
tde := &testDirEntry{fileInfo: &testFileInfo{name: "testfileIgnore"}}
td.dirents = append(td.dirents, tde)
scn.rdf = func(name string) ([]fs.DirEntry, error) { return td.readDir() }
scn.scan(0, "testdata")
scn.wait()
if scn.stats.errorCount != 0 || scn.stats.ignoreCount != 2 {
t.Error("scan didn't ignore parent dir or ignoreBases string", scn.stats)
}
got := stderr.String()
for _, exp := range []string{"Ignored bases:", "Ignored type d:"} {
if !strings.Contains(got, exp) {
t.Errorf("Ignore message is not prefixed with '%s' got '%s'\n", exp, got)
}
}
}
func TestScannerMaxDepth(t *testing.T) {
testCases := []struct {
maxDepth uint
expectedCandidates int
}{
{0, 3},
{1, 0}, // No files in testdata/maxdir, just subdirs
{2, 1},
{3, 2},
{4, 3},
{5, 3},
{10, 3},
}
for ix, tc := range testCases {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
cfg.suppressErrors.v = false
cfg.maxDepth.v = tc.maxDepth
scn, can, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
scn.descend(0, "testdata/maxdir")
scn.wait()
got := len(can.cf)
if got != tc.expectedCandidates {
t.Error(ix, "Candidates expected", tc.expectedCandidates, "got", got)
}
}
}
func TestScannerMaxAge(t *testing.T) {
testCases := []struct {
name string
offset time.Duration
}{
{"3min", -3 * time.Minute},
{"chickenDinner", -1 * time.Minute},
{"2min", -2 * time.Minute},
}
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
cfg.maxAge.seconds = 120
scn, can, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
now := time.Now()
td := testDir{}
for _, tc := range testCases {
tde := &testDirEntry{fileInfo: &testFileInfo{name: tc.name, modTime: now.Add(tc.offset)}}
td.dirents = append(td.dirents, tde)
}
scn.rdf = func(name string) ([]fs.DirEntry, error) { return td.readDir() }
scn.scan(0, ".")
scn.wait()
if len(can.cf) != 1 {
t.Error("Expected one candidate got", len(can.cf))
}
if can.cf[0].path != "chickenDinner" { // Is it a "winner winner"?
t.Error("Expected a Chicken Dinner. Got", can.cf[0])
}
}
func TestScannerScanError(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
scn, _, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
scn.scan(0, "testdata/noexist")
scn.wait()
if scn.stats.errorCount != 1 || scn.stats.sum() != 1 {
t.Error("Expected error,sum == '1 1' not",
scn.stats.errorCount, scn.stats.sum(), scn.stats)
}
got := stderr.String()
exp := "no such file"
if !strings.Contains(got, exp) {
t.Error("Expected", exp, "Got", got)
}
}
// Test that starting directory gets set as youngest if directory entries are not ignored.
func TestScannerScanYoungest(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
cfg.ignoreTypes.v = fTypeTemporary // Needs to be set to avoid defaults
cfg.maxDepth.v = 1
scn, can, err := testScannerSetup(cfg, &stderr, 10)
if err != nil {
t.Fatal(err)
}
scn.descend(0, "testdata")
scn.wait()
if scn.stats.dirCount != 1 || scn.stats.sum() != 1 {
t.Error("Expected dir,sum == '1 1' not",
scn.stats.dirCount, scn.stats.sum(), scn.stats)
}
if len(can.cf) != 1 {
t.Fatal("Expected one candidate, not", len(can.cf))
}
}
// Test that starting directory gets set as youngest if directory entries are not ignored.
func TestScannerIgnoreTypes(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
cfg.ignoreTypes.v = fTypeDir + "," + fTypeFile
cfg.printIgnored.v = true
cfg.maxDepth.v = 2
scn, can, err := testScannerSetup(cfg, &stderr, 1)
if err != nil {
t.Fatal(err)
}
scn.descend(0, "testdata")
scn.wait()
if scn.stats.dirCount != scn.stats.ignoreCount {
t.Error("Expected dir == ignore, not",
scn.stats.dirCount, scn.stats.ignoreCount, scn.stats)
}
if len(can.cf) != 0 {
t.Fatal("Did not expect any candidates, but got", len(can.cf))
}
got := stderr.String()
exp := "Ignored type d"
if !strings.Contains(got, exp) {
t.Error("Expected", exp, "got", got)
}
}
func testFsfError(f *os.File) (fs.FileInfo, error) {
return nil, errors.New("fsf Failed")
}
// Test for fstat() failure
func TestScannerStatFail(t *testing.T) {
var stderr bytes.Buffer
cfg := newConfig(flag.NewFlagSet(Name, flag.ContinueOnError),
func() (string, error) { return "", nil })
scn, _, err := testScannerSetup(cfg, &stderr, 1)
if err != nil {
t.Fatal(err)
}
scn.fsf = testFsfError
scn.descend(0, "testdata")
scn.wait()
got := stderr.String()
exp := "Getting File Stat"
if !strings.Contains(got, exp) {
t.Error("Expected", exp, "got", got)
}
}