Skip to content

Commit 676ee57

Browse files
[3.12] gh-129350: Make tests for glob with trailing slash more strict (GH-129376) (GH-129652)
Test that the trailing pathname separator is preserved. Multiple trailing pathname separators are only preserved if the pattern does not contain metacharacters, otherwise only one trailing pathname separator is preserved. This is rather an implementation detail. (cherry picked from commit 8b5c850) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 0d51b29 commit 676ee57

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

Lib/test/test_glob.py

+36-29
Original file line numberDiff line numberDiff line change
@@ -167,46 +167,53 @@ def test_glob_directory_names(self):
167167
self.norm('aab', 'F')])
168168

169169
def test_glob_directory_with_trailing_slash(self):
170-
# Patterns ending with a slash shouldn't match non-dirs
171-
res = glob.glob(self.norm('Z*Z') + os.sep)
172-
self.assertEqual(res, [])
173-
res = glob.glob(self.norm('ZZZ') + os.sep)
174-
self.assertEqual(res, [])
175-
# When there is a wildcard pattern which ends with os.sep, glob()
176-
# doesn't blow up.
177-
res = glob.glob(self.norm('aa*') + os.sep)
178-
self.assertEqual(len(res), 2)
179-
# either of these results is reasonable
180-
self.assertIn(set(res), [
181-
{self.norm('aaa'), self.norm('aab')},
182-
{self.norm('aaa') + os.sep, self.norm('aab') + os.sep},
183-
])
170+
seps = (os.sep, os.altsep) if os.altsep else (os.sep,)
171+
for sep in seps:
172+
# Patterns ending with a slash shouldn't match non-dirs
173+
self.assertEqual(glob.glob(self.norm('Z*Z') + sep), [])
174+
self.assertEqual(glob.glob(self.norm('ZZZ') + sep), [])
175+
self.assertEqual(glob.glob(self.norm('aaa') + sep),
176+
[self.norm('aaa') + sep])
177+
# Preserving the redundant separators is an implementation detail.
178+
self.assertEqual(glob.glob(self.norm('aaa') + sep*2),
179+
[self.norm('aaa') + sep*2])
180+
# When there is a wildcard pattern which ends with a pathname
181+
# separator, glob() doesn't blow.
182+
# The result should end with the pathname separator.
183+
# Normalizing the trailing separator is an implementation detail.
184+
eq = self.assertSequencesEqual_noorder
185+
eq(glob.glob(self.norm('aa*') + sep),
186+
[self.norm('aaa') + os.sep, self.norm('aab') + os.sep])
187+
# Stripping the redundant separators is an implementation detail.
188+
eq(glob.glob(self.norm('aa*') + sep*2),
189+
[self.norm('aaa') + os.sep, self.norm('aab') + os.sep])
184190

185191
def test_glob_bytes_directory_with_trailing_slash(self):
186192
# Same as test_glob_directory_with_trailing_slash, but with a
187193
# bytes argument.
188-
res = glob.glob(os.fsencode(self.norm('Z*Z') + os.sep))
189-
self.assertEqual(res, [])
190-
res = glob.glob(os.fsencode(self.norm('ZZZ') + os.sep))
191-
self.assertEqual(res, [])
192-
res = glob.glob(os.fsencode(self.norm('aa*') + os.sep))
193-
self.assertEqual(len(res), 2)
194-
# either of these results is reasonable
195-
self.assertIn(set(res), [
196-
{os.fsencode(self.norm('aaa')),
197-
os.fsencode(self.norm('aab'))},
198-
{os.fsencode(self.norm('aaa') + os.sep),
199-
os.fsencode(self.norm('aab') + os.sep)},
200-
])
194+
seps = (os.sep, os.altsep) if os.altsep else (os.sep,)
195+
for sep in seps:
196+
self.assertEqual(glob.glob(os.fsencode(self.norm('Z*Z') + sep)), [])
197+
self.assertEqual(glob.glob(os.fsencode(self.norm('ZZZ') + sep)), [])
198+
self.assertEqual(glob.glob(os.fsencode(self.norm('aaa') + sep)),
199+
[os.fsencode(self.norm('aaa') + sep)])
200+
self.assertEqual(glob.glob(os.fsencode(self.norm('aaa') + sep*2)),
201+
[os.fsencode(self.norm('aaa') + sep*2)])
202+
eq = self.assertSequencesEqual_noorder
203+
eq(glob.glob(os.fsencode(self.norm('aa*') + sep)),
204+
[os.fsencode(self.norm('aaa') + os.sep),
205+
os.fsencode(self.norm('aab') + os.sep)])
206+
eq(glob.glob(os.fsencode(self.norm('aa*') + sep*2)),
207+
[os.fsencode(self.norm('aaa') + os.sep),
208+
os.fsencode(self.norm('aab') + os.sep)])
201209

202210
@skip_unless_symlink
203211
def test_glob_symlinks(self):
204212
eq = self.assertSequencesEqual_noorder
205213
eq(self.glob('sym3'), [self.norm('sym3')])
206214
eq(self.glob('sym3', '*'), [self.norm('sym3', 'EF'),
207215
self.norm('sym3', 'efg')])
208-
self.assertIn(self.glob('sym3' + os.sep),
209-
[[self.norm('sym3')], [self.norm('sym3') + os.sep]])
216+
eq(self.glob('sym3' + os.sep), [self.norm('sym3') + os.sep])
210217
eq(self.glob('*', '*F'),
211218
[self.norm('aaa', 'zzzF'),
212219
self.norm('aab', 'F'), self.norm('sym3', 'EF')])

0 commit comments

Comments
 (0)