Debian Codegraph @

tests/test_06_gitignore.py

raw
1 """
2 This script tests :class:`.GitIgnoreSpec`.
3 """
4
5 import unittest
6 from collections.abc import (
7 Iterable,
8 Iterator,
9 Sequence)
10 from contextlib import (
11 AbstractContextManager,
12 contextmanager)
13 from functools import (
14 partial)
15 from typing import (
16 Callable, # Replaced by `collections.abc.Callable` in 3.9.2.
17 Optional) # Replaced by `X | None` in 3.10.
18 from unittest import (
19 SkipTest)
20
21 from pathspec.backend import (
22 BackendNamesHint,
23 _Backend)
24 from pathspec._backends.hyperscan.gitignore import (
25 HyperscanGiBackend)
26 from pathspec._backends.re2.gitignore import (
27 Re2GiBackend)
28 from pathspec._backends.simple.gitignore import (
29 SimpleGiBackend)
30 from pathspec.gitignore import (
31 GitIgnoreSpec)
32 from pathspec.pattern import (
33 Pattern)
34 from pathspec._typing import (
35 AnyStr) # Removed in 3.18.
36
37 from .util import (
38 debug_results,
39 get_includes,
40 require_backend,
41 reverse_inplace,
42 shuffle_inplace)
43
44 BACKENDS: list[BackendNamesHint] = [
45 'hyperscan',
46 're2',
47 ]
48 """
49 The backend parameters.
50 """
51
52
53 class GitIgnoreSpecTest(unittest.TestCase):
54 """
55 The :class:`GitIgnoreSpecTest` class tests the :class:`.GitIgnoreSpec` class.
56 """
57
58 def parameterize_from_lines(
59 self,
60 lines: Iterable[AnyStr],
61 ) -> Iterator[Callable[[], AbstractContextManager[GitIgnoreSpec]]]:
62 """
63 Parameterize `GitIgnoreSpec.from_lines()` for each backend and configuration
64 to begin a subtest.
65
66 *pattern_factory* (:class:`str`) is the pattern factory.
67
68 *lines* (:class:`Iterable` of :class:`str`) yields the lines.
69
70 Returns an :class:`Iterator` yielding each context for the
71 :class:`GitIgnoreSpec`.
72 """
73 lines = list(lines)
74
75 configs: list[tuple[
76 str, BackendNamesHint, Optional[Callable[[Sequence[Pattern]], _Backend]]
77 ]] = []
78
79 # Simple backend, no optimizations.
80 configs.append((
81 "simple (unopt)",
82 'simple',
83 partial(SimpleGiBackend, no_filter=True, no_reverse=True),
84 ))
85
86 # Simple backend, minimal optimizations.
87 configs.append((
88 "simple (minopt)",
89 'simple',
90 None,
91 ))
92
93 # Add additional backends.
94 for backend in BACKENDS:
95 if backend == 'hyperscan':
96 configs.append((
97 f"hyperscan (forward)",
98 backend,
99 partial(HyperscanGiBackend, _debug_exprs=True),
100 ))
101 configs.append((
102 f"hyperscan (reverse)",
103 backend,
104 partial(HyperscanGiBackend, _debug_exprs=True, _test_sort=reverse_inplace)
105 ))
106 configs.append((
107 f"hyperscan (shuffle)",
108 backend,
109 partial(HyperscanGiBackend, _debug_exprs=True, _test_sort=shuffle_inplace)
110 ))
111 elif backend == 're2':
112 configs.append((
113 f"re2 (forward)",
114 backend,
115 partial(Re2GiBackend, _debug_regex=True),
116 ))
117 configs.append((
118 f"re2 (reverse)",
119 backend,
120 partial(Re2GiBackend, _debug_regex=True, _test_sort=reverse_inplace)
121 ))
122 configs.append((
123 f"re2 (shuffle)",
124 backend,
125 partial(Re2GiBackend, _debug_regex=True, _test_sort=shuffle_inplace)
126 ))
127 else:
128 configs.append((
129 backend,
130 backend,
131 None,
132 ))
133
134 for label, backend, backend_factory in configs:
135 try:
136 require_backend(backend)
137 except SkipTest:
138 with self.subTest(label):
139 raise
140 continue
141
142 @contextmanager
143 def _sub_test(
144 backend=backend,
145 backend_factory=backend_factory,
146 label=label,
147 ):
148 with self.subTest(label):
149 yield GitIgnoreSpec.from_lines(
150 lines,
151 backend=backend,
152 _test_backend_factory=backend_factory,
153 )
154
155 yield _sub_test
156
157 def test_01_reversed_args(self):
158 """
159 Test reversed args for `.from_lines()`.
160 """
161 spec = GitIgnoreSpec.from_lines('gitignore', ['*.txt'])
162 files = {
163 'a.txt',
164 'b.bin',
165 }
166
167 results = list(spec.check_files(files))
168 ignores = get_includes(results)
169 debug = debug_results(spec, results)
170
171 self.assertEqual(ignores, {
172 'a.txt',
173 }, debug)
174
175 def test_02_dir_exclusions(self):
176 """
177 Test directory exclusions.
178 """
179 for sub_test in self.parameterize_from_lines([
180 '*.txt',
181 '!test1/',
182 ]):
183 with sub_test() as spec:
184 files = {
185 'test1/a.txt',
186 'test1/b.bin',
187 'test1/c/c.txt',
188 'test2/a.txt',
189 'test2/b.bin',
190 'test2/c/c.txt',
191 }
192
193 results = list(spec.check_files(files))
194 ignores = get_includes(results)
195 debug = debug_results(spec, results)
196
197 self.assertEqual(ignores, {
198 'test1/a.txt',
199 'test1/c/c.txt',
200 'test2/a.txt',
201 'test2/c/c.txt',
202 }, debug)
203 self.assertEqual(files - ignores, {
204 'test1/b.bin',
205 'test2/b.bin',
206 }, debug)
207
208 def test_02_file_exclusions(self):
209 """
210 Test file exclusions.
211 """
212 for sub_test in self.parameterize_from_lines([
213 '*.txt',
214 '!b.txt',
215 ]):
216 with sub_test() as spec:
217 files = {
218 'X/a.txt',
219 'X/b.txt',
220 'X/Z/c.txt',
221 'Y/a.txt',
222 'Y/b.txt',
223 'Y/Z/c.txt',
224 }
225
226 results = list(spec.check_files(files))
227 ignores = get_includes(results)
228 debug = debug_results(spec, results)
229
230 self.assertEqual(ignores, {
231 'X/a.txt',
232 'X/Z/c.txt',
233 'Y/a.txt',
234 'Y/Z/c.txt',
235 }, debug)
236 self.assertEqual(files - ignores, {
237 'X/b.txt',
238 'Y/b.txt',
239 }, debug)
240
241 def test_02_issue_41_a(self):
242 """
243 Test including a file and excluding a directory with the same name pattern,
244 scenario A.
245 """
246 for sub_test in self.parameterize_from_lines([
247 '*.yaml',
248 '!*.yaml/',
249 ]):
250 with sub_test() as spec:
251 # Confirmed results with git (v2.42.0).
252 files = {
253 'dir.yaml/file.sql', # -
254 'dir.yaml/file.yaml', # 1:*.yaml
255 'dir.yaml/index.txt', # -
256 'dir/file.sql', # -
257 'dir/file.yaml', # 1:*.yaml
258 'dir/index.txt', # -
259 'file.yaml', # 1:*.yaml
260 }
261
262 results = list(spec.check_files(files))
263 ignores = get_includes(results)
264 debug = debug_results(spec, results)
265
266 self.assertEqual(ignores, {
267 'dir.yaml/file.yaml',
268 'dir/file.yaml',
269 'file.yaml',
270 }, debug)
271 self.assertEqual(files - ignores, {
272 'dir.yaml/file.sql',
273 'dir.yaml/index.txt',
274 'dir/file.sql',
275 'dir/index.txt',
276 }, debug)
277
278 def test_02_issue_41_b(self):
279 """
280 Test including a file and excluding a directory with the same name pattern,
281 scenario B.
282 """
283 for sub_test in self.parameterize_from_lines([
284 '!*.yaml/',
285 '*.yaml',
286 ]):
287 with sub_test() as spec:
288 # Confirmed results with git (v2.42.0).
289 files = {
290 'dir.yaml/file.sql', # 2:*.yaml
291 'dir.yaml/file.yaml', # 2:*.yaml
292 'dir.yaml/index.txt', # 2:*.yaml
293 'dir/file.sql', # -
294 'dir/file.yaml', # 2:*.yaml
295 'dir/index.txt', # -
296 'file.yaml', # 2:*.yaml
297 }
298
299 results = list(spec.check_files(files))
300 ignores = get_includes(results)
301 debug = debug_results(spec, results)
302
303 self.assertEqual(ignores, {
304 'dir.yaml/file.sql',
305 'dir.yaml/file.yaml',
306 'dir.yaml/index.txt',
307 'dir/file.yaml',
308 'file.yaml',
309 }, debug)
310 self.assertEqual(files - ignores, {
311 'dir/file.sql',
312 'dir/index.txt',
313 }, debug)
314
315 def test_02_issue_41_c(self):
316 """
317 Test including a file and excluding a directory with the same name pattern,
318 scenario C.
319 """
320 for sub_test in self.parameterize_from_lines([
321 '*.yaml',
322 '!dir.yaml',
323 ]):
324 with sub_test() as spec:
325 # Confirmed results with git check-ignore (v2.42.0).
326 files = {
327 'dir.yaml/file.sql', # -
328 'dir.yaml/file.yaml', # 1:*.yaml
329 'dir.yaml/index.txt', # -
330 'dir/file.sql', # -
331 'dir/file.yaml', # 1:*.yaml
332 'dir/index.txt', # -
333 'file.yaml', # 1:*.yaml
334 }
335
336 results = list(spec.check_files(files))
337 ignores = get_includes(results)
338 debug = debug_results(spec, results)
339
340 self.assertEqual(ignores, {
341 'dir.yaml/file.yaml',
342 'dir/file.yaml',
343 'file.yaml',
344 }, debug)
345 self.assertEqual(files - ignores, {
346 'dir.yaml/file.sql',
347 'dir.yaml/index.txt',
348 'dir/file.sql',
349 'dir/index.txt',
350 }, debug)
351
352 def test_03_subdir(self):
353 """
354 Test matching files in a subdirectory of an included directory.
355 """
356 for sub_test in self.parameterize_from_lines([
357 "dirG/",
358 ]):
359 with sub_test() as spec:
360 files = {
361 'fileA',
362 'fileB',
363 'dirD/fileE',
364 'dirD/fileF',
365 'dirG/dirH/fileI',
366 'dirG/dirH/fileJ',
367 'dirG/fileO',
368 }
369
370 results = list(spec.check_files(files))
371 ignores = get_includes(results)
372 debug = debug_results(spec, results)
373
374 self.assertEqual(ignores, {
375 'dirG/dirH/fileI',
376 'dirG/dirH/fileJ',
377 'dirG/fileO',
378 }, debug)
379 self.assertEqual(files - ignores, {
380 'fileA',
381 'fileB',
382 'dirD/fileE',
383 'dirD/fileF',
384 }, debug)
385
386 def test_03_issue_19_a(self):
387 """
388 Test matching files in a subdirectory of an included directory, scenario A.
389 """
390 for sub_test in self.parameterize_from_lines([
391 "dirG/",
392 ]):
393 with sub_test() as spec:
394 files = {
395 'fileA',
396 'fileB',
397 'dirD/fileE',
398 'dirD/fileF',
399 'dirG/dirH/fileI',
400 'dirG/dirH/fileJ',
401 'dirG/fileO',
402 }
403
404 results = list(spec.check_files(files))
405 ignores = get_includes(results)
406 debug = debug_results(spec, results)
407
408 self.assertEqual(ignores, {
409 'dirG/dirH/fileI',
410 'dirG/dirH/fileJ',
411 'dirG/fileO',
412 }, debug)
413 self.assertEqual(files - ignores, {
414 'fileA',
415 'fileB',
416 'dirD/fileE',
417 'dirD/fileF',
418 }, debug)
419
420 def test_03_issue_19_b(self):
421 """
422 Test matching files in a subdirectory of an included directory, scenario B.
423 """
424 for sub_test in self.parameterize_from_lines([
425 "dirG/*",
426 ]):
427 with sub_test() as spec:
428 files = {
429 'fileA',
430 'fileB',
431 'dirD/fileE',
432 'dirD/fileF',
433 'dirG/dirH/fileI',
434 'dirG/dirH/fileJ',
435 'dirG/fileO',
436 }
437
438 results = list(spec.check_files(files))
439 ignores = get_includes(results)
440 debug = debug_results(spec, results)
441
442 self.assertEqual(ignores, {
443 'dirG/dirH/fileI',
444 'dirG/dirH/fileJ',
445 'dirG/fileO',
446 }, debug)
447 self.assertEqual(files - ignores, {
448 'fileA',
449 'fileB',
450 'dirD/fileE',
451 'dirD/fileF',
452 }, debug)
453
454 def test_03_issue_19_c(self):
455 """
456 Test matching files in a subdirectory of an included directory, scenario C.
457 """
458 for sub_test in self.parameterize_from_lines([
459 "dirG/**",
460 ]):
461 with sub_test() as spec:
462 files = {
463 'fileA',
464 'fileB',
465 'dirD/fileE',
466 'dirD/fileF',
467 'dirG/dirH/fileI',
468 'dirG/dirH/fileJ',
469 'dirG/fileO',
470 }
471
472 results = list(spec.check_files(files))
473 ignores = get_includes(results)
474 debug = debug_results(spec, results)
475
476 self.assertEqual(ignores, {
477 'dirG/dirH/fileI',
478 'dirG/dirH/fileJ',
479 'dirG/fileO',
480 }, debug)
481 self.assertEqual(files - ignores, {
482 'fileA',
483 'fileB',
484 'dirD/fileE',
485 'dirD/fileF',
486 }, debug)
487
488 def test_04_issue_62(self):
489 """
490 Test including all files and excluding a directory.
491 """
492 for sub_test in self.parameterize_from_lines([
493 '*',
494 '!product_dir/',
495 ]):
496 with sub_test() as spec:
497 files = {
498 'anydir/file.txt',
499 'product_dir/file.txt',
500 }
501
502 results = list(spec.check_files(files))
503 ignores = get_includes(results)
504 debug = debug_results(spec, results)
505
506 self.assertEqual(ignores, {
507 'anydir/file.txt',
508 'product_dir/file.txt',
509 }, debug)
510
511 def test_05_issue_39(self):
512 """
513 Test excluding files in a directory.
514 """
515 for sub_test in self.parameterize_from_lines([
516 '*.log',
517 '!important/*.log',
518 'trace.*',
519 ]):
520 with sub_test() as spec:
521 files = {
522 'a.log',
523 'b.txt',
524 'important/d.log',
525 'important/e.txt',
526 'trace.c',
527 }
528
529 results = list(spec.check_files(files))
530 ignores = get_includes(results)
531 debug = debug_results(spec, results)
532
533 self.assertEqual(ignores, {
534 'a.log',
535 'trace.c',
536 }, debug)
537 self.assertEqual(files - ignores, {
538 'b.txt',
539 'important/d.log',
540 'important/e.txt',
541 }, debug)
542
543 def test_06_issue_64(self):
544 """
545 Test using a double asterisk pattern.
546 """
547 for sub_test in self.parameterize_from_lines([
548 "**",
549 ]):
550 with sub_test() as spec:
551 files = {
552 'x',
553 'y.py',
554 'A/x',
555 'A/y.py',
556 'A/B/x',
557 'A/B/y.py',
558 'A/B/C/x',
559 'A/B/C/y.py',
560 }
561
562 results = list(spec.check_files(files))
563 ignores = get_includes(results)
564 debug = debug_results(spec, results)
565
566 self.assertEqual(ignores, files, debug)
567
568 def test_07_issue_74(self):
569 """
570 Test include directory should override exclude file.
571 """
572 for sub_test in self.parameterize_from_lines([
573 '*', # Ignore all files by default
574 '!*/', # but scan all directories
575 '!*.txt', # Text files
576 '/test1/**', # ignore all in the directory
577 ]):
578 with sub_test() as spec:
579 files = {
580 'test1/a.txt', # 4:/test1/**
581 'test1/b.bin', # 4:/test1/**
582 'test1/c/c.txt', # 4:/test1/**
583 'test2/a.txt', # -
584 'test2/b.bin', # 1:*
585 'test2/c/c.txt', # -
586 }
587
588 results = list(spec.check_files(files))
589 ignores = get_includes(results)
590 debug = debug_results(spec, results)
591
592 self.assertEqual(ignores, {
593 'test1/a.txt',
594 'test1/b.bin',
595 'test1/c/c.txt',
596 'test2/b.bin',
597 }, debug)
598 self.assertEqual(files - ignores, {
599 'test2/a.txt',
600 'test2/c/c.txt',
601 }, debug)
602
603 def test_08_issue_81_a(self):
604 """
605 Test issue 81 whitelist, scenario A.
606 """
607 for sub_test in self.parameterize_from_lines([
608 "*",
609 "!libfoo",
610 "!libfoo/**",
611 ]):
612 with sub_test() as spec:
613 # Confirmed results with git (v2.42.0).
614 files = {
615 "ignore.txt", # 1:*
616 "libfoo/__init__.py", # 3:!libfoo/**
617 }
618
619 results = list(spec.check_files(files))
620 ignores = get_includes(results)
621 debug = debug_results(spec, results)
622
623 self.assertEqual(ignores, {
624 "ignore.txt",
625 }, debug)
626 self.assertEqual(files - ignores, {
627 "libfoo/__init__.py",
628 }, debug)
629
630 def test_08_issue_81_b(self):
631 """
632 Test issue 81 whitelist, scenario B.
633 """
634 for sub_test in self.parameterize_from_lines([
635 "*",
636 "!libfoo",
637 "!libfoo/*",
638 ]):
639 with sub_test() as spec:
640 # Confirmed results with git (v2.42.0).
641 files = {
642 "ignore.txt", # 1:*
643 "libfoo/__init__.py", # 3:!libfoo/*
644 }
645
646 results = list(spec.check_files(files))
647 ignores = get_includes(results)
648 debug = debug_results(spec, results)
649
650 self.assertEqual(ignores, {
651 "ignore.txt",
652 }, debug)
653 self.assertEqual(files - ignores, {
654 "libfoo/__init__.py",
655 }, debug)
656
657 def test_08_issue_81_c(self):
658 """
659 Test issue 81 whitelist, scenario C.
660 """
661 for sub_test in self.parameterize_from_lines([
662 "*",
663 "!libfoo",
664 "!libfoo/",
665 ]):
666 with sub_test() as spec:
667 # Confirmed results with git (v2.42.0).
668 files = {
669 "ignore.txt", # 1:*
670 "libfoo/__init__.py", # 1:*
671 }
672 results = list(spec.check_files(files))
673 ignores = get_includes(results)
674 debug = debug_results(spec, results)
675 self.assertEqual(ignores, {
676 "ignore.txt",
677 "libfoo/__init__.py",
678 }, debug)
679 self.assertEqual(files - ignores, set())
680
681 def test_09_issue_100(self):
682 """
683 Test an empty list of patterns.
684 """
685 for sub_test in self.parameterize_from_lines([]):
686 with sub_test() as spec:
687 files = {'foo'}
688 results = list(spec.check_files(files))
689 includes = get_includes(results)
690 debug = debug_results(spec, results)
691 self.assertEqual(includes, set(), debug)