Skip to content

Commit 7935c0f

Browse files
[3.13] gh-129350: Make tests for glob with trailing slash more strict (GH-129376) (GH-129651)
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 4cf3e80 commit 7935c0f

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

Lib/test/test_glob.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -170,46 +170,53 @@ def test_glob_directory_names(self):
170170
self.norm('aab', 'F')])
171171

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

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

205213
@skip_unless_symlink
206214
def test_glob_symlinks(self):
207215
eq = self.assertSequencesEqual_noorder
208216
eq(self.glob('sym3'), [self.norm('sym3')])
209217
eq(self.glob('sym3', '*'), [self.norm('sym3', 'EF'),
210218
self.norm('sym3', 'efg')])
211-
self.assertIn(self.glob('sym3' + os.sep),
212-
[[self.norm('sym3')], [self.norm('sym3') + os.sep]])
219+
eq(self.glob('sym3' + os.sep), [self.norm('sym3') + os.sep])
213220
eq(self.glob('*', '*F'),
214221
[self.norm('aaa', 'zzzF'),
215222
self.norm('aab', 'F'), self.norm('sym3', 'EF')])

0 commit comments

Comments
 (0)