-
Notifications
You must be signed in to change notification settings - Fork 1
/
directory_test.go
76 lines (67 loc) · 1.78 KB
/
directory_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
package scrubber
import (
"os"
"testing"
)
// TestExclude checks if exclude rules are applied correctly.
func TestExclude(t *testing.T) {
fs := &mockedFs{
files: []os.FileInfo{
mockedFileInfo{name: "include.txt"},
mockedFileInfo{name: "include.pdf"},
mockedFileInfo{name: "exclude.exe"},
mockedFileInfo{name: "exclude.zip"},
},
}
d := newDirectoryScanner(&directory{
Name: "Logs", Path: testPath, Exclude: []string{"zip", "exe"},
}, fs)
files, err := d.getFiles()
if err != nil {
t.Errorf("Failed to load files: %s", err)
}
files = d.filterFiles(files)
checkInclusion(files, t)
}
// TestInclude checks if exclude rules are applied correctly.
func TestInclude(t *testing.T) {
fs := &mockedFs{
files: []os.FileInfo{
mockedFileInfo{name: "include.txt"},
mockedFileInfo{name: "include.pdf"},
mockedFileInfo{name: "exclude.exe"},
mockedFileInfo{name: "exclude.zip"},
},
}
d := newDirectoryScanner(&directory{
Name: "Logs", Path: testPath, Include: []string{"txt", "pdf"},
}, fs)
files, err := d.getFiles()
if err != nil {
t.Errorf("Failed to load files: %s", err)
}
files = d.filterFiles(files)
checkInclusion(files, t)
}
// checkInclusion is a helper function to check if the include/exclude rules were applied correctly.
func checkInclusion(files []os.FileInfo, t *testing.T) {
gotIncludeTxt := false
gotIncludePdf := false
for _, f := range files {
if f.Name() == "include.txt" {
gotIncludeTxt = true
}
if f.Name() == "include.pdf" {
gotIncludePdf = true
}
if f.Name() == "exclude.exe" || f.Name() == "exclude.zip" {
t.Errorf("file \"%s\" shoud be excluded.\n", f.Name())
}
}
if !gotIncludeTxt {
t.Errorf("file 'include.txt' shoud be included.\n")
}
if !gotIncludePdf {
t.Errorf("file 'include.pdf' shoud be included.\n")
}
}