Catch situations where currentframe() returns None. See SF patch #1447410, this is...
[python.git] / Lib / test / test_glob.py
blob8a6ef7fada370d5442447ff52e7bb15bbb86efa5
1 import unittest
2 from test.test_support import run_unittest, TESTFN
3 import glob
4 import os
5 import shutil
7 class GlobTests(unittest.TestCase):
9 def norm(self, *parts):
10 return os.path.normpath(os.path.join(self.tempdir, *parts))
12 def mktemp(self, *parts):
13 filename = self.norm(*parts)
14 base, file = os.path.split(filename)
15 if not os.path.exists(base):
16 os.makedirs(base)
17 f = open(filename, 'w')
18 f.close()
20 def setUp(self):
21 self.tempdir = TESTFN+"_dir"
22 self.mktemp('a', 'D')
23 self.mktemp('aab', 'F')
24 self.mktemp('aaa', 'zzzF')
25 self.mktemp('ZZZ')
26 self.mktemp('a', 'bcd', 'EF')
27 self.mktemp('a', 'bcd', 'efg', 'ha')
28 if hasattr(os, 'symlink'):
29 os.symlink(self.norm('broken'), self.norm('sym1'))
30 os.symlink(self.norm('broken'), self.norm('sym2'))
32 def tearDown(self):
33 shutil.rmtree(self.tempdir)
35 def glob(self, *parts):
36 if len(parts) == 1:
37 pattern = parts[0]
38 else:
39 pattern = os.path.join(*parts)
40 p = os.path.join(self.tempdir, pattern)
41 res = glob.glob(p)
42 self.assertEqual(list(glob.iglob(p)), res)
43 return res
45 def assertSequencesEqual_noorder(self, l1, l2):
46 self.assertEqual(set(l1), set(l2))
48 def test_glob_literal(self):
49 eq = self.assertSequencesEqual_noorder
50 eq(self.glob('a'), [self.norm('a')])
51 eq(self.glob('a', 'D'), [self.norm('a', 'D')])
52 eq(self.glob('aab'), [self.norm('aab')])
53 eq(self.glob('zymurgy'), [])
55 def test_glob_one_directory(self):
56 eq = self.assertSequencesEqual_noorder
57 eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa']))
58 eq(self.glob('*a'), map(self.norm, ['a', 'aaa']))
59 eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab']))
60 eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab']))
61 eq(self.glob('*q'), [])
63 def test_glob_nested_directory(self):
64 eq = self.assertSequencesEqual_noorder
65 if os.path.normcase("abCD") == "abCD":
66 # case-sensitive filesystem
67 eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')])
68 else:
69 # case insensitive filesystem
70 eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'),
71 self.norm('a', 'bcd', 'efg')])
72 eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')])
74 def test_glob_directory_names(self):
75 eq = self.assertSequencesEqual_noorder
76 eq(self.glob('*', 'D'), [self.norm('a', 'D')])
77 eq(self.glob('*', '*a'), [])
78 eq(self.glob('a', '*', '*', '*a'),
79 [self.norm('a', 'bcd', 'efg', 'ha')])
80 eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'),
81 os.path.join('aab', 'F')]))
83 def test_glob_broken_symlinks(self):
84 if hasattr(os, 'symlink'):
85 eq = self.assertSequencesEqual_noorder
86 eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2')])
87 eq(self.glob('sym1'), [self.norm('sym1')])
88 eq(self.glob('sym2'), [self.norm('sym2')])
91 def test_main():
92 run_unittest(GlobTests)
95 if __name__ == "__main__":
96 test_main()