cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/glob_test.go
357lines · modecode
| 1 | package discovery_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path" |
| 8 | "regexp" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/stretchr/testify/require" |
| 14 | |
| 15 | "github.com/cloudflare/pint/internal/discovery" |
| 16 | "github.com/cloudflare/pint/internal/git" |
| 17 | "github.com/cloudflare/pint/internal/parser" |
| 18 | ) |
| 19 | |
| 20 | func TestGlobPathFinder(t *testing.T) { |
| 21 | type testCaseT struct { |
| 22 | files map[string]string |
| 23 | symlinks map[string]string |
| 24 | err string |
| 25 | finder discovery.GlobFinder |
| 26 | entries []discovery.Entry |
| 27 | } |
| 28 | |
| 29 | p := parser.NewParser(false) |
| 30 | testRuleBody := "# pint file/owner bob\n\n- record: foo\n expr: sum(foo)\n" |
| 31 | testRules, err := p.Parse([]byte(testRuleBody)) |
| 32 | require.NoError(t, err) |
| 33 | |
| 34 | testCases := []testCaseT{ |
| 35 | { |
| 36 | files: map[string]string{}, |
| 37 | finder: discovery.NewGlobFinder([]string{"[]"}, git.NewPathFilter(nil, nil, nil)), |
| 38 | err: "failed to expand file path pattern []: syntax error in pattern", |
| 39 | }, |
| 40 | { |
| 41 | files: map[string]string{}, |
| 42 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 43 | err: "no matching files", |
| 44 | }, |
| 45 | { |
| 46 | files: map[string]string{}, |
| 47 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 48 | err: "no matching files", |
| 49 | }, |
| 50 | { |
| 51 | files: map[string]string{}, |
| 52 | finder: discovery.NewGlobFinder([]string{"foo/*"}, git.NewPathFilter(nil, nil, nil)), |
| 53 | err: "no matching files", |
| 54 | }, |
| 55 | { |
| 56 | files: map[string]string{"bar.yml": testRuleBody}, |
| 57 | finder: discovery.NewGlobFinder([]string{"foo/*"}, git.NewPathFilter(nil, nil, nil)), |
| 58 | err: "no matching files", |
| 59 | }, |
| 60 | { |
| 61 | files: map[string]string{"bar.yml": testRuleBody}, |
| 62 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, []*regexp.Regexp{regexp.MustCompile(".*")})), |
| 63 | entries: []discovery.Entry{ |
| 64 | { |
| 65 | State: discovery.Noop, |
| 66 | Path: discovery.Path{ |
| 67 | Name: "bar.yml", |
| 68 | SymlinkTarget: "bar.yml", |
| 69 | }, |
| 70 | Rule: testRules[0], |
| 71 | ModifiedLines: testRules[0].Lines.Expand(), |
| 72 | Owner: "bob", |
| 73 | }, |
| 74 | }, |
| 75 | }, |
| 76 | { |
| 77 | files: map[string]string{"foo/bar.yml": testRuleBody + "\n\n# pint file/owner alice\n"}, |
| 78 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, []*regexp.Regexp{regexp.MustCompile(".*")})), |
| 79 | entries: []discovery.Entry{ |
| 80 | { |
| 81 | State: discovery.Noop, |
| 82 | Path: discovery.Path{ |
| 83 | Name: "foo/bar.yml", |
| 84 | SymlinkTarget: "foo/bar.yml", |
| 85 | }, |
| 86 | Rule: testRules[0], |
| 87 | ModifiedLines: testRules[0].Lines.Expand(), |
| 88 | Owner: "alice", |
| 89 | }, |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | files: map[string]string{"bar.yml": testRuleBody}, |
| 94 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 95 | entries: []discovery.Entry{ |
| 96 | { |
| 97 | State: discovery.Noop, |
| 98 | Path: discovery.Path{ |
| 99 | Name: "bar.yml", |
| 100 | SymlinkTarget: "bar.yml", |
| 101 | }, |
| 102 | PathError: parser.ParseError{ |
| 103 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 104 | Line: 3, |
| 105 | }, |
| 106 | ModifiedLines: []int{1, 2, 3, 4}, |
| 107 | Owner: "bob", |
| 108 | }, |
| 109 | }, |
| 110 | }, |
| 111 | { |
| 112 | files: map[string]string{"bar.yml": "record:::{}\n expr: sum(foo)\n\n# pint file/owner bob\n"}, |
| 113 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, []*regexp.Regexp{regexp.MustCompile(".*")})), |
| 114 | entries: []discovery.Entry{ |
| 115 | { |
| 116 | State: discovery.Noop, |
| 117 | Path: discovery.Path{ |
| 118 | Name: "bar.yml", |
| 119 | SymlinkTarget: "bar.yml", |
| 120 | }, |
| 121 | PathError: parser.ParseError{ |
| 122 | Err: errors.New("mapping values are not allowed in this context"), |
| 123 | Line: 2, |
| 124 | }, |
| 125 | ModifiedLines: []int{1, 2, 3, 4}, |
| 126 | Owner: "bob", |
| 127 | }, |
| 128 | }, |
| 129 | }, |
| 130 | { |
| 131 | files: map[string]string{"bar.yml": testRuleBody}, |
| 132 | symlinks: map[string]string{"link.yml": "bar.yml"}, |
| 133 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 134 | entries: []discovery.Entry{ |
| 135 | { |
| 136 | State: discovery.Noop, |
| 137 | Path: discovery.Path{ |
| 138 | Name: "bar.yml", |
| 139 | SymlinkTarget: "bar.yml", |
| 140 | }, |
| 141 | PathError: parser.ParseError{ |
| 142 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 143 | Line: 3, |
| 144 | }, |
| 145 | ModifiedLines: []int{1, 2, 3, 4}, |
| 146 | Owner: "bob", |
| 147 | }, |
| 148 | { |
| 149 | State: discovery.Noop, |
| 150 | Path: discovery.Path{ |
| 151 | Name: "link.yml", |
| 152 | SymlinkTarget: "bar.yml", |
| 153 | }, |
| 154 | PathError: parser.ParseError{ |
| 155 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 156 | Line: 3, |
| 157 | }, |
| 158 | ModifiedLines: []int{1, 2, 3, 4}, |
| 159 | Owner: "bob", |
| 160 | }, |
| 161 | }, |
| 162 | }, |
| 163 | { |
| 164 | files: map[string]string{"a/bar.yml": testRuleBody}, |
| 165 | symlinks: map[string]string{ |
| 166 | "b/link.yml": "../a/bar.yml", |
| 167 | "b/c/link.yml": "../../a/bar.yml", |
| 168 | }, |
| 169 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 170 | entries: []discovery.Entry{ |
| 171 | { |
| 172 | State: discovery.Noop, |
| 173 | Path: discovery.Path{ |
| 174 | Name: "a/bar.yml", |
| 175 | SymlinkTarget: "a/bar.yml", |
| 176 | }, |
| 177 | PathError: parser.ParseError{ |
| 178 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 179 | Line: 3, |
| 180 | }, |
| 181 | ModifiedLines: []int{1, 2, 3, 4}, |
| 182 | Owner: "bob", |
| 183 | }, |
| 184 | { |
| 185 | State: discovery.Noop, |
| 186 | Path: discovery.Path{ |
| 187 | Name: "b/c/link.yml", |
| 188 | SymlinkTarget: "a/bar.yml", |
| 189 | }, |
| 190 | PathError: parser.ParseError{ |
| 191 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 192 | Line: 3, |
| 193 | }, |
| 194 | ModifiedLines: []int{1, 2, 3, 4}, |
| 195 | Owner: "bob", |
| 196 | }, |
| 197 | { |
| 198 | State: discovery.Noop, |
| 199 | Path: discovery.Path{ |
| 200 | Name: "b/link.yml", |
| 201 | SymlinkTarget: "a/bar.yml", |
| 202 | }, |
| 203 | PathError: parser.ParseError{ |
| 204 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 205 | Line: 3, |
| 206 | }, |
| 207 | ModifiedLines: []int{1, 2, 3, 4}, |
| 208 | Owner: "bob", |
| 209 | }, |
| 210 | }, |
| 211 | }, |
| 212 | { |
| 213 | files: map[string]string{"a/bar.yml": testRuleBody}, |
| 214 | symlinks: map[string]string{ |
| 215 | "b/link.yml": "../a/bar.yml", |
| 216 | "b/c/link.yml": "../a/bar.yml", |
| 217 | }, |
| 218 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 219 | err: "b/c/link.yml is a symlink but target file cannot be evaluated: lstat b/a: no such file or directory", |
| 220 | }, |
| 221 | { |
| 222 | files: map[string]string{"a/bar.yml": "xxx:\n"}, |
| 223 | symlinks: map[string]string{ |
| 224 | "b/c/link.yml": "../../a/bar.yml", |
| 225 | }, |
| 226 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, []*regexp.Regexp{regexp.MustCompile(".*")})), |
| 227 | }, |
| 228 | { |
| 229 | files: map[string]string{"a/bar.yml": "xxx:\nyyy:\n"}, |
| 230 | symlinks: map[string]string{ |
| 231 | "b/c/link.yml": "../../a/bar.yml", |
| 232 | }, |
| 233 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 234 | entries: []discovery.Entry{ |
| 235 | { |
| 236 | State: discovery.Noop, |
| 237 | Path: discovery.Path{ |
| 238 | Name: "a/bar.yml", |
| 239 | SymlinkTarget: "a/bar.yml", |
| 240 | }, |
| 241 | PathError: parser.ParseError{ |
| 242 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 243 | Line: 1, |
| 244 | }, |
| 245 | ModifiedLines: []int{1, 2}, |
| 246 | Owner: "", |
| 247 | }, |
| 248 | { |
| 249 | State: discovery.Noop, |
| 250 | Path: discovery.Path{ |
| 251 | Name: "b/c/link.yml", |
| 252 | SymlinkTarget: "a/bar.yml", |
| 253 | }, |
| 254 | PathError: parser.ParseError{ |
| 255 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 256 | Line: 1, |
| 257 | }, |
| 258 | ModifiedLines: []int{1, 2}, |
| 259 | Owner: "", |
| 260 | }, |
| 261 | }, |
| 262 | }, |
| 263 | { |
| 264 | files: map[string]string{"a/bar.yml": "xxx:\nyyy:\n"}, |
| 265 | symlinks: map[string]string{ |
| 266 | "b/c/link.yml": "../../a/bar.yml", |
| 267 | }, |
| 268 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, []*regexp.Regexp{regexp.MustCompile(".*")})), |
| 269 | }, |
| 270 | { |
| 271 | files: map[string]string{"a/bar.yml": "xxx:\nyyy:\n"}, |
| 272 | symlinks: map[string]string{ |
| 273 | "b/c/d": "../../a", |
| 274 | }, |
| 275 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, []*regexp.Regexp{regexp.MustCompile(".*")})), |
| 276 | }, |
| 277 | { |
| 278 | files: map[string]string{"a/bar.yml": testRuleBody}, |
| 279 | symlinks: map[string]string{ |
| 280 | "b/c/link.yml": "../../a/bar.yml", |
| 281 | }, |
| 282 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 283 | entries: []discovery.Entry{ |
| 284 | { |
| 285 | State: discovery.Noop, |
| 286 | Path: discovery.Path{ |
| 287 | Name: "a/bar.yml", |
| 288 | SymlinkTarget: "a/bar.yml", |
| 289 | }, |
| 290 | PathError: parser.ParseError{ |
| 291 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 292 | Line: 3, |
| 293 | }, |
| 294 | ModifiedLines: []int{1, 2, 3, 4}, |
| 295 | Owner: "bob", |
| 296 | }, |
| 297 | { |
| 298 | State: discovery.Noop, |
| 299 | Path: discovery.Path{ |
| 300 | Name: "b/c/link.yml", |
| 301 | SymlinkTarget: "a/bar.yml", |
| 302 | }, |
| 303 | PathError: parser.ParseError{ |
| 304 | Err: errors.New("YAML list is not allowed here, expected a YAML mapping"), |
| 305 | Line: 3, |
| 306 | }, |
| 307 | ModifiedLines: []int{1, 2, 3, 4}, |
| 308 | Owner: "bob", |
| 309 | }, |
| 310 | }, |
| 311 | }, |
| 312 | { |
| 313 | symlinks: map[string]string{ |
| 314 | "input.yml": "/xx/ccc/fdd", |
| 315 | }, |
| 316 | finder: discovery.NewGlobFinder([]string{"*"}, git.NewPathFilter(nil, nil, nil)), |
| 317 | err: "input.yml is a symlink but target file cannot be evaluated: lstat /xx: no such file or directory", |
| 318 | }, |
| 319 | } |
| 320 | |
| 321 | for i, tc := range testCases { |
| 322 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 323 | workdir := t.TempDir() |
| 324 | err := os.Chdir(workdir) |
| 325 | require.NoError(t, err) |
| 326 | |
| 327 | for p, content := range tc.files { |
| 328 | if strings.Contains(p, "/") { |
| 329 | err = os.MkdirAll(path.Dir(p), 0o755) |
| 330 | require.NoError(t, err) |
| 331 | } |
| 332 | err = os.WriteFile(p, []byte(content), 0o644) |
| 333 | require.NoError(t, err) |
| 334 | } |
| 335 | for symlink, target := range tc.symlinks { |
| 336 | if strings.Contains(symlink, "/") { |
| 337 | err = os.MkdirAll(path.Dir(symlink), 0o755) |
| 338 | require.NoError(t, err) |
| 339 | } |
| 340 | require.NoError(t, os.Symlink(target, symlink)) |
| 341 | } |
| 342 | |
| 343 | entries, err := tc.finder.Find() |
| 344 | if tc.err != "" { |
| 345 | require.EqualError(t, err, tc.err) |
| 346 | } else { |
| 347 | require.NoError(t, err) |
| 348 | |
| 349 | expected, err := json.MarshalIndent(tc.entries, "", " ") |
| 350 | require.NoError(t, err, "json(expected)") |
| 351 | got, err := json.MarshalIndent(entries, "", " ") |
| 352 | require.NoError(t, err, "json(got)") |
| 353 | require.Equal(t, string(expected), string(got)) |
| 354 | } |
| 355 | }) |
| 356 | } |
| 357 | } |
| 358 | |