Commit 33138df3 authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #16696: fix comparison between bytes and string. Also, improve glob tests.

parents 3c331bb7 5461558d
...@@ -63,7 +63,7 @@ def glob1(dirname, pattern): ...@@ -63,7 +63,7 @@ def glob1(dirname, pattern):
return fnmatch.filter(names, pattern) return fnmatch.filter(names, pattern)
def glob0(dirname, basename): def glob0(dirname, basename):
if basename == '': if not basename:
# `os.path.split()` returns an empty basename for paths ending with a # `os.path.split()` returns an empty basename for paths ending with a
# directory separator. 'q*x/' should match only directories. # directory separator. 'q*x/' should match only directories.
if os.path.isdir(dirname): if os.path.isdir(dirname):
......
...@@ -97,12 +97,35 @@ class GlobTests(unittest.TestCase): ...@@ -97,12 +97,35 @@ class GlobTests(unittest.TestCase):
os.path.join('aab', 'F')])) os.path.join('aab', 'F')]))
def test_glob_directory_with_trailing_slash(self): def test_glob_directory_with_trailing_slash(self):
# We are verifying that when there is wildcard pattern which # Patterns ending with a slash shouldn't match non-dirs
# ends with os.sep doesn't blow up. res = glob.glob(os.path.join(self.tempdir, 'Z*Z') + os.sep)
res = glob.glob(self.tempdir + '*' + os.sep) self.assertEqual(res, [])
self.assertEqual(len(res), 1) res = glob.glob(os.path.join(self.tempdir, 'ZZZ') + os.sep)
self.assertEqual(res, [])
# When there is wildcard pattern which ends with os.sep, glob()
# doesn't blow up.
res = glob.glob(os.path.join(self.tempdir, 'aa*') + os.sep)
self.assertEqual(len(res), 2)
# either of these results are reasonable # either of these results are reasonable
self.assertIn(res[0], [self.tempdir, self.tempdir + os.sep]) 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.
res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'Z*Z') + os.sep))
self.assertEqual(res, [])
res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'ZZZ') + os.sep))
self.assertEqual(res, [])
res = glob.glob(os.fsencode(os.path.join(self.tempdir, 'aa*') + os.sep))
self.assertEqual(len(res), 2)
# either of these results are reasonable
self.assertIn({os.fsdecode(x) for x in res}, [
{self.norm('aaa'), self.norm('aab')},
{self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
])
@skip_unless_symlink @skip_unless_symlink
def test_glob_broken_symlinks(self): def test_glob_broken_symlinks(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment