Skip to content

Commit 31f3f8b

Browse files
committed
Change handling of root dir under Windows
- as Windows has no root dir as such, we use the current drive as root dir to better emulate the fs - as in the real fs, if a path starts with a path separator, it points to the current drive (e.g. the root of cwd) - fixes pytest-dev#673
1 parent 9a38796 commit 31f3f8b

7 files changed

+287
-107
lines changed

pyfakefs/fake_filesystem.py

+181-57
Large diffs are not rendered by default.

pyfakefs/fake_pathlib.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ def home(cls):
641641
home = os.path.join('C:', 'Users', username)
642642
else:
643643
home = os.path.join('home', username)
644-
cls.filesystem.create_dir(home)
644+
if not cls.filesystem.exists(home):
645+
cls.filesystem.create_dir(home)
645646
return cls(home.replace(os.sep, cls.filesystem.path_separator))
646647

647648
def samefile(self, other_path):

pyfakefs/tests/fake_filesystem_test.py

+85-40
Large diffs are not rendered by default.

pyfakefs/tests/fake_filesystem_unittest_test.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,11 @@ class PathlibTest(TestCase):
733733
@patchfs
734734
def test_cwd(self, fs):
735735
"""Make sure fake file system is used for os in pathlib"""
736-
self.assertEqual(os.path.sep, str(pathlib.Path.cwd()))
736+
is_windows = sys.platform.startswith('win')
737+
root_dir = 'C:' + os.path.sep if is_windows else os.path.sep
738+
self.assertEqual(root_dir, str(pathlib.Path.cwd()))
737739
dot_abs = pathlib.Path(".").absolute()
738-
self.assertEqual(os.path.sep, str(dot_abs))
740+
self.assertEqual(root_dir, str(dot_abs))
739741
self.assertTrue(dot_abs.exists())
740742

741743

@@ -796,13 +798,13 @@ def setUp(self):
796798
def test_real_file_with_home(self):
797799
"""Regression test for #558"""
798800
self.fs.is_windows_fs = os.name != 'nt'
801+
if self.fs.is_windows_fs:
802+
self.fs.is_macos = False
803+
self.fs.reset()
799804
self.fs.add_real_file(__file__)
800805
with open(__file__) as f:
801806
self.assertTrue(f.read())
802807
home = Path.home()
803-
if sys.version_info < (3, 6):
804-
# fspath support since Python 3.6
805-
home = str(home)
806808
os.chdir(home)
807809
with open(__file__) as f:
808810
self.assertTrue(f.read())
@@ -869,5 +871,13 @@ def test_drivelike_path(self):
869871
self.assertTrue(os.path.exists(str(file_path.relative_to(folder))))
870872

871873

874+
@unittest.skipIf(sys.platform != 'win32', 'Windows-specific behavior')
875+
class TestAbsolutePathOnWindows(fake_filesystem_unittest.TestCase):
876+
@patchfs
877+
def test_is_absolute(self, fs):
878+
# regression test for #673
879+
self.assertTrue(pathlib.Path(".").absolute().is_absolute())
880+
881+
872882
if __name__ == "__main__":
873883
unittest.main()

pyfakefs/tests/fake_filesystem_vs_real_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _get_errno(raised_error):
4343

4444
class TestCase(unittest.TestCase):
4545
is_windows = sys.platform.startswith('win')
46-
_FAKE_FS_BASE = sep('/fakefs')
46+
_FAKE_FS_BASE = 'C:\\fakefs' if is_windows else '/fakefs'
4747

4848

4949
class FakeFilesystemVsRealTest(TestCase):

pyfakefs/tests/fake_os_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ def test_get_cwd(self):
106106
self.skip_real_fs()
107107
dirname = self.make_path('foo', 'bar')
108108
self.create_dir(dirname)
109-
self.assertEqual(self.os.getcwd(), self.os.path.sep)
109+
self.assertEqual(self.filesystem.root_dir_name, self.os.getcwd())
110110
self.os.chdir(dirname)
111-
self.assertEqual(self.os.getcwd(), dirname)
111+
self.assertEqual(dirname, self.os.getcwd())
112112

113113
def test_listdir(self):
114114
self.assert_raises_os_error(

pyfakefs/tests/fake_tempfile_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_mkstemp_dir(self):
7777
self.assertEqual(2, len(temporary))
7878
self.assertEqual(next_fd, temporary[0])
7979
self.assertTrue(temporary[1].startswith(
80-
os.path.join(os.sep, 'dir', 'tmp')))
80+
os.path.join(self.fs.root_dir_name, 'dir', 'tmp')))
8181
self.assertTrue(self.fs.exists(temporary[1]))
8282
mode = 0o666 if self.fs.is_windows_fs else 0o600
8383
self.assertEqual(self.fs.get_object(temporary[1]).st_mode,

0 commit comments

Comments
 (0)