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