github.com/AndrewDeryabin/doublestar/v4@v4.0.0-20230123132908-d9476b7d41be/globwalk_test.go (about) 1 package doublestar 2 3 import ( 4 "io/fs" 5 "os" 6 "testing" 7 ) 8 9 type SkipTest struct { 10 pattern string // pattern to test 11 skipOn string // a path to skip 12 shouldNotContain string // a path that should not match 13 numResults int // number of expected matches 14 winNumResults int // number of expected matches on windows 15 } 16 17 var skipTests = []SkipTest{ 18 {"a", "a", "a", 0, 0}, 19 {"a/", "a", "a", 1, 1}, 20 {"*", "b", "c", 11, 9}, 21 {"a/**", "a", "a", 0, 0}, 22 {"a/**", "a/abc", "a/b", 1, 1}, 23 {"a/**", "a/b/c", "a/b/c/d", 5, 5}, 24 {"a/{**,c/*}", "a/b/c", "a/b/c/d", 5, 5}, 25 {"a/{**,c/*}", "a/abc", "a/b", 1, 1}, 26 } 27 28 func TestSkipDirInGlobWalk(t *testing.T) { 29 fsys := os.DirFS("test") 30 for idx, tt := range skipTests { 31 testSkipDirInGlobWalkWith(t, idx, tt, fsys) 32 } 33 } 34 35 func testSkipDirInGlobWalkWith(t *testing.T, idx int, tt SkipTest, fsys fs.FS) { 36 defer func() { 37 if r := recover(); r != nil { 38 t.Errorf("#%v. GlobWalk(%#q) panicked: %#v", idx, tt.pattern, r) 39 } 40 }() 41 42 var matches []string 43 hadBadMatch := false 44 GlobWalk(fsys, tt.pattern, func(p string, d fs.DirEntry) error { 45 if p == tt.skipOn { 46 return SkipDir 47 } 48 if p == tt.shouldNotContain { 49 hadBadMatch = true 50 } 51 matches = append(matches, p) 52 return nil 53 }) 54 55 expected := tt.numResults 56 if onWindows { 57 expected = tt.winNumResults 58 } 59 if len(matches) != expected { 60 t.Errorf("#%v. GlobWalk(%#q) = %#v - should have %#v results, got %#v", idx, tt.pattern, matches, expected, len(matches)) 61 } 62 if hadBadMatch { 63 t.Errorf("#%v. GlobWalk(%#q) should not have matched %#q, but did", idx, tt.pattern, tt.shouldNotContain) 64 } 65 }