Skip to content

Commit bbb7ad6

Browse files
committed
Fix result of os.walk with a path-like top directory
- used to return a Path object for the top dir instead of a string - fixes #915
1 parent d48dc21 commit bbb7ad6

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ The released versions correspond to PyPI releases.
77
* fixed a problem with patching `_io` under Python 3.12 (see [#910](../../issues/910))
88
* fixed a problem with accessing the temp path if emulating Linux under Windows
99
(see [#912](../../issues/912))
10+
* fixed result of `os.walk` with a path-like top directory
11+
(see [#915](../../issues/915))
1012

1113
## [Version 5.3.1](https://pypi.python.org/pypi/pyfakefs/5.3.0) (2023-11-15)
1214
Mostly a bugfix release.

pyfakefs/fake_scandir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def do_walk(top_dir, top_most=False):
261261
if not topdown:
262262
yield top_contents
263263

264-
return do_walk(to_string(top), top_most=True)
264+
return do_walk(make_string_path(to_string(top)), top_most=True)
265265

266266

267267
class FakeScanDirModule:

pyfakefs/tests/fake_pathlib_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,18 @@ def test_owner_and_group_windows(self):
11391139
with self.assertRaises(NotImplementedError):
11401140
self.path(path).group()
11411141

1142+
def test_walk(self):
1143+
"""Regression test for #915 - walk results shall be strings."""
1144+
base_dir = self.make_path("foo")
1145+
base_path = self.path(base_dir)
1146+
self.create_dir(base_path)
1147+
self.create_file(base_path / "1.txt")
1148+
self.create_file(base_path / "bar" / "2.txt")
1149+
result = list(step for step in self.os.walk(base_path))
1150+
assert len(result) == 2
1151+
assert result[0] == (base_dir, ["bar"], ["1.txt"])
1152+
assert result[1] == (self.os.path.join(base_dir, "bar"), [], ["2.txt"])
1153+
11421154

11431155
class RealPathlibUsageInOsFunctionsTest(FakePathlibUsageInOsFunctionsTest):
11441156
def use_real_fs(self):

0 commit comments

Comments
 (0)