github.com/posener/gitfs@v1.2.2-0.20200410105819-ea4e48d73ab9/fsutil/glob_test.go (about) 1 package fsutil 2 3 import ( 4 "net/http" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 // pwd is the filesystem on which all tests run. 13 var pwd = http.Dir(".") 14 15 func TestGlobOpen(t *testing.T) { 16 t.Parallel() 17 tests := []struct { 18 patterns []string 19 matches []string 20 notMatches []string 21 }{ 22 { 23 patterns: []string{""}, 24 notMatches: []string{"testdata", "./testdata"}, 25 }, 26 { 27 patterns: []string{"testdata"}, 28 matches: []string{"testdata", "./testdata", "testdata/", "./testdata/"}, 29 }, 30 { 31 patterns: []string{"", "testdata"}, 32 matches: []string{"testdata"}, 33 }, 34 { 35 patterns: []string{"testdata", ""}, 36 matches: []string{"testdata"}, 37 }, 38 { 39 patterns: []string{"*/*1.gotmpl"}, 40 matches: []string{"testdata/tmpl1.gotmpl", "./testdata/tmpl1.gotmpl", "./testdata/tmpl1.gotmpl/"}, 41 notMatches: []string{"testdata/tmpl2.gotmpl", "./testdata/tmpl2.gotmpl", "./testdata/tmpl2.gotmpl/"}, 42 }, 43 } 44 for _, tt := range tests { 45 t.Run(strings.Join(tt.patterns, ":"), func(t *testing.T) { 46 g, err := Glob(pwd, tt.patterns...) 47 assert.NoError(t, err) 48 for _, match := range tt.matches { 49 t.Run("matches:"+match, func(t *testing.T) { 50 _, err = g.Open(match) 51 assert.NoError(t, err) 52 }) 53 } 54 for _, notMatch := range tt.notMatches { 55 t.Run("not matches:"+notMatch, func(t *testing.T) { 56 _, err = g.Open(notMatch) 57 assert.EqualError(t, err, "file does not exist") 58 }) 59 } 60 }) 61 } 62 } 63 64 func TestGlobListDir(t *testing.T) { 65 t.Parallel() 66 tests := []struct { 67 patterns []string 68 open string 69 foundFiles []string 70 }{ 71 { 72 patterns: []string{"testdata"}, 73 open: "testdata", 74 }, 75 { 76 patterns: []string{"", "testdata"}, 77 open: "testdata", 78 }, 79 { 80 patterns: []string{"testdata", ""}, 81 open: "testdata", 82 }, 83 { 84 patterns: []string{"*/*1.gotmpl"}, 85 open: "testdata", 86 foundFiles: []string{"tmpl1.gotmpl"}, 87 }, 88 { 89 patterns: []string{"*/*.gotmpl"}, 90 open: "testdata", 91 foundFiles: []string{"tmpl1.gotmpl", "tmpl2.gotmpl"}, 92 }, 93 { 94 // Extra part of path, there is no directory that fit this. 95 patterns: []string{"*/*.gotmpl/*"}, 96 open: "testdata", 97 }, 98 { 99 // No slash, only directory is available, but not the files in it. 100 patterns: []string{"*"}, 101 open: "testdata", 102 }, 103 { 104 // Matching a two components glob should match only directories. 105 patterns: []string{"*/*"}, 106 open: ".", 107 foundFiles: []string{"testdata"}, 108 }, 109 } 110 for _, tt := range tests { 111 t.Run(strings.Join(tt.patterns, ":"), func(t *testing.T) { 112 g, err := Glob(pwd, tt.patterns...) 113 assert.NoError(t, err) 114 dir, err := g.Open(tt.open) 115 require.NoError(t, err) 116 files, err := dir.Readdir(0) 117 require.NoError(t, err) 118 // Copy file names 119 names := make([]string, 0, len(files)) 120 for _, file := range files { 121 names = append(names, file.Name()) 122 } 123 assert.ElementsMatch(t, names, tt.foundFiles) 124 }) 125 } 126 } 127 128 func TestGlobOpenDir_failure(t *testing.T) { 129 t.Parallel() 130 tests := []struct { 131 patterns []string 132 open string 133 }{ 134 { 135 patterns: []string{""}, 136 open: "testdata", 137 }, 138 { 139 patterns: []string{"*"}, 140 open: "testdata1", 141 }, 142 } 143 for _, tt := range tests { 144 t.Run(strings.Join(tt.patterns, ":"), func(t *testing.T) { 145 g, err := Glob(pwd, tt.patterns...) 146 assert.NoError(t, err) 147 _, err = g.Open(tt.open) 148 require.Error(t, err) 149 }) 150 } 151 } 152 153 func TestGlobReadDir_failure(t *testing.T) { 154 t.Parallel() 155 g, err := Glob(pwd, "*/*") 156 assert.NoError(t, err) 157 f, err := g.Open("testdata/tmpl1.gotmpl") 158 require.NoError(t, err) 159 // This is a file, so Readdir should fail 160 _, err = f.Readdir(0) 161 assert.Error(t, err) 162 } 163 164 func TestGlob_badPattern(t *testing.T) { 165 t.Parallel() 166 _, err := Glob(pwd, "[") // Missing closing bracket. 167 assert.Error(t, err) 168 } 169 170 func TestGlob_noPattern(t *testing.T) { 171 t.Parallel() 172 g, err := Glob(pwd) 173 require.NoError(t, err) 174 assert.Equal(t, pwd, g) 175 }