-
Notifications
You must be signed in to change notification settings - Fork 142
/
find_test.go
124 lines (103 loc) · 2.87 KB
/
find_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
package the_platinum_searcher
import (
"path/filepath"
"regexp"
"testing"
)
func TestFind(t *testing.T) {
out := make(chan string)
find := find{out, defaultOption()}
go find.start([]string{"files"}, nil)
testPath := makeAssertPaths(out)
// Ensure these files were not returned
if e := "files/.hidden/hidden.txt"; testPath(e) {
t.Errorf("Found %s, It should not contains file under hidden directory.", e)
}
// Enumerate found paths and ensure a couple of them are in there.
if e := "files/ascii.txt"; !testPath(e) {
t.Errorf("Find failed to locate: %s", e)
}
if e := "files/depth/file_1.txt"; !testPath(e) {
t.Errorf("Find failed to locate: %s", e)
}
}
func TestFindWithHidden(t *testing.T) {
out := make(chan string)
opt := defaultOption()
opt.SearchOption.Hidden = true
find := find{out, opt}
go find.start([]string{"files"}, nil)
testPath := makeAssertPaths(out)
// Enumerate found paths and ensure a couple of them are in there.
if e := "files/.hidden/hidden.txt"; !testPath(e) {
t.Errorf("Find failed to locate: %s", e)
}
if e := "files/.hidden/.hidden.txt"; !testPath(e) {
t.Errorf("Find failed to locate: %s", e)
}
}
func TestFindWithIgnore(t *testing.T) {
out := make(chan string)
opt := defaultOption()
opt.SearchOption.VcsIgnore = []string{".vcsignore"}
find := find{out, opt}
go find.start([]string{"files/vcs"}, nil)
testPath := makeAssertPaths(out)
ignores := []string{
"match/ignore.txt",
"ignore/ignore.txt",
"absolute/ignore.txt",
}
for _, ignore := range ignores {
// Ensure these files were not returned
if e := "files/vcs/" + ignore; testPath(e) {
t.Errorf("Found %s, It should not contains ignore file.", e)
}
}
}
func TestFindWithDepth(t *testing.T) {
out := make(chan string)
opt := defaultOption()
opt.SearchOption.Depth = 1
find := find{out, opt}
go find.start([]string{"files/depth"}, nil)
testPath := makeAssertPaths(out)
// Ensure these files were not returned
if e := "files/depth/dir_1/dir_2/file_3.txt"; testPath(e) {
t.Errorf("Found %s, It should not contains file from over max depth.", e)
}
}
func TestFindWithFileSearchPattern(t *testing.T) {
out := make(chan string)
find := find{out, defaultOption()}
go find.start([]string{"files/vcs/match"}, regexp.MustCompile("match.txt"))
testPath := makeAssertPaths(out)
// Ensure these files were not returned
if e := "files/vcs/match/ignore.txt"; testPath(e) {
t.Errorf("Found %s, It should not contains no match file.", e)
}
}
func makeAssertPaths(ch chan string) func(f string) bool {
var list []string
for path := range ch {
list = append(list, filepath.ToSlash(path))
}
return func(m string) bool {
for _, s := range list {
if m == s {
return true
}
}
return false
}
}
func defaultOption() Option {
return Option{
OutputOption: &OutputOption{
EnableLineNumber: true,
},
SearchOption: &SearchOption{
Depth: 25,
},
}
}