diff --git a/CHANGELOG.md b/CHANGELOG.md index ef16734e..a7679990 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). Closes [#459](https://github.com/PyFilesystem/pyfilesystem2/issues/459). ### Fixed +- Recursive glob patterns such as `folder/**/*.py` only match + descendants of `folder`. - Elaborated documentation of `filter_dirs` and `exclude_dirs` in `fs.walk.Walker`. Closes [#371](https://github.com/PyFilesystem/pyfilesystem2/issues/371). - Fixes a backward incompatibility where `fs.move.move_file` raises `DestinationExists` diff --git a/fs/glob.py b/fs/glob.py index 4e783652..9ff9f83f 100644 --- a/fs/glob.py +++ b/fs/glob.py @@ -124,7 +124,11 @@ def _translate_glob(pattern): recursive = False re_patterns = [""] for component in iteratepath(pattern): - if "**" in component: + if component == "**": + recursive = True + # Consuming path text requires a new directory separator. + re_patterns.append("(?:/.*)?") + elif "**" in component: recursive = True split = component.split("**") split_re = [_translate(s) for s in split] diff --git a/tests/test_glob.py b/tests/test_glob.py index 9a5d8827..ab7bce50 100644 --- a/tests/test_glob.py +++ b/tests/test_glob.py @@ -66,6 +66,23 @@ def test_count_recurse_dir(self): counts = globber("**/*.py").count() self.assertEqual(counts, glob.Counts(files=5, directories=0, data=43)) + def test_recursive_glob_matches_complete_directory_names(self): + self.fs.makedirs("my_folder/subdir") + self.fs.touch("my_folder/direct.txt") + self.fs.touch("my_folder/subdir/nested.txt") + self.fs.makedir("my_folder_extra") + self.fs.touch("my_folder_extra/unrelated.txt") + + matches = sorted( + match.path + for match in self.fs.glob("my_folder/**/*") + if not match.info.is_dir + ) + + self.assertEqual( + matches, ["/my_folder/direct.txt", "/my_folder/subdir/nested.txt"] + ) + def test_count_lines(self): globber = glob.BoundGlobber(self.fs) line_counts = globber("**/*.py").count_lines()