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