Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 1 | import unittest |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 2 | from test.support import run_unittest, TESTFN, skip_unless_symlink, can_symlink |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 3 | import glob |
| 4 | import os |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 5 | import shutil |
Antoine Pitrou | 3d068b2 | 2012-12-16 13:49:37 +0100 | [diff] [blame^] | 6 | import sys |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 7 | |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 8 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 9 | class GlobTests(unittest.TestCase): |
| 10 | |
| 11 | def norm(self, *parts): |
| 12 | return os.path.normpath(os.path.join(self.tempdir, *parts)) |
| 13 | |
| 14 | def mktemp(self, *parts): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 15 | filename = self.norm(*parts) |
| 16 | base, file = os.path.split(filename) |
| 17 | if not os.path.exists(base): |
| 18 | os.makedirs(base) |
| 19 | f = open(filename, 'w') |
| 20 | f.close() |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 21 | |
| 22 | def setUp(self): |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 23 | self.tempdir = TESTFN + "_dir" |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 24 | self.mktemp('a', 'D') |
| 25 | self.mktemp('aab', 'F') |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 26 | self.mktemp('.aa', 'G') |
| 27 | self.mktemp('.bb', 'H') |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 28 | self.mktemp('aaa', 'zzzF') |
| 29 | self.mktemp('ZZZ') |
| 30 | self.mktemp('a', 'bcd', 'EF') |
| 31 | self.mktemp('a', 'bcd', 'efg', 'ha') |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 32 | if can_symlink(): |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 33 | os.symlink(self.norm('broken'), self.norm('sym1')) |
| 34 | os.symlink(self.norm('broken'), self.norm('sym2')) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 35 | |
| 36 | def tearDown(self): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 37 | shutil.rmtree(self.tempdir) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 38 | |
| 39 | def glob(self, *parts): |
| 40 | if len(parts) == 1: |
| 41 | pattern = parts[0] |
| 42 | else: |
| 43 | pattern = os.path.join(*parts) |
| 44 | p = os.path.join(self.tempdir, pattern) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 45 | res = glob.glob(p) |
| 46 | self.assertEqual(list(glob.iglob(p)), res) |
| 47 | return res |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 48 | |
| 49 | def assertSequencesEqual_noorder(self, l1, l2): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 50 | self.assertEqual(set(l1), set(l2)) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 51 | |
| 52 | def test_glob_literal(self): |
| 53 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 54 | eq(self.glob('a'), [self.norm('a')]) |
| 55 | eq(self.glob('a', 'D'), [self.norm('a', 'D')]) |
| 56 | eq(self.glob('aab'), [self.norm('aab')]) |
| 57 | eq(self.glob('zymurgy'), []) |
| 58 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 59 | # test return types are unicode, but only if os.listdir |
| 60 | # returns unicode filenames |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 61 | uniset = set([str]) |
| 62 | tmp = os.listdir('.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 63 | if set(type(x) for x in tmp) == uniset: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 64 | u1 = glob.glob('*') |
| 65 | u2 = glob.glob('./*') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 66 | self.assertEqual(set(type(r) for r in u1), uniset) |
| 67 | self.assertEqual(set(type(r) for r in u2), uniset) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 68 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 69 | def test_glob_one_directory(self): |
| 70 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 71 | eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa'])) |
| 72 | eq(self.glob('*a'), map(self.norm, ['a', 'aaa'])) |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 73 | eq(self.glob('.*'), map(self.norm, ['.aa', '.bb'])) |
| 74 | eq(self.glob('?aa'), map(self.norm, ['aaa'])) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 75 | eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab'])) |
| 76 | eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab'])) |
| 77 | eq(self.glob('*q'), []) |
| 78 | |
| 79 | def test_glob_nested_directory(self): |
| 80 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 81 | if os.path.normcase("abCD") == "abCD": |
| 82 | # case-sensitive filesystem |
| 83 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')]) |
| 84 | else: |
| 85 | # case insensitive filesystem |
| 86 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'), |
| 87 | self.norm('a', 'bcd', 'efg')]) |
| 88 | eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')]) |
| 89 | |
| 90 | def test_glob_directory_names(self): |
| 91 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 92 | eq(self.glob('*', 'D'), [self.norm('a', 'D')]) |
| 93 | eq(self.glob('*', '*a'), []) |
| 94 | eq(self.glob('a', '*', '*', '*a'), |
| 95 | [self.norm('a', 'bcd', 'efg', 'ha')]) |
| 96 | eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'), |
| 97 | os.path.join('aab', 'F')])) |
| 98 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 99 | def test_glob_directory_with_trailing_slash(self): |
| 100 | # We are verifying that when there is wildcard pattern which |
| 101 | # ends with os.sep doesn't blow up. |
| 102 | res = glob.glob(self.tempdir + '*' + os.sep) |
| 103 | self.assertEqual(len(res), 1) |
| 104 | # either of these results are reasonable |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 105 | self.assertIn(res[0], [self.tempdir, self.tempdir + os.sep]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 106 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 107 | @skip_unless_symlink |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 108 | def test_glob_broken_symlinks(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 109 | eq = self.assertSequencesEqual_noorder |
| 110 | eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2')]) |
| 111 | eq(self.glob('sym1'), [self.norm('sym1')]) |
| 112 | eq(self.glob('sym2'), [self.norm('sym2')]) |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | 3d068b2 | 2012-12-16 13:49:37 +0100 | [diff] [blame^] | 114 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific test") |
| 115 | def test_glob_magic_in_drive(self): |
| 116 | eq = self.assertSequencesEqual_noorder |
| 117 | eq(glob.glob('*:'), []) |
| 118 | eq(glob.glob(b'*:'), []) |
| 119 | eq(glob.glob('?:'), []) |
| 120 | eq(glob.glob(b'?:'), []) |
| 121 | eq(glob.glob('\\\\?\\c:\\'), ['\\\\?\\c:\\']) |
| 122 | eq(glob.glob(b'\\\\?\\c:\\'), [b'\\\\?\\c:\\']) |
| 123 | eq(glob.glob('\\\\*\\*\\'), []) |
| 124 | eq(glob.glob(b'\\\\*\\*\\'), []) |
| 125 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 126 | |
| 127 | def test_main(): |
| 128 | run_unittest(GlobTests) |
| 129 | |
| 130 | |
| 131 | if __name__ == "__main__": |
| 132 | test_main() |