Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test.support import run_unittest, TESTFN |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 3 | import glob |
| 4 | import os |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 5 | import shutil |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 6 | |
| 7 | class GlobTests(unittest.TestCase): |
| 8 | |
| 9 | def norm(self, *parts): |
| 10 | return os.path.normpath(os.path.join(self.tempdir, *parts)) |
| 11 | |
| 12 | def mktemp(self, *parts): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 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() |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 19 | |
| 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') |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 28 | if hasattr(os, 'symlink'): |
| 29 | os.symlink(self.norm('broken'), self.norm('sym1')) |
| 30 | os.symlink(self.norm('broken'), self.norm('sym2')) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 31 | |
| 32 | def tearDown(self): |
Johannes Gijsbers | a3beee1 | 2005-01-08 13:28:54 +0000 | [diff] [blame] | 33 | shutil.rmtree(self.tempdir) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 34 | |
| 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) |
Johannes Gijsbers | 836f543 | 2005-01-08 13:13:19 +0000 | [diff] [blame] | 41 | res = glob.glob(p) |
| 42 | self.assertEqual(list(glob.iglob(p)), res) |
| 43 | return res |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 44 | |
| 45 | def assertSequencesEqual_noorder(self, l1, l2): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 46 | self.assertEqual(set(l1), set(l2)) |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 47 | |
| 48 | def test_glob_literal(self): |
| 49 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 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'), []) |
| 54 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | # test return types are unicode, but only if os.listdir |
| 56 | # returns unicode filenames |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 57 | uniset = set([str]) |
| 58 | tmp = os.listdir('.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 59 | if set(type(x) for x in tmp) == uniset: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 60 | u1 = glob.glob('*') |
| 61 | u2 = glob.glob('./*') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 62 | self.assertEquals(set(type(r) for r in u1), uniset) |
| 63 | self.assertEquals(set(type(r) for r in u2), uniset) |
| 64 | |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 65 | def test_glob_one_directory(self): |
| 66 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 67 | eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa'])) |
| 68 | eq(self.glob('*a'), map(self.norm, ['a', 'aaa'])) |
| 69 | eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab'])) |
| 70 | eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab'])) |
| 71 | eq(self.glob('*q'), []) |
| 72 | |
| 73 | def test_glob_nested_directory(self): |
| 74 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 75 | if os.path.normcase("abCD") == "abCD": |
| 76 | # case-sensitive filesystem |
| 77 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')]) |
| 78 | else: |
| 79 | # case insensitive filesystem |
| 80 | eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'), |
| 81 | self.norm('a', 'bcd', 'efg')]) |
| 82 | eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')]) |
| 83 | |
| 84 | def test_glob_directory_names(self): |
| 85 | eq = self.assertSequencesEqual_noorder |
Fred Drake | 99e87f9 | 2001-07-23 16:08:36 +0000 | [diff] [blame] | 86 | eq(self.glob('*', 'D'), [self.norm('a', 'D')]) |
| 87 | eq(self.glob('*', '*a'), []) |
| 88 | eq(self.glob('a', '*', '*', '*a'), |
| 89 | [self.norm('a', 'bcd', 'efg', 'ha')]) |
| 90 | eq(self.glob('?a?', '*F'), map(self.norm, [os.path.join('aaa', 'zzzF'), |
| 91 | os.path.join('aab', 'F')])) |
| 92 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | def test_glob_directory_with_trailing_slash(self): |
| 94 | # We are verifying that when there is wildcard pattern which |
| 95 | # ends with os.sep doesn't blow up. |
| 96 | res = glob.glob(self.tempdir + '*' + os.sep) |
| 97 | self.assertEqual(len(res), 1) |
| 98 | # either of these results are reasonable |
| 99 | self.assertTrue(res[0] in [self.tempdir, self.tempdir + os.sep]) |
| 100 | |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 101 | def test_glob_broken_symlinks(self): |
| 102 | if hasattr(os, 'symlink'): |
| 103 | eq = self.assertSequencesEqual_noorder |
| 104 | eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2')]) |
| 105 | eq(self.glob('sym1'), [self.norm('sym1')]) |
| 106 | eq(self.glob('sym2'), [self.norm('sym2')]) |
| 107 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 108 | |
| 109 | def test_main(): |
| 110 | run_unittest(GlobTests) |
| 111 | |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | test_main() |