| 1 |
""" |
| 2 |
This script tests :class:`.PathSpec`. |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
import shutil |
| 7 |
import tempfile |
| 8 |
import unittest |
| 9 |
from collections.abc import ( |
| 10 |
Iterable, |
| 11 |
Iterator, |
| 12 |
Sequence) |
| 13 |
from contextlib import ( |
| 14 |
AbstractContextManager, |
| 15 |
contextmanager) |
| 16 |
from functools import ( |
| 17 |
partial) |
| 18 |
from pathlib import ( |
| 19 |
Path) |
| 20 |
from typing import ( |
| 21 |
Callable, # Replaced by `collections.abc.Callable` in 3.9.2. |
| 22 |
Literal, |
| 23 |
Optional, # Replaced by `X | None` in 3.10. |
| 24 |
overload) |
| 25 |
from unittest import ( |
| 26 |
SkipTest) |
| 27 |
|
| 28 |
from pathspec import ( |
| 29 |
PathSpec) |
| 30 |
from pathspec.backend import ( |
| 31 |
BackendNamesHint, |
| 32 |
_Backend) |
| 33 |
from pathspec._backends.hyperscan.pathspec import ( |
| 34 |
HyperscanPsBackend) |
| 35 |
from pathspec._backends.re2.pathspec import ( |
| 36 |
Re2PsBackend) |
| 37 |
from pathspec._backends.simple.pathspec import ( |
| 38 |
SimplePsBackend) |
| 39 |
from pathspec.pattern import ( |
| 40 |
Pattern) |
| 41 |
from pathspec.patterns.gitignore.base import ( |
| 42 |
GitIgnorePatternError) |
| 43 |
from pathspec.patterns.gitignore.basic import ( |
| 44 |
GitIgnoreBasicPattern) |
| 45 |
from pathspec._typing import ( |
| 46 |
AnyStr) # Removed in 3.18. |
| 47 |
from pathspec.util import ( |
| 48 |
iter_tree_entries) |
| 49 |
|
| 50 |
from .util import ( |
| 51 |
CheckResult, |
| 52 |
debug_includes, |
| 53 |
debug_results, |
| 54 |
get_includes, |
| 55 |
get_paths_from_entries, |
| 56 |
make_dirs, |
| 57 |
make_files, |
| 58 |
ospath, |
| 59 |
require_backend, |
| 60 |
reverse_inplace, |
| 61 |
shuffle_inplace) |
| 62 |
|
| 63 |
BACKENDS: list[BackendNamesHint] = [ |
| 64 |
'hyperscan', |
| 65 |
're2', |
| 66 |
] |
| 67 |
""" |
| 68 |
The backend parameters. |
| 69 |
""" |
| 70 |
|
| 71 |
|
| 72 |
class PathSpecTest(unittest.TestCase): |
| 73 |
""" |
| 74 |
The :class:`PathSpecTest` class tests the :class:`.PathSpec` class. |
| 75 |
""" |
| 76 |
|
| 77 |
def clear_temp_dir(self) -> None: |
| 78 |
""" |
| 79 |
Clear the temp directory. |
| 80 |
""" |
| 81 |
for path in os.scandir(self.temp_dir): |
| 82 |
shutil.rmtree(path.path) |
| 83 |
|
| 84 |
def make_dirs(self, dirs: Iterable[str]) -> None: |
| 85 |
""" |
| 86 |
Create the specified directories. |
| 87 |
""" |
| 88 |
make_dirs(self.temp_dir, dirs) |
| 89 |
|
| 90 |
def make_files(self, files: Iterable[str]) -> None: |
| 91 |
""" |
| 92 |
Create the specified files. |
| 93 |
""" |
| 94 |
return make_files(self.temp_dir, files) |
| 95 |
|
| 96 |
@overload |
| 97 |
def parameterize_from_lines( |
| 98 |
self, |
| 99 |
pattern_factory: Literal['gitignore'], |
| 100 |
lines: Iterable[AnyStr], |
| 101 |
) -> Iterator[Callable[[], AbstractContextManager[PathSpec[GitIgnoreBasicPattern]]]]: |
| 102 |
... |
| 103 |
|
| 104 |
@overload |
| 105 |
def parameterize_from_lines( |
| 106 |
self, |
| 107 |
pattern_factory: str, |
| 108 |
lines: Iterable[AnyStr], |
| 109 |
) -> Iterator[Callable[[], AbstractContextManager[PathSpec[Pattern]]]]: |
| 110 |
... |
| 111 |
|
| 112 |
def parameterize_from_lines( |
| 113 |
self, |
| 114 |
pattern_factory: str, |
| 115 |
lines: Iterable[AnyStr], |
| 116 |
) -> Iterator[Callable[[], AbstractContextManager[PathSpec]]]: |
| 117 |
""" |
| 118 |
Parameterize `PathSpec.from_lines()` for each backend and configuration to |
| 119 |
begin a subtest. |
| 120 |
|
| 121 |
*pattern_factory* (:class:`str`) is the pattern factory. |
| 122 |
|
| 123 |
*lines* (:class:`Iterable` of :class:`str`) yields the lines. |
| 124 |
|
| 125 |
Returns an :class:`Iterator` yielding each subtest context for the |
| 126 |
:class:`PathSpec`. |
| 127 |
""" |
| 128 |
lines = list(lines) |
| 129 |
|
| 130 |
configs: list[tuple[ |
| 131 |
str, BackendNamesHint, Optional[Callable[[Sequence[Pattern]], _Backend]] |
| 132 |
]] = [] |
| 133 |
|
| 134 |
# Simple backend, no optimizations. |
| 135 |
configs.append(( |
| 136 |
"simple (unopt)", |
| 137 |
'simple', |
| 138 |
partial(SimplePsBackend, no_filter=True, no_reverse=True), |
| 139 |
)) |
| 140 |
|
| 141 |
# Simple backend, minimal optimizations. |
| 142 |
configs.append(( |
| 143 |
"simple (minopt)", |
| 144 |
'simple', |
| 145 |
None, |
| 146 |
)) |
| 147 |
|
| 148 |
# Add additional backends. |
| 149 |
for backend in BACKENDS: |
| 150 |
if backend == 'hyperscan': |
| 151 |
configs.append(( |
| 152 |
f"hyperscan (forward)", |
| 153 |
backend, |
| 154 |
partial(HyperscanPsBackend, _debug_exprs=True), |
| 155 |
)) |
| 156 |
configs.append(( |
| 157 |
f"hyperscan (reverse)", |
| 158 |
backend, |
| 159 |
partial(HyperscanPsBackend, _debug_exprs=True, _test_sort=reverse_inplace) |
| 160 |
)) |
| 161 |
configs.append(( |
| 162 |
f"hyperscan (shuffle)", |
| 163 |
backend, |
| 164 |
partial(HyperscanPsBackend, _debug_exprs=True, _test_sort=shuffle_inplace) |
| 165 |
)) |
| 166 |
elif backend == 're2': |
| 167 |
configs.append(( |
| 168 |
f"re2 (forward)", |
| 169 |
backend, |
| 170 |
partial(Re2PsBackend, _debug_regex=True), |
| 171 |
)) |
| 172 |
configs.append(( |
| 173 |
f"re2 (reverse)", |
| 174 |
backend, |
| 175 |
partial(Re2PsBackend, _debug_regex=True, _test_sort=reverse_inplace), |
| 176 |
)) |
| 177 |
configs.append(( |
| 178 |
f"re2 (shuffle)", |
| 179 |
backend, |
| 180 |
partial(Re2PsBackend, _debug_regex=True, _test_sort=shuffle_inplace), |
| 181 |
)) |
| 182 |
else: |
| 183 |
configs.append(( |
| 184 |
backend, |
| 185 |
backend, |
| 186 |
None, |
| 187 |
)) |
| 188 |
|
| 189 |
for label, backend, backend_factory in configs: |
| 190 |
try: |
| 191 |
require_backend(backend) |
| 192 |
except SkipTest: |
| 193 |
with self.subTest(label): |
| 194 |
raise |
| 195 |
continue |
| 196 |
|
| 197 |
@contextmanager |
| 198 |
def _sub_test( |
| 199 |
backend=backend, |
| 200 |
backend_factory=backend_factory, |
| 201 |
label=label, |
| 202 |
): |
| 203 |
self.clear_temp_dir() |
| 204 |
with self.subTest(label): |
| 205 |
yield PathSpec.from_lines( |
| 206 |
pattern_factory, |
| 207 |
lines, |
| 208 |
backend=backend, |
| 209 |
_test_backend_factory=backend_factory, |
| 210 |
) |
| 211 |
|
| 212 |
yield _sub_test |
| 213 |
|
| 214 |
def setUp(self) -> None: |
| 215 |
""" |
| 216 |
Called before each test. |
| 217 |
""" |
| 218 |
self.temp_dir = Path(tempfile.mkdtemp()) |
| 219 |
|
| 220 |
def tearDown(self) -> None: |
| 221 |
""" |
| 222 |
Called after each test. |
| 223 |
""" |
| 224 |
shutil.rmtree(self.temp_dir) |
| 225 |
|
| 226 |
def test_01_absolute_dir_paths_1(self): |
| 227 |
""" |
| 228 |
Tests that absolute paths will be properly normalized and matched. |
| 229 |
""" |
| 230 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 231 |
'foo', |
| 232 |
]): |
| 233 |
with sub_test() as spec: |
| 234 |
files = { |
| 235 |
'/a.py', |
| 236 |
'/foo/a.py', |
| 237 |
'/x/a.py', |
| 238 |
'/x/foo/a.py', |
| 239 |
'a.py', |
| 240 |
'foo/a.py', |
| 241 |
'x/a.py', |
| 242 |
'x/foo/a.py', |
| 243 |
} |
| 244 |
|
| 245 |
results = list(spec.check_files(files)) |
| 246 |
includes = get_includes(results) |
| 247 |
debug = debug_results(spec, results) |
| 248 |
|
| 249 |
self.assertEqual(includes, { |
| 250 |
'/foo/a.py', |
| 251 |
'/x/foo/a.py', |
| 252 |
'foo/a.py', |
| 253 |
'x/foo/a.py', |
| 254 |
}, debug) |
| 255 |
|
| 256 |
def test_01_absolute_dir_paths_2(self): |
| 257 |
""" |
| 258 |
Tests that absolute paths will be properly normalized and matched. |
| 259 |
""" |
| 260 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 261 |
'/foo', |
| 262 |
]): |
| 263 |
with sub_test() as spec: |
| 264 |
files = { |
| 265 |
'/a.py', |
| 266 |
'/foo/a.py', |
| 267 |
'/x/a.py', |
| 268 |
'/x/foo/a.py', |
| 269 |
'a.py', |
| 270 |
'foo/a.py', |
| 271 |
'x/a.py', |
| 272 |
'x/foo/a.py', |
| 273 |
} |
| 274 |
|
| 275 |
results = list(spec.check_files(files)) |
| 276 |
includes = get_includes(results) |
| 277 |
debug = debug_results(spec, results) |
| 278 |
|
| 279 |
self.assertEqual(includes, { |
| 280 |
'/foo/a.py', |
| 281 |
'foo/a.py', |
| 282 |
}, debug) |
| 283 |
|
| 284 |
def test_01_check_file_1_include(self): |
| 285 |
""" |
| 286 |
Test checking a single file that is included. |
| 287 |
""" |
| 288 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 289 |
"*.txt", |
| 290 |
"!test/", |
| 291 |
]): |
| 292 |
with sub_test() as spec: |
| 293 |
result = spec.check_file("include.txt") |
| 294 |
debug = debug_results(spec, [result]) |
| 295 |
|
| 296 |
self.assertEqual(result, CheckResult("include.txt", True, 0), debug) |
| 297 |
|
| 298 |
def test_01_check_file_2_exclude(self): |
| 299 |
""" |
| 300 |
Test checking a single file that is excluded. |
| 301 |
""" |
| 302 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 303 |
"*.txt", |
| 304 |
"!test/", |
| 305 |
]): |
| 306 |
with sub_test() as spec: |
| 307 |
result = spec.check_file("test/exclude.txt") |
| 308 |
debug = debug_results(spec, [result]) |
| 309 |
|
| 310 |
self.assertEqual(result, CheckResult("test/exclude.txt", False, 1), debug) |
| 311 |
|
| 312 |
def test_01_check_file_3_unmatch(self): |
| 313 |
""" |
| 314 |
Test checking a single file that is unmatched. |
| 315 |
""" |
| 316 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 317 |
"*.txt", |
| 318 |
"!test/", |
| 319 |
]): |
| 320 |
with sub_test() as spec: |
| 321 |
result = spec.check_file("unmatch.bin") |
| 322 |
debug = debug_results(spec, [result]) |
| 323 |
|
| 324 |
self.assertEqual(result, CheckResult("unmatch.bin", None, None), debug) |
| 325 |
|
| 326 |
def test_01_check_file_4_many(self): |
| 327 |
""" |
| 328 |
Test that checking files one at a time yields the same results as checking |
| 329 |
multiples files at once. |
| 330 |
""" |
| 331 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 332 |
'*.txt', |
| 333 |
'!test1/', |
| 334 |
]): |
| 335 |
with sub_test() as spec: |
| 336 |
files = { |
| 337 |
'test1/a.txt', |
| 338 |
'test1/b.txt', |
| 339 |
'test1/c/c.txt', |
| 340 |
'test2/a.txt', |
| 341 |
'test2/b.txt', |
| 342 |
'test2/c/c.txt', |
| 343 |
} |
| 344 |
|
| 345 |
single_results = set(map(spec.check_file, files)) |
| 346 |
multi_results = set(spec.check_files(files)) |
| 347 |
debug = debug_results(spec, single_results) |
| 348 |
|
| 349 |
self.assertEqual(single_results, multi_results, debug) |
| 350 |
|
| 351 |
def test_01_check_match_files(self): |
| 352 |
""" |
| 353 |
Test that checking files and matching files yield the same results. |
| 354 |
""" |
| 355 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 356 |
'*.txt', |
| 357 |
'!test1/**', |
| 358 |
]): |
| 359 |
with sub_test() as spec: |
| 360 |
files = { |
| 361 |
'src/test1/a.txt', |
| 362 |
'src/test1/b.txt', |
| 363 |
'src/test1/c/c.txt', |
| 364 |
'src/test2/a.txt', |
| 365 |
'src/test2/b.txt', |
| 366 |
'src/test2/c/c.txt', |
| 367 |
} |
| 368 |
|
| 369 |
check_results = set(spec.check_files(files)) |
| 370 |
check_includes = get_includes(check_results) |
| 371 |
match_files = set(spec.match_files(files)) |
| 372 |
debug = debug_results(spec, check_results) |
| 373 |
|
| 374 |
self.assertEqual(check_includes, match_files, debug) |
| 375 |
|
| 376 |
def test_01_current_dir_paths(self): |
| 377 |
""" |
| 378 |
Tests that paths referencing the current directory will be properly |
| 379 |
normalized and matched. |
| 380 |
""" |
| 381 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 382 |
'*.txt', |
| 383 |
'!test1/', |
| 384 |
]): |
| 385 |
with sub_test() as spec: |
| 386 |
files = { |
| 387 |
'./src/test1/a.txt', |
| 388 |
'./src/test1/b.txt', |
| 389 |
'./src/test1/c/c.txt', |
| 390 |
'./src/test2/a.txt', |
| 391 |
'./src/test2/b.txt', |
| 392 |
'./src/test2/c/c.txt', |
| 393 |
} |
| 394 |
|
| 395 |
results = list(spec.check_files(files)) |
| 396 |
includes = get_includes(results) |
| 397 |
debug = debug_results(spec, results) |
| 398 |
|
| 399 |
self.assertEqual(includes, { |
| 400 |
'./src/test2/a.txt', |
| 401 |
'./src/test2/b.txt', |
| 402 |
'./src/test2/c/c.txt', |
| 403 |
}, debug) |
| 404 |
|
| 405 |
def test_01_empty_path_1(self): |
| 406 |
""" |
| 407 |
Tests that patterns that end with an escaped space will be treated properly. |
| 408 |
""" |
| 409 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 410 |
'\\ ', |
| 411 |
'abc\\ ', |
| 412 |
]): |
| 413 |
with sub_test() as spec: |
| 414 |
files = { |
| 415 |
' ', |
| 416 |
' ', |
| 417 |
'abc ', |
| 418 |
'somefile', |
| 419 |
} |
| 420 |
|
| 421 |
results = list(spec.check_files(files)) |
| 422 |
includes = get_includes(results) |
| 423 |
debug = debug_results(spec, results) |
| 424 |
|
| 425 |
self.assertEqual(includes, { |
| 426 |
' ', |
| 427 |
'abc ', |
| 428 |
}, debug) |
| 429 |
|
| 430 |
def test_01_empty_path_2(self): |
| 431 |
""" |
| 432 |
Tests that patterns that end with an escaped space will be treated properly. |
| 433 |
""" |
| 434 |
with self.assertRaises(GitIgnorePatternError): |
| 435 |
# An escape with double spaces is invalid. Disallow it. Better to be |
| 436 |
# safe than sorry. |
| 437 |
PathSpec.from_lines('gitignore', [ |
| 438 |
'\\ ', |
| 439 |
], backend='simple') |
| 440 |
|
| 441 |
def test_01_match_file_1_include(self): |
| 442 |
""" |
| 443 |
Test matching a single file that is included. |
| 444 |
""" |
| 445 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 446 |
"*.txt", |
| 447 |
"!test/", |
| 448 |
]): |
| 449 |
with sub_test() as spec: |
| 450 |
include = spec.match_file("include.txt") |
| 451 |
|
| 452 |
self.assertIs(include, True) |
| 453 |
|
| 454 |
def test_01_match_file_2_exclude(self): |
| 455 |
""" |
| 456 |
Test matching a single file that is excluded. |
| 457 |
""" |
| 458 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 459 |
"*.txt", |
| 460 |
"!test/", |
| 461 |
]): |
| 462 |
with sub_test() as spec: |
| 463 |
file = 'test/exclude.txt' |
| 464 |
|
| 465 |
include = spec.match_file(file) |
| 466 |
includes = {file} if include else {} |
| 467 |
debug = debug_includes(spec, {file}, includes) |
| 468 |
|
| 469 |
self.assertIs(include, False, debug) |
| 470 |
|
| 471 |
def test_01_match_file_3_unmatch(self): |
| 472 |
""" |
| 473 |
Test match a single file that is unmatched. |
| 474 |
""" |
| 475 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 476 |
"*.txt", |
| 477 |
"!test/", |
| 478 |
]): |
| 479 |
with sub_test() as spec: |
| 480 |
file = 'unmatch.bin' |
| 481 |
|
| 482 |
include = spec.match_file(file) |
| 483 |
includes = {file} if include else {} |
| 484 |
debug = debug_includes(spec, {file}, includes) |
| 485 |
|
| 486 |
self.assertIs(include, False, debug) |
| 487 |
|
| 488 |
def test_01_match_files(self): |
| 489 |
""" |
| 490 |
Test that matching files one at a time yields the same results as matching |
| 491 |
multiples files at once. |
| 492 |
""" |
| 493 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 494 |
'*.txt', |
| 495 |
'!test1/', |
| 496 |
]): |
| 497 |
with sub_test() as spec: |
| 498 |
files = { |
| 499 |
'test1/a.txt', |
| 500 |
'test1/b.txt', |
| 501 |
'test1/c/c.txt', |
| 502 |
'test2/a.txt', |
| 503 |
'test2/b.txt', |
| 504 |
'test2/c/c.txt', |
| 505 |
} |
| 506 |
|
| 507 |
single_files = set(filter(spec.match_file, files)) |
| 508 |
multi_files = set(spec.match_files(files)) |
| 509 |
debug = debug_includes(spec, files, single_files) |
| 510 |
|
| 511 |
self.assertEqual(single_files, multi_files, debug) |
| 512 |
|
| 513 |
def test_01_windows_current_dir_paths(self): |
| 514 |
""" |
| 515 |
Tests that paths referencing the current directory will be properly |
| 516 |
normalized and matched. |
| 517 |
""" |
| 518 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 519 |
'*.txt', |
| 520 |
'!test1/', |
| 521 |
]): |
| 522 |
with sub_test() as spec: |
| 523 |
files = { |
| 524 |
'.\\test1\\a.txt', |
| 525 |
'.\\test1\\b.txt', |
| 526 |
'.\\test1\\c\\c.txt', |
| 527 |
'.\\test2\\a.txt', |
| 528 |
'.\\test2\\b.txt', |
| 529 |
'.\\test2\\c\\c.txt', |
| 530 |
} |
| 531 |
|
| 532 |
results = list(spec.check_files(files, separators=['\\'])) |
| 533 |
includes = get_includes(results) |
| 534 |
debug = debug_results(spec, results) |
| 535 |
|
| 536 |
self.assertEqual(includes, { |
| 537 |
'.\\test2\\a.txt', |
| 538 |
'.\\test2\\b.txt', |
| 539 |
'.\\test2\\c\\c.txt', |
| 540 |
}, debug) |
| 541 |
|
| 542 |
def test_01_windows_paths(self): |
| 543 |
""" |
| 544 |
Tests that Windows paths will be properly normalized and matched. |
| 545 |
""" |
| 546 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 547 |
'*.txt', |
| 548 |
'!test1/', |
| 549 |
]): |
| 550 |
with sub_test() as spec: |
| 551 |
files = { |
| 552 |
'test1\\a.txt', |
| 553 |
'test1\\b.txt', |
| 554 |
'test1\\c\\c.txt', |
| 555 |
'test2\\a.txt', |
| 556 |
'test2\\b.txt', |
| 557 |
'test2\\c\\c.txt', |
| 558 |
} |
| 559 |
|
| 560 |
results = list(spec.check_files(files, separators=['\\'])) |
| 561 |
includes = get_includes(results) |
| 562 |
debug = debug_results(spec, results) |
| 563 |
|
| 564 |
self.assertEqual(includes, { |
| 565 |
'test2\\a.txt', |
| 566 |
'test2\\b.txt', |
| 567 |
'test2\\c\\c.txt', |
| 568 |
}, debug) |
| 569 |
|
| 570 |
def test_02_eq(self): |
| 571 |
""" |
| 572 |
Tests equality. |
| 573 |
""" |
| 574 |
first_spec = PathSpec.from_lines('gitignore', [ |
| 575 |
'*.txt', |
| 576 |
'!test1/**', |
| 577 |
], backend='simple') |
| 578 |
second_spec = PathSpec.from_lines('gitignore', [ |
| 579 |
'*.txt', |
| 580 |
'!test1/**', |
| 581 |
], backend='simple') |
| 582 |
self.assertEqual(first_spec, second_spec) |
| 583 |
|
| 584 |
def test_02_ne(self): |
| 585 |
""" |
| 586 |
Tests inequality. |
| 587 |
""" |
| 588 |
first_spec = PathSpec.from_lines('gitignore', [ |
| 589 |
'*.txt', |
| 590 |
], backend='simple') |
| 591 |
second_spec = PathSpec.from_lines('gitignore', [ |
| 592 |
'!*.txt', |
| 593 |
], backend='simple') |
| 594 |
self.assertNotEqual(first_spec, second_spec) |
| 595 |
|
| 596 |
def test_03_add_1_simple(self): |
| 597 |
""" |
| 598 |
Test spec addition using :data:`+` operator. |
| 599 |
""" |
| 600 |
first_spec = PathSpec.from_lines('gitignore', [ |
| 601 |
'test.png', |
| 602 |
'test.txt', |
| 603 |
], backend='simple') |
| 604 |
second_spec = PathSpec.from_lines('gitignore', [ |
| 605 |
'test.html', |
| 606 |
'test.jpg', |
| 607 |
], backend='simple') |
| 608 |
combined_spec = first_spec + second_spec |
| 609 |
files = { |
| 610 |
'test.html', |
| 611 |
'test.jpg', |
| 612 |
'test.png', |
| 613 |
'test.txt', |
| 614 |
} |
| 615 |
|
| 616 |
results = list(combined_spec.check_files(files)) |
| 617 |
includes = get_includes(results) |
| 618 |
debug = debug_results(combined_spec, results) |
| 619 |
|
| 620 |
self.assertEqual(includes, { |
| 621 |
'test.html', |
| 622 |
'test.jpg', |
| 623 |
'test.png', |
| 624 |
'test.txt', |
| 625 |
}, debug) |
| 626 |
|
| 627 |
def test_03_add_2_mixed(self): |
| 628 |
""" |
| 629 |
Test spec addition using :data:`+` operator with mixed sequence types. |
| 630 |
""" |
| 631 |
first_spec = PathSpec(tuple(map(GitIgnoreBasicPattern, [ |
| 632 |
'test.png', |
| 633 |
'test.txt', |
| 634 |
])), backend='simple') |
| 635 |
second_spec = PathSpec(list(map(GitIgnoreBasicPattern, [ |
| 636 |
'test.html', |
| 637 |
'test.jpg', |
| 638 |
])), backend='simple') |
| 639 |
combined_spec = first_spec + second_spec |
| 640 |
files = { |
| 641 |
'test.html', |
| 642 |
'test.jpg', |
| 643 |
'test.png', |
| 644 |
'test.txt', |
| 645 |
} |
| 646 |
|
| 647 |
results = list(combined_spec.check_files(files)) |
| 648 |
includes = get_includes(results) |
| 649 |
debug = debug_results(combined_spec, results) |
| 650 |
|
| 651 |
self.assertEqual(includes, { |
| 652 |
'test.html', |
| 653 |
'test.jpg', |
| 654 |
'test.png', |
| 655 |
'test.txt', |
| 656 |
}, debug) |
| 657 |
|
| 658 |
def test_03_iadd_1_simple(self): |
| 659 |
""" |
| 660 |
Test spec addition using :data:`+=` operator. |
| 661 |
""" |
| 662 |
spec = PathSpec.from_lines('gitignore', [ |
| 663 |
'test.png', |
| 664 |
'test.txt', |
| 665 |
], backend='simple') |
| 666 |
spec += PathSpec.from_lines('gitignore', [ |
| 667 |
'test.html', |
| 668 |
'test.jpg', |
| 669 |
], backend='simple') |
| 670 |
files = { |
| 671 |
'test.html', |
| 672 |
'test.jpg', |
| 673 |
'test.png', |
| 674 |
'test.txt', |
| 675 |
} |
| 676 |
|
| 677 |
results = list(spec.check_files(files)) |
| 678 |
includes = get_includes(results) |
| 679 |
debug = debug_results(spec, results) |
| 680 |
|
| 681 |
self.assertEqual(includes, { |
| 682 |
'test.html', |
| 683 |
'test.jpg', |
| 684 |
'test.png', |
| 685 |
'test.txt', |
| 686 |
}, debug) |
| 687 |
|
| 688 |
def test_03_iadd_2_mixed(self): |
| 689 |
""" |
| 690 |
Test spec addition using :data:`+=` operator with mixed sequence types. |
| 691 |
""" |
| 692 |
spec = PathSpec(tuple(map(GitIgnoreBasicPattern, [ |
| 693 |
'test.png', |
| 694 |
'test.txt', |
| 695 |
])), backend='simple') |
| 696 |
spec += PathSpec(list(map(GitIgnoreBasicPattern, [ |
| 697 |
'test.html', |
| 698 |
'test.jpg', |
| 699 |
])), backend='simple') |
| 700 |
files = { |
| 701 |
'test.html', |
| 702 |
'test.jpg', |
| 703 |
'test.png', |
| 704 |
'test.txt', |
| 705 |
} |
| 706 |
|
| 707 |
results = list(spec.check_files(files)) |
| 708 |
includes = get_includes(results) |
| 709 |
debug = debug_results(spec, results) |
| 710 |
|
| 711 |
self.assertEqual(includes, { |
| 712 |
'test.html', |
| 713 |
'test.jpg', |
| 714 |
'test.png', |
| 715 |
'test.txt', |
| 716 |
}, debug) |
| 717 |
|
| 718 |
def test_04_len(self): |
| 719 |
""" |
| 720 |
Test spec length. |
| 721 |
""" |
| 722 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 723 |
'foo', |
| 724 |
'bar', |
| 725 |
]): |
| 726 |
with sub_test() as spec: |
| 727 |
self.assertEqual(len(spec), 2) |
| 728 |
|
| 729 |
def test_05_match_entries(self): |
| 730 |
""" |
| 731 |
Test matching files collectively. |
| 732 |
""" |
| 733 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 734 |
'*.txt', |
| 735 |
'!b.txt', |
| 736 |
]): |
| 737 |
with sub_test() as spec: |
| 738 |
self.make_dirs([ |
| 739 |
'X', |
| 740 |
'X/Z', |
| 741 |
'Y', |
| 742 |
'Y/Z', |
| 743 |
]) |
| 744 |
self.make_files([ |
| 745 |
'X/a.txt', |
| 746 |
'X/b.txt', |
| 747 |
'X/Z/c.txt', |
| 748 |
'Y/a.txt', |
| 749 |
'Y/b.txt', |
| 750 |
'Y/Z/c.txt', |
| 751 |
]) |
| 752 |
|
| 753 |
entries = iter_tree_entries(self.temp_dir) |
| 754 |
includes = get_paths_from_entries(spec.match_entries(entries)) |
| 755 |
|
| 756 |
self.assertEqual(includes, set(map(ospath, [ |
| 757 |
'X/a.txt', |
| 758 |
'X/Z/c.txt', |
| 759 |
'Y/a.txt', |
| 760 |
'Y/Z/c.txt', |
| 761 |
]))) |
| 762 |
|
| 763 |
def test_05_match_file(self): |
| 764 |
""" |
| 765 |
Test matching files individually. |
| 766 |
""" |
| 767 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 768 |
'*.txt', |
| 769 |
'!b.txt', |
| 770 |
]): |
| 771 |
with sub_test() as spec: |
| 772 |
files = { |
| 773 |
'X/a.txt', |
| 774 |
'X/b.txt', |
| 775 |
'X/Z/c.txt', |
| 776 |
'Y/a.txt', |
| 777 |
'Y/b.txt', |
| 778 |
'Y/Z/c.txt', |
| 779 |
} |
| 780 |
|
| 781 |
includes = set(filter(spec.match_file, files)) |
| 782 |
debug = debug_includes(spec, files, includes) |
| 783 |
|
| 784 |
self.assertEqual(includes, { |
| 785 |
'X/a.txt', |
| 786 |
'X/Z/c.txt', |
| 787 |
'Y/a.txt', |
| 788 |
'Y/Z/c.txt', |
| 789 |
}, debug) |
| 790 |
|
| 791 |
def test_05_match_files(self): |
| 792 |
""" |
| 793 |
Test matching files collectively. |
| 794 |
""" |
| 795 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 796 |
'*.txt', |
| 797 |
'!b.txt', |
| 798 |
]): |
| 799 |
with sub_test() as spec: |
| 800 |
files = { |
| 801 |
'X/a.txt', |
| 802 |
'X/b.txt', |
| 803 |
'X/Z/c.txt', |
| 804 |
'Y/a.txt', |
| 805 |
'Y/b.txt', |
| 806 |
'Y/Z/c.txt', |
| 807 |
} |
| 808 |
|
| 809 |
includes = set(spec.match_files(files)) |
| 810 |
debug = debug_includes(spec, files, includes) |
| 811 |
|
| 812 |
self.assertEqual(includes, { |
| 813 |
'X/a.txt', |
| 814 |
'X/Z/c.txt', |
| 815 |
'Y/a.txt', |
| 816 |
'Y/Z/c.txt', |
| 817 |
}, debug) |
| 818 |
|
| 819 |
def test_05_match_tree_entries(self): |
| 820 |
""" |
| 821 |
Test matching a file tree. |
| 822 |
""" |
| 823 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 824 |
'*.txt', |
| 825 |
'!b.txt', |
| 826 |
]): |
| 827 |
with sub_test() as spec: |
| 828 |
files = set(map(ospath, [ |
| 829 |
'X/a.txt', |
| 830 |
'X/b.txt', |
| 831 |
'X/Z/c.txt', |
| 832 |
'Y/a.txt', |
| 833 |
'Y/b.txt', |
| 834 |
'Y/Z/c.txt', |
| 835 |
])) |
| 836 |
|
| 837 |
self.make_dirs([ |
| 838 |
'X', |
| 839 |
'X/Z', |
| 840 |
'Y', |
| 841 |
'Y/Z', |
| 842 |
]) |
| 843 |
self.make_files(files) |
| 844 |
|
| 845 |
entries = spec.match_tree_entries(self.temp_dir) |
| 846 |
includes = get_paths_from_entries(entries) |
| 847 |
debug = debug_includes(spec, files, includes) |
| 848 |
|
| 849 |
self.assertEqual(includes, set(map(ospath, [ |
| 850 |
'X/a.txt', |
| 851 |
'X/Z/c.txt', |
| 852 |
'Y/a.txt', |
| 853 |
'Y/Z/c.txt', |
| 854 |
])), debug) |
| 855 |
|
| 856 |
def test_05_match_tree_files(self): |
| 857 |
""" |
| 858 |
Test matching a file tree. |
| 859 |
""" |
| 860 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 861 |
'*.txt', |
| 862 |
'!b.txt', |
| 863 |
]): |
| 864 |
with sub_test() as spec: |
| 865 |
files = set(map(ospath, [ |
| 866 |
'X/a.txt', |
| 867 |
'X/b.txt', |
| 868 |
'X/Z/c.txt', |
| 869 |
'Y/a.txt', |
| 870 |
'Y/b.txt', |
| 871 |
'Y/Z/c.txt', |
| 872 |
])) |
| 873 |
|
| 874 |
self.make_dirs([ |
| 875 |
'X', |
| 876 |
'X/Z', |
| 877 |
'Y', |
| 878 |
'Y/Z', |
| 879 |
]) |
| 880 |
self.make_files(files) |
| 881 |
|
| 882 |
includes = set(spec.match_tree_files(self.temp_dir)) |
| 883 |
debug = debug_includes(spec, files, includes) |
| 884 |
|
| 885 |
self.assertEqual(includes, set(map(ospath, [ |
| 886 |
'X/a.txt', |
| 887 |
'X/Z/c.txt', |
| 888 |
'Y/a.txt', |
| 889 |
'Y/Z/c.txt', |
| 890 |
])), debug) |
| 891 |
|
| 892 |
def test_06_issue_41_a(self): |
| 893 |
""" |
| 894 |
Test including a file and excluding a directory with the same name pattern, |
| 895 |
scenario A. |
| 896 |
""" |
| 897 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 898 |
'*.yaml', |
| 899 |
'!*.yaml/', |
| 900 |
]): |
| 901 |
with sub_test() as spec: |
| 902 |
files = { |
| 903 |
'dir.yaml/file.sql', |
| 904 |
'dir.yaml/file.yaml', |
| 905 |
'dir.yaml/index.txt', |
| 906 |
'dir/file.sql', |
| 907 |
'dir/file.yaml', |
| 908 |
'dir/index.txt', |
| 909 |
'file.yaml', |
| 910 |
} |
| 911 |
|
| 912 |
results = list(spec.check_files(files)) |
| 913 |
ignores = get_includes(results) |
| 914 |
debug = debug_results(spec, results) |
| 915 |
|
| 916 |
self.assertEqual(ignores, { |
| 917 |
#'dir.yaml/file.yaml', # Discrepancy with Git. |
| 918 |
'dir/file.yaml', |
| 919 |
'file.yaml', |
| 920 |
}, debug) |
| 921 |
self.assertEqual(files - ignores, { |
| 922 |
'dir.yaml/file.sql', |
| 923 |
'dir.yaml/file.yaml', # Discrepancy with Git. |
| 924 |
'dir.yaml/index.txt', |
| 925 |
'dir/file.sql', |
| 926 |
'dir/index.txt', |
| 927 |
}, debug) |
| 928 |
|
| 929 |
def test_06_issue_41_b(self): |
| 930 |
""" |
| 931 |
Test including a file and excluding a directory with the same name |
| 932 |
pattern, scenario B. |
| 933 |
""" |
| 934 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 935 |
'!*.yaml/', |
| 936 |
'*.yaml', |
| 937 |
]): |
| 938 |
with sub_test() as spec: |
| 939 |
files = { |
| 940 |
'dir.yaml/file.sql', |
| 941 |
'dir.yaml/file.yaml', |
| 942 |
'dir.yaml/index.txt', |
| 943 |
'dir/file.sql', |
| 944 |
'dir/file.yaml', |
| 945 |
'dir/index.txt', |
| 946 |
'file.yaml', |
| 947 |
} |
| 948 |
|
| 949 |
results = list(spec.check_files(files)) |
| 950 |
ignores = get_includes(results) |
| 951 |
debug = debug_results(spec, results) |
| 952 |
|
| 953 |
self.assertEqual(ignores, { |
| 954 |
'dir.yaml/file.sql', |
| 955 |
'dir.yaml/file.yaml', |
| 956 |
'dir.yaml/index.txt', |
| 957 |
'dir/file.yaml', |
| 958 |
'file.yaml', |
| 959 |
}, debug) |
| 960 |
self.assertEqual(files - ignores, { |
| 961 |
'dir/file.sql', |
| 962 |
'dir/index.txt', |
| 963 |
}, debug) |
| 964 |
|
| 965 |
def test_06_issue_41_c(self): |
| 966 |
""" |
| 967 |
Test including a file and excluding a directory with the same name |
| 968 |
pattern, scenario C. |
| 969 |
""" |
| 970 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 971 |
'*.yaml', |
| 972 |
'!dir.yaml', |
| 973 |
]): |
| 974 |
with sub_test() as spec: |
| 975 |
files = { |
| 976 |
'dir.yaml/file.sql', |
| 977 |
'dir.yaml/file.yaml', |
| 978 |
'dir.yaml/index.txt', |
| 979 |
'dir/file.sql', |
| 980 |
'dir/file.yaml', |
| 981 |
'dir/index.txt', |
| 982 |
'file.yaml', |
| 983 |
} |
| 984 |
|
| 985 |
results = list(spec.check_files(files)) |
| 986 |
ignores = get_includes(results) |
| 987 |
debug = debug_results(spec, results) |
| 988 |
|
| 989 |
self.assertEqual(ignores, { |
| 990 |
#'dir.yaml/file.yaml', # Discrepancy with Git. |
| 991 |
'dir/file.yaml', |
| 992 |
'file.yaml', |
| 993 |
}, debug) |
| 994 |
self.assertEqual(files - ignores, { |
| 995 |
'dir.yaml/file.sql', |
| 996 |
'dir.yaml/file.yaml', # Discrepancy with Git. |
| 997 |
'dir.yaml/index.txt', |
| 998 |
'dir/file.sql', |
| 999 |
'dir/index.txt', |
| 1000 |
}, debug) |
| 1001 |
|
| 1002 |
def test_07_issue_62(self): |
| 1003 |
""" |
| 1004 |
Test including all files and excluding a directory. |
| 1005 |
""" |
| 1006 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 1007 |
'*', |
| 1008 |
'!product_dir/', |
| 1009 |
]): |
| 1010 |
with sub_test() as spec: |
| 1011 |
files = { |
| 1012 |
'anydir/file.txt', |
| 1013 |
'product_dir/file.txt', |
| 1014 |
} |
| 1015 |
|
| 1016 |
results = list(spec.check_files(files)) |
| 1017 |
includes = get_includes(results) |
| 1018 |
debug = debug_results(spec, results) |
| 1019 |
|
| 1020 |
self.assertEqual(includes, { |
| 1021 |
'anydir/file.txt', |
| 1022 |
}, debug) |
| 1023 |
|
| 1024 |
def test_08_issue_39(self): |
| 1025 |
""" |
| 1026 |
Test excluding files in a directory. |
| 1027 |
""" |
| 1028 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 1029 |
'*.log', |
| 1030 |
'!important/*.log', |
| 1031 |
'trace.*', |
| 1032 |
]): |
| 1033 |
with sub_test() as spec: |
| 1034 |
files = { |
| 1035 |
'a.log', |
| 1036 |
'b.txt', |
| 1037 |
'important/d.log', |
| 1038 |
'important/e.txt', |
| 1039 |
'trace.c', |
| 1040 |
} |
| 1041 |
|
| 1042 |
results = list(spec.check_files(files)) |
| 1043 |
ignores = get_includes(results) |
| 1044 |
debug = debug_results(spec, results) |
| 1045 |
|
| 1046 |
self.assertEqual(ignores, { |
| 1047 |
'a.log', |
| 1048 |
'trace.c', |
| 1049 |
}, debug) |
| 1050 |
self.assertEqual(files - ignores, { |
| 1051 |
'b.txt', |
| 1052 |
'important/d.log', |
| 1053 |
'important/e.txt', |
| 1054 |
}, debug) |
| 1055 |
|
| 1056 |
def test_09_issue_80_a(self): |
| 1057 |
""" |
| 1058 |
Test negating patterns. |
| 1059 |
""" |
| 1060 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 1061 |
'build', |
| 1062 |
'*.log', |
| 1063 |
'.*', |
| 1064 |
'!.gitignore', |
| 1065 |
]): |
| 1066 |
with sub_test() as spec: |
| 1067 |
files = { |
| 1068 |
'.c-tmp', # 3:.* |
| 1069 |
'.gitignore', # 4:!.gitignore |
| 1070 |
'a.log', # 2:*.log |
| 1071 |
'b.txt', # - |
| 1072 |
'build/d.log', # 1:build |
| 1073 |
'build/trace.bin', # 1:build |
| 1074 |
'trace.c', # - |
| 1075 |
} |
| 1076 |
|
| 1077 |
keeps = set(spec.match_files(files, negate=True)) |
| 1078 |
includes = files - keeps |
| 1079 |
debug = debug_includes(spec, files, includes) |
| 1080 |
|
| 1081 |
self.assertEqual(keeps, { |
| 1082 |
'.gitignore', |
| 1083 |
'b.txt', |
| 1084 |
'trace.c', |
| 1085 |
}, debug) |
| 1086 |
|
| 1087 |
def test_09_issue_80_b(self): |
| 1088 |
""" |
| 1089 |
Test negating patterns. |
| 1090 |
""" |
| 1091 |
for sub_test in self.parameterize_from_lines('gitignore', [ |
| 1092 |
'build', |
| 1093 |
'*.log', |
| 1094 |
'.*', |
| 1095 |
'!.gitignore', |
| 1096 |
]): |
| 1097 |
with sub_test() as spec: |
| 1098 |
files = { |
| 1099 |
'.c-tmp', # 3:.* |
| 1100 |
'.gitignore', # 4:!.gitignore |
| 1101 |
'a.log', # 2:*.log |
| 1102 |
'b.txt', # - |
| 1103 |
'build/d.log', # 1:build |
| 1104 |
'build/trace.bin', # 1:build |
| 1105 |
'trace.c', # - |
| 1106 |
} |
| 1107 |
|
| 1108 |
keeps = set(spec.match_files(files, negate=True)) |
| 1109 |
ignores = set(spec.match_files(files)) |
| 1110 |
|
| 1111 |
self.assertEqual(files - ignores, keeps) |
| 1112 |
self.assertEqual(files - keeps, ignores) |
| 1113 |
|
| 1114 |
def test_10_issue_100(self): |
| 1115 |
""" |
| 1116 |
Test an empty list of patterns. |
| 1117 |
""" |
| 1118 |
for sub_test in self.parameterize_from_lines('gitignore', []): |
| 1119 |
with sub_test() as spec: |
| 1120 |
files = {'foo'} |
| 1121 |
results = list(spec.check_files(files)) |
| 1122 |
includes = get_includes(results) |
| 1123 |
debug = debug_results(spec, results) |
| 1124 |
self.assertEqual(includes, set(), debug) |
| 1125 |
|
| 1126 |
def test_11_repr(self): |
| 1127 |
""" |
| 1128 |
Test the path-spec debug representation. |
| 1129 |
""" |
| 1130 |
spec = PathSpec.from_lines('gitignore', ['*.py'], backend='simple') |
| 1131 |
self.assertEqual( |
| 1132 |
repr(spec), |
| 1133 |
"PathSpec(patterns=[GitIgnoreBasicPattern(pattern='*.py', include=True)], backend='simple')", |
| 1134 |
) |