aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2023-05-30 21:18:09 +0100
committerGitHub <noreply@github.com>2023-05-30 20:18:09 +0000
commit49f90ba1eae56708b1894441418c13ad8e8ea9a8 (patch)
tree84113e0493e1d74e6f6dce582e827dc8a155540b /Lib/test/test_pathlib.py
parent4c770617c0feae18ce3b05e0c8acd0910acc7082 (diff)
downloadcpython-49f90ba1eae56708b1894441418c13ad8e8ea9a8.tar.gz
cpython-49f90ba1eae56708b1894441418c13ad8e8ea9a8.zip
GH-73435: Implement recursive wildcards in `pathlib.PurePath.match()` (#101398)
`PurePath.match()` now handles the `**` wildcard as in `Path.glob()`, i.e. it matches any number of path segments. We now compile a `re.Pattern` object for the entire pattern. This is made more difficult by `fnmatch` not treating directory separators as special when evaluating wildcards (`*`, `?`, etc), and so we arrange the path parts onto separate *lines* in a string, and ensure we don't set `re.DOTALL`. Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib/test/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 4391d685d3c..076ace3d930 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -310,8 +310,30 @@ class _BasePurePathTest(object):
self.assertFalse(P('/ab.py').match('/a/*.py'))
self.assertFalse(P('/a/b/c.py').match('/a/*.py'))
# Multi-part glob-style pattern.
- self.assertFalse(P('/a/b/c.py').match('/**/*.py'))
+ self.assertTrue(P('a').match('**'))
+ self.assertTrue(P('c.py').match('**'))
+ self.assertTrue(P('a/b/c.py').match('**'))
+ self.assertTrue(P('/a/b/c.py').match('**'))
+ self.assertTrue(P('/a/b/c.py').match('/**'))
+ self.assertTrue(P('/a/b/c.py').match('**/'))
+ self.assertTrue(P('/a/b/c.py').match('/a/**'))
+ self.assertTrue(P('/a/b/c.py').match('**/*.py'))
+ self.assertTrue(P('/a/b/c.py').match('/**/*.py'))
self.assertTrue(P('/a/b/c.py').match('/a/**/*.py'))
+ self.assertTrue(P('/a/b/c.py').match('/a/b/**/*.py'))
+ self.assertTrue(P('/a/b/c.py').match('/**/**/**/**/*.py'))
+ self.assertFalse(P('c.py').match('**/a.py'))
+ self.assertFalse(P('c.py').match('c/**'))
+ self.assertFalse(P('a/b/c.py').match('**/a'))
+ self.assertFalse(P('a/b/c.py').match('**/a/b'))
+ self.assertFalse(P('a/b/c.py').match('**/a/b/c'))
+ self.assertFalse(P('a/b/c.py').match('**/a/b/c.'))
+ self.assertFalse(P('a/b/c.py').match('**/a/b/c./**'))
+ self.assertFalse(P('a/b/c.py').match('**/a/b/c./**'))
+ self.assertFalse(P('a/b/c.py').match('/a/b/c.py/**'))
+ self.assertFalse(P('a/b/c.py').match('/**/a/b/c.py'))
+ self.assertRaises(ValueError, P('a').match, '**a/b/c')
+ self.assertRaises(ValueError, P('a').match, 'a/b/c**')
# Case-sensitive flag
self.assertFalse(P('A.py').match('a.PY', case_sensitive=True))
self.assertTrue(P('A.py').match('a.PY', case_sensitive=False))