Repository: jmcgeheeiv/pyfakefs
Pull request #694 (2022-07-20 17:03:54)

Fix handling of Windows drive as root path
- was always detected as existing
- caused part of #692


Patch:
diff --git a/CHANGES.md b/CHANGES.md
index e4bbb29..e28be19 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -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.
 
diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py
index 3eea18e..4365b34 100644
--- a/pyfakefs/fake_filesystem.py
+++ b/pyfakefs/fake_filesystem.py
@@ -1923,9 +1923,11 @@ class FakeFilesystem:
             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:


Test patch:
diff --git a/pyfakefs/tests/fake_filesystem_test.py b/pyfakefs/tests/fake_filesystem_test.py
index 445fcd1..2e9642f 100644
--- a/pyfakefs/tests/fake_filesystem_test.py
+++ b/pyfakefs/tests/fake_filesystem_test.py
@@ -23,7 +23,9 @@ import sys
 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
 
@@ -1023,6 +1025,16 @@ class FakePathModuleTest(TestCase):
         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'
diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py
index 97b5f7b..0f4678e 100644
--- a/pyfakefs/tests/fake_filesystem_unittest_test.py
+++ b/pyfakefs/tests/fake_filesystem_unittest_test.py
@@ -866,8 +866,7 @@ class TestOtherFS(fake_filesystem_unittest.TestCase):
         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))))