github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/walker/walk_test.go (about) 1 package walker 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func Test_shouldSkipFile(t *testing.T) { 12 testCases := []struct { 13 skipFiles []string 14 skipMap map[string]bool 15 }{ 16 { 17 skipFiles: []string{filepath.Join("/etc/*")}, 18 skipMap: map[string]bool{ 19 filepath.Join("/etc/foo"): true, 20 filepath.Join("/etc/foo/bar"): false, 21 }, 22 }, 23 { 24 skipFiles: []string{filepath.Join("/etc/*/*")}, 25 skipMap: map[string]bool{ 26 filepath.Join("/etc/foo"): false, 27 filepath.Join("/etc/foo/bar"): true, 28 }, 29 }, 30 { 31 skipFiles: []string{filepath.Join("**/*.txt")}, 32 skipMap: map[string]bool{ 33 filepath.Join("/etc/foo"): false, 34 filepath.Join("/etc/foo/bar"): false, 35 filepath.Join("/var/log/bar.txt"): true, 36 }, 37 }, 38 { 39 skipFiles: []string{filepath.Join("/etc/*/*"), filepath.Join("/var/log/*.txt")}, 40 skipMap: map[string]bool{ 41 filepath.Join("/etc/foo"): false, 42 filepath.Join("/etc/foo/bar"): true, 43 filepath.Join("/var/log/bar.txt"): true, 44 }, 45 }, 46 { 47 skipFiles: []string{filepath.Join(`[^etc`)}, // filepath.Match returns ErrBadPattern 48 skipMap: map[string]bool{ 49 filepath.Join("/etc/foo"): false, 50 filepath.Join("/etc/foo/bar"): false, 51 }, 52 }, 53 } 54 55 for i, tc := range testCases { 56 t.Run(fmt.Sprint(i), func(t *testing.T) { 57 w := newWalker(tc.skipFiles, nil) 58 for file, skipResult := range tc.skipMap { 59 assert.Equal(t, skipResult, w.shouldSkipFile(filepath.ToSlash(filepath.Clean(file))), fmt.Sprintf("skipFiles: %s, file: %s", tc.skipFiles, file)) 60 } 61 }) 62 } 63 } 64 65 func Test_shouldSkipDir(t *testing.T) { 66 testCases := []struct { 67 skipDirs []string 68 skipMap map[string]bool 69 }{ 70 { 71 skipDirs: nil, 72 skipMap: map[string]bool{ 73 ".git": true, // AppDir 74 "proc": true, // SystemDir 75 "foo.bar": false, // random directory 76 }, 77 }, 78 { 79 skipDirs: []string{filepath.Join("/*")}, 80 skipMap: map[string]bool{ 81 filepath.Join("/etc"): true, 82 filepath.Join("/etc/foo/bar"): false, 83 }, 84 }, 85 { 86 skipDirs: []string{filepath.Join("/etc/*/*")}, 87 skipMap: map[string]bool{ 88 filepath.Join("/etc/foo"): false, 89 filepath.Join("/etc/foo/bar"): true, 90 }, 91 }, 92 { 93 skipDirs: []string{filepath.Join("/etc/*/*"), filepath.Join("/var/log/*")}, 94 skipMap: map[string]bool{ 95 filepath.Join("/etc/foo"): false, 96 filepath.Join("/etc/foo/bar"): true, 97 filepath.Join("/var/log/bar"): true, 98 }, 99 }, 100 { 101 skipDirs: []string{filepath.Join(`[^etc`)}, // filepath.Match returns ErrBadPattern 102 skipMap: map[string]bool{ 103 filepath.Join("/etc/foo"): false, 104 filepath.Join("/etc/foo/bar"): false, 105 }, 106 }, 107 { 108 skipDirs: []string{"**/.terraform"}, 109 skipMap: map[string]bool{ 110 ".terraform": true, 111 "test/foo/bar/.terraform": true, 112 }, 113 }, 114 } 115 116 for i, tc := range testCases { 117 t.Run(fmt.Sprint(i), func(t *testing.T) { 118 w := newWalker(nil, tc.skipDirs) 119 for dir, skipResult := range tc.skipMap { 120 assert.Equal(t, skipResult, w.shouldSkipDir(filepath.ToSlash(filepath.Clean(dir))), fmt.Sprintf("skipDirs: %s, dir: %s", tc.skipDirs, dir)) 121 } 122 }) 123 } 124 }