2001-07-23 13:08:36 -03:00
|
|
|
import glob
|
|
|
|
import os
|
2005-01-08 09:28:54 -04:00
|
|
|
import shutil
|
2012-12-16 08:55:47 -04:00
|
|
|
import sys
|
2012-12-27 05:24:21 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from test.test_support import run_unittest, TESTFN
|
|
|
|
|
|
|
|
|
|
|
|
def fsdecode(s):
|
|
|
|
return unicode(s, sys.getfilesystemencoding())
|
2001-07-23 13:08:36 -03:00
|
|
|
|
2012-12-16 07:30:57 -04:00
|
|
|
|
2001-07-23 13:08:36 -03:00
|
|
|
class GlobTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def norm(self, *parts):
|
|
|
|
return os.path.normpath(os.path.join(self.tempdir, *parts))
|
|
|
|
|
|
|
|
def mktemp(self, *parts):
|
2005-01-08 09:28:54 -04:00
|
|
|
filename = self.norm(*parts)
|
|
|
|
base, file = os.path.split(filename)
|
|
|
|
if not os.path.exists(base):
|
|
|
|
os.makedirs(base)
|
|
|
|
f = open(filename, 'w')
|
|
|
|
f.close()
|
2001-07-23 13:08:36 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2012-12-16 07:30:57 -04:00
|
|
|
self.tempdir = TESTFN + "_dir"
|
2001-07-23 13:08:36 -03:00
|
|
|
self.mktemp('a', 'D')
|
|
|
|
self.mktemp('aab', 'F')
|
2012-12-16 07:30:57 -04:00
|
|
|
self.mktemp('.aa', 'G')
|
|
|
|
self.mktemp('.bb', 'H')
|
2001-07-23 13:08:36 -03:00
|
|
|
self.mktemp('aaa', 'zzzF')
|
|
|
|
self.mktemp('ZZZ')
|
|
|
|
self.mktemp('a', 'bcd', 'EF')
|
|
|
|
self.mktemp('a', 'bcd', 'efg', 'ha')
|
2004-08-30 07:19:56 -03:00
|
|
|
if hasattr(os, 'symlink'):
|
|
|
|
os.symlink(self.norm('broken'), self.norm('sym1'))
|
2012-12-27 05:24:21 -04:00
|
|
|
os.symlink('broken', self.norm('sym2'))
|
|
|
|
os.symlink(os.path.join('a', 'bcd'), self.norm('sym3'))
|
2001-07-23 13:08:36 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
2005-01-08 09:28:54 -04:00
|
|
|
shutil.rmtree(self.tempdir)
|
2001-07-23 13:08:36 -03:00
|
|
|
|
|
|
|
def glob(self, *parts):
|
|
|
|
if len(parts) == 1:
|
|
|
|
pattern = parts[0]
|
|
|
|
else:
|
|
|
|
pattern = os.path.join(*parts)
|
|
|
|
p = os.path.join(self.tempdir, pattern)
|
2005-01-08 09:13:19 -04:00
|
|
|
res = glob.glob(p)
|
2018-01-17 19:15:46 -04:00
|
|
|
self.assertItemsEqual(glob.iglob(p), res)
|
2012-12-27 05:24:21 -04:00
|
|
|
ures = [fsdecode(x) for x in res]
|
2018-01-17 19:15:46 -04:00
|
|
|
self.assertItemsEqual(glob.glob(fsdecode(p)), ures)
|
|
|
|
self.assertItemsEqual(glob.iglob(fsdecode(p)), ures)
|
2005-01-08 09:13:19 -04:00
|
|
|
return res
|
2001-07-23 13:08:36 -03:00
|
|
|
|
|
|
|
def assertSequencesEqual_noorder(self, l1, l2):
|
2012-12-27 05:24:21 -04:00
|
|
|
l1 = list(l1)
|
|
|
|
l2 = list(l2)
|
2003-11-16 12:17:49 -04:00
|
|
|
self.assertEqual(set(l1), set(l2))
|
2012-12-27 05:24:21 -04:00
|
|
|
self.assertEqual(sorted(l1), sorted(l2))
|
2001-07-23 13:08:36 -03:00
|
|
|
|
|
|
|
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'), [])
|
|
|
|
|
2012-12-27 05:24:21 -04:00
|
|
|
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})
|
|
|
|
|
2007-03-07 04:31:51 -04:00
|
|
|
# test return types are unicode, but only if os.listdir
|
|
|
|
# returns unicode filenames
|
2012-12-27 05:24:21 -04:00
|
|
|
tmp = os.listdir(fsdecode(os.curdir))
|
|
|
|
if {type(x) for x in tmp} == {unicode}:
|
|
|
|
res = glob.glob(u'*')
|
|
|
|
self.assertEqual({type(r) for r in res}, {unicode})
|
|
|
|
res = glob.glob(os.path.join(fsdecode(os.curdir), u'*'))
|
|
|
|
self.assertEqual({type(r) for r in res}, {unicode})
|
2007-03-07 04:31:51 -04:00
|
|
|
|
2001-07-23 13:08:36 -03:00
|
|
|
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']))
|
2012-12-16 07:30:57 -04:00
|
|
|
eq(self.glob('.*'), map(self.norm, ['.aa', '.bb']))
|
|
|
|
eq(self.glob('?aa'), map(self.norm, ['aaa']))
|
2001-07-23 13:08:36 -03:00
|
|
|
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')])
|
2012-12-27 05:24:21 -04:00
|
|
|
eq(self.glob('?a?', '*F'), [self.norm('aaa', 'zzzF'),
|
|
|
|
self.norm('aab', 'F')])
|
2001-07-23 13:08:36 -03:00
|
|
|
|
2006-04-09 00:35:43 -03:00
|
|
|
def test_glob_directory_with_trailing_slash(self):
|
2012-12-27 05:24:21 -04:00
|
|
|
# Patterns ending with a slash shouldn't match non-dirs
|
|
|
|
res = glob.glob(self.norm('Z*Z') + os.sep)
|
|
|
|
self.assertEqual(res, [])
|
|
|
|
res = glob.glob(self.norm('ZZZ') + os.sep)
|
|
|
|
self.assertEqual(res, [])
|
|
|
|
# When there is a wildcard pattern which ends with os.sep, glob()
|
|
|
|
# doesn't blow up.
|
|
|
|
res = glob.glob(self.norm('aa*') + os.sep)
|
|
|
|
self.assertEqual(len(res), 2)
|
|
|
|
# either of these results is reasonable
|
|
|
|
self.assertIn(set(res), [
|
|
|
|
{self.norm('aaa'), self.norm('aab')},
|
|
|
|
{self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_glob_unicode_directory_with_trailing_slash(self):
|
|
|
|
# Same as test_glob_directory_with_trailing_slash, but with an
|
|
|
|
# unicode argument.
|
|
|
|
res = glob.glob(fsdecode(self.norm('Z*Z') + os.sep))
|
|
|
|
self.assertEqual(res, [])
|
|
|
|
res = glob.glob(fsdecode(self.norm('ZZZ') + os.sep))
|
|
|
|
self.assertEqual(res, [])
|
|
|
|
res = glob.glob(fsdecode(self.norm('aa*') + os.sep))
|
|
|
|
self.assertEqual(len(res), 2)
|
|
|
|
# either of these results is reasonable
|
|
|
|
self.assertIn(set(res), [
|
|
|
|
{fsdecode(self.norm('aaa')), fsdecode(self.norm('aab'))},
|
|
|
|
{fsdecode(self.norm('aaa') + os.sep),
|
|
|
|
fsdecode(self.norm('aab') + os.sep)},
|
|
|
|
])
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'symlink'), "Requires symlink support")
|
|
|
|
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')])
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'symlink'), "Requires symlink support")
|
2004-08-30 07:19:56 -03:00
|
|
|
def test_glob_broken_symlinks(self):
|
2012-12-27 05:24:21 -04:00
|
|
|
eq = self.assertSequencesEqual_noorder
|
|
|
|
eq(self.glob('sym*'), [self.norm('sym1'), self.norm('sym2'),
|
|
|
|
self.norm('sym3')])
|
|
|
|
eq(self.glob('sym1'), [self.norm('sym1')])
|
|
|
|
eq(self.glob('sym2'), [self.norm('sym2')])
|
2004-08-30 07:19:56 -03:00
|
|
|
|
2012-12-16 08:55:47 -04:00
|
|
|
@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(u'*:'), [])
|
|
|
|
eq(glob.glob('?:'), [])
|
|
|
|
eq(glob.glob(u'?:'), [])
|
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(GlobTests)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|