|
2 | 2 |
|
3 | 3 | import io |
4 | 4 | import pathlib |
| 5 | +import re |
5 | 6 | import stat |
6 | 7 | import sys |
7 | 8 | import tempfile |
@@ -316,6 +317,67 @@ def test_content_of_copied_archive(self): |
316 | 317 | with zipfile.ZipFile(new_target, 'r') as z: |
317 | 318 | self.assertEqual(set(z.namelist()), {'__main__.py'}) |
318 | 319 |
|
| 320 | + def test_create_archive_with_include_pattern(self): |
| 321 | + source = self.tmpdir / 'source' |
| 322 | + source.mkdir() |
| 323 | + (source / '.DS_Store').touch() |
| 324 | + (source / 'zed.py').touch() |
| 325 | + (source / 'bin').mkdir() |
| 326 | + (source / 'bin' / 'qux').touch() |
| 327 | + (source / 'bin' / 'baz').touch() |
| 328 | + (source / '__main__.py').touch() |
| 329 | + target = io.BytesIO() |
| 330 | + zipapp.create_archive( |
| 331 | + source=str(source), |
| 332 | + target=target, |
| 333 | + include_pattern=re.compile(r".*\.py") |
| 334 | + ) |
| 335 | + target.seek(0) |
| 336 | + with zipfile.ZipFile(target, 'r') as zf: |
| 337 | + self.assertEqual(zf.namelist(), |
| 338 | + ["__main__.py", "zed.py"]) |
| 339 | + |
| 340 | + def test_create_archive_with_exclude_pattern(self): |
| 341 | + source = self.tmpdir / 'source' |
| 342 | + source.mkdir() |
| 343 | + (source / '.DS_Store').touch() |
| 344 | + (source / 'zed.py').touch() |
| 345 | + (source / 'bin').mkdir() |
| 346 | + (source / 'bin' / 'qux').touch() |
| 347 | + (source / 'bin' / 'baz').touch() |
| 348 | + (source / '__main__.py').touch() |
| 349 | + target = io.BytesIO() |
| 350 | + zipapp.create_archive( |
| 351 | + source=str(source), |
| 352 | + target=target, |
| 353 | + exclude_pattern=re.compile(r".*\.py") |
| 354 | + ) |
| 355 | + target.seek(0) |
| 356 | + with zipfile.ZipFile(target, 'r') as zf: |
| 357 | + self.assertEqual(zf.namelist(), |
| 358 | + [".DS_Store", "bin/", "bin/baz", "bin/qux"]) |
| 359 | + |
| 360 | + def test_create_archive_with_include_and_exclude_pattern(self): |
| 361 | + source = self.tmpdir / 'source' |
| 362 | + source.mkdir() |
| 363 | + (source / '.DS_Store').touch() |
| 364 | + (source / 'zed.py').touch() |
| 365 | + (source / 'bin').mkdir() |
| 366 | + (source / 'bin' / 'qux').touch() |
| 367 | + (source / 'bin' / 'baz').touch() |
| 368 | + (source / '__main__.py').touch() |
| 369 | + target = io.BytesIO() |
| 370 | + zipapp.create_archive( |
| 371 | + source=str(source), |
| 372 | + target=target, |
| 373 | + include_pattern=re.compile(r".*\.py"), |
| 374 | + exclude_pattern=re.compile(r".*z.*") |
| 375 | + ) |
| 376 | + target.seek(0) |
| 377 | + with zipfile.ZipFile(target, 'r') as zf: |
| 378 | + self.assertEqual(zf.namelist(), |
| 379 | + ["__main__.py"]) |
| 380 | + |
319 | 381 | # (Unix only) tests that archives with shebang lines are made executable |
320 | 382 | @unittest.skipIf(sys.platform == 'win32', |
321 | 383 | 'Windows does not support an executable bit') |
|
0 commit comments