https://github.com/python/cpython/commit/676ee57bd6f177556a696136d3bae3fb136cfd01
commit: 676ee57bd6f177556a696136d3bae3fb136cfd01
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-02-04T14:42:14Z
summary:

[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 8b5c8508c7f5d98b09792e159ef2396c73da68cd)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
M Lib/test/test_glob.py

diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py
index da3d6e2a9a4fe9..81e6304fbc52ed 100644
--- a/Lib/test/test_glob.py
+++ b/Lib/test/test_glob.py
@@ -167,37 +167,45 @@ def test_glob_directory_names(self):
                                     self.norm('aab', 'F')])
 
     def test_glob_directory_with_trailing_slash(self):
-        # 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},
-                      ])
+        seps = (os.sep, os.altsep) if os.altsep else (os.sep,)
+        for sep in seps:
+            # Patterns ending with a slash shouldn't match non-dirs
+            self.assertEqual(glob.glob(self.norm('Z*Z') + sep), [])
+            self.assertEqual(glob.glob(self.norm('ZZZ') + sep), [])
+            self.assertEqual(glob.glob(self.norm('aaa') + sep),
+                             [self.norm('aaa') + sep])
+            # Preserving the redundant separators is an implementation detail.
+            self.assertEqual(glob.glob(self.norm('aaa') + sep*2),
+                             [self.norm('aaa') + sep*2])
+            # When there is a wildcard pattern which ends with a pathname
+            # separator, glob() doesn't blow.
+            # The result should end with the pathname separator.
+            # Normalizing the trailing separator is an implementation detail.
+            eq = self.assertSequencesEqual_noorder
+            eq(glob.glob(self.norm('aa*') + sep),
+               [self.norm('aaa') + os.sep, self.norm('aab') + os.sep])
+            # Stripping the redundant separators is an implementation detail.
+            eq(glob.glob(self.norm('aa*') + sep*2),
+               [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(self.norm('Z*Z') + os.sep))
-        self.assertEqual(res, [])
-        res = glob.glob(os.fsencode(self.norm('ZZZ') + os.sep))
-        self.assertEqual(res, [])
-        res = glob.glob(os.fsencode(self.norm('aa*') + os.sep))
-        self.assertEqual(len(res), 2)
-        # 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)},
-                      ])
+        seps = (os.sep, os.altsep) if os.altsep else (os.sep,)
+        for sep in seps:
+            self.assertEqual(glob.glob(os.fsencode(self.norm('Z*Z') + sep)), 
[])
+            self.assertEqual(glob.glob(os.fsencode(self.norm('ZZZ') + sep)), 
[])
+            self.assertEqual(glob.glob(os.fsencode(self.norm('aaa') + sep)),
+               [os.fsencode(self.norm('aaa') + sep)])
+            self.assertEqual(glob.glob(os.fsencode(self.norm('aaa') + sep*2)),
+               [os.fsencode(self.norm('aaa') + sep*2)])
+            eq = self.assertSequencesEqual_noorder
+            eq(glob.glob(os.fsencode(self.norm('aa*') + sep)),
+               [os.fsencode(self.norm('aaa') + os.sep),
+                os.fsencode(self.norm('aab') + os.sep)])
+            eq(glob.glob(os.fsencode(self.norm('aa*') + sep*2)),
+               [os.fsencode(self.norm('aaa') + os.sep),
+                os.fsencode(self.norm('aab') + os.sep)])
 
     @skip_unless_symlink
     def test_glob_symlinks(self):
@@ -205,8 +213,7 @@ def test_glob_symlinks(self):
         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('sym3' + os.sep), [self.norm('sym3') + os.sep])
         eq(self.glob('*', '*F'),
            [self.norm('aaa', 'zzzF'),
             self.norm('aab', 'F'), self.norm('sym3', 'EF')])

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to