Debian Codegraph @

tests/test_03_gitignore_basic.py

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