Skip to content

Commit

Permalink
Only adapt the separators for the non-current fs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed May 5, 2024
1 parent 5c9083e commit f95140f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
27 changes: 21 additions & 6 deletions pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@ def init_module(filesystem):
# but points to the os-specific path module (posixpath/ntpath)
fake_posix_os = FakeOsModule(filesystem)
fake_posix_path = fake_posix_os.path
if filesystem.is_windows_fs:
# The POSIX path separators must be set if running under Windows
fake_posix_path.sep = "/"
fake_posix_path.altsep = None
fake_posix_path = fake_posix_os.path
FakePathlibModule.PurePosixPath._flavour = fake_posix_path
# The Windows path separators must be customized.
fake_nt_os = FakeOsModule(filesystem)
fake_nt_path = fake_nt_os.path
fake_nt_path.sep = "\\"
fake_nt_path.altsep = "/"
if not filesystem.is_windows_fs:
# The Windows path separators must be set if running under POSIX
fake_nt_path.sep = "\\"
fake_nt_path.altsep = "/"
FakePathlibModule.PureWindowsPath._flavour = fake_nt_path


Expand Down Expand Up @@ -399,10 +405,11 @@ class _FakeWindowsFlavour(_FakeFlavour):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Because this class is used with pure Windows paths,
# If this class is used with pure Windows paths under POSIX,
# the separators must be filesystem-independent.
self.sep = "\\"
self.altsep = "/"
if not self.filesystem.is_windows_fs:
self.sep = "\\"
self.altsep = "/"

def is_reserved(self, parts):
"""Return True if the path is considered reserved under Windows."""
Expand Down Expand Up @@ -480,6 +487,14 @@ class _FakePosixFlavour(_FakeFlavour):

pathmod = posixpath

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# If this class is used with pure POSIX paths under Windows,
# the separators must be filesystem-independent.
if self.filesystem.is_windows_fs:
self.sep = "/"
self.altsep = None

def is_reserved(self, parts):
return False

Expand Down
6 changes: 3 additions & 3 deletions pyfakefs/pytest_tests/pytest_reload_pandas_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

from pathlib import Path

import pandas as pd
import pytest

try:
import pandas as pd
import parquet
except ImportError:
parquet = None


@pytest.mark.skipif(parquet is None, reason="parquet not installed")
@pytest.mark.skipif(parquet is None, reason="pandas or parquet not installed")
def test_1(fs):
dir_ = Path(__file__).parent / "data"
fs.add_real_directory(dir_)
pd.read_parquet(dir_ / "test.parquet")


@pytest.mark.skipif(parquet is None, reason="parquet not installed")
@pytest.mark.skipif(parquet is None, reason="pandas or parquet not installed")
def test_2():
dir_ = Path(__file__).parent / "data"
pd.read_parquet(dir_ / "test.parquet")
11 changes: 10 additions & 1 deletion pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,12 +1296,21 @@ def test_windows_pure_path_parsing(self):
pathlib.PureWindowsPath(path).stem,
)

path = r"C:/Windows/cmd.exe"
path = "C:/Windows/cmd.exe"
self.assertEqual(
fake_pathlib.FakePathlibModule.PureWindowsPath(path).stem,
pathlib.PureWindowsPath(path).stem,
)

def test_posix_pure_path_parsing(self):
"""Verify faked pure POSIX paths use filesystem-independent separators."""

path = "/foo/bar"
self.assertEqual(
fake_pathlib.FakePathlibModule.PurePosixPath(path).stem,
pathlib.PurePosixPath(path).stem,
)


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit f95140f

Please sign in to comment.