##// END OF EJS Templates
Windows aware tests for shellglob
Takafumi Arakaki -
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
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():
466 # glob returns unordered list. that's why sorted is required.
480 # glob returns unordered list. that's why sorted is required.
467 nt.assert_equals(sorted(path.shellglob(patterns)),
481 nt.assert_equals(sorted(path.shellglob(patterns)),
468 sorted(matches))
482 sorted(matches))
469
483
470 assert_match(['*'], filenames)
484 def common_cases(self):
471 assert_match(['a*'], filenames_start_with_a)
485 return [
472 assert_match(['*c'], ['*c'])
486 (['*'], self.filenames),
473 assert_match(['*', 'a*', '*b', '*c'],
487 (['a*'], self.filenames_start_with_a),
474 filenames
488 (['*c'], ['*c']),
475 + filenames_start_with_a
489 (['*', 'a*', '*b', '*c'], self.filenames
476 + filenames_end_with_b
490 + self.filenames_start_with_a
477 + ['*c'])
491 + self.filenames_end_with_b
478
492 + ['*c']),
479 assert_match([r'\*'], ['*'])
493 (['a[012]'], self.filenames_start_with_a),
480 assert_match([r'a\*', 'a*'], ['a*'] + filenames_start_with_a)
494 ]
481 assert_match(['a[012]'], filenames_start_with_a)
495
482 assert_match([r'a\[012]'], ['a[012]'])
496 @skip_win32
483 finally:
497 def test_match_posix(self):
484 os.chdir(save)
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