Skip to content

Commit

Permalink
Correctly handle alternative path separators in path resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed May 13, 2020
1 parent 7b377f3 commit 8127a82
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ The released versions correspond to PyPi releases.
(see [#526](../../issues/526))
* Make sure filesystem modules in `pathlib` are patched
(see [#527](../../issues/527))

* Make sure that alternative path separators are correctly handled under Windows
(see [#530](../../issues/530))

#### Infrastructure
* Make sure all temporary files from real fs tests are removed

Expand Down
23 changes: 11 additions & 12 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,13 @@ def _alternative_path_separator(self, path):
"""Return the alternative path separator as the same type as path"""
return self._matching_string(path, self.alternative_path_separator)

def _starts_with_sep(self, path):
"""Return True if path starts with a path separator."""
sep = self._path_separator(path)
altsep = self._alternative_path_separator(path)
return (path.startswith(sep) or altsep is not None and
path.startswith(altsep))

def add_mount_point(self, path, total_size=None):
"""Add a new mount point for a filesystem device.
The mount point gets a new unique device number.
Expand Down Expand Up @@ -1409,7 +1416,8 @@ def components_to_path():
path_components[len(normalized_components):])
sep = self._path_separator(path)
normalized_path = sep.join(normalized_components)
if path.startswith(sep) and not normalized_path.startswith(sep):
if (self._starts_with_sep(path) and not
self._starts_with_sep(normalized_path)):
normalized_path = sep + normalized_path
return normalized_path

Expand Down Expand Up @@ -3142,13 +3150,7 @@ def isabs(self, path):
if self.filesystem.is_windows_fs:
path = self.splitdrive(path)[1]
path = make_string_path(path)
sep = self.filesystem._path_separator(path)
altsep = self.filesystem._alternative_path_separator(path)
if self.filesystem.is_windows_fs:
return len(path) > 0 and path[:1] in (sep, altsep)
else:
return (path.startswith(sep) or
altsep is not None and path.startswith(altsep))
return self.filesystem._starts_with_sep(path)

def isdir(self, path):
"""Determine if path identifies a directory."""
Expand Down Expand Up @@ -3244,13 +3246,10 @@ def getcwd():
return self.os.getcwd()

path = make_string_path(path)
sep = self.filesystem._path_separator(path)
altsep = self.filesystem._alternative_path_separator(path)
if not self.isabs(path):
path = self.join(getcwd(), path)
elif (self.filesystem.is_windows_fs and
path.startswith(sep) or altsep is not None and
path.startswith(altsep)):
self.filesystem._starts_with_sep(path)):
cwd = getcwd()
if self.filesystem._starts_with_drive_letter(cwd):
path = self.join(cwd[:2], path)
Expand Down
9 changes: 9 additions & 0 deletions pyfakefs/pytest_tests/pytest_fixture_param_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# Example for a test using a custom pytest fixture with an argument to Patcher
# Needs Python >= 3.6
import os

import pytest

Expand Down Expand Up @@ -41,3 +42,11 @@ def check_that_example_file_is_in_fake_fs():
assert file.read() == 'stuff here'
assert example.EXAMPLE_FILE.read_text() == 'stuff here'
assert example.EXAMPLE_FILE.is_file()


def test_twice_chdir(fs):
# regression test for #530 - make sure that
# alternative path separators are correctly handled under Windows
fs.create_dir("/absolute/path/to/directory")
os.chdir("/absolute/path/to/directory")
os.chdir("/absolute/path/to/directory")

0 comments on commit 8127a82

Please sign in to comment.