1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
"""
This script tests :class:`.GitWildMatchPattern`.
"""
import re
import unittest
import pathspec.patterns.gitwildmatch
from pathspec.patterns.gitwildmatch import (
GitWildMatchPattern,
GitWildMatchPatternError,
_BYTES_ENCODING,
_DIR_MARK)
from pathspec.util import (
lookup_pattern)
RE_DIR = f"(?P<{_DIR_MARK}>/)"
"""
This regular expression matches the directory marker.
"""
RE_SUB = f"(?:{RE_DIR}.*)?"
"""
This regular expression matches an optional sub-path.
"""
class GitWildMatchTest(unittest.TestCase):
"""
The :class:`GitWildMatchTest` class tests the :class:`GitWildMatchPattern`
implementation.
"""
def _check_invalid_pattern(self, git_ignore_pattern):
expected_message_pattern = re.escape(repr(git_ignore_pattern))
with self.assertRaisesRegex(GitWildMatchPatternError, expected_message_pattern):
GitWildMatchPattern(git_ignore_pattern)
def test_00_empty(self):
"""
Tests an empty pattern.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('')
self.assertIsNone(include)
self.assertIsNone(regex)
def test_01_absolute(self):
"""
Tests an absolute path pattern.
This should match:
an/absolute/file/path
an/absolute/file/path/foo
This should NOT match:
foo/an/absolute/file/path
"""
regex, include = GitWildMatchPattern.pattern_to_regex('/an/absolute/file/path')
self.assertTrue(include)
self.assertEqual(regex, f'^an/absolute/file/path{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'an/absolute/file/path',
'an/absolute/file/path/foo',
'foo/an/absolute/file/path',
]))
self.assertEqual(results, {
'an/absolute/file/path',
'an/absolute/file/path/foo',
})
def test_01_absolute_ignore(self):
"""
Tests an ignore absolute path pattern.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('!/foo/build')
self.assertIs(include, False)
self.assertEqual(regex, f'^foo/build{RE_SUB}$')
# NOTE: The pattern match is backwards because the pattern itself
# does not consider the include attribute.
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'build/file.py',
'foo/build/file.py',
]))
self.assertEqual(results, {
'foo/build/file.py',
})
def test_01_absolute_root(self):
"""
Tests a single root absolute path pattern.
This should NOT match any file (according to git check-ignore
(v2.4.1)).
"""
regex, include = GitWildMatchPattern.pattern_to_regex('/')
self.assertIsNone(include)
self.assertIsNone(regex)
def test_01_relative(self):
"""
Tests a relative path pattern.
This should match:
spam
spam/
foo/spam
spam/foo
foo/spam/bar
"""
regex, include = GitWildMatchPattern.pattern_to_regex('spam')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?spam{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'spam',
'spam/',
'foo/spam',
'spam/foo',
'foo/spam/bar',
]))
self.assertEqual(results, {
'spam',
'spam/',
'foo/spam',
'spam/foo',
'foo/spam/bar',
})
def test_01_relative_nested(self):
"""
Tests a relative nested path pattern.
This should match:
foo/spam
foo/spam/bar
This should **not** match (according to git check-ignore (v2.4.1)):
bar/foo/spam
"""
regex, include = GitWildMatchPattern.pattern_to_regex('foo/spam')
self.assertTrue(include)
self.assertEqual(regex, f'^foo/spam{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'foo/spam',
'foo/spam/bar',
'bar/foo/spam',
]))
self.assertEqual(results, {
'foo/spam',
'foo/spam/bar',
})
def test_02_comment(self):
"""
Tests a comment pattern.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('# Cork soakers.')
self.assertIsNone(include)
self.assertIsNone(regex)
def test_02_ignore(self):
"""
Tests an exclude pattern.
This should NOT match (according to git check-ignore (v2.4.1)):
temp/foo
"""
regex, include = GitWildMatchPattern.pattern_to_regex('!temp')
self.assertIs(include, False)
self.assertEqual(regex, f'^(?:.+/)?temp{RE_SUB}$')
# NOTE: The pattern match is backwards because the pattern itself
# does not consider the include attribute.
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'temp/foo',
]))
self.assertEqual(results, {
'temp/foo',
})
def test_03_child_double_asterisk(self):
"""
Tests a directory name with a double-asterisk child
directory.
This should match:
spam/bar
This should **not** match (according to git check-ignore (v2.4.1)):
foo/spam/bar
"""
regex, include = GitWildMatchPattern.pattern_to_regex('spam/**')
self.assertTrue(include)
self.assertEqual(regex, "^spam/.*$")
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'spam/bar',
'foo/spam/bar',
]))
self.assertEqual(results, {'spam/bar'})
def test_03_inner_double_asterisk(self):
"""
Tests a path with an inner double-asterisk directory.
This should match:
left/right
left/bar/right
left/foo/bar/right
left/bar/right/foo
This should **not** match (according to git check-ignore (v2.4.1)):
foo/left/bar/right
"""
regex, include = GitWildMatchPattern.pattern_to_regex('left/**/right')
self.assertTrue(include)
self.assertEqual(regex, f'^left(?:/.+)?/right{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'left/right',
'left/bar/right',
'left/foo/bar/right',
'left/bar/right/foo',
'foo/left/bar/right',
]))
self.assertEqual(results, {
'left/right',
'left/bar/right',
'left/foo/bar/right',
'left/bar/right/foo',
})
def test_03_only_double_asterisk(self):
"""
Tests a double-asterisk pattern which matches everything.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('**')
self.assertTrue(include)
self.assertEqual(regex, f'^[^/]+(?:/.*)?$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'x',
'y.py',
'A/x',
'A/y.py',
'A/B/x',
'A/B/y.py',
'A/B/C/x',
'A/B/C/y.py',
]))
self.assertEqual(results, {
'x',
'y.py',
'A/x',
'A/y.py',
'A/B/x',
'A/B/y.py',
'A/B/C/x',
'A/B/C/y.py',
})
def test_03_parent_double_asterisk(self):
"""
Tests a file name with a double-asterisk parent directory.
This should match:
spam
foo/spam
foo/spam/bar
"""
regex, include = GitWildMatchPattern.pattern_to_regex('**/spam')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?spam{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'spam',
'foo/spam',
'foo/spam/bar',
]))
self.assertEqual(results, {
'spam',
'foo/spam',
'foo/spam/bar',
})
def test_03_duplicate_leading_double_asterisk_edge_case(self):
"""
Regression test for duplicate leading **/ bug.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('**')
self.assertTrue(include)
self.assertEqual(regex, "^[^/]+(?:/.*)?$")
equivalent_regex, include = GitWildMatchPattern.pattern_to_regex('**/**')
self.assertTrue(include)
self.assertEqual(equivalent_regex, regex)
equivalent_regex, include = GitWildMatchPattern.pattern_to_regex('**/**/**')
self.assertTrue(include)
self.assertEqual(equivalent_regex, regex)
regex, include = GitWildMatchPattern.pattern_to_regex('**/api')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?api{RE_SUB}$')
equivalent_regex, include = GitWildMatchPattern.pattern_to_regex(f'**/**/api')
self.assertTrue(include)
self.assertEqual(equivalent_regex, regex)
regex, include = GitWildMatchPattern.pattern_to_regex('**/api/')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?api{RE_DIR}.*$')
equivalent_regex, include = GitWildMatchPattern.pattern_to_regex(f'**/**/api/')
self.assertTrue(include)
self.assertEqual(equivalent_regex, regex)
regex, include = GitWildMatchPattern.pattern_to_regex('**/api/**')
self.assertTrue(include)
self.assertEqual(regex, "^(?:.+/)?api/.*$")
equivalent_regex, include = GitWildMatchPattern.pattern_to_regex('**/**/api/**/**')
self.assertTrue(include)
self.assertEqual(equivalent_regex, regex)
def test_03_double_asterisk_trailing_slash_edge_case(self):
"""
Tests the edge-case **/ pattern.
This should match everything except individual files in the root directory.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('**/')
self.assertTrue(include)
self.assertEqual(regex, f'^.+{RE_DIR}.*$')
equivalent_regex, include = GitWildMatchPattern.pattern_to_regex('**/**/')
self.assertTrue(include)
self.assertEqual(equivalent_regex, regex)
def test_04_infix_wildcard(self):
"""
Tests a pattern with an infix wildcard.
This should match:
foo--bar
foo-hello-bar
a/foo-hello-bar
foo-hello-bar/b
a/foo-hello-bar/b
"""
regex, include = GitWildMatchPattern.pattern_to_regex('foo-*-bar')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?foo\\-[^/]*\\-bar{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'foo--bar',
'foo-hello-bar',
'a/foo-hello-bar',
'foo-hello-bar/b',
'a/foo-hello-bar/b',
]))
self.assertEqual(results, {
'foo--bar',
'foo-hello-bar',
'a/foo-hello-bar',
'foo-hello-bar/b',
'a/foo-hello-bar/b',
})
def test_04_postfix_wildcard(self):
"""
Tests a pattern with a postfix wildcard.
This should match:
~temp-
~temp-foo
~temp-foo/bar
foo/~temp-bar
foo/~temp-bar/baz
"""
regex, include = GitWildMatchPattern.pattern_to_regex('~temp-*')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?\\~temp\\-[^/]*{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'~temp-',
'~temp-foo',
'~temp-foo/bar',
'foo/~temp-bar',
'foo/~temp-bar/baz',
]))
self.assertEqual(results, {
'~temp-',
'~temp-foo',
'~temp-foo/bar',
'foo/~temp-bar',
'foo/~temp-bar/baz',
})
def test_04_prefix_wildcard(self):
"""
Tests a pattern with a prefix wildcard.
This should match:
bar.py
bar.py/
foo/bar.py
foo/bar.py/baz
"""
regex, include = GitWildMatchPattern.pattern_to_regex('*.py')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?[^/]*\\.py{RE_SUB}$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'bar.py',
'bar.py/',
'foo/bar.py',
'foo/bar.py/baz',
]))
self.assertEqual(results, {
'bar.py',
'bar.py/',
'foo/bar.py',
'foo/bar.py/baz',
})
def test_05_directory(self):
"""
Tests a directory pattern.
This should match:
dir/
foo/dir/
foo/dir/bar
This should **not** match:
dir
"""
regex, include = GitWildMatchPattern.pattern_to_regex('dir/')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?dir{RE_DIR}.*$')
pattern = GitWildMatchPattern(re.compile(regex), include)
results = set(filter(pattern.match_file, [
'dir/',
'foo/dir/',
'foo/dir/bar',
'dir',
]))
self.assertEqual(results, {
'dir/',
'foo/dir/',
'foo/dir/bar',
})
def test_06_registered(self):
"""
Tests that the pattern is registered.
"""
self.assertIs(lookup_pattern('gitwildmatch'), GitWildMatchPattern)
def test_06_access_deprecated(self):
"""
Tests that the pattern is accessible from the root module using the
deprecated alias.
"""
self.assertTrue(hasattr(pathspec, 'GitIgnorePattern'))
self.assertTrue(issubclass(pathspec.GitIgnorePattern, GitWildMatchPattern))
def test_06_registered_deprecated(self):
"""
Tests that the pattern is registered under the deprecated alias.
"""
self.assertIs(lookup_pattern('gitignore'), pathspec.GitIgnorePattern)
def test_07_encode_bytes(self):
"""
Test encoding bytes.
"""
encoded = "".join(map(chr, range(0, 256))).encode(_BYTES_ENCODING)
expected = (
b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10"
b"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
b" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\"
b"]^_`abcdefghijklmnopqrstuvwxyz{|}~"
b"\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d"
b"\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c"
b"\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab"
b"\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba"
b"\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9"
b"\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8"
b"\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7"
b"\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6"
b"\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
)
self.assertEqual(encoded, expected)
def test_07_decode_bytes(self):
"""
Test decoding bytes.
"""
decoded = bytes(bytearray(range(0, 256))).decode(_BYTES_ENCODING)
expected = (
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\"
"]^_`abcdefghijklmnopqrstuvwxyz{|}~"
"\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d"
"\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c"
"\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab"
"\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba"
"\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9"
"\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8"
"\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7"
"\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6"
"\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
)
self.assertEqual(decoded, expected)
def test_07_match_bytes_and_bytes(self):
"""
Test byte string patterns matching byte string paths.
"""
pattern = GitWildMatchPattern(b'*.py')
results = set(filter(pattern.match_file, [b'a.py']))
self.assertEqual(results, {b'a.py'})
def test_07_match_bytes_and_bytes_complete(self):
"""
Test byte string patterns matching byte string paths.
"""
encoded = bytes(bytearray(range(0, 256)))
# Forward slashes cannot be escaped with the current implementation.
# Remove ASCII 47.
fs_ord = ord('/')
encoded = encoded[:fs_ord] + encoded[fs_ord+1:]
escaped = b"".join(b"\\" + encoded[i:i+1] for i in range(len(encoded)))
pattern = GitWildMatchPattern(escaped)
results = set(filter(pattern.match_file, [encoded]))
self.assertEqual(results, {encoded})
def test_07_match_bytes_and_unicode_fail(self):
"""
Test byte string patterns matching byte string paths.
"""
pattern = GitWildMatchPattern(b'*.py')
with self.assertRaises(TypeError):
pattern.match_file('a.py')
def test_07_match_unicode_and_bytes_fail(self):
"""
Test unicode patterns with byte paths.
"""
pattern = GitWildMatchPattern('*.py')
with self.assertRaises(TypeError):
pattern.match_file(b'a.py')
def test_07_match_unicode_and_unicode(self):
"""
Test unicode patterns with unicode paths.
"""
pattern = GitWildMatchPattern('*.py')
results = set(filter(pattern.match_file, ['a.py']))
self.assertEqual(results, {'a.py'})
def test_08_escape(self):
"""
Test escaping a string with meta-characters
"""
fname = "file!with*weird#naming_[1].t?t"
escaped = r"file\!with\*weird\#naming_\[1\].t\?t"
result = GitWildMatchPattern.escape(fname)
self.assertEqual(result, escaped)
def test_09_single_escape_fail(self):
"""
Test an escape on a line by itself.
"""
self._check_invalid_pattern("\\")
def test_09_single_exclamation_mark_fail(self):
"""
Test an escape on a line by itself.
"""
self._check_invalid_pattern("!")
def test_10_escape_asterisk_end(self):
"""
Test escaping an asterisk at the end of a line.
"""
pattern = GitWildMatchPattern("asteris\\*")
results = set(filter(pattern.match_file, [
"asteris*",
"asterisk",
]))
self.assertEqual(results, {"asteris*"})
def test_10_escape_asterisk_mid(self):
"""
Test escaping an asterisk in the middle of a line.
"""
pattern = GitWildMatchPattern("as\\*erisk")
results = set(filter(pattern.match_file, [
"as*erisk",
"asterisk",
]))
self.assertEqual(results, {"as*erisk"})
def test_10_escape_asterisk_start(self):
"""
Test escaping an asterisk at the start of a line.
"""
pattern = GitWildMatchPattern("\\*sterisk")
results = set(filter(pattern.match_file, [
"*sterisk",
"asterisk",
]))
self.assertEqual(results, {"*sterisk"})
def test_10_escape_exclamation_mark_start(self):
"""
Test escaping an exclamation mark at the start of a line.
"""
pattern = GitWildMatchPattern("\\!mark")
results = set(filter(pattern.match_file, [
"!mark",
]))
self.assertEqual(results, {"!mark"})
def test_10_escape_pound_start(self):
"""
Test escaping a pound sign at the start of a line.
"""
pattern = GitWildMatchPattern("\\#sign")
results = set(filter(pattern.match_file, [
"#sign",
]))
self.assertEqual(results, {"#sign"})
def test_11_issue_19_directory_a(self):
"""
Test a directory discrepancy, scenario A.
"""
# NOTE: The result from GitWildMatchPattern will differ from GitIgnoreSpec.
pattern = GitWildMatchPattern("dirG/")
results = set(filter(pattern.match_file, [
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
]))
self.assertEqual(results, {
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
})
def test_11_issue_19_directory_b(self):
"""
Test a directory discrepancy, scenario B.
"""
# NOTE: The result from GitWildMatchPattern will differ from GitIgnoreSpec.
pattern = GitWildMatchPattern("dirG/*")
results = set(filter(pattern.match_file, [
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
]))
self.assertEqual(results, {
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
})
def test_11_issue_19_directory_c(self):
"""
Test a directory discrepancy, scenario C.
"""
# NOTE: The result from GitWildMatchPattern will differ from GitIgnoreSpec.
pattern = GitWildMatchPattern("dirG/**")
results = set(filter(pattern.match_file, [
'fileA',
'fileB',
'dirD/fileE',
'dirD/fileF',
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
]))
self.assertEqual(results, {
'dirG/dirH/fileI',
'dirG/dirH/fileJ',
'dirG/fileO',
})
def test_12_asterisk_1_regex(self):
"""
Test a relative asterisk path pattern's regular expression.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('*')
self.assertTrue(include)
self.assertEqual(regex, f'^(?:.+/)?[^/]+{RE_SUB}$')
def test_12_asterisk_2_regex_equivalent(self):
"""
Test a path pattern equivalent to the relative asterisk using double
asterisk.
"""
regex, include = GitWildMatchPattern.pattern_to_regex('*')
self.assertTrue(include)
equiv_regex, include = GitWildMatchPattern.pattern_to_regex('**/*')
self.assertTrue(include)
self.assertEqual(regex, equiv_regex)
def test_12_asterisk_3_child(self):
"""
Test a relative asterisk path pattern matching a direct child path.
"""
pattern = GitWildMatchPattern("*")
self.assertTrue(pattern.match_file("file.txt"))
def test_12_asterisk_4_descendant(self):
"""
Test a relative asterisk path pattern matching a descendant path.
"""
pattern = GitWildMatchPattern("*")
self.assertTrue(pattern.match_file("anydir/file.txt"))
def test_12_issue_62(self):
"""
Test including all files, scenario A.
"""
pattern = GitWildMatchPattern("*")
results = set(filter(pattern.match_file, [
"file.txt",
"anydir/file.txt",
]))
self.assertEqual(results, {
"file.txt",
"anydir/file.txt",
})
def test_13_issue_77_1_negate_with_caret(self):
"""
Test negation using the caret symbol ("^").
"""
pattern = GitWildMatchPattern("a[^gy]c")
results = set(filter(pattern.match_file, [
"agc",
"ayc",
"abc",
"adc",
]))
self.assertEqual(results, {
"abc",
"adc",
})
def test_13_issue_77_1_negate_with_exclamation_mark(self):
"""
Test negation using the exclamation mark ("!").
"""
pattern = GitWildMatchPattern("a[!gy]c")
results = set(filter(pattern.match_file, [
"agc",
"ayc",
"abc",
"adc",
]))
self.assertEqual(results, {
"abc",
"adc",
})
def test_13_issue_77_2_regex(self):
"""
Test the resulting regex for regex bracket expression negation.
"""
regex, include = GitWildMatchPattern.pattern_to_regex("a[^b]c")
self.assertTrue(include)
equiv_regex, include = GitWildMatchPattern.pattern_to_regex("a[!b]c")
self.assertTrue(include)
self.assertEqual(regex, equiv_regex)
def test_14_issue_81_a(self):
"""
Test ignoring files in a directory, scenario A.
"""
pattern = GitWildMatchPattern("!libfoo/**")
self.assertEqual(pattern.regex.pattern, "^libfoo/.*$")
self.assertIs(pattern.include, False)
self.assertTrue(pattern.match_file("libfoo/__init__.py"))
def test_14_issue_81_b(self):
"""
Test ignoring files in a directory, scenario B.
"""
pattern = GitWildMatchPattern("!libfoo/*")
self.assertEqual(pattern.regex.pattern, f"^libfoo/[^/]+{RE_SUB}$")
self.assertIs(pattern.include, False)
self.assertTrue(pattern.match_file("libfoo/__init__.py"))
def test_14_issue_81_c(self):
"""
Test ignoring files in a directory, scenario C.
"""
# GitWildMatchPattern will match the file, but GitIgnoreSpec should not.
pattern = GitWildMatchPattern("!libfoo/")
self.assertEqual(pattern.regex.pattern, f"^(?:.+/)?libfoo{RE_DIR}.*$")
self.assertIs(pattern.include, False)
self.assertTrue(pattern.match_file("libfoo/__init__.py"))
|