2 from test
.test_support
import run_unittest
, TESTFN
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
):
17 f
= open(filename
, 'w')
21 self
.tempdir
= TESTFN
+"_dir"
23 self
.mktemp('aab', 'F')
24 self
.mktemp('aaa', 'zzzF')
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'))
33 shutil
.rmtree(self
.tempdir
)
35 def glob(self
, *parts
):
39 pattern
= os
.path
.join(*parts
)
40 p
= os
.path
.join(self
.tempdir
, pattern
)
42 self
.assertEqual(list(glob
.iglob(p
)), 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')])
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')])
92 run_unittest(GlobTests
)
95 if __name__
== "__main__":