-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
globwalk_test.go
65 lines (58 loc) · 1.58 KB
/
globwalk_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
package doublestar
import (
"io/fs"
"os"
"testing"
)
type SkipTest struct {
pattern string // pattern to test
skipOn string // a path to skip
shouldNotContain string // a path that should not match
numResults int // number of expected matches
winNumResults int // number of expected matches on windows
}
var skipTests = []SkipTest{
{"a", "a", "a", 0, 0},
{"a/", "a", "a", 1, 1},
{"*", "b", "c", 11, 9},
{"a/**", "a", "a", 0, 0},
{"a/**", "a/abc", "a/b", 1, 1},
{"a/**", "a/b/c", "a/b/c/d", 5, 5},
{"a/{**,c/*}", "a/b/c", "a/b/c/d", 5, 5},
{"a/{**,c/*}", "a/abc", "a/b", 1, 1},
}
func TestSkipDirInGlobWalk(t *testing.T) {
fsys := os.DirFS("test")
for idx, tt := range skipTests {
testSkipDirInGlobWalkWith(t, idx, tt, fsys)
}
}
func testSkipDirInGlobWalkWith(t *testing.T, idx int, tt SkipTest, fsys fs.FS) {
defer func() {
if r := recover(); r != nil {
t.Errorf("#%v. GlobWalk(%#q) panicked: %#v", idx, tt.pattern, r)
}
}()
var matches []string
hadBadMatch := false
GlobWalk(fsys, tt.pattern, func(p string, d fs.DirEntry) error {
if p == tt.skipOn {
return SkipDir
}
if p == tt.shouldNotContain {
hadBadMatch = true
}
matches = append(matches, p)
return nil
})
expected := tt.numResults
if onWindows {
expected = tt.winNumResults
}
if len(matches) != expected {
t.Errorf("#%v. GlobWalk(%#q) = %#v - should have %#v results, got %#v", idx, tt.pattern, matches, expected, len(matches))
}
if hadBadMatch {
t.Errorf("#%v. GlobWalk(%#q) should not have matched %#q, but did", idx, tt.pattern, tt.shouldNotContain)
}
}