Skip to content

Commit

Permalink
Fix result of os.walk with a path-like top directory
Browse files Browse the repository at this point in the history
- used to return a Path object for the top dir
  instead of a string
- fixes #915
  • Loading branch information
mrbean-bremen committed Nov 28, 2023
1 parent d48dc21 commit bbb7ad6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The released versions correspond to PyPI releases.
* fixed a problem with patching `_io` under Python 3.12 (see [#910](../../issues/910))
* fixed a problem with accessing the temp path if emulating Linux under Windows
(see [#912](../../issues/912))
* fixed result of `os.walk` with a path-like top directory
(see [#915](../../issues/915))

## [Version 5.3.1](https://pypi.python.org/pypi/pyfakefs/5.3.0) (2023-11-15)
Mostly a bugfix release.
Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/fake_scandir.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def do_walk(top_dir, top_most=False):
if not topdown:
yield top_contents

return do_walk(to_string(top), top_most=True)
return do_walk(make_string_path(to_string(top)), top_most=True)


class FakeScanDirModule:
Expand Down
12 changes: 12 additions & 0 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,18 @@ def test_owner_and_group_windows(self):
with self.assertRaises(NotImplementedError):
self.path(path).group()

def test_walk(self):
"""Regression test for #915 - walk results shall be strings."""
base_dir = self.make_path("foo")
base_path = self.path(base_dir)
self.create_dir(base_path)
self.create_file(base_path / "1.txt")
self.create_file(base_path / "bar" / "2.txt")
result = list(step for step in self.os.walk(base_path))
assert len(result) == 2
assert result[0] == (base_dir, ["bar"], ["1.txt"])
assert result[1] == (self.os.path.join(base_dir, "bar"), [], ["2.txt"])


class RealPathlibUsageInOsFunctionsTest(FakePathlibUsageInOsFunctionsTest):
def use_real_fs(self):
Expand Down

0 comments on commit bbb7ad6

Please sign in to comment.