|
12 | 12 | from functools import (
|
13 | 13 | partial)
|
14 | 14 | from typing import (
|
15 |
| - Iterable, |
16 |
| - Optional, |
17 |
| - Tuple) |
| 15 | + Iterable, # Replaced by `collections.abc.Iterable` in 3.9. |
| 16 | + Optional, # Replaced by `X | None` in 3.10. |
| 17 | + Tuple) # Replaced by `tuple` in 3.9. |
18 | 18 |
|
19 | 19 | from pathspec.patterns.gitwildmatch import (
|
20 | 20 | GitWildMatchPattern)
|
21 | 21 | from pathspec.util import (
|
22 | 22 | RecursionError,
|
| 23 | + check_match_file, |
23 | 24 | iter_tree_entries,
|
24 | 25 | iter_tree_files,
|
25 | 26 | match_file,
|
|
32 | 33 | ospath)
|
33 | 34 |
|
34 | 35 |
|
| 36 | +class CheckMatchFileTest(unittest.TestCase): |
| 37 | + """ |
| 38 | + The :class:`CheckMatchFileTest` class tests the :meth:`.check_match_file` |
| 39 | + function. |
| 40 | + """ |
| 41 | + |
| 42 | + def test_01_single_1_include(self): |
| 43 | + """ |
| 44 | + Test checking a single file that is included. |
| 45 | + """ |
| 46 | + patterns = list(enumerate(map(GitWildMatchPattern, [ |
| 47 | + "*.txt", |
| 48 | + "!test/", |
| 49 | + ]))) |
| 50 | + |
| 51 | + include_index = check_match_file(patterns, "include.txt") |
| 52 | + |
| 53 | + self.assertEqual(include_index, (True, 0)) |
| 54 | + |
| 55 | + def test_01_single_2_exclude(self): |
| 56 | + """ |
| 57 | + Test checking a single file that is excluded. |
| 58 | + """ |
| 59 | + patterns = list(enumerate(map(GitWildMatchPattern, [ |
| 60 | + "*.txt", |
| 61 | + "!test/", |
| 62 | + ]))) |
| 63 | + |
| 64 | + include_index = check_match_file(patterns, "test/exclude.txt") |
| 65 | + |
| 66 | + self.assertEqual(include_index, (False, 1)) |
| 67 | + |
| 68 | + def test_01_single_3_unmatch(self): |
| 69 | + """ |
| 70 | + Test checking a single file that is ignored. |
| 71 | + """ |
| 72 | + patterns = list(enumerate(map(GitWildMatchPattern, [ |
| 73 | + "*.txt", |
| 74 | + "!test/", |
| 75 | + ]))) |
| 76 | + |
| 77 | + include_index = check_match_file(patterns, "unmatch.bin") |
| 78 | + |
| 79 | + self.assertEqual(include_index, (None, None)) |
| 80 | + |
| 81 | + def test_02_many(self): |
| 82 | + """ |
| 83 | + Test matching files individually. |
| 84 | + """ |
| 85 | + patterns = list(enumerate(map(GitWildMatchPattern, [ |
| 86 | + '*.txt', |
| 87 | + '!b.txt', |
| 88 | + ]))) |
| 89 | + files = { |
| 90 | + 'X/a.txt', |
| 91 | + 'X/b.txt', |
| 92 | + 'X/Z/c.txt', |
| 93 | + 'Y/a.txt', |
| 94 | + 'Y/b.txt', |
| 95 | + 'Y/Z/c.txt', |
| 96 | + } |
| 97 | + |
| 98 | + includes = { |
| 99 | + __file |
| 100 | + for __file in files |
| 101 | + if check_match_file(patterns, __file)[0] |
| 102 | + } |
| 103 | + |
| 104 | + self.assertEqual(includes, { |
| 105 | + 'X/a.txt', |
| 106 | + 'X/Z/c.txt', |
| 107 | + 'Y/a.txt', |
| 108 | + 'Y/Z/c.txt', |
| 109 | + }) |
| 110 | + |
| 111 | + |
35 | 112 | class IterTreeTest(unittest.TestCase):
|
36 | 113 | """
|
37 | 114 | The :class:`IterTreeTest` class tests :meth:`.iter_tree_entries` and
|
@@ -345,23 +422,65 @@ class MatchFileTest(unittest.TestCase):
|
345 | 422 | function.
|
346 | 423 | """
|
347 | 424 |
|
348 |
| - def test_01_match_file(self): |
| 425 | + def test_01_single_1_include(self): |
| 426 | + """ |
| 427 | + Test checking a single file that is included. |
| 428 | + """ |
| 429 | + patterns = list(map(GitWildMatchPattern, [ |
| 430 | + "*.txt", |
| 431 | + "!test/", |
| 432 | + ])) |
| 433 | + |
| 434 | + include = match_file(patterns, "include.txt") |
| 435 | + |
| 436 | + self.assertIs(include, True) |
| 437 | + |
| 438 | + def test_01_single_2_exclude(self): |
| 439 | + """ |
| 440 | + Test checking a single file that is excluded. |
| 441 | + """ |
| 442 | + patterns = list(map(GitWildMatchPattern, [ |
| 443 | + "*.txt", |
| 444 | + "!test/", |
| 445 | + ])) |
| 446 | + |
| 447 | + include = match_file(patterns, "test/exclude.txt") |
| 448 | + |
| 449 | + self.assertIs(include, False) |
| 450 | + |
| 451 | + def test_01_single_3_unmatch(self): |
| 452 | + """ |
| 453 | + Test checking a single file that is ignored. |
| 454 | + """ |
| 455 | + patterns = list(map(GitWildMatchPattern, [ |
| 456 | + "*.txt", |
| 457 | + "!test/", |
| 458 | + ])) |
| 459 | + |
| 460 | + include = match_file(patterns, "unmatch.bin") |
| 461 | + |
| 462 | + self.assertIs(include, False) |
| 463 | + |
| 464 | + def test_02_many(self): |
349 | 465 | """
|
350 | 466 | Test matching files individually.
|
351 | 467 | """
|
352 | 468 | patterns = list(map(GitWildMatchPattern, [
|
353 | 469 | '*.txt',
|
354 | 470 | '!b.txt',
|
355 | 471 | ]))
|
356 |
| - results = set(filter(partial(match_file, patterns), [ |
| 472 | + files = { |
357 | 473 | 'X/a.txt',
|
358 | 474 | 'X/b.txt',
|
359 | 475 | 'X/Z/c.txt',
|
360 | 476 | 'Y/a.txt',
|
361 | 477 | 'Y/b.txt',
|
362 | 478 | 'Y/Z/c.txt',
|
363 |
| - ])) |
364 |
| - self.assertEqual(results, { |
| 479 | + } |
| 480 | + |
| 481 | + includes = set(filter(partial(match_file, patterns), files)) |
| 482 | + |
| 483 | + self.assertEqual(includes, { |
365 | 484 | 'X/a.txt',
|
366 | 485 | 'X/Z/c.txt',
|
367 | 486 | 'Y/a.txt',
|
|
0 commit comments