PyXR

c:\python24\lib \ test \ test_glob.py



0001 import unittest
0002 from test.test_support import run_unittest, TESTFN
0003 import glob
0004 import os
0005 
0006 def mkdirs(fname):
0007     if os.path.exists(fname) or fname == '':
0008         return
0009     base, file = os.path.split(fname)
0010     mkdirs(base)
0011     os.mkdir(fname)
0012 
0013 def touchfile(fname):
0014     base, file = os.path.split(fname)
0015     mkdirs(base)
0016     f = open(fname, 'w')
0017     f.close()
0018 
0019 def deltree(fname):
0020     for f in os.listdir(fname):
0021         fullname = os.path.join(fname, f)
0022         if os.path.isdir(fullname):
0023             deltree(fullname)
0024         else:
0025             try:
0026                 os.unlink(fullname)
0027             except:
0028                 pass
0029     try:
0030         os.rmdir(fname)
0031     except:
0032         pass
0033 
0034 
0035 class GlobTests(unittest.TestCase):
0036 
0037     def norm(self, *parts):
0038         return os.path.normpath(os.path.join(self.tempdir, *parts))
0039 
0040     def mktemp(self, *parts):
0041         touchfile(self.norm(*parts))
0042 
0043     def setUp(self):
0044         self.tempdir = TESTFN+"_dir"
0045         self.mktemp('a', 'D')
0046         self.mktemp('aab', 'F')
0047         self.mktemp('aaa', 'zzzF')
0048         self.mktemp('ZZZ')
0049         self.mktemp('a', 'bcd', 'EF')
0050         self.mktemp('a', 'bcd', 'efg', 'ha')
0051         if hasattr(os, 'symlink'):
0052             os.symlink(self.norm('broken'), self.norm('sym1'))
0053             os.symlink(self.norm('broken'), self.norm('sym2'))
0054 
0055     def tearDown(self):
0056         deltree(self.tempdir)
0057 
0058     def glob(self, *parts):
0059         if len(parts) == 1:
0060             pattern = parts[0]
0061         else:
0062             pattern = os.path.join(*parts)
0063         p = os.path.join(self.tempdir, pattern)
0064         return glob.glob(p)
0065 
0066     def assertSequencesEqual_noorder(self, l1, l2):
0067         self.assertEqual(set(l1), set(l2))
0068 
0069     def test_glob_literal(self):
0070         eq = self.assertSequencesEqual_noorder
0071         eq(self.glob('a'), [self.norm('a')])
0072         eq(self.glob('a', 'D'), [self.norm('a', 'D')])
0073         eq(self.glob('aab'), [self.norm('aab')])
0074         eq(self.glob('zymurgy'), [])
0075 
0076     def test_glob_one_directory(self):
0077         eq = self.assertSequencesEqual_noorder
0078         eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa']))
0079         eq(self.glob('*a'), map(self.norm, ['a', 'aaa']))
0080         eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab']))
0081         eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab']))
0082         eq(self.glob('*q'), [])
0083 
0084     def test_glob_nested_directory(self):
0085         eq = self.assertSequencesEqual_noorder
0086         if os.path.normcase("abCD") == "abCD":
0087             # case-sensitive filesystem
0088             eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')])
0089         else:
0090             # case insensitive filesystem
0091             eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'),
0092                                              self.norm('a', 'bcd', 'efg')])
0093         eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')])
0094 
0095     def test_glob_directory_names(self):
0096         eq = self.assertSequencesEqual_noorder
0097         eq(self.glob('*', 'D'), [self.norm('a', 'D')])
0098         eq(self.glob('*', '*a'), [])
0099         eq(self.glob('a', '*', '*', '*a'),
0100            [self.norm('a', 'bcd', 'efg', 'ha')])
0101         eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'),
0102                                                    os.path.join('aab', 'F')]))
0103 
0104     def test_glob_broken_symlinks(self):
0105         if hasattr(os, 'symlink'):
0106             eq = self.assertSequencesEqual_noorder
0107             eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2')])
0108             eq(self.glob('sym1'), [self.norm('sym1')])
0109             eq(self.glob('sym2'), [self.norm('sym2')])
0110 
0111 
0112 def test_main():
0113     run_unittest(GlobTests)
0114 
0115 
0116 if __name__ == "__main__":
0117     test_main()
0118 

Generated by PyXR 0.9.4
SourceForge.net Logo