ยปCore Development>Code coverage>Lib/test/test_glob.py

Python code coverage for Lib/test/test_glob.py

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