Skip to content

Commit 8a43f53

Browse files
committed
Improved handling of drive-like paths under Posix
- 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
1 parent 981865a commit 8a43f53

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pyfakefs/fake_filesystem.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,11 +1695,20 @@ def _starts_with_drive_letter(self, file_path):
16951695
the path starts with a drive letter.
16961696
"""
16971697
colon = matching_string(file_path, ':')
1698-
# we also allow a drive letter if only the real fs is Windows
1699-
# to allow for real path names
1700-
return ((self.is_windows_fs or os.name == 'nt') and
1701-
len(file_path) >= 2 and
1702-
file_path[:1].isalpha and (file_path[1:2]) == colon)
1698+
if (len(file_path) >= 2 and
1699+
file_path[:1].isalpha and file_path[1:2] == colon):
1700+
if self.is_windows_fs:
1701+
return True
1702+
if os.name == 'nt':
1703+
# special case if we are emulating Posix under Windows
1704+
# check if the path exists because it has been mapped in
1705+
# this is not foolproof, but handles most cases
1706+
try:
1707+
self.get_object_from_normpath(file_path)
1708+
return True
1709+
except OSError:
1710+
return False
1711+
return False
17031712

17041713
def _starts_with_root_path(self, file_path):
17051714
root_name = matching_string(file_path, self.root.name)

pyfakefs/tests/fake_filesystem_unittest_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,16 @@ def test_macos(self):
796796
self.assertTrue(os.path.ismount('/'))
797797
self.assertFalse(os.path.ismount('//share/foo'))
798798

799+
def test_drivelike_path(self):
800+
self.fs.os = OSType.LINUX
801+
folder = Path('/test')
802+
file_path = folder / 'C:/testfile'
803+
file_path.parent.mkdir(parents=True)
804+
file_path.touch()
805+
# use str() to be Python 3.5 compatible
806+
os.chdir(str(folder))
807+
self.assertTrue(os.path.exists(str(file_path.relative_to(folder))))
808+
799809

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

0 commit comments

Comments
 (0)