-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_test.go
186 lines (169 loc) · 5.04 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
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
package find
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestFind(t *testing.T) {
basePath := "/tmp/test-go-find"
if err := os.RemoveAll(basePath); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(basePath, "foo", "bar", "baz"), os.FileMode(int(0777))); err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(basePath)
}()
if err := os.MkdirAll(filepath.Join(basePath, "some", "other", "dir", "foo"), os.FileMode(int(0777))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "file0.txt"), nil, os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "file1.txt"), []byte("hello here 1\n"), os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "file2.txt"), []byte("hello here 2\n"), os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "foo", "file1.txt"), []byte("hello there 1\n"), os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "foo", "file2.txt"), []byte("hello there 2\n"), os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "foo", "bar", "baz", "file1.txt"), []byte("hello over there 1\n"), os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "some", "other", "file0.txt"), nil, os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(basePath, "some", "other", "dir", "null.txt"), nil, os.FileMode(int(0600))); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(basePath, "some", "other", "dir", "null.txt"), filepath.Join(basePath, "lnk")); err != nil {
t.Fatal(err)
}
// No predicates
{
hits, err := NewFind(basePath).Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 17, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// MinDepth
{
hits, err := NewFind(basePath).MinDepth(1).Name("file1.txt").Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 2, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// MaxDepth
{
hits, err := NewFind(basePath).MaxDepth(1).Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 6, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Name (exact)
{
hits, err := NewFind(basePath).Name("file1.txt").Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 3, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Name (glob)
{
hits, err := NewFind(basePath).Name("*1.txt").Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 3, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// MaxDepth + Name
{
hits, err := NewFind(basePath).MaxDepth(1).Name("file1.txt").Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 1, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Wholename
{
hits, err := NewFind(basePath).WholeName("*foo*2.txt").Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 1, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Type dir
{
hits, err := NewFind(basePath).Type("d").Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 8, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Type file
{
hits, err := NewFind(basePath).Type("d").Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 8, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Empty
{
hits, err := NewFind(basePath).Empty().Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 4, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Empty dirs
{
hits, err := NewFind(basePath).Type("d").Empty().Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 1, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
// Empty files
{
hits, err := NewFind(basePath).Type("f").Empty().Evaluate()
if err != nil {
t.Fatal(err)
}
if expected, actual := 3, len(hits); actual != expected {
t.Errorf("Expected num–hits=%v but actual=%v\nHits: % #v)\n", expected, actual, hits)
}
}
}