Show More
@@ -19,6 +19,7 b' import shutil' | |||||
19 | import sys |
|
19 | import sys | |
20 | import tempfile |
|
20 | import tempfile | |
21 | from io import StringIO |
|
21 | from io import StringIO | |
|
22 | from contextlib import contextmanager | |||
22 |
|
23 | |||
23 | from os.path import join, abspath, split |
|
24 | from os.path import join, abspath, split | |
24 |
|
25 | |||
@@ -447,41 +448,70 b' def test_unicode_in_filename():' | |||||
447 | str(ex) |
|
448 | str(ex) | |
448 |
|
449 | |||
449 |
|
450 | |||
450 | def test_shellglob(): |
|
451 | class TestShellGlob(object): | |
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 |
|
|||
455 |
|
452 | |||
456 | with TemporaryDirectory() as td: |
|
453 | @classmethod | |
457 | save = os.getcwdu() |
|
454 | def setUpClass(cls): | |
458 | try: |
|
455 | cls.filenames_start_with_a = map('a{0}'.format, range(3)) | |
459 | os.chdir(td) |
|
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 | # Create empty files |
|
462 | # Create empty files | |
462 | for fname in filenames: |
|
463 | for fname in cls.filenames: | |
463 | open(os.path.join(td, fname), 'w').close() |
|
464 | open(os.path.join(td, fname), 'w').close() | |
464 |
|
465 | |||
465 | def assert_match(patterns, matches): |
|
466 | @classmethod | |
466 | # glob returns unordered list. that's why sorted is required. |
|
467 | def tearDownClass(cls): | |
467 | nt.assert_equals(sorted(path.shellglob(patterns)), |
|
468 | cls.tempdir.cleanup() | |
468 | sorted(matches)) |
|
469 | ||
469 |
|
470 | @classmethod | ||
470 | assert_match(['*'], filenames) |
|
471 | @contextmanager | |
471 | assert_match(['a*'], filenames_start_with_a) |
|
472 | def in_tempdir(cls): | |
472 | assert_match(['*c'], ['*c']) |
|
473 | save = os.getcwdu() | |
473 | assert_match(['*', 'a*', '*b', '*c'], |
|
474 | os.chdir(cls.tempdir.name) | |
474 | filenames |
|
475 | yield | |
475 | + filenames_start_with_a |
|
476 | os.chdir(save) | |
476 | + filenames_end_with_b |
|
477 | ||
477 | + ['*c']) |
|
478 | def check_match(self, patterns, matches): | |
478 |
|
479 | with self.in_tempdir(): | ||
479 | assert_match([r'\*'], ['*']) |
|
480 | # glob returns unordered list. that's why sorted is required. | |
480 | assert_match([r'a\*', 'a*'], ['a*'] + filenames_start_with_a) |
|
481 | nt.assert_equals(sorted(path.shellglob(patterns)), | |
481 | assert_match(['a[012]'], filenames_start_with_a) |
|
482 | sorted(matches)) | |
482 | assert_match([r'a\[012]'], ['a[012]']) |
|
483 | ||
483 | finally: |
|
484 | def common_cases(self): | |
484 | os.chdir(save) |
|
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 | def test_unescape_glob(): |
|
517 | def test_unescape_glob(): |
General Comments 0
You need to be logged in to leave comments.
Login now