Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 1 | import glob |
| 2 | import os |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 3 | import shutil |
Antoine Pitrou | 3d068b2 | 2012-12-16 13:49:37 +0100 | [diff] [blame] | 4 | import sys |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 5 | import unittest |
| 6 | |
| 7 | from test.support import run_unittest, TESTFN, skip_unless_symlink, can_symlink |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 8 | |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 9 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 10 | class GlobTests(unittest.TestCase): |
| 11 | |
| 12 | def norm(self, *parts): |
| 13 | return os.path.normpath(os.path.join(self.tempdir, *parts)) |
| 14 | |
| 15 | def mktemp(self, *parts): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 16 | filename = self.norm(*parts) |
| 17 | base, file = os.path.split(filename) |
| 18 | if not os.path.exists(base): |
| 19 | os.makedirs(base) |
| 20 | f = open(filename, 'w') |
| 21 | f.close() |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 22 | |
| 23 | def setUp(self): |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 24 | self.tempdir = TESTFN + "_dir" |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 25 | self.mktemp('a', 'D') |
| 26 | self.mktemp('aab', 'F') |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 27 | self.mktemp('.aa', 'G') |
| 28 | self.mktemp('.bb', 'H') |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 29 | self.mktemp('aaa', 'zzzF') |
| 30 | self.mktemp('ZZZ') |
| 31 | self.mktemp('a', 'bcd', 'EF') |
| 32 | self.mktemp('a', 'bcd', 'efg', 'ha') |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 33 | if can_symlink(): |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 34 | os.symlink(self.norm('broken'), self.norm('sym1')) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 35 | os.symlink('broken', self.norm('sym2')) |
| 36 | os.symlink(os.path.join('a', 'bcd'), self.norm('sym3')) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 37 | |
| 38 | def tearDown(self): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 39 | shutil.rmtree(self.tempdir) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 40 | |
| 41 | def glob(self, *parts): |
| 42 | if len(parts) == 1: |
| 43 | pattern = parts[0] |
| 44 | else: |
| 45 | pattern = os.path.join(*parts) |
| 46 | p = os.path.join(self.tempdir, pattern) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 47 | res = glob.glob(p) |
| 48 | self.assertEqual(list(glob.iglob(p)), res) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 49 | bres = [os.fsencode(x) for x in res] |
| 50 | self.assertEqual(glob.glob(os.fsencode(p)), bres) |
| 51 | self.assertEqual(list(glob.iglob(os.fsencode(p))), bres) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 52 | return res |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 53 | |
| 54 | def assertSequencesEqual_noorder(self, l1, l2): |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 55 | l1 = list(l1) |
| 56 | l2 = list(l2) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 57 | self.assertEqual(set(l1), set(l2)) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 58 | self.assertEqual(sorted(l1), sorted(l2)) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 59 | |
| 60 | def test_glob_literal(self): |
| 61 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 62 | eq(self.glob('a'), [self.norm('a')]) |
| 63 | eq(self.glob('a', 'D'), [self.norm('a', 'D')]) |
| 64 | eq(self.glob('aab'), [self.norm('aab')]) |
| 65 | eq(self.glob('zymurgy'), []) |
| 66 | |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 67 | res = glob.glob('*') |
| 68 | self.assertEqual({type(r) for r in res}, {str}) |
| 69 | res = glob.glob(os.path.join(os.curdir, '*')) |
| 70 | self.assertEqual({type(r) for r in res}, {str}) |
| 71 | |
| 72 | res = glob.glob(b'*') |
| 73 | self.assertEqual({type(r) for r in res}, {bytes}) |
| 74 | res = glob.glob(os.path.join(os.fsencode(os.curdir), b'*')) |
| 75 | self.assertEqual({type(r) for r in res}, {bytes}) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 76 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 77 | def test_glob_one_directory(self): |
| 78 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 79 | eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa'])) |
| 80 | eq(self.glob('*a'), map(self.norm, ['a', 'aaa'])) |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 81 | eq(self.glob('.*'), map(self.norm, ['.aa', '.bb'])) |
| 82 | eq(self.glob('?aa'), map(self.norm, ['aaa'])) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 83 | eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab'])) |
| 84 | eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab'])) |
| 85 | eq(self.glob('*q'), []) |
| 86 | |
| 87 | def test_glob_nested_directory(self): |
| 88 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 89 | if os.path.normcase("abCD") == "abCD": |
| 90 | # case-sensitive filesystem |
| 91 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')]) |
| 92 | else: |
| 93 | # case insensitive filesystem |
| 94 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'), |
| 95 | self.norm('a', 'bcd', 'efg')]) |
| 96 | eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')]) |
| 97 | |
| 98 | def test_glob_directory_names(self): |
| 99 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 100 | eq(self.glob('*', 'D'), [self.norm('a', 'D')]) |
| 101 | eq(self.glob('*', '*a'), []) |
| 102 | eq(self.glob('a', '*', '*', '*a'), |
| 103 | [self.norm('a', 'bcd', 'efg', 'ha')]) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 104 | eq(self.glob('?a?', '*F'), [self.norm('aaa', 'zzzF'), |
| 105 | self.norm('aab', 'F')]) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 106 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 107 | def test_glob_directory_with_trailing_slash(self): |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 108 | # Patterns ending with a slash shouldn't match non-dirs |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 109 | res = glob.glob(self.norm('Z*Z') + os.sep) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 110 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 111 | res = glob.glob(self.norm('ZZZ') + os.sep) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 112 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 113 | # When there is a wildcard pattern which ends with os.sep, glob() |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 114 | # doesn't blow up. |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 115 | res = glob.glob(self.norm('aa*') + os.sep) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 116 | self.assertEqual(len(res), 2) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 117 | # either of these results is reasonable |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 118 | self.assertIn(set(res), [ |
| 119 | {self.norm('aaa'), self.norm('aab')}, |
| 120 | {self.norm('aaa') + os.sep, self.norm('aab') + os.sep}, |
| 121 | ]) |
| 122 | |
| 123 | def test_glob_bytes_directory_with_trailing_slash(self): |
| 124 | # Same as test_glob_directory_with_trailing_slash, but with a |
| 125 | # bytes argument. |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 126 | res = glob.glob(os.fsencode(self.norm('Z*Z') + os.sep)) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 127 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 128 | res = glob.glob(os.fsencode(self.norm('ZZZ') + os.sep)) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 129 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 130 | res = glob.glob(os.fsencode(self.norm('aa*') + os.sep)) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 131 | self.assertEqual(len(res), 2) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 132 | # either of these results is reasonable |
| 133 | self.assertIn(set(res), [ |
| 134 | {os.fsencode(self.norm('aaa')), |
| 135 | os.fsencode(self.norm('aab'))}, |
| 136 | {os.fsencode(self.norm('aaa') + os.sep), |
| 137 | os.fsencode(self.norm('aab') + os.sep)}, |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 138 | ]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 139 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 140 | @skip_unless_symlink |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 141 | def test_glob_symlinks(self): |
| 142 | eq = self.assertSequencesEqual_noorder |
| 143 | eq(self.glob('sym3'), [self.norm('sym3')]) |
| 144 | eq(self.glob('sym3', '*'), [self.norm('sym3', 'EF'), |
| 145 | self.norm('sym3', 'efg')]) |
| 146 | self.assertIn(self.glob('sym3' + os.sep), |
| 147 | [[self.norm('sym3')], [self.norm('sym3') + os.sep]]) |
| 148 | eq(self.glob('*', '*F'), |
| 149 | [self.norm('aaa', 'zzzF'), |
| 150 | self.norm('aab', 'F'), self.norm('sym3', 'EF')]) |
| 151 | |
| 152 | @skip_unless_symlink |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 153 | def test_glob_broken_symlinks(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 154 | eq = self.assertSequencesEqual_noorder |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame^] | 155 | eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2'), |
| 156 | self.norm('sym3')]) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 157 | eq(self.glob('sym1'), [self.norm('sym1')]) |
| 158 | eq(self.glob('sym2'), [self.norm('sym2')]) |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | 3d068b2 | 2012-12-16 13:49:37 +0100 | [diff] [blame] | 160 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific test") |
| 161 | def test_glob_magic_in_drive(self): |
| 162 | eq = self.assertSequencesEqual_noorder |
| 163 | eq(glob.glob('*:'), []) |
| 164 | eq(glob.glob(b'*:'), []) |
| 165 | eq(glob.glob('?:'), []) |
| 166 | eq(glob.glob(b'?:'), []) |
| 167 | eq(glob.glob('\\\\?\\c:\\'), ['\\\\?\\c:\\']) |
| 168 | eq(glob.glob(b'\\\\?\\c:\\'), [b'\\\\?\\c:\\']) |
| 169 | eq(glob.glob('\\\\*\\*\\'), []) |
| 170 | eq(glob.glob(b'\\\\*\\*\\'), []) |
| 171 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 172 | |
| 173 | def test_main(): |
| 174 | run_unittest(GlobTests) |
| 175 | |
| 176 | |
| 177 | if __name__ == "__main__": |
| 178 | test_main() |