Skip to content

Commit

Permalink
Improved handling of drive-like paths under Posix
Browse files Browse the repository at this point in the history
- if a Windows systems emulates a Posix system, it shall
  handle both the cases of a mapped-in real file, and a native
  file that has a drive-like name - this handling has been
  improved no lower chances of false drive detection
- see #558
  • Loading branch information
mrbean-bremen committed Oct 18, 2020
1 parent 981865a commit 8a43f53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1695,11 +1695,20 @@ def _starts_with_drive_letter(self, file_path):
the path starts with a drive letter.
"""
colon = matching_string(file_path, ':')
# we also allow a drive letter if only the real fs is Windows
# to allow for real path names
return ((self.is_windows_fs or os.name == 'nt') and
len(file_path) >= 2 and
file_path[:1].isalpha and (file_path[1:2]) == colon)
if (len(file_path) >= 2 and
file_path[:1].isalpha and file_path[1:2] == colon):
if self.is_windows_fs:
return True
if os.name == 'nt':
# special case if we are emulating Posix under Windows
# check if the path exists because it has been mapped in
# this is not foolproof, but handles most cases
try:
self.get_object_from_normpath(file_path)
return True
except OSError:
return False
return False

def _starts_with_root_path(self, file_path):
root_name = matching_string(file_path, self.root.name)
Expand Down
10 changes: 10 additions & 0 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,16 @@ def test_macos(self):
self.assertTrue(os.path.ismount('/'))
self.assertFalse(os.path.ismount('//share/foo'))

def test_drivelike_path(self):
self.fs.os = OSType.LINUX
folder = Path('/test')
file_path = folder / 'C:/testfile'
file_path.parent.mkdir(parents=True)
file_path.touch()
# use str() to be Python 3.5 compatible
os.chdir(str(folder))
self.assertTrue(os.path.exists(str(file_path.relative_to(folder))))


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

0 comments on commit 8a43f53

Please sign in to comment.