|
| 1 | +package pack |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "testing/fstest" |
| 12 | + |
| 13 | + "github.com/apex/log" |
| 14 | + "github.com/otiai10/copy" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_ignoreFilter(t *testing.T) { |
| 19 | + type testFile struct { |
| 20 | + path string |
| 21 | + expected bool |
| 22 | + } |
| 23 | + |
| 24 | + type testCase struct { |
| 25 | + name string |
| 26 | + patterns []string |
| 27 | + files []testFile |
| 28 | + } |
| 29 | + |
| 30 | + createFS := func(tc testCase) fs.FS { |
| 31 | + fsys := fstest.MapFS{} |
| 32 | + if tc.patterns != nil { |
| 33 | + fsys[ignoreFile] = &fstest.MapFile{ |
| 34 | + Data: []byte(strings.Join(tc.patterns, "\n")), |
| 35 | + Mode: fs.FileMode(0644), |
| 36 | + } |
| 37 | + } |
| 38 | + for _, file := range tc.files { |
| 39 | + fsys[file.path] = &fstest.MapFile{ |
| 40 | + Mode: fs.FileMode(0644), |
| 41 | + } |
| 42 | + } |
| 43 | + return fsys |
| 44 | + } |
| 45 | + |
| 46 | + runTest := func(t *testing.T, dst string, tc testCase) { |
| 47 | + fsys := createFS(tc) |
| 48 | + |
| 49 | + filter, err := ignoreFilter(fsys, ignoreFile) |
| 50 | + assert.Nil(t, err) |
| 51 | + assert.NotNil(t, filter) |
| 52 | + |
| 53 | + dst = filepath.Join(dst, tc.name) |
| 54 | + log.Infof("do_test: dst=%s", dst) |
| 55 | + err = os.MkdirAll(dst, 0755) |
| 56 | + if err != nil { |
| 57 | + assert.Nil(t, err) |
| 58 | + } |
| 59 | + |
| 60 | + err = copy.Copy(".", dst, copy.Options{ |
| 61 | + FS: fsys, |
| 62 | + Skip: func(srcinfo os.FileInfo, src, dest string) (bool, error) { |
| 63 | + return filter(srcinfo, src), nil |
| 64 | + }, |
| 65 | + PermissionControl: copy.AddPermission(0755), |
| 66 | + }) |
| 67 | + assert.Nil(t, err) |
| 68 | + for _, f := range tc.files { |
| 69 | + if f.expected { |
| 70 | + assert.NoFileExists(t, path.Join(dst, f.path)) |
| 71 | + } else { |
| 72 | + assert.FileExists(t, path.Join(dst, f.path)) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + runTestSet := func(t *testing.T, dst string, name string, testCases []testCase) { |
| 78 | + for _, tc := range testCases { |
| 79 | + t.Run(name+"_"+tc.name, func(t *testing.T) { |
| 80 | + runTest(t, filepath.Join(dst, name), tc) |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + dst := t.TempDir() |
| 86 | + |
| 87 | + // No ignore file. |
| 88 | + t.Run("no ignore file", func(t *testing.T) { |
| 89 | + f, err := ignoreFilter(fstest.MapFS{}, ignoreFile) |
| 90 | + assert.NotNil(t, err) |
| 91 | + assert.True(t, errors.Is(err, fs.ErrNotExist)) |
| 92 | + assert.Nil(t, f) |
| 93 | + }) |
| 94 | + |
| 95 | + // Single ignore pattern. |
| 96 | + testCases_single := []testCase{ |
| 97 | + { |
| 98 | + name: "simple", |
| 99 | + patterns: []string{ |
| 100 | + "foo", |
| 101 | + }, |
| 102 | + files: []testFile{ |
| 103 | + {"foo", true}, |
| 104 | + {"foo_blabla", false}, |
| 105 | + {"blabla_foo", false}, |
| 106 | + {"bla_foo_bla", false}, |
| 107 | + {"bar", false}, |
| 108 | + {"dir/foo", true}, |
| 109 | + {"dir/foo_blabla", false}, |
| 110 | + {"dir/blabla_foo", false}, |
| 111 | + {"dir/bla_foo_bla", false}, |
| 112 | + {"dir/bar", false}, |
| 113 | + {"dir2/foo/bar", true}, |
| 114 | + {"dir2/foo/blabla", true}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "question_prefix", |
| 119 | + patterns: []string{ |
| 120 | + "?foo", |
| 121 | + }, |
| 122 | + files: []testFile{ |
| 123 | + {"foo", false}, |
| 124 | + {".foo", true}, |
| 125 | + {"2foo", true}, |
| 126 | + {"foo_blabla", false}, |
| 127 | + {"blabla_foo", false}, |
| 128 | + {"bla_foo_bla", false}, |
| 129 | + {"bar", false}, |
| 130 | + {"dir/foo", false}, |
| 131 | + {"dir/.foo", true}, |
| 132 | + {"dir/2foo", true}, |
| 133 | + {"dir/foo_blabla", false}, |
| 134 | + {"dir/blabla_foo", false}, |
| 135 | + {"dir/bla_foo_bla", false}, |
| 136 | + {"dir/bar", false}, |
| 137 | + {"dir2/2foo/bar", true}, |
| 138 | + {"dir2/2foo/blabla", true}, |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "question_suffix", |
| 143 | + patterns: []string{ |
| 144 | + "foo?", |
| 145 | + }, |
| 146 | + files: []testFile{ |
| 147 | + {"foo", false}, |
| 148 | + {"foo.", true}, |
| 149 | + {"foo2", true}, |
| 150 | + {"foo_blabla", false}, |
| 151 | + {"blabla_foo", false}, |
| 152 | + {"bla_foo_bla", false}, |
| 153 | + {"bar", false}, |
| 154 | + {"dir/foo", false}, |
| 155 | + {"dir/foo.", true}, |
| 156 | + {"dir/foo2", true}, |
| 157 | + {"dir/foo_blabla", false}, |
| 158 | + {"dir/blabla_foo", false}, |
| 159 | + {"dir/bla_foo_bla", false}, |
| 160 | + {"dir/bar", false}, |
| 161 | + {"dir2/foo2/bar", true}, |
| 162 | + {"dir2/foo2/blabla", true}, |
| 163 | + }, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "question", |
| 167 | + patterns: []string{ |
| 168 | + "f?o", |
| 169 | + }, |
| 170 | + files: []testFile{ |
| 171 | + {"foo", true}, |
| 172 | + {"f2o", true}, |
| 173 | + {"fooo", false}, |
| 174 | + {"2foo", false}, |
| 175 | + {"foo_blabla", false}, |
| 176 | + {"blabla_foo", false}, |
| 177 | + {"bla_foo_bla", false}, |
| 178 | + {"bar", false}, |
| 179 | + {"dir/foo", true}, |
| 180 | + {"dir/f2o", true}, |
| 181 | + {"dir/fooo", false}, |
| 182 | + {"dir/2foo", false}, |
| 183 | + {"dir/foo_blabla", false}, |
| 184 | + {"dir/blabla_foo", false}, |
| 185 | + {"dir/bla_foo_bla", false}, |
| 186 | + {"dir/bar", false}, |
| 187 | + {"dir2/f2o/bar", true}, |
| 188 | + {"dir2/f2o/blabla", true}, |
| 189 | + }, |
| 190 | + }, |
| 191 | + { |
| 192 | + name: "asterisk_prefix", |
| 193 | + patterns: []string{ |
| 194 | + "*foo", |
| 195 | + }, |
| 196 | + files: []testFile{ |
| 197 | + {"foo", true}, |
| 198 | + {".foo", true}, |
| 199 | + {"foo_blabla", false}, |
| 200 | + {"blabla_foo", true}, |
| 201 | + {"bla_foo_bla", false}, |
| 202 | + {"bar", false}, |
| 203 | + {"dir/foo", true}, |
| 204 | + {"dir/.foo", true}, |
| 205 | + {"dir/foo_blabla", false}, |
| 206 | + {"dir/blabla_foo", true}, |
| 207 | + {"dir/bla_foo_bla", false}, |
| 208 | + {"dir/bar", false}, |
| 209 | + {"dir2/blabla_foo/bar", true}, |
| 210 | + {"dir2/blabla_foo/blabla", true}, |
| 211 | + }, |
| 212 | + }, |
| 213 | + { |
| 214 | + name: "asterisk_suffix", |
| 215 | + patterns: []string{ |
| 216 | + "foo*", |
| 217 | + }, |
| 218 | + files: []testFile{ |
| 219 | + {"foo", true}, |
| 220 | + {"foo.", true}, |
| 221 | + {"foo_blabla", true}, |
| 222 | + {"blabla_foo", false}, |
| 223 | + {"bla_foo_bla", false}, |
| 224 | + {"bar", false}, |
| 225 | + {"dir/foo", true}, |
| 226 | + {"dir/foo.", true}, |
| 227 | + {"dir/foo_blabla", true}, |
| 228 | + {"dir/blabla_foo", false}, |
| 229 | + {"dir/bla_foo_bla", false}, |
| 230 | + {"dir/bar", false}, |
| 231 | + {"dir2/foo_blabla/bar", true}, |
| 232 | + {"dir2/foo_blabla/blabla", true}, |
| 233 | + }, |
| 234 | + }, |
| 235 | + { |
| 236 | + name: "asterisk", |
| 237 | + patterns: []string{ |
| 238 | + "f*o", |
| 239 | + }, |
| 240 | + files: []testFile{ |
| 241 | + {"fo", true}, |
| 242 | + {"foo", true}, |
| 243 | + {"foo_blabla_foo", true}, |
| 244 | + {"foo_blabla", false}, |
| 245 | + {"blabla_foo", false}, |
| 246 | + {"bla_foo_bla", false}, |
| 247 | + {"bar", false}, |
| 248 | + {"dir/fo", true}, |
| 249 | + {"dir/foo", true}, |
| 250 | + {"dir/foo_blabla_foo", true}, |
| 251 | + {"dir/foo_blabla", false}, |
| 252 | + {"dir/blabla_foo", false}, |
| 253 | + {"dir/bla_foo_bla", false}, |
| 254 | + {"dir/bar", false}, |
| 255 | + {"dir2/foo_blabla_foo/bar", true}, |
| 256 | + {"dir2/foo_blabla_foo/blabla", true}, |
| 257 | + }, |
| 258 | + }, |
| 259 | + { |
| 260 | + name: "range", |
| 261 | + patterns: []string{ |
| 262 | + "f[m-p]o", |
| 263 | + }, |
| 264 | + files: []testFile{ |
| 265 | + {"foo", true}, |
| 266 | + {"fmo", true}, |
| 267 | + {"f2o", false}, |
| 268 | + {"fo", false}, |
| 269 | + {"foo_blabla", false}, |
| 270 | + {"blabla_foo", false}, |
| 271 | + {"bla_foo_bla", false}, |
| 272 | + {"bar", false}, |
| 273 | + {"dir/foo", true}, |
| 274 | + {"dir/fmo", true}, |
| 275 | + {"dir/f2o", false}, |
| 276 | + {"dir/fo", false}, |
| 277 | + {"dir/foo_blabla", false}, |
| 278 | + {"dir/blabla_foo", false}, |
| 279 | + {"dir/bla_foo_bla", false}, |
| 280 | + {"dir/bar", false}, |
| 281 | + {"dir2/foo/bar", true}, |
| 282 | + {"dir2/foo/blabla", true}, |
| 283 | + }, |
| 284 | + }, |
| 285 | + { |
| 286 | + name: "range_exclude", |
| 287 | + patterns: []string{ |
| 288 | + "[^a-eg-z]oo", |
| 289 | + }, |
| 290 | + files: []testFile{ |
| 291 | + {"foo", true}, |
| 292 | + {"goo", false}, |
| 293 | + {"foo_blabla", false}, |
| 294 | + {"blabla_foo", false}, |
| 295 | + {"bla_foo_bla", false}, |
| 296 | + {"bar", false}, |
| 297 | + {"dir/foo", true}, |
| 298 | + {"dir/goo", false}, |
| 299 | + {"dir/foo_blabla", false}, |
| 300 | + {"dir/blabla_foo", false}, |
| 301 | + {"dir/bla_foo_bla", false}, |
| 302 | + {"dir/bar", false}, |
| 303 | + {"dir2/foo/bar", true}, |
| 304 | + {"dir2/foo/blabla", true}, |
| 305 | + }, |
| 306 | + }, |
| 307 | + { |
| 308 | + name: "mix", |
| 309 | + patterns: []string{ |
| 310 | + "?o[2ox]*", |
| 311 | + }, |
| 312 | + files: []testFile{ |
| 313 | + {"foo", true}, |
| 314 | + {"foo_blabla", true}, |
| 315 | + {"blabla_foo", false}, |
| 316 | + {"bla_foo_bla", false}, |
| 317 | + {"bar", false}, |
| 318 | + {"dir/foo", true}, |
| 319 | + {"dir/foo_blabla", true}, |
| 320 | + {"dir/blabla_foo", false}, |
| 321 | + {"dir/bla_foo_bla", false}, |
| 322 | + {"dir/bar", false}, |
| 323 | + {"dir2/go2_blabla/bar", true}, |
| 324 | + {"dir2/go2_blabla/blabla", true}, |
| 325 | + }, |
| 326 | + }, |
| 327 | + { |
| 328 | + name: "dir_simple", |
| 329 | + patterns: []string{ |
| 330 | + "dir/", |
| 331 | + }, |
| 332 | + files: []testFile{ |
| 333 | + {"foo", false}, |
| 334 | + {"dir/foo", true}, |
| 335 | + {"2dir/foo", false}, |
| 336 | + {"dir2/foo", false}, |
| 337 | + }, |
| 338 | + }, |
| 339 | + { |
| 340 | + name: "dir_question_prefix", |
| 341 | + patterns: []string{ |
| 342 | + "?dir/", |
| 343 | + }, |
| 344 | + files: []testFile{ |
| 345 | + {"foo", false}, |
| 346 | + {"dir/foo", false}, |
| 347 | + {"2dir/foo", true}, |
| 348 | + {"22dir/foo", false}, |
| 349 | + {"dir2/foo", false}, |
| 350 | + }, |
| 351 | + }, |
| 352 | + { |
| 353 | + name: "dir_question_suffix", |
| 354 | + patterns: []string{ |
| 355 | + "dir?/", |
| 356 | + }, |
| 357 | + files: []testFile{ |
| 358 | + {"foo", false}, |
| 359 | + {"dir/foo", false}, |
| 360 | + {"2dir/foo", false}, |
| 361 | + {"dir2/foo", true}, |
| 362 | + {"dir22/foo", false}, |
| 363 | + }, |
| 364 | + }, |
| 365 | + { |
| 366 | + name: "dir_question", |
| 367 | + patterns: []string{ |
| 368 | + "d?r/", |
| 369 | + }, |
| 370 | + files: []testFile{ |
| 371 | + {"foo", false}, |
| 372 | + {"dir/foo", true}, |
| 373 | + {"d2r/foo", true}, |
| 374 | + {"d22r/foo", false}, |
| 375 | + {"2dir/foo", false}, |
| 376 | + {"dir2/foo", false}, |
| 377 | + }, |
| 378 | + }, |
| 379 | + { |
| 380 | + name: "dir_asterisk_prefix", |
| 381 | + patterns: []string{ |
| 382 | + "*dir/", |
| 383 | + }, |
| 384 | + files: []testFile{ |
| 385 | + {"foo", false}, |
| 386 | + {"dir/foo", true}, |
| 387 | + {"2dir/foo", true}, |
| 388 | + {"22dir/foo", true}, |
| 389 | + {"dir2/foo", false}, |
| 390 | + }, |
| 391 | + }, |
| 392 | + { |
| 393 | + name: "dir_asterisk_suffix", |
| 394 | + patterns: []string{ |
| 395 | + "dir*/", |
| 396 | + }, |
| 397 | + files: []testFile{ |
| 398 | + {"foo", false}, |
| 399 | + {"dir/foo", true}, |
| 400 | + {"2dir/foo", false}, |
| 401 | + {"dir2/foo", true}, |
| 402 | + {"dir22/foo", true}, |
| 403 | + }, |
| 404 | + }, |
| 405 | + { |
| 406 | + name: "dir_asterisk", |
| 407 | + patterns: []string{ |
| 408 | + "d*r/", |
| 409 | + }, |
| 410 | + files: []testFile{ |
| 411 | + {"foo", false}, |
| 412 | + {"dir/foo", true}, |
| 413 | + {"d2r/foo", true}, |
| 414 | + {"d22r/foo", true}, |
| 415 | + {"2dir/foo", false}, |
| 416 | + {"dir2/foo", false}, |
| 417 | + }, |
| 418 | + }, |
| 419 | + } |
| 420 | + runTestSet(t, dst, "singlepattern", testCases_single) |
| 421 | + |
| 422 | + // An ignore pattern followed with the same but negated (started with '!'). |
| 423 | + var testCases_selfNegate []testCase |
| 424 | + for _, tc := range testCases_single { |
| 425 | + var files []testFile |
| 426 | + for _, f := range tc.files { |
| 427 | + files = append(files, testFile{f.path, false}) |
| 428 | + } |
| 429 | + testCases_selfNegate = append(testCases_selfNegate, testCase{ |
| 430 | + name: tc.name, |
| 431 | + patterns: []string{tc.patterns[0], "!" + tc.patterns[0]}, |
| 432 | + files: files, |
| 433 | + }) |
| 434 | + } |
| 435 | + runTestSet(t, dst, "singlepattern_and_selfnegate", testCases_selfNegate) |
| 436 | + |
| 437 | + // An ignore pattern preceded with the same but negated (thus negated one has no effect). |
| 438 | + var testCases_selfNegateWrongOrder []testCase |
| 439 | + for i, tc := range testCases_selfNegate { |
| 440 | + testCases_selfNegateWrongOrder = append(testCases_selfNegateWrongOrder, testCase{ |
| 441 | + name: tc.name, |
| 442 | + patterns: []string{tc.patterns[1], tc.patterns[0]}, |
| 443 | + files: testCases_single[i].files, |
| 444 | + }) |
| 445 | + } |
| 446 | + runTestSet(t, dst, "singlepattern_and_selfnegate_wrong_order", testCases_selfNegateWrongOrder) |
| 447 | + |
| 448 | + // Single ignore pattern that is negate. |
| 449 | + var testCases_negateOnly []testCase |
| 450 | + for _, tc := range testCases_selfNegate { |
| 451 | + testCases_negateOnly = append(testCases_negateOnly, testCase{ |
| 452 | + name: tc.name, |
| 453 | + patterns: []string{tc.patterns[1]}, |
| 454 | + files: tc.files, |
| 455 | + }) |
| 456 | + } |
| 457 | + runTestSet(t, dst, "singlepattern_negate", testCases_negateOnly) |
| 458 | + |
| 459 | + // Multiple patterns. |
| 460 | + testCases_multi := []testCase{ |
| 461 | + { |
| 462 | + name: "simple", |
| 463 | + patterns: []string{ |
| 464 | + "foo", |
| 465 | + "dir", |
| 466 | + }, |
| 467 | + files: []testFile{ |
| 468 | + {"foo", true}, |
| 469 | + {"foo_blabla", false}, |
| 470 | + {"blabla_foo", false}, |
| 471 | + {"bla_foo_bla", false}, |
| 472 | + {"bar", false}, |
| 473 | + {"dir/foo", true}, |
| 474 | + {"dir/foo_blabla", false}, |
| 475 | + {"dir/blabla_foo", false}, |
| 476 | + {"dir/bla_foo_bla", false}, |
| 477 | + {"dir/bar", false}, |
| 478 | + }, |
| 479 | + }, |
| 480 | + { |
| 481 | + name: "question", |
| 482 | + patterns: []string{ |
| 483 | + "?foo", |
| 484 | + "?bla?", |
| 485 | + }, |
| 486 | + files: []testFile{ |
| 487 | + {"foo", false}, |
| 488 | + {".foo", true}, |
| 489 | + {"2foo", true}, |
| 490 | + {"foo_blabla", false}, |
| 491 | + {"blabla_foo", false}, |
| 492 | + {"bla_foo_bla", false}, |
| 493 | + {"bar", true}, |
| 494 | + {"dir/foo", false}, |
| 495 | + {"dir/.foo", true}, |
| 496 | + {"dir/2foo", true}, |
| 497 | + {"dir/foo_blabla", false}, |
| 498 | + {"dir/blabla_foo", false}, |
| 499 | + {"dir/bla_foo_bla", false}, |
| 500 | + {"dir/bar", true}, |
| 501 | + }, |
| 502 | + }, |
| 503 | + { |
| 504 | + name: "asterisk", |
| 505 | + patterns: []string{ |
| 506 | + "foo*", |
| 507 | + "*bla*", |
| 508 | + }, |
| 509 | + files: []testFile{ |
| 510 | + {"foo", false}, |
| 511 | + {"foo.", true}, |
| 512 | + {"foo2", true}, |
| 513 | + {"foo_blabla", false}, |
| 514 | + {"blabla_foo", false}, |
| 515 | + {"bla_foo_bla", false}, |
| 516 | + {"bar", true}, |
| 517 | + {"dir/foo", false}, |
| 518 | + {"dir/foo.", true}, |
| 519 | + {"dir/foo2", true}, |
| 520 | + {"dir/foo_blabla", false}, |
| 521 | + {"dir/blabla_foo", false}, |
| 522 | + {"dir/bla_foo_bla", false}, |
| 523 | + {"dir/bar", true}, |
| 524 | + }, |
| 525 | + }, |
| 526 | + { |
| 527 | + name: "range", |
| 528 | + patterns: []string{ |
| 529 | + "bar", |
| 530 | + "*foo", |
| 531 | + }, |
| 532 | + files: []testFile{ |
| 533 | + {"foo", true}, |
| 534 | + {".foo", true}, |
| 535 | + {"foo_blabla", false}, |
| 536 | + {"blabla_foo", true}, |
| 537 | + {"bla_foo_bla", false}, |
| 538 | + {"bar", true}, |
| 539 | + {"dir/foo", true}, |
| 540 | + {"dir/.foo", true}, |
| 541 | + {"dir/foo_blabla", false}, |
| 542 | + {"dir/blabla_foo", true}, |
| 543 | + {"dir/bla_foo_bla", false}, |
| 544 | + {"dir/bar", true}, |
| 545 | + }, |
| 546 | + }, |
| 547 | + { |
| 548 | + name: "asterisk_suffix", |
| 549 | + patterns: []string{ |
| 550 | + "bar", |
| 551 | + "foo*", |
| 552 | + }, |
| 553 | + files: []testFile{ |
| 554 | + {"foo", true}, |
| 555 | + {"foo.", true}, |
| 556 | + {"foo_blabla", true}, |
| 557 | + {"blabla_foo", false}, |
| 558 | + {"bla_foo_bla", false}, |
| 559 | + {"bar", true}, |
| 560 | + {"dir/foo", true}, |
| 561 | + {"dir/foo.", true}, |
| 562 | + {"dir/foo_blabla", true}, |
| 563 | + {"dir/blabla_foo", false}, |
| 564 | + {"dir/bla_foo_bla", false}, |
| 565 | + {"dir/bar", true}, |
| 566 | + }, |
| 567 | + }, |
| 568 | + } |
| 569 | + runTestSet(t, dst, "multipatterns", testCases_multi) |
| 570 | +} |
0 commit comments