|
| 1 | +package excludes |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "go.podman.io/storage/pkg/fileutils" |
| 9 | +) |
| 10 | + |
| 11 | +func TestShouldDescendExcludedDir(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + path string |
| 15 | + patterns []string |
| 16 | + want bool |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "nil matcher", |
| 20 | + path: "cmd", |
| 21 | + patterns: nil, |
| 22 | + want: false, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "no exclusions", |
| 26 | + path: "cmd", |
| 27 | + patterns: []string{"*"}, |
| 28 | + want: false, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "literal prefix match", |
| 32 | + path: "cmd", |
| 33 | + patterns: []string{"*", "!cmd/main.go"}, |
| 34 | + want: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "literal prefix no match", |
| 38 | + path: "other", |
| 39 | + patterns: []string{"*", "!cmd/main.go"}, |
| 40 | + want: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "double star at start matches any dir", |
| 44 | + path: "cmd", |
| 45 | + patterns: []string{"**", "!**/*.go"}, |
| 46 | + want: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "double star at start matches nested dir", |
| 50 | + path: "cmd/sub", |
| 51 | + patterns: []string{"**", "!**/*.go"}, |
| 52 | + want: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "double star with prefix matches dir under prefix", |
| 56 | + path: "cmd/sub", |
| 57 | + patterns: []string{"**", "!cmd/**/*.go"}, |
| 58 | + want: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "double star with prefix no match for other dir", |
| 62 | + path: "other", |
| 63 | + patterns: []string{"**", "!cmd/**/*.go"}, |
| 64 | + want: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "single star at start matches any dir", |
| 68 | + path: "cmd", |
| 69 | + patterns: []string{"*", "!*/*.go"}, |
| 70 | + want: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "single star at start matches nested dir", |
| 74 | + path: "cmd/sub", |
| 75 | + patterns: []string{"*", "!*/*.go"}, |
| 76 | + want: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "single star with prefix matches dir under prefix", |
| 80 | + path: "src/pkg", |
| 81 | + patterns: []string{"**", "!src/*/*.go"}, |
| 82 | + want: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "single star with prefix no match for other dir", |
| 86 | + path: "other", |
| 87 | + patterns: []string{"**", "!src/*/*.go"}, |
| 88 | + want: false, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "leading slash is stripped", |
| 92 | + path: "/cmd", |
| 93 | + patterns: []string{"*", "!cmd/main.go"}, |
| 94 | + want: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "deep nested with double star prefix", |
| 98 | + path: "src/internal/pkg", |
| 99 | + patterns: []string{"**", "!src/**/*.go"}, |
| 100 | + want: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "dir prefix match is not a partial match", |
| 104 | + path: "cmds", |
| 105 | + patterns: []string{"*", "!cmd/main.go"}, |
| 106 | + want: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "wildcard mid-segment descends parent dir", |
| 110 | + path: "cmd/images", |
| 111 | + patterns: []string{"**", "!cmd/image*/main.go"}, |
| 112 | + want: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "wildcard mid-segment matches parent", |
| 116 | + path: "cmd", |
| 117 | + patterns: []string{"**", "!cmd/image*"}, |
| 118 | + want: true, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "question mark wildcard matches any dir", |
| 122 | + path: "cmd", |
| 123 | + patterns: []string{"**", "!cm?/*.go"}, |
| 124 | + want: true, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "question mark wildcard no literal prefix matches any dir", |
| 128 | + path: "other", |
| 129 | + patterns: []string{"**", "!?md/*.go"}, |
| 130 | + want: true, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "bracket wildcard matches dir", |
| 134 | + path: "cmd", |
| 135 | + patterns: []string{"**", "!cm[d]/*.go"}, |
| 136 | + want: true, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "bracket wildcard no literal prefix matches any dir", |
| 140 | + path: "other", |
| 141 | + patterns: []string{"**", "![c]md/*.go"}, |
| 142 | + want: true, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "bracket wildcard with prefix matches dir under prefix", |
| 146 | + path: "src/cmd", |
| 147 | + patterns: []string{"**", "!src/cm[d]/*.go"}, |
| 148 | + want: true, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "bracket wildcard with prefix no match for other dir", |
| 152 | + path: "other", |
| 153 | + patterns: []string{"**", "!src/cm[d]/*.go"}, |
| 154 | + want: false, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "non-exclusion patterns are ignored", |
| 158 | + path: "cmd", |
| 159 | + patterns: []string{"cmd/**/*.go"}, |
| 160 | + want: false, |
| 161 | + }, |
| 162 | + } |
| 163 | + for _, tt := range tests { |
| 164 | + t.Run(tt.name, func(t *testing.T) { |
| 165 | + var pm *fileutils.PatternMatcher |
| 166 | + if tt.patterns != nil { |
| 167 | + var err error |
| 168 | + pm, err = fileutils.NewPatternMatcher(tt.patterns) |
| 169 | + require.NoError(t, err) |
| 170 | + } |
| 171 | + got := ShouldDescendExcludedDir(tt.path, pm) |
| 172 | + assert.Equal(t, tt.want, got, "ShouldDescendExcludedDir(%q)", tt.path) |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
0 commit comments