test_glob.py 6.69 KB
Newer Older
1 2
import glob
import os
3
import shutil
4
import sys
5 6
import unittest

7 8
from test.support import (run_unittest, TESTFN, skip_unless_symlink,
                          can_symlink, create_empty_file)
9

10

11 12 13 14 15 16
class GlobTests(unittest.TestCase):

    def norm(self, *parts):
        return os.path.normpath(os.path.join(self.tempdir, *parts))

    def mktemp(self, *parts):
17 18 19 20
        filename = self.norm(*parts)
        base, file = os.path.split(filename)
        if not os.path.exists(base):
            os.makedirs(base)
21
        create_empty_file(filename)
22 23

    def setUp(self):
24
        self.tempdir = TESTFN + "_dir"
25 26
        self.mktemp('a', 'D')
        self.mktemp('aab', 'F')
27 28
        self.mktemp('.aa', 'G')
        self.mktemp('.bb', 'H')
29 30 31 32
        self.mktemp('aaa', 'zzzF')
        self.mktemp('ZZZ')
        self.mktemp('a', 'bcd', 'EF')
        self.mktemp('a', 'bcd', 'efg', 'ha')
33
        if can_symlink():
34
            os.symlink(self.norm('broken'), self.norm('sym1'))
35 36
            os.symlink('broken', self.norm('sym2'))
            os.symlink(os.path.join('a', 'bcd'), self.norm('sym3'))
37 38

    def tearDown(self):
39
        shutil.rmtree(self.tempdir)
40 41 42 43 44 45 46

    def glob(self, *parts):
        if len(parts) == 1:
            pattern = parts[0]
        else:
            pattern = os.path.join(*parts)
        p = os.path.join(self.tempdir, pattern)
Johannes Gijsbers's avatar
Johannes Gijsbers committed
47 48
        res = glob.glob(p)
        self.assertEqual(list(glob.iglob(p)), res)
49 50 51
        bres = [os.fsencode(x) for x in res]
        self.assertEqual(glob.glob(os.fsencode(p)), bres)
        self.assertEqual(list(glob.iglob(os.fsencode(p))), bres)
Johannes Gijsbers's avatar
Johannes Gijsbers committed
52
        return res
53 54

    def assertSequencesEqual_noorder(self, l1, l2):
55 56
        l1 = list(l1)
        l2 = list(l2)
57
        self.assertEqual(set(l1), set(l2))
58
        self.assertEqual(sorted(l1), sorted(l2))
59 60 61 62 63 64 65 66

    def test_glob_literal(self):
        eq = self.assertSequencesEqual_noorder
        eq(self.glob('a'), [self.norm('a')])
        eq(self.glob('a', 'D'), [self.norm('a', 'D')])
        eq(self.glob('aab'), [self.norm('aab')])
        eq(self.glob('zymurgy'), [])

67 68 69 70 71 72 73 74 75
        res = glob.glob('*')
        self.assertEqual({type(r) for r in res}, {str})
        res = glob.glob(os.path.join(os.curdir, '*'))
        self.assertEqual({type(r) for r in res}, {str})

        res = glob.glob(b'*')
        self.assertEqual({type(r) for r in res}, {bytes})
        res = glob.glob(os.path.join(os.fsencode(os.curdir), b'*'))
        self.assertEqual({type(r) for r in res}, {bytes})
76

77 78 79 80
    def test_glob_one_directory(self):
        eq = self.assertSequencesEqual_noorder
        eq(self.glob('a*'), map(self.norm, ['a', 'aab', 'aaa']))
        eq(self.glob('*a'), map(self.norm, ['a', 'aaa']))
81 82
        eq(self.glob('.*'), map(self.norm, ['.aa', '.bb']))
        eq(self.glob('?aa'), map(self.norm, ['aaa']))
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        eq(self.glob('aa?'), map(self.norm, ['aaa', 'aab']))
        eq(self.glob('aa[ab]'), map(self.norm, ['aaa', 'aab']))
        eq(self.glob('*q'), [])

    def test_glob_nested_directory(self):
        eq = self.assertSequencesEqual_noorder
        if os.path.normcase("abCD") == "abCD":
            # case-sensitive filesystem
            eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF')])
        else:
            # case insensitive filesystem
            eq(self.glob('a', 'bcd', 'E*'), [self.norm('a', 'bcd', 'EF'),
                                             self.norm('a', 'bcd', 'efg')])
        eq(self.glob('a', 'bcd', '*g'), [self.norm('a', 'bcd', 'efg')])

    def test_glob_directory_names(self):
        eq = self.assertSequencesEqual_noorder
        eq(self.glob('*', 'D'), [self.norm('a', 'D')])
        eq(self.glob('*', '*a'), [])
        eq(self.glob('a', '*', '*', '*a'),
           [self.norm('a', 'bcd', 'efg', 'ha')])
104 105
        eq(self.glob('?a?', '*F'), [self.norm('aaa', 'zzzF'),
                                    self.norm('aab', 'F')])
106

107
    def test_glob_directory_with_trailing_slash(self):
108
        # Patterns ending with a slash shouldn't match non-dirs
109
        res = glob.glob(self.norm('Z*Z') + os.sep)
110
        self.assertEqual(res, [])
111
        res = glob.glob(self.norm('ZZZ') + os.sep)
112
        self.assertEqual(res, [])
113
        # When there is a wildcard pattern which ends with os.sep, glob()
114
        # doesn't blow up.
115
        res = glob.glob(self.norm('aa*') + os.sep)
116
        self.assertEqual(len(res), 2)
117
        # either of these results is reasonable
118 119 120 121 122 123 124 125
        self.assertIn(set(res), [
                      {self.norm('aaa'), self.norm('aab')},
                      {self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
                      ])

    def test_glob_bytes_directory_with_trailing_slash(self):
        # Same as test_glob_directory_with_trailing_slash, but with a
        # bytes argument.
126
        res = glob.glob(os.fsencode(self.norm('Z*Z') + os.sep))
127
        self.assertEqual(res, [])
128
        res = glob.glob(os.fsencode(self.norm('ZZZ') + os.sep))
129
        self.assertEqual(res, [])
130
        res = glob.glob(os.fsencode(self.norm('aa*') + os.sep))
131
        self.assertEqual(len(res), 2)
132 133 134 135 136 137
        # either of these results is reasonable
        self.assertIn(set(res), [
                      {os.fsencode(self.norm('aaa')),
                       os.fsencode(self.norm('aab'))},
                      {os.fsencode(self.norm('aaa') + os.sep),
                       os.fsencode(self.norm('aab') + os.sep)},
138
                      ])
139

140 141 142 143 144 145 146 147 148 149 150 151
    @skip_unless_symlink
    def test_glob_symlinks(self):
        eq = self.assertSequencesEqual_noorder
        eq(self.glob('sym3'), [self.norm('sym3')])
        eq(self.glob('sym3', '*'), [self.norm('sym3', 'EF'),
                                    self.norm('sym3', 'efg')])
        self.assertIn(self.glob('sym3' + os.sep),
                      [[self.norm('sym3')], [self.norm('sym3') + os.sep]])
        eq(self.glob('*', '*F'),
           [self.norm('aaa', 'zzzF'),
            self.norm('aab', 'F'), self.norm('sym3', 'EF')])

152
    @skip_unless_symlink
153
    def test_glob_broken_symlinks(self):
154
        eq = self.assertSequencesEqual_noorder
155 156
        eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2'),
                               self.norm('sym3')])
157 158
        eq(self.glob('sym1'), [self.norm('sym1')])
        eq(self.glob('sym2'), [self.norm('sym2')])
159

160 161 162 163 164 165 166 167 168 169 170 171
    @unittest.skipUnless(sys.platform == "win32", "Win32 specific test")
    def test_glob_magic_in_drive(self):
        eq = self.assertSequencesEqual_noorder
        eq(glob.glob('*:'), [])
        eq(glob.glob(b'*:'), [])
        eq(glob.glob('?:'), [])
        eq(glob.glob(b'?:'), [])
        eq(glob.glob('\\\\?\\c:\\'), ['\\\\?\\c:\\'])
        eq(glob.glob(b'\\\\?\\c:\\'), [b'\\\\?\\c:\\'])
        eq(glob.glob('\\\\*\\*\\'), [])
        eq(glob.glob(b'\\\\*\\*\\'), [])

172 173 174 175 176 177 178

def test_main():
    run_unittest(GlobTests)


if __name__ == "__main__":
    test_main()