blob: 8cf6ecf47120d22b0b1a9c3c37c98798656682e0 [file] [log] [blame]
Fred Drake99e87f92001-07-23 16:08:36 +00001import unittest
2from test_support import run_unittest, TESTFN
3import glob
4import os
5
6def mkdirs(fname):
7 if os.path.exists(fname) or fname == '':
8 return
9 base, file = os.path.split(fname)
10 mkdirs(base)
11 os.mkdir(fname)
12
13def touchfile(fname):
14 base, file = os.path.split(fname)
15 mkdirs(base)
16 f = open(fname, 'w')
17 f.close()
18
19def deltree(fname):
20 for f in os.listdir(fname):
21 fullname = os.path.join(fname, f)
22 if os.path.isdir(fullname):
23 deltree(fullname)
24 else:
25 try:
26 os.unlink(fullname)
27 except:
28 pass
29 try:
30 os.rmdir(fname)
31 except:
32 pass
33
34
35class GlobTests(unittest.TestCase):
36
37 def norm(self, *parts):
38 return os.path.normpath(os.path.join(self.tempdir, *parts))
39
40 def mktemp(self, *parts):
41 touchfile(self.norm(*parts))
42
43 def setUp(self):
44 self.tempdir = TESTFN+"_dir"
45 self.mktemp('a', 'D')
46 self.mktemp('aab', 'F')
47 self.mktemp('aaa', 'zzzF')
48 self.mktemp('ZZZ')
49 self.mktemp('a', 'bcd', 'EF')
50 self.mktemp('a', 'bcd', 'efg', 'ha')
51
52 def tearDown(self):
53 deltree(self.tempdir)
54
55 def glob(self, *parts):
56 if len(parts) == 1:
57 pattern = parts[0]
58 else:
59 pattern = os.path.join(*parts)
60 p = os.path.join(self.tempdir, pattern)
61 return glob.glob(p)
62
63 def assertSequencesEqual_noorder(self, l1, l2):
64 l1 = list(l1)
65 l2 = list(l2)
66 l1.sort()
67 l2.sort()
68 self.assertEqual(l1, l2)
69
70 def test_glob_literal(self):
71 eq = self.assertSequencesEqual_noorder
72 np = lambda *f: norm(self.tempdir, *f)
73 eq(self.glob('a'), [self.norm('a')])
74 eq(self.glob('a', 'D'), [self.norm('a', 'D')])
75 eq(self.glob('aab'), [self.norm('aab')])
76 eq(self.glob('zymurgy'), [])
77
78 def test_glob_one_directory(self):
79 eq = self.assertSequencesEqual_noorder
80 np = lambda *f: norm(self.tempdir, *f)
81 eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa']))
82 eq(self.glob('*a'), map(self.norm, ['a', 'aaa']))
83 eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab']))
84 eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab']))
85 eq(self.glob('*q'), [])
86
87 def test_glob_nested_directory(self):
88 eq = self.assertSequencesEqual_noorder
89 np = lambda *f: norm(self.tempdir, *f)
90 if os.path.normcase("abCD") == "abCD":
91 # case-sensitive filesystem
92 eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')])
93 else:
94 # case insensitive filesystem
95 eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'),
96 self.norm('a', 'bcd', 'efg')])
97 eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')])
98
99 def test_glob_directory_names(self):
100 eq = self.assertSequencesEqual_noorder
101 np = lambda *f: norm(self.tempdir, *f)
102 eq(self.glob('*', 'D'), [self.norm('a', 'D')])
103 eq(self.glob('*', '*a'), [])
104 eq(self.glob('a', '*', '*', '*a'),
105 [self.norm('a', 'bcd', 'efg', 'ha')])
106 eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'),
107 os.path.join('aab', 'F')]))
108
Fred Drake2e2be372001-09-20 21:33:42 +0000109
110def test_main():
111 run_unittest(GlobTests)
112
113
114if __name__ == "__main__":
115 test_main()