forked from bmatcuk/doublestar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublestar_test.go
355 lines (320 loc) · 10.8 KB
/
doublestar_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
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
// This file is mostly copied from Go's path/match_test.go
package doublestar
import (
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
)
type MatchTest struct {
pattern, testPath string // a pattern and path to test the pattern on
shouldMatch bool // true if the pattern should match the path
expectedErr error // an expected error
testOnDisk bool // true: test pattern against files in "test" directory
}
// Tests which contain escapes and symlinks will not work on Windows
var onWindows = runtime.GOOS == "windows"
var matchTests = []MatchTest{
{"*", "", true, nil, false},
{"*", "/", false, nil, false},
{"/*", "/", true, nil, false},
{"/*", "/debug/", false, nil, false},
{"/*", "//", false, nil, false},
{"abc", "abc", true, nil, true},
{"*", "abc", true, nil, true},
{"*c", "abc", true, nil, true},
{"a*", "a", true, nil, true},
{"a*", "abc", true, nil, true},
{"a*", "ab/c", false, nil, true},
{"a*/b", "abc/b", true, nil, true},
{"a*/b", "a/c/b", false, nil, true},
{"a*b*c*d*e*", "axbxcxdxe", true, nil, true},
{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil, true},
{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil, true},
{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil, true},
{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil, true},
{"a*b?c*x", "abxbbxdbxebxczzx", true, nil, true},
{"a*b?c*x", "abxbbxdbxebxczzy", false, nil, true},
{"ab[c]", "abc", true, nil, true},
{"ab[b-d]", "abc", true, nil, true},
{"ab[e-g]", "abc", false, nil, true},
{"ab[^c]", "abc", false, nil, true},
{"ab[^b-d]", "abc", false, nil, true},
{"ab[^e-g]", "abc", true, nil, true},
{"a\\*b", "ab", false, nil, true},
{"a?b", "a☺b", true, nil, true},
{"a[^a]b", "a☺b", true, nil, true},
{"a???b", "a☺b", false, nil, true},
{"a[^a][^a][^a]b", "a☺b", false, nil, true},
{"[a-ζ]*", "α", true, nil, true},
{"*[a-ζ]", "A", false, nil, true},
{"a?b", "a/b", false, nil, true},
{"a*b", "a/b", false, nil, true},
{"[\\]a]", "]", true, nil, !onWindows},
{"[\\-]", "-", true, nil, !onWindows},
{"[x\\-]", "x", true, nil, !onWindows},
{"[x\\-]", "-", true, nil, !onWindows},
{"[x\\-]", "z", false, nil, !onWindows},
{"[\\-x]", "x", true, nil, !onWindows},
{"[\\-x]", "-", true, nil, !onWindows},
{"[\\-x]", "a", false, nil, !onWindows},
{"[]a]", "]", false, ErrBadPattern, true},
{"[-]", "-", false, ErrBadPattern, true},
{"[x-]", "x", false, ErrBadPattern, true},
{"[x-]", "-", false, ErrBadPattern, true},
{"[x-]", "z", false, ErrBadPattern, true},
{"[-x]", "x", false, ErrBadPattern, true},
{"[-x]", "-", false, ErrBadPattern, true},
{"[-x]", "a", false, ErrBadPattern, true},
{"\\", "a", false, ErrBadPattern, !onWindows},
{"[a-b-c]", "a", false, ErrBadPattern, true},
{"[", "a", false, ErrBadPattern, true},
{"[^", "a", false, ErrBadPattern, true},
{"[^bc", "a", false, ErrBadPattern, true},
{"a[", "a", false, nil, false},
{"a[", "ab", false, ErrBadPattern, true},
{"*x", "xxx", true, nil, true},
{"[abc]", "b", true, nil, true},
{"a/**", "a", false, nil, true},
{"a/**", "a/b", true, nil, true},
{"a/**", "a/b/c", true, nil, true},
{"**/c", "c", true, nil, true},
{"**/c", "b/c", true, nil, true},
{"**/c", "a/b/c", true, nil, true},
{"**/c", "a/b", false, nil, true},
{"**/c", "abcd", false, nil, true},
{"**/c", "a/abc", false, nil, true},
{"a/**/b", "a/b", true, nil, true},
{"a/**/c", "a/b/c", true, nil, true},
{"a/**/d", "a/b/c/d", true, nil, true},
{"a/\\**", "a/b/c", false, nil, !onWindows},
// this is an odd case: filepath.Glob() will return results
{"a//b/c", "a/b/c", false, nil, false},
{"a/b/c", "a/b//c", false, nil, true},
// also odd: Glob + filepath.Glob return results
{"a/", "a", false, nil, false},
{"ab{c,d}", "abc", true, nil, true},
{"ab{c,d,*}", "abcde", true, nil, true},
{"ab{c,d}[", "abcd", false, ErrBadPattern, true},
{"a{,bc}", "a", true, nil, true},
{"a{,bc}", "abc", true, nil, true},
{"a/{b/c,c/b}", "a/b/c", true, nil, true},
{"a/{b/c,c/b}", "a/c/b", true, nil, true},
{"{a/{b,c},abc}", "a/b", true, nil, true},
{"{a/{b,c},abc}", "a/c", true, nil, true},
{"{a/{b,c},abc}", "abc", true, nil, true},
{"{a/{b,c},abc}", "a/b/c", false, nil, true},
{"{a/ab*}", "a/abc", true, nil, true},
{"{a/*}", "a/b", true, nil, true},
{"{a/abc}", "a/abc", true, nil, true},
{"{a/b,a/c}", "a/c", true, nil, true},
{"abc/**", "abc/b", true, nil, true},
{"**/abc", "abc", true, nil, true},
{"abc**", "abc/b", false, nil, true},
{"broken-symlink", "broken-symlink", true, nil, !onWindows},
{"working-symlink/c/*", "working-symlink/c/d", true, nil, !onWindows},
{"working-sym*/*", "working-symlink/c", true, nil, !onWindows},
{"b/**/f", "b/symlink-dir/f", true, nil, !onWindows},
}
func TestMatch(t *testing.T) {
for idx, tt := range matchTests {
// Since Match() always uses "/" as the separator, we
// don't need to worry about the tt.testOnDisk flag
testMatchWith(t, idx, tt)
}
}
func testMatchWith(t *testing.T, idx int, tt MatchTest) {
defer func() {
if r := recover(); r != nil {
t.Errorf("#%v. Match(%#q, %#q) panicked: %#v", idx, tt.pattern, tt.testPath, r)
}
}()
// Match() always uses "/" as the separator
ok, err := Match(tt.pattern, tt.testPath)
if ok != tt.shouldMatch || err != tt.expectedErr {
t.Errorf("#%v. Match(%#q, %#q) = %v, %v want %v, %v", idx, tt.pattern, tt.testPath, ok, err, tt.shouldMatch, tt.expectedErr)
}
if isStandardPattern(tt.pattern) {
stdOk, stdErr := path.Match(tt.pattern, tt.testPath)
if ok != stdOk || !compareErrors(err, stdErr) {
t.Errorf("#%v. Match(%#q, %#q) != path.Match(...). Got %v, %v want %v, %v", idx, tt.pattern, tt.testPath, ok, err, stdOk, stdErr)
}
}
}
func TestPathMatch(t *testing.T) {
for idx, tt := range matchTests {
// Even though we aren't actually matching paths on disk, we are using
// PathMatch() which will use the system's separator. As a result, any
// patterns that might cause problems on-disk need to also be avoided
// here in this test.
if tt.testOnDisk {
testPathMatchWith(t, idx, tt)
}
}
}
func testPathMatchWith(t *testing.T, idx int, tt MatchTest) {
defer func() {
if r := recover(); r != nil {
t.Errorf("#%v. Match(%#q, %#q) panicked: %#v", idx, tt.pattern, tt.testPath, r)
}
}()
pattern := filepath.FromSlash(tt.pattern)
testPath := filepath.FromSlash(tt.testPath)
ok, err := PathMatch(pattern, testPath)
if ok != tt.shouldMatch || err != tt.expectedErr {
t.Errorf("#%v. Match(%#q, %#q) = %v, %v want %v, %v", idx, pattern, testPath, ok, err, tt.shouldMatch, tt.expectedErr)
}
if isStandardPattern(tt.pattern) {
stdOk, stdErr := filepath.Match(pattern, testPath)
if ok != stdOk || !compareErrors(err, stdErr) {
t.Errorf("#%v. PathMatch(%#q, %#q) != filepath.Match(...). Got %v, %v want %v, %v", idx, pattern, testPath, ok, err, stdOk, stdErr)
}
}
}
func TestGlob(t *testing.T) {
abspath, err := os.Getwd()
if err != nil {
t.Errorf("Error getting current working directory: %v", err)
return
}
abspath = filepath.Join(abspath, "test")
for idx, tt := range matchTests {
if tt.testOnDisk {
// test both relative paths and absolute paths
testGlobWith(t, idx, tt, "test")
testGlobWith(t, idx, tt, abspath)
volumeName := filepath.VolumeName(abspath)
if volumeName != "" && !strings.HasPrefix(volumeName, `\\`) {
testGlobWith(t, idx, tt, strings.TrimPrefix(abspath, volumeName))
}
}
}
}
func testGlobWith(t *testing.T, idx int, tt MatchTest, basepath string) {
defer func() {
if r := recover(); r != nil {
t.Errorf("#%v. Glob(%#q) panicked: %#v", idx, tt.pattern, r)
}
}()
pattern := joinWithoutClean(basepath, filepath.FromSlash(tt.pattern))
testPath := joinWithoutClean(basepath, filepath.FromSlash(tt.testPath))
matches, err := Glob(pattern)
if inSlice(testPath, matches) != tt.shouldMatch {
if tt.shouldMatch {
t.Errorf("#%v. Glob(%#q) = %#v - doesn't contain %v, but should", idx, pattern, matches, tt.testPath)
} else {
t.Errorf("#%v. Glob(%#q) = %#v - contains %v, but shouldn't", idx, pattern, matches, tt.testPath)
}
}
if err != tt.expectedErr {
t.Errorf("#%v. Glob(%#q) has error %v, but should be %v", idx, pattern, err, tt.expectedErr)
}
if isStandardPattern(tt.pattern) {
stdMatches, stdErr := filepath.Glob(pattern)
if !compareSlices(matches, stdMatches) || !compareErrors(err, stdErr) {
t.Errorf("#%v. Glob(%#q) != filepath.Glob(...). Got %#v, %v want %#v, %v", idx, pattern, matches, err, stdMatches, stdErr)
}
}
}
func joinWithoutClean(elem ...string) string {
return strings.Join(elem, string(os.PathSeparator))
}
func isStandardPattern(pattern string) bool {
return !strings.Contains(pattern, "**") && indexRuneWithEscaping(pattern, '{') == -1
}
func compareErrors(a, b error) bool {
if a == nil {
return b == nil
}
return b != nil
}
func inSlice(s string, a []string) bool {
for _, i := range a {
if i == s {
return true
}
}
return false
}
func compareSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
diff := make(map[string]int, len(a))
for _, x := range a {
diff[x]++
}
for _, y := range b {
if _, ok := diff[y]; !ok {
return false
}
diff[y]--
if diff[y] == 0 {
delete(diff, y)
}
}
return len(diff) == 0
}
func mkdirp(parts ...string) {
dirs := path.Join(parts...)
err := os.MkdirAll(dirs, 0755)
if err != nil {
log.Fatalf("Could not create test directories %v: %v\n", dirs, err)
}
}
func touch(parts ...string) {
filename := path.Join(parts...)
f, err := os.Create(filename)
if err != nil {
log.Fatalf("Could not create test file %v: %v\n", filename, err)
}
f.Close()
}
func symlink(oldname, newname string) {
// since this will only run on non-windows, we can assume "/" as path separator
err := os.Symlink(oldname, newname)
if err != nil && !os.IsExist(err) {
log.Fatalf("Could not create symlink %v -> %v: %v\n", oldname, newname, err)
}
}
func TestMain(m *testing.M) {
// create the test directory
mkdirp("test", "a", "b", "c")
mkdirp("test", "a", "c")
mkdirp("test", "abc")
mkdirp("test", "axbxcxdxe", "xxx")
mkdirp("test", "axbxcxdxexxx")
mkdirp("test", "b")
// create test files
touch("test", "a", "abc")
touch("test", "a", "b", "c", "d")
touch("test", "a", "c", "b")
touch("test", "abc", "b")
touch("test", "abcd")
touch("test", "abcde")
touch("test", "abxbbxdbxebxczzx")
touch("test", "abxbbxdbxebxczzy")
touch("test", "axbxcxdxe", "f")
touch("test", "axbxcxdxe", "xxx", "f")
touch("test", "axbxcxdxexxx", "f")
touch("test", "axbxcxdxexxx", "fff")
touch("test", "a☺b")
touch("test", "b", "c")
touch("test", "c")
touch("test", "x")
touch("test", "xxx")
touch("test", "z")
touch("test", "α")
if !onWindows {
// these files/symlinks won't work on Windows
touch("test", "-")
touch("test", "]")
symlink("../axbxcxdxe/", "test/b/symlink-dir")
symlink("/tmp/nonexistant-file-20160902155705", "test/broken-symlink")
symlink("a/b", "test/working-symlink")
}
os.Exit(m.Run())
}