github.com/Jeffail/benthos/v3@v3.65.0/internal/filepath/glob_test.go (about) 1 package filepath 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestGlobPatterns(t *testing.T) { 13 dirStructure := []string{ 14 `src/cats/a.js`, 15 `src/cats/b.js`, 16 `src/cats/b.txt`, 17 `src/cats/toys`, 18 `src/cats/meows/c.js`, 19 `src/cats/meows/c.js.tmp`, 20 } 21 22 tmpDir := t.TempDir() 23 24 for _, path := range dirStructure { 25 tmpPath := filepath.Join(tmpDir, path) 26 if filepath.Ext(tmpPath) == "" { 27 require.NoError(t, os.MkdirAll(tmpPath, 0o755)) 28 } else { 29 require.NoError(t, os.MkdirAll(filepath.Dir(tmpPath), 0o755)) 30 require.NoError(t, os.WriteFile(tmpPath, []byte("keep me"), 0o755)) 31 } 32 } 33 34 tests := []struct { 35 pattern string 36 matches []string 37 }{ 38 { 39 pattern: `/src/cats/*.js`, 40 matches: []string{ 41 `src/cats/a.js`, 42 `src/cats/b.js`, 43 }, 44 }, 45 { 46 pattern: `/src/cats/a.js`, 47 matches: []string{ 48 `src/cats/a.js`, 49 }, 50 }, 51 { 52 pattern: `/src/cats/z.js`, 53 matches: []string{ 54 `src/cats/z.js`, 55 }, 56 }, 57 { 58 pattern: `/src/**/a.js`, 59 matches: []string{ 60 `src/cats/a.js`, 61 }, 62 }, 63 { 64 pattern: `/src/**/*.js`, 65 matches: []string{ 66 `src/cats/a.js`, 67 `src/cats/b.js`, 68 `src/cats/meows/c.js`, 69 }, 70 }, 71 { 72 pattern: `/src/**/*`, 73 matches: []string{ 74 `src/cats/a.js`, 75 `src/cats/b.js`, 76 `src/cats/b.txt`, 77 `src/cats/meows/c.js`, 78 `src/cats/meows/c.js.tmp`, 79 }, 80 }, 81 } 82 83 for _, test := range tests { 84 t.Run(test.pattern, func(t *testing.T) { 85 matches, err := Globs([]string{tmpDir + test.pattern}) 86 require.NoError(t, err) 87 88 for i, match := range matches { 89 matches[i], err = filepath.Rel(tmpDir, match) 90 require.NoError(t, err) 91 } 92 assert.ElementsMatch(t, test.matches, matches) 93 }) 94 } 95 }