|
| 1 | +""" |
| 2 | +This script tests the trailing space trimming behavior for gitignore |
| 3 | +patterns which end with a backslash followed by a space. |
| 4 | +
|
| 5 | +Git's *trim_trailing_spaces()* function (dir.c) only keeps a trailing |
| 6 | +space when it is escaped by a backslash, and a space is only escaped when |
| 7 | +it is preceded by an odd number of consecutive backslashes. When the run |
| 8 | +of backslashes preceding the trailing space is even, the backslashes |
| 9 | +escape each other and the trailing space is unescaped — Git strips it. |
| 10 | +
|
| 11 | +E.g., with an escaped backslash ('foo\\\\') followed by a space, Git |
| 12 | +treats the space as unescaped trailing whitespace and matches the file |
| 13 | +'foo\\' rather than 'foo\\ '. |
| 14 | +""" |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +from pathspec import ( |
| 19 | + PathSpec) |
| 20 | +from pathspec.patterns.gitignore.basic import ( |
| 21 | + GitIgnoreBasicPattern) |
| 22 | +from pathspec.patterns.gitignore.spec import ( |
| 23 | + GitIgnoreSpecPattern) |
| 24 | + |
| 25 | +BS = '\\' |
| 26 | +""" |
| 27 | +Backslash. |
| 28 | +""" |
| 29 | + |
| 30 | + |
| 31 | +class TrailingSpaceAfterEscapedBackslashTest(unittest.TestCase): |
| 32 | + """ |
| 33 | + The :class:`TrailingSpaceAfterEscapedBackslashTest` class tests that a |
| 34 | + trailing space preceded by an escaped backslash is stripped, matching |
| 35 | + Git's behavior. |
| 36 | + """ |
| 37 | + |
| 38 | + def _assert_matches(self, pattern_class, raw_pattern: str, file: str, expected: bool) -> None: |
| 39 | + pattern = pattern_class(raw_pattern) |
| 40 | + actual = pattern.match_file(file) |
| 41 | + self.assertIs( |
| 42 | + actual is not None, |
| 43 | + expected, |
| 44 | + f"Pattern {raw_pattern!r} matching file {file!r}: expected {expected}, got {actual is not None} (regex: {pattern.regex.pattern if pattern.regex is not None else None})", |
| 45 | + ) |
| 46 | + |
| 47 | + def test_00_even_backslash_run_trailing_space_stripped(self): |
| 48 | + """ |
| 49 | + Tests that a trailing space preceded by an even number of |
| 50 | + backslashes is unescaped and stripped. |
| 51 | + """ |
| 52 | + # 'foo\\ ' (escaped backslash followed by space): Git strips the |
| 53 | + # unescaped trailing space and matches the file 'foo\'. |
| 54 | + for pattern_class in (GitIgnoreBasicPattern, GitIgnoreSpecPattern): |
| 55 | + with self.subTest(pattern_class=pattern_class.__name__): |
| 56 | + self._assert_matches(pattern_class, f'foo{BS * 2} ', f'foo{BS}', True) |
| 57 | + self._assert_matches(pattern_class, f'foo{BS * 2} ', f'foo{BS} ', False) |
| 58 | + |
| 59 | + def test_01_four_backslashes_trailing_space_stripped(self): |
| 60 | + """ |
| 61 | + Tests that a trailing space preceded by four backslashes (two |
| 62 | + escaped backslashes) is unescaped and stripped. |
| 63 | + """ |
| 64 | + for pattern_class in (GitIgnoreBasicPattern, GitIgnoreSpecPattern): |
| 65 | + with self.subTest(pattern_class=pattern_class.__name__): |
| 66 | + self._assert_matches(pattern_class, f'foo{BS * 4} ', f'foo{BS * 2}', True) |
| 67 | + self._assert_matches(pattern_class, f'foo{BS * 4} ', f'foo{BS * 2} ', False) |
| 68 | + |
| 69 | + def test_02_odd_backslash_run_trailing_space_kept(self): |
| 70 | + """ |
| 71 | + Tests that a trailing space preceded by an odd number of |
| 72 | + backslashes is escaped and kept. |
| 73 | + """ |
| 74 | + # 'foo\ ' has an escaped space and matches the file 'foo '. |
| 75 | + for pattern_class in (GitIgnoreBasicPattern, GitIgnoreSpecPattern): |
| 76 | + with self.subTest(pattern_class=pattern_class.__name__): |
| 77 | + self._assert_matches(pattern_class, f'foo{BS} ', f'foo ', True) |
| 78 | + self._assert_matches(pattern_class, f'foo{BS} ', f'foo', False) |
| 79 | + |
| 80 | + def test_03_three_backslashes_trailing_space_kept(self): |
| 81 | + """ |
| 82 | + Tests that a trailing space preceded by three backslashes (an |
| 83 | + escaped backslash followed by an escaped space) is kept. |
| 84 | + """ |
| 85 | + for pattern_class in (GitIgnoreBasicPattern, GitIgnoreSpecPattern): |
| 86 | + with self.subTest(pattern_class=pattern_class.__name__): |
| 87 | + self._assert_matches(pattern_class, f'foo{BS * 3} ', f'foo{BS} ', True) |
| 88 | + self._assert_matches(pattern_class, f'foo{BS * 3} ', f'foo{BS}', False) |
| 89 | + |
| 90 | + def test_04_regex_normalization(self): |
| 91 | + """ |
| 92 | + Tests the compiled regular expressions directly. |
| 93 | + """ |
| 94 | + # With an even run, the trailing space must be stripped before |
| 95 | + # compiling: 'foo\\ ' compiles like 'foo\\'. |
| 96 | + pattern = GitIgnoreBasicPattern(f'foo{BS * 2} ') |
| 97 | + self.assertEqual(pattern.regex.pattern, GitIgnoreBasicPattern(f'foo{BS * 2}').regex.pattern) |
| 98 | + |
| 99 | + def test_05_pathspec_end_to_end(self): |
| 100 | + """ |
| 101 | + Tests the behavior through the PathSpec interface. |
| 102 | + """ |
| 103 | + spec = PathSpec.from_lines('gitignore', [f'foo{BS * 2} ']) |
| 104 | + # These POSIX-style paths contain literal backslashes. Keep Windows |
| 105 | + # from normalizing those characters into directory separators. |
| 106 | + self.assertIs(spec.match_file(f'foo{BS}', separators=('/',)), True) |
| 107 | + self.assertIs(spec.match_file(f'foo{BS} ', separators=('/',)), False) |
| 108 | + |
| 109 | + spec = PathSpec.from_lines('gitignore', [f'foo{BS} ']) |
| 110 | + self.assertIs(spec.match_file('foo '), True) |
| 111 | + self.assertIs(spec.match_file('foo'), False) |
0 commit comments