Show More
@@ -19,6 +19,7 b' import shutil' | |||
|
19 | 19 | import sys |
|
20 | 20 | import tempfile |
|
21 | 21 | from io import StringIO |
|
22 | from contextlib import contextmanager | |
|
22 | 23 | |
|
23 | 24 | from os.path import join, abspath, split |
|
24 | 25 | |
@@ -447,41 +448,70 b' def test_unicode_in_filename():' | |||
|
447 | 448 | str(ex) |
|
448 | 449 | |
|
449 | 450 | |
|
450 | def test_shellglob(): | |
|
451 | """Test glob expansion for %run magic.""" | |
|
452 | filenames_start_with_a = map('a{0}'.format, range(3)) | |
|
453 | filenames_end_with_b = map('{0}b'.format, range(3)) | |
|
454 | filenames = filenames_start_with_a + filenames_end_with_b | |
|
451 | class TestShellGlob(object): | |
|
455 | 452 | |
|
456 | with TemporaryDirectory() as td: | |
|
457 | save = os.getcwdu() | |
|
458 | try: | |
|
459 | os.chdir(td) | |
|
453 | @classmethod | |
|
454 | def setUpClass(cls): | |
|
455 | cls.filenames_start_with_a = map('a{0}'.format, range(3)) | |
|
456 | cls.filenames_end_with_b = map('{0}b'.format, range(3)) | |
|
457 | cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b | |
|
458 | cls.tempdir = TemporaryDirectory() | |
|
459 | td = cls.tempdir.name | |
|
460 | 460 | |
|
461 | with cls.in_tempdir(): | |
|
461 | 462 | # Create empty files |
|
462 | for fname in filenames: | |
|
463 | for fname in cls.filenames: | |
|
463 | 464 | open(os.path.join(td, fname), 'w').close() |
|
464 | 465 | |
|
465 | def assert_match(patterns, matches): | |
|
466 | # glob returns unordered list. that's why sorted is required. | |
|
467 | nt.assert_equals(sorted(path.shellglob(patterns)), | |
|
468 | sorted(matches)) | |
|
469 | ||
|
470 | assert_match(['*'], filenames) | |
|
471 | assert_match(['a*'], filenames_start_with_a) | |
|
472 | assert_match(['*c'], ['*c']) | |
|
473 | assert_match(['*', 'a*', '*b', '*c'], | |
|
474 | filenames | |
|
475 | + filenames_start_with_a | |
|
476 | + filenames_end_with_b | |
|
477 | + ['*c']) | |
|
478 | ||
|
479 | assert_match([r'\*'], ['*']) | |
|
480 | assert_match([r'a\*', 'a*'], ['a*'] + filenames_start_with_a) | |
|
481 | assert_match(['a[012]'], filenames_start_with_a) | |
|
482 | assert_match([r'a\[012]'], ['a[012]']) | |
|
483 | finally: | |
|
484 | os.chdir(save) | |
|
466 | @classmethod | |
|
467 | def tearDownClass(cls): | |
|
468 | cls.tempdir.cleanup() | |
|
469 | ||
|
470 | @classmethod | |
|
471 | @contextmanager | |
|
472 | def in_tempdir(cls): | |
|
473 | save = os.getcwdu() | |
|
474 | os.chdir(cls.tempdir.name) | |
|
475 | yield | |
|
476 | os.chdir(save) | |
|
477 | ||
|
478 | def check_match(self, patterns, matches): | |
|
479 | with self.in_tempdir(): | |
|
480 | # glob returns unordered list. that's why sorted is required. | |
|
481 | nt.assert_equals(sorted(path.shellglob(patterns)), | |
|
482 | sorted(matches)) | |
|
483 | ||
|
484 | def common_cases(self): | |
|
485 | return [ | |
|
486 | (['*'], self.filenames), | |
|
487 | (['a*'], self.filenames_start_with_a), | |
|
488 | (['*c'], ['*c']), | |
|
489 | (['*', 'a*', '*b', '*c'], self.filenames | |
|
490 | + self.filenames_start_with_a | |
|
491 | + self.filenames_end_with_b | |
|
492 | + ['*c']), | |
|
493 | (['a[012]'], self.filenames_start_with_a), | |
|
494 | ] | |
|
495 | ||
|
496 | @skip_win32 | |
|
497 | def test_match_posix(self): | |
|
498 | for (patterns, matches) in self.common_cases() + [ | |
|
499 | ([r'\*'], ['*']), | |
|
500 | ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a), | |
|
501 | ([r'a\[012]'], ['a[012]']), | |
|
502 | ]: | |
|
503 | yield (self.check_match, patterns, matches) | |
|
504 | ||
|
505 | @skip_if_not_win32 | |
|
506 | def test_match_windows(self): | |
|
507 | for (patterns, matches) in self.common_cases() + [ | |
|
508 | # In windows, backslash is interpreted as path | |
|
509 | # separator. Therefore, you can't escape glob | |
|
510 | # using it. | |
|
511 | ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a), | |
|
512 | ([r'a\[012]'], [r'a\[012]']), | |
|
513 | ]: | |
|
514 | yield (self.check_match, patterns, matches) | |
|
485 | 515 | |
|
486 | 516 | |
|
487 | 517 | def test_unescape_glob(): |
General Comments 0
You need to be logged in to leave comments.
Login now