annotate Lib/test/test_glob.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children 028ecc3900f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
2 from test.test_support import run_unittest, TESTFN
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 import glob
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 import os
34366
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
5 import shutil
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7 class GlobTests(unittest.TestCase):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9 def norm(self, *parts):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 return os.path.normpath(os.path.join(self.tempdir, *parts))
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 def mktemp(self, *parts):
34366
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
13 filename = self.norm(*parts)
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
14 base, file = os.path.split(filename)
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
15 if not os.path.exists(base):
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
16 os.makedirs(base)
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
17 f = open(filename, 'w')
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
18 f.close()
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20 def setUp(self):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 self.tempdir = TESTFN+"_dir"
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 self.mktemp('a', 'D')
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 self.mktemp('aab', 'F')
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 self.mktemp('aaa', 'zzzF')
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 self.mktemp('ZZZ')
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 self.mktemp('a', 'bcd', 'EF')
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 self.mktemp('a', 'bcd', 'efg', 'ha')
33332
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
28 if hasattr(os, 'symlink'):
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
29 os.symlink(self.norm('broken'), self.norm('sym1'))
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
30 os.symlink(self.norm('broken'), self.norm('sym2'))
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 def tearDown(self):
34366
b22ebfc488f3 Clean up tests by reusing functions from other modules:
Johannes Gijsbers <jlg@dds.nl>
parents: 34365
diff changeset
33 shutil.rmtree(self.tempdir)
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 def glob(self, *parts):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 if len(parts) == 1:
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 pattern = parts[0]
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 else:
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 pattern = os.path.join(*parts)
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 p = os.path.join(self.tempdir, pattern)
34365
aa246b9f6184 Patch #943206:
Johannes Gijsbers <jlg@dds.nl>
parents: 33332
diff changeset
41 res = glob.glob(p)
aa246b9f6184 Patch #943206:
Johannes Gijsbers <jlg@dds.nl>
parents: 33332
diff changeset
42 self.assertEqual(list(glob.iglob(p)), res)
aa246b9f6184 Patch #943206:
Johannes Gijsbers <jlg@dds.nl>
parents: 33332
diff changeset
43 return res
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 def assertSequencesEqual_noorder(self, l1, l2):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30911
diff changeset
46 self.assertEqual(set(l1), set(l2))
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 def test_glob_literal(self):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 eq = self.assertSequencesEqual_noorder
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 eq(self.glob('a'), [self.norm('a')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 eq(self.glob('a', 'D'), [self.norm('a', 'D')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52 eq(self.glob('aab'), [self.norm('aab')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 eq(self.glob('zymurgy'), [])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
68255
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
55 # test return types are unicode, but only if os.listdir
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
56 # returns unicode filenames
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
57 uniset = set([unicode])
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
58 tmp = os.listdir(u'.')
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
59 if set(type(x) for x in tmp) == uniset:
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
60 u1 = glob.glob(u'*')
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
61 u2 = glob.glob(u'./*')
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
62 self.assertEqual(set(type(r) for r in u1), uniset)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
63 self.assertEqual(set(type(r) for r in u2), uniset)
68255
787b969d37f0 Dummy-merge 2.5 branch into 2.6 branch.
Georg Brandl <georg@python.org>
parents: 41103
diff changeset
64
18806
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 def test_glob_one_directory(self):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 eq = self.assertSequencesEqual_noorder
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa']))
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 eq(self.glob('*a'), map(self.norm, ['a', 'aaa']))
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab']))
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab']))
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
71 eq(self.glob('*q'), [])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 def test_glob_nested_directory(self):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 eq = self.assertSequencesEqual_noorder
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 if os.path.normcase("abCD") == "abCD":
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 # case-sensitive filesystem
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 else:
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 # case insensitive filesystem
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80 eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'),
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 self.norm('a', 'bcd', 'efg')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 def test_glob_directory_names(self):
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 eq = self.assertSequencesEqual_noorder
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 eq(self.glob('*', 'D'), [self.norm('a', 'D')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
87 eq(self.glob('*', '*a'), [])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
88 eq(self.glob('a', '*', '*', '*a'),
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
89 [self.norm('a', 'bcd', 'efg', 'ha')])
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'),
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
91 os.path.join('aab', 'F')]))
74f050f7c364 Test for the "glob" module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92
37545
a8d4928292d8 glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34366
diff changeset
93 def test_glob_directory_with_trailing_slash(self):
a8d4928292d8 glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34366
diff changeset
94 # We are verifying that when there is wildcard pattern which
a8d4928292d8 glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34366
diff changeset
95 # ends with os.sep doesn't blow up.
a8d4928292d8 glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34366
diff changeset
96 res = glob.glob(self.tempdir + '*' + os.sep)
a8d4928292d8 glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34366
diff changeset
97 self.assertEqual(len(res), 1)
a8d4928292d8 glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34366
diff changeset
98 # either of these results are reasonable
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 41050
diff changeset
99 self.assertIn(res[0], [self.tempdir, self.tempdir + os.sep])
37545
a8d4928292d8 glob('anything*/') would fail because isdir is in os.path, not os.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34366
diff changeset
100
33332
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
101 def test_glob_broken_symlinks(self):
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
102 if hasattr(os, 'symlink'):
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
103 eq = self.assertSequencesEqual_noorder
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
104 eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2')])
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
105 eq(self.glob('sym1'), [self.norm('sym1')])
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
106 eq(self.glob('sym2'), [self.norm('sym2')])
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 30929
diff changeset
107
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
108
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
109 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
110 run_unittest(GlobTests)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
111
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
112
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
113 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19119
diff changeset
114 test_main()