Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ The released versions correspond to PyPI releases.
* fixed `os.rename` failing for the root user in a directory owned by
another user
(see [#1341](https://github.com/pytest-dev/pyfakefs/issues/1341))
* fixed `os.path.basename`, `os.path.splitext` and `os.path.commonpath`
not using the path semantics of the simulated file system
(see [#1348](https://github.com/pytest-dev/pyfakefs/issues/1348))

## [Version 6.2.0](https://pypi.python.org/pypi/pyfakefs/6.2.0) (2026-04-12)

Expand Down
21 changes: 21 additions & 0 deletions pyfakefs/fake_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import errno
import functools
import inspect
import ntpath
import os
import posixpath
import sys
from collections.abc import Callable
from stat import (
Expand Down Expand Up @@ -79,6 +81,8 @@ def dir() -> list[str]:
"""
dir_list = [
"abspath",
"basename",
"commonpath",
"dirname",
"exists",
"expanduser",
Expand All @@ -99,6 +103,7 @@ def dir() -> list[str]:
"relpath",
"split",
"splitdrive",
"splitext",
"samefile",
]
if sys.version_info >= (3, 12):
Expand Down Expand Up @@ -316,6 +321,22 @@ def splitdrive(self, path: AnyStr) -> tuple[AnyStr, AnyStr]:
supported."""
return self.filesystem.splitdrive(path)

def _path_module(self) -> Any:
"""Return the path module matching the simulated file system."""
return ntpath if self.filesystem.is_windows_fs else posixpath

def basename(self, path: AnyStr) -> AnyStr:
"""Returns the final component of a pathname."""
return self._path_module().basename(path)

def splitext(self, path: AnyStr) -> tuple[AnyStr, AnyStr]:
"""Split the path into the file name and the file extension."""
return self._path_module().splitext(path)

def commonpath(self, paths: list[AnyStr]) -> AnyStr:
"""Returns the longest common sub-path of the given paths."""
return self._path_module().commonpath(paths)

def normpath(self, path: AnyStr) -> AnyStr:
"""Normalize path, eliminating double slashes, etc."""
return self.filesystem.normpath(path)
Expand Down
34 changes: 34 additions & 0 deletions pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,40 @@ def test_dirname(self):
dirname = "foo!bar"
self.assertEqual(dirname, self.path.dirname(f"{dirname}!baz"))

def test_basename_windows(self):
# regression test for #1348: backslashes are path separators
self.filesystem.is_windows_fs = True
self.assertEqual("t.x", self.path.basename(r"C:\a/foo\t.x"))
self.assertEqual("t.x", self.path.basename(r"C:\a\foo\t.x"))
self.assertEqual(b"t.x", self.path.basename(rb"C:\a\foo\t.x"))

def test_basename_posix(self):
self.filesystem.is_windows_fs = False
self.assertEqual(r"foo\t.x", self.path.basename(r"C:\a/foo\t.x"))
self.assertEqual(r"C:\a\foo\t.x", self.path.basename(r"C:\a\foo\t.x"))

def test_splitext_windows(self):
# regression test for #1348: a dot after a separator is not an extension
self.filesystem.is_windows_fs = True
self.assertEqual((r"C:\a.b\foo", ""), self.path.splitext(r"C:\a.b\foo"))
self.assertEqual((r"C:\a\foo", ".txt"), self.path.splitext(r"C:\a\foo.txt"))
self.assertEqual((rb"C:\a.b\foo", b""), self.path.splitext(rb"C:\a.b\foo"))

def test_splitext_posix(self):
self.filesystem.is_windows_fs = False
self.assertEqual((r"C:\a", r".b\foo"), self.path.splitext(r"C:\a.b\foo"))

def test_commonpath_windows(self):
# regression test for #1348: mixed separators in the same drive
self.filesystem.is_windows_fs = True
self.assertEqual(r"C:\a", self.path.commonpath([r"C:\a\b", r"C:/a\c"]))
self.assertEqual(r"C:\a", self.path.commonpath([r"C:\a\b", r"C:\a\c"]))
self.assertEqual(rb"C:\a", self.path.commonpath([rb"C:\a\b", rb"C:\a\c"]))

def test_commonpath_posix(self):
self.filesystem.is_windows_fs = False
self.assertEqual("", self.path.commonpath([r"C:\a\b", r"C:/a\c"]))

def test_join_strings(self):
components = ["foo", "bar", "baz"]
self.assertEqual("foo!bar!baz", self.path.join(*components))
Expand Down
Loading