Skip to content

Commit ba4f217

Browse files
committed
Fix pathlib.Path.mkdir() with unmounted path
- automount drive or UNC path under Windows if needed in FakeFilesystem.makedir() (used by Path.mkdir) - fixes #890
1 parent 76c274f commit ba4f217

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The released versions correspond to PyPI releases.
66
### Fixes
77
* removed a leftover debug print statement (see [#869](../../issues/869))
88
* make sure tests work without HOME environment set (see [#870](../../issues/870))
9+
* automount drive or UNC path under Windows if needed for `pathlib.Path.mkdir()`
10+
(see [#890](../../issues/890))
911

1012
## [Version 5.2.3](https://pypi.python.org/pypi/pyfakefs/5.2.3) (2023-08-18)
1113
Fixes a rare problem on pytest shutdown.

pyfakefs/fake_filesystem.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2642,12 +2642,16 @@ def makedir(self, dir_path: AnyPath, mode: int = helpers.PERM_DEF) -> None:
26422642

26432643
if self.is_windows_fs:
26442644
dir_name = self.absnormpath(dir_name)
2645-
parent_dir, _ = self.splitpath(dir_name)
2645+
parent_dir, rest = self.splitpath(dir_name)
26462646
if parent_dir:
26472647
base_dir = self.normpath(parent_dir)
26482648
ellipsis = matching_string(parent_dir, self.path_separator + "..")
26492649
if parent_dir.endswith(ellipsis) and not self.is_windows_fs:
26502650
base_dir, dummy_dotdot, _ = parent_dir.partition(ellipsis)
2651+
if self.is_windows_fs and not rest and not self.exists(base_dir):
2652+
# under Windows, the parent dir may be a drive or UNC path
2653+
# which has to be mounted
2654+
self._auto_mount_drive_if_needed(parent_dir)
26512655
if not self.exists(base_dir):
26522656
self.raise_os_error(errno.ENOENT, base_dir)
26532657

pyfakefs/tests/fake_pathlib_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,18 @@ def test_mkdir_exist_ok(self):
706706
errno.EEXIST, self.path(file_name).mkdir, exist_ok=True
707707
)
708708

709+
@unittest.skipIf(not is_windows, "Windows specific behavior")
710+
def test_mkdir_with_automount_unc_path(self):
711+
self.skip_real_fs()
712+
self.path(r"\\test\unc\foo").mkdir(parents=True)
713+
self.assertTrue(self.path(r"\\test\unc\foo").exists())
714+
715+
@unittest.skipIf(not is_windows, "Windows specific behavior")
716+
def test_mkdir_with_automount_drive(self):
717+
self.skip_real_fs()
718+
self.path(r"d:\foo\bar").mkdir(parents=True)
719+
self.assertTrue(self.path(r"d:\foo\bar").exists())
720+
709721
def test_rmdir(self):
710722
dir_name = self.make_path("foo", "bar")
711723
self.create_dir(dir_name)

0 commit comments

Comments
 (0)