Skip to content

Commit

Permalink
Fix handling of Windows drive as root path
Browse files Browse the repository at this point in the history
- was always detected as existing
- caused part of pytest-dev#692
  • Loading branch information
mrbean-bremen committed Jul 20, 2022
1 parent f5640e3 commit 34829c0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ The released versions correspond to PyPi releases.

## Unreleased

### Fixes
* fixed regression: `os.exists` returned `True` for any root drive path under Windows

## [Version 4.6.2](https://pypi.python.org/pypi/pyfakefs/4.6.2) (2022-07-14)
Patch release that fixes an error in the previous patch.

Expand Down
8 changes: 5 additions & 3 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1923,9 +1923,11 @@ def is_mount_point(self, file_path: AnyStr) -> bool:
if (file_path == mount_point or not self.is_case_sensitive and
file_path.lower() == mount_point.lower()):
return True
if self.is_windows_fs:
return (2 <= len(file_path) <= 3 and
self.starts_with_drive_letter(file_path))
if (self.is_windows_fs and len(file_path) == 3 and
len(mount_point) == 2 and
self.starts_with_drive_letter(file_path) and
file_path[:2].lower() == mount_point.lower()):
return True
return False

def ends_with_path_separator(self, path: Union[int, AnyPath]) -> bool:
Expand Down
14 changes: 13 additions & 1 deletion pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import unittest

from pyfakefs import fake_filesystem
from pyfakefs.fake_filesystem import set_uid, set_gid, is_root, reset_ids
from pyfakefs.fake_filesystem import (
set_uid, set_gid, is_root, reset_ids, OSType
)
from pyfakefs.helpers import IS_WIN
from pyfakefs.tests.test_utils import TestCase, RealFsTestCase, time_mock

Expand Down Expand Up @@ -1023,6 +1025,16 @@ def test_exists(self):
self.assertTrue(self.path.exists(file_path_bytes))
self.assertFalse(self.path.exists('!some!other!bogus!path'))

def test_exists_with_drive(self):
self.filesystem.os = OSType.WINDOWS
self.filesystem.add_mount_point('F:')
self.assertTrue(self.path.exists('C:'))
self.assertTrue(self.path.exists('c:\\'))
self.assertTrue(self.path.exists('f:'))
self.assertTrue(self.path.exists('F:\\'))
self.assertFalse(self.path.exists('Z:'))
self.assertFalse(self.path.exists('z:\\'))

def test_lexists(self):
file_path = 'foo!bar!baz'
file_path_bytes = b'foo!bar!baz'
Expand Down
3 changes: 1 addition & 2 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,7 @@ def test_drivelike_path(self):
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))
os.chdir(folder)
self.assertTrue(os.path.exists(str(file_path.relative_to(folder))))


Expand Down

0 comments on commit 34829c0

Please sign in to comment.