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 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 7 | from test.support import (TESTFN, skip_unless_symlink, |
Serhiy Storchaka | a1b16ba | 2014-09-11 14:33:02 +0300 | [diff] [blame] | 8 | can_symlink, create_empty_file, change_cwd) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 9 | |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 10 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 11 | class GlobTests(unittest.TestCase): |
| 12 | |
| 13 | def norm(self, *parts): |
| 14 | return os.path.normpath(os.path.join(self.tempdir, *parts)) |
| 15 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 16 | def joins(self, *tuples): |
| 17 | return [os.path.join(self.tempdir, *parts) for parts in tuples] |
| 18 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 19 | def mktemp(self, *parts): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 20 | filename = self.norm(*parts) |
| 21 | base, file = os.path.split(filename) |
| 22 | if not os.path.exists(base): |
| 23 | os.makedirs(base) |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 24 | create_empty_file(filename) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 25 | |
| 26 | def setUp(self): |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 27 | self.tempdir = TESTFN + "_dir" |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 28 | self.mktemp('a', 'D') |
| 29 | self.mktemp('aab', 'F') |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 30 | self.mktemp('.aa', 'G') |
| 31 | self.mktemp('.bb', 'H') |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 32 | self.mktemp('aaa', 'zzzF') |
| 33 | self.mktemp('ZZZ') |
Serhiy Storchaka | 735b790 | 2015-11-09 23:12:07 +0200 | [diff] [blame] | 34 | self.mktemp('EF') |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 35 | self.mktemp('a', 'bcd', 'EF') |
| 36 | self.mktemp('a', 'bcd', 'efg', 'ha') |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 37 | if can_symlink(): |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 38 | os.symlink(self.norm('broken'), self.norm('sym1')) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 39 | os.symlink('broken', self.norm('sym2')) |
| 40 | os.symlink(os.path.join('a', 'bcd'), self.norm('sym3')) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 41 | |
| 42 | def tearDown(self): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 43 | shutil.rmtree(self.tempdir) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 44 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 45 | def glob(self, *parts, **kwargs): |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 46 | if len(parts) == 1: |
| 47 | pattern = parts[0] |
| 48 | else: |
| 49 | pattern = os.path.join(*parts) |
| 50 | p = os.path.join(self.tempdir, pattern) |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 51 | res = glob.glob(p, **kwargs) |
| 52 | self.assertEqual(list(glob.iglob(p, **kwargs)), res) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 53 | bres = [os.fsencode(x) for x in res] |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 54 | self.assertEqual(glob.glob(os.fsencode(p), **kwargs), bres) |
| 55 | self.assertEqual(list(glob.iglob(os.fsencode(p), **kwargs)), bres) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 56 | return res |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 57 | |
| 58 | def assertSequencesEqual_noorder(self, l1, l2): |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 59 | l1 = list(l1) |
| 60 | l2 = list(l2) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 61 | self.assertEqual(set(l1), set(l2)) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 62 | self.assertEqual(sorted(l1), sorted(l2)) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 63 | |
| 64 | def test_glob_literal(self): |
| 65 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 66 | eq(self.glob('a'), [self.norm('a')]) |
| 67 | eq(self.glob('a', 'D'), [self.norm('a', 'D')]) |
| 68 | eq(self.glob('aab'), [self.norm('aab')]) |
| 69 | eq(self.glob('zymurgy'), []) |
| 70 | |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 71 | res = glob.glob('*') |
| 72 | self.assertEqual({type(r) for r in res}, {str}) |
| 73 | res = glob.glob(os.path.join(os.curdir, '*')) |
| 74 | self.assertEqual({type(r) for r in res}, {str}) |
| 75 | |
| 76 | res = glob.glob(b'*') |
| 77 | self.assertEqual({type(r) for r in res}, {bytes}) |
| 78 | res = glob.glob(os.path.join(os.fsencode(os.curdir), b'*')) |
| 79 | self.assertEqual({type(r) for r in res}, {bytes}) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 80 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 81 | def test_glob_one_directory(self): |
| 82 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 83 | eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa'])) |
| 84 | eq(self.glob('*a'), map(self.norm, ['a', 'aaa'])) |
Hynek Schlawack | dec59ec | 2012-12-16 12:39:11 +0100 | [diff] [blame] | 85 | eq(self.glob('.*'), map(self.norm, ['.aa', '.bb'])) |
| 86 | eq(self.glob('?aa'), map(self.norm, ['aaa'])) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 87 | eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab'])) |
| 88 | eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab'])) |
| 89 | eq(self.glob('*q'), []) |
| 90 | |
| 91 | def test_glob_nested_directory(self): |
| 92 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 93 | if os.path.normcase("abCD") == "abCD": |
| 94 | # case-sensitive filesystem |
| 95 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')]) |
| 96 | else: |
| 97 | # case insensitive filesystem |
| 98 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'), |
| 99 | self.norm('a', 'bcd', 'efg')]) |
| 100 | eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')]) |
| 101 | |
| 102 | def test_glob_directory_names(self): |
| 103 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 104 | eq(self.glob('*', 'D'), [self.norm('a', 'D')]) |
| 105 | eq(self.glob('*', '*a'), []) |
| 106 | eq(self.glob('a', '*', '*', '*a'), |
| 107 | [self.norm('a', 'bcd', 'efg', 'ha')]) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 108 | eq(self.glob('?a?', '*F'), [self.norm('aaa', 'zzzF'), |
| 109 | self.norm('aab', 'F')]) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 110 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | def test_glob_directory_with_trailing_slash(self): |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 112 | # Patterns ending with a slash shouldn't match non-dirs |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 113 | res = glob.glob(self.norm('Z*Z') + os.sep) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 114 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 115 | res = glob.glob(self.norm('ZZZ') + os.sep) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 116 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 117 | # When there is a wildcard pattern which ends with os.sep, glob() |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 118 | # doesn't blow up. |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 119 | res = glob.glob(self.norm('aa*') + os.sep) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 120 | self.assertEqual(len(res), 2) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 121 | # either of these results is reasonable |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 122 | self.assertIn(set(res), [ |
| 123 | {self.norm('aaa'), self.norm('aab')}, |
| 124 | {self.norm('aaa') + os.sep, self.norm('aab') + os.sep}, |
| 125 | ]) |
| 126 | |
| 127 | def test_glob_bytes_directory_with_trailing_slash(self): |
| 128 | # Same as test_glob_directory_with_trailing_slash, but with a |
| 129 | # bytes argument. |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 130 | res = glob.glob(os.fsencode(self.norm('Z*Z') + os.sep)) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 131 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 132 | res = glob.glob(os.fsencode(self.norm('ZZZ') + os.sep)) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 133 | self.assertEqual(res, []) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 134 | res = glob.glob(os.fsencode(self.norm('aa*') + os.sep)) |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 135 | self.assertEqual(len(res), 2) |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 136 | # either of these results is reasonable |
| 137 | self.assertIn(set(res), [ |
| 138 | {os.fsencode(self.norm('aaa')), |
| 139 | os.fsencode(self.norm('aab'))}, |
| 140 | {os.fsencode(self.norm('aaa') + os.sep), |
| 141 | os.fsencode(self.norm('aab') + os.sep)}, |
Antoine Pitrou | 5461558 | 2012-12-16 16:03:01 +0100 | [diff] [blame] | 142 | ]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 143 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 144 | @skip_unless_symlink |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 145 | def test_glob_symlinks(self): |
| 146 | eq = self.assertSequencesEqual_noorder |
| 147 | eq(self.glob('sym3'), [self.norm('sym3')]) |
| 148 | eq(self.glob('sym3', '*'), [self.norm('sym3', 'EF'), |
| 149 | self.norm('sym3', 'efg')]) |
| 150 | self.assertIn(self.glob('sym3' + os.sep), |
| 151 | [[self.norm('sym3')], [self.norm('sym3') + os.sep]]) |
| 152 | eq(self.glob('*', '*F'), |
| 153 | [self.norm('aaa', 'zzzF'), |
| 154 | self.norm('aab', 'F'), self.norm('sym3', 'EF')]) |
| 155 | |
| 156 | @skip_unless_symlink |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 157 | def test_glob_broken_symlinks(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 158 | eq = self.assertSequencesEqual_noorder |
Hynek Schlawack | e26568f | 2012-12-27 10:10:11 +0100 | [diff] [blame] | 159 | eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2'), |
| 160 | self.norm('sym3')]) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 161 | eq(self.glob('sym1'), [self.norm('sym1')]) |
| 162 | eq(self.glob('sym2'), [self.norm('sym2')]) |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | 3d068b2 | 2012-12-16 13:49:37 +0100 | [diff] [blame] | 164 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific test") |
| 165 | def test_glob_magic_in_drive(self): |
| 166 | eq = self.assertSequencesEqual_noorder |
| 167 | eq(glob.glob('*:'), []) |
| 168 | eq(glob.glob(b'*:'), []) |
| 169 | eq(glob.glob('?:'), []) |
| 170 | eq(glob.glob(b'?:'), []) |
| 171 | eq(glob.glob('\\\\?\\c:\\'), ['\\\\?\\c:\\']) |
| 172 | eq(glob.glob(b'\\\\?\\c:\\'), [b'\\\\?\\c:\\']) |
| 173 | eq(glob.glob('\\\\*\\*\\'), []) |
| 174 | eq(glob.glob(b'\\\\*\\*\\'), []) |
| 175 | |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 176 | def check_escape(self, arg, expected): |
| 177 | self.assertEqual(glob.escape(arg), expected) |
| 178 | self.assertEqual(glob.escape(os.fsencode(arg)), os.fsencode(expected)) |
| 179 | |
| 180 | def test_escape(self): |
| 181 | check = self.check_escape |
| 182 | check('abc', 'abc') |
| 183 | check('[', '[[]') |
| 184 | check('?', '[?]') |
| 185 | check('*', '[*]') |
| 186 | check('[[_/*?*/_]]', '[[][[]_/[*][?][*]/_]]') |
| 187 | check('/[[_/*?*/_]]/', '/[[][[]_/[*][?][*]/_]]/') |
| 188 | |
| 189 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific test") |
| 190 | def test_escape_windows(self): |
| 191 | check = self.check_escape |
| 192 | check('?:?', '?:[?]') |
| 193 | check('*:*', '*:[*]') |
| 194 | check(r'\\?\c:\?', r'\\?\c:\[?]') |
| 195 | check(r'\\*\*\*', r'\\*\*\[*]') |
| 196 | check('//?/c:/?', '//?/c:/[?]') |
| 197 | check('//*/*/*', '//*/*/[*]') |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 198 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 199 | def rglob(self, *parts, **kwargs): |
| 200 | return self.glob(*parts, recursive=True, **kwargs) |
| 201 | |
| 202 | def test_recursive_glob(self): |
| 203 | eq = self.assertSequencesEqual_noorder |
Serhiy Storchaka | 735b790 | 2015-11-09 23:12:07 +0200 | [diff] [blame] | 204 | full = [('EF',), ('ZZZ',), |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 205 | ('a',), ('a', 'D'), |
| 206 | ('a', 'bcd'), |
| 207 | ('a', 'bcd', 'EF'), |
| 208 | ('a', 'bcd', 'efg'), |
| 209 | ('a', 'bcd', 'efg', 'ha'), |
| 210 | ('aaa',), ('aaa', 'zzzF'), |
| 211 | ('aab',), ('aab', 'F'), |
| 212 | ] |
| 213 | if can_symlink(): |
| 214 | full += [('sym1',), ('sym2',), |
| 215 | ('sym3',), |
| 216 | ('sym3', 'EF'), |
| 217 | ('sym3', 'efg'), |
| 218 | ('sym3', 'efg', 'ha'), |
| 219 | ] |
| 220 | eq(self.rglob('**'), self.joins(('',), *full)) |
Serhiy Storchaka | 735b790 | 2015-11-09 23:12:07 +0200 | [diff] [blame] | 221 | eq(self.rglob(os.curdir, '**'), |
| 222 | self.joins((os.curdir, ''), *((os.curdir,) + i for i in full))) |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 223 | dirs = [('a', ''), ('a', 'bcd', ''), ('a', 'bcd', 'efg', ''), |
| 224 | ('aaa', ''), ('aab', '')] |
| 225 | if can_symlink(): |
| 226 | dirs += [('sym3', ''), ('sym3', 'efg', '')] |
| 227 | eq(self.rglob('**', ''), self.joins(('',), *dirs)) |
| 228 | |
| 229 | eq(self.rglob('a', '**'), self.joins( |
| 230 | ('a', ''), ('a', 'D'), ('a', 'bcd'), ('a', 'bcd', 'EF'), |
| 231 | ('a', 'bcd', 'efg'), ('a', 'bcd', 'efg', 'ha'))) |
| 232 | eq(self.rglob('a**'), self.joins(('a',), ('aaa',), ('aab',))) |
Serhiy Storchaka | 735b790 | 2015-11-09 23:12:07 +0200 | [diff] [blame] | 233 | expect = [('a', 'bcd', 'EF'), ('EF',)] |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 234 | if can_symlink(): |
| 235 | expect += [('sym3', 'EF')] |
| 236 | eq(self.rglob('**', 'EF'), self.joins(*expect)) |
Serhiy Storchaka | 735b790 | 2015-11-09 23:12:07 +0200 | [diff] [blame] | 237 | expect = [('a', 'bcd', 'EF'), ('aaa', 'zzzF'), ('aab', 'F'), ('EF',)] |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 238 | if can_symlink(): |
| 239 | expect += [('sym3', 'EF')] |
| 240 | eq(self.rglob('**', '*F'), self.joins(*expect)) |
| 241 | eq(self.rglob('**', '*F', ''), []) |
| 242 | eq(self.rglob('**', 'bcd', '*'), self.joins( |
| 243 | ('a', 'bcd', 'EF'), ('a', 'bcd', 'efg'))) |
| 244 | eq(self.rglob('a', '**', 'bcd'), self.joins(('a', 'bcd'))) |
| 245 | |
Serhiy Storchaka | 5fbadb6 | 2015-09-06 14:14:49 +0300 | [diff] [blame] | 246 | with change_cwd(self.tempdir): |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 247 | join = os.path.join |
| 248 | eq(glob.glob('**', recursive=True), [join(*i) for i in full]) |
| 249 | eq(glob.glob(join('**', ''), recursive=True), |
| 250 | [join(*i) for i in dirs]) |
Serhiy Storchaka | 735b790 | 2015-11-09 23:12:07 +0200 | [diff] [blame] | 251 | eq(glob.glob(join('**', '*'), recursive=True), |
| 252 | [join(*i) for i in full]) |
| 253 | eq(glob.glob(join(os.curdir, '**'), recursive=True), |
| 254 | [join(os.curdir, '')] + [join(os.curdir, *i) for i in full]) |
| 255 | eq(glob.glob(join(os.curdir, '**', ''), recursive=True), |
| 256 | [join(os.curdir, '')] + [join(os.curdir, *i) for i in dirs]) |
| 257 | eq(glob.glob(join(os.curdir, '**', '*'), recursive=True), |
| 258 | [join(os.curdir, *i) for i in full]) |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 259 | eq(glob.glob(join('**','zz*F'), recursive=True), |
| 260 | [join('aaa', 'zzzF')]) |
| 261 | eq(glob.glob('**zz*F', recursive=True), []) |
Serhiy Storchaka | 735b790 | 2015-11-09 23:12:07 +0200 | [diff] [blame] | 262 | expect = [join('a', 'bcd', 'EF'), 'EF'] |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 263 | if can_symlink(): |
| 264 | expect += [join('sym3', 'EF')] |
| 265 | eq(glob.glob(join('**', 'EF'), recursive=True), expect) |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 266 | |
| 267 | |
| 268 | @skip_unless_symlink |
| 269 | class SymlinkLoopGlobTests(unittest.TestCase): |
| 270 | |
| 271 | def test_selflink(self): |
| 272 | tempdir = TESTFN + "_dir" |
| 273 | os.makedirs(tempdir) |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 274 | self.addCleanup(shutil.rmtree, tempdir) |
Serhiy Storchaka | a1b16ba | 2014-09-11 14:33:02 +0300 | [diff] [blame] | 275 | with change_cwd(tempdir): |
| 276 | os.makedirs('dir') |
| 277 | create_empty_file(os.path.join('dir', 'file')) |
| 278 | os.symlink(os.curdir, os.path.join('dir', 'link')) |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 279 | |
Serhiy Storchaka | a1b16ba | 2014-09-11 14:33:02 +0300 | [diff] [blame] | 280 | results = glob.glob('**', recursive=True) |
| 281 | self.assertEqual(len(results), len(set(results))) |
| 282 | results = set(results) |
| 283 | depth = 0 |
| 284 | while results: |
| 285 | path = os.path.join(*(['dir'] + ['link'] * depth)) |
| 286 | self.assertIn(path, results) |
| 287 | results.remove(path) |
| 288 | if not results: |
| 289 | break |
| 290 | path = os.path.join(path, 'file') |
| 291 | self.assertIn(path, results) |
| 292 | results.remove(path) |
| 293 | depth += 1 |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 294 | |
Serhiy Storchaka | a1b16ba | 2014-09-11 14:33:02 +0300 | [diff] [blame] | 295 | results = glob.glob(os.path.join('**', 'file'), recursive=True) |
| 296 | self.assertEqual(len(results), len(set(results))) |
| 297 | results = set(results) |
| 298 | depth = 0 |
| 299 | while results: |
| 300 | path = os.path.join(*(['dir'] + ['link'] * depth + ['file'])) |
| 301 | self.assertIn(path, results) |
| 302 | results.remove(path) |
| 303 | depth += 1 |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 304 | |
Serhiy Storchaka | a1b16ba | 2014-09-11 14:33:02 +0300 | [diff] [blame] | 305 | results = glob.glob(os.path.join('**', ''), recursive=True) |
| 306 | self.assertEqual(len(results), len(set(results))) |
| 307 | results = set(results) |
| 308 | depth = 0 |
| 309 | while results: |
| 310 | path = os.path.join(*(['dir'] + ['link'] * depth + [''])) |
| 311 | self.assertIn(path, results) |
| 312 | results.remove(path) |
| 313 | depth += 1 |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 314 | |
| 315 | |
| 316 | if __name__ == "__main__": |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 317 | unittest.main() |