Skip to content

Commit

Permalink
Change handling of root dir under Windows
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mrbean-bremen committed May 11, 2022
1 parent 9a38796 commit cff98eb
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 202 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ The released versions correspond to PyPi releases.

## Unreleased

### Changes
* under Windows, the root path is now effectively `C:\` instead of `\`; a
path starting with `\` points to the current drive as in the real file
system (see [#673](../../issues/673))

## [Version 4.5.6](https://pypi.python.org/pypi/pyfakefs/4.5.6) (2022-03-17)
Fixes a regression which broke tests with older pytest versions (< 3.9).

Expand Down
261 changes: 200 additions & 61 deletions pyfakefs/fake_filesystem.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ def home(cls):
home = os.path.join('C:', 'Users', username)
else:
home = os.path.join('home', username)
cls.filesystem.create_dir(home)
if not cls.filesystem.exists(home):
cls.filesystem.create_dir(home)
return cls(home.replace(os.sep, cls.filesystem.path_separator))

def samefile(self, other_path):
Expand Down
352 changes: 221 additions & 131 deletions pyfakefs/tests/fake_filesystem_test.py

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,11 @@ class PathlibTest(TestCase):
@patchfs
def test_cwd(self, fs):
"""Make sure fake file system is used for os in pathlib"""
self.assertEqual(os.path.sep, str(pathlib.Path.cwd()))
is_windows = sys.platform.startswith('win')
root_dir = 'C:' + os.path.sep if is_windows else os.path.sep
self.assertEqual(root_dir, str(pathlib.Path.cwd()))
dot_abs = pathlib.Path(".").absolute()
self.assertEqual(os.path.sep, str(dot_abs))
self.assertEqual(root_dir, str(dot_abs))
self.assertTrue(dot_abs.exists())


Expand Down Expand Up @@ -796,13 +798,13 @@ def setUp(self):
def test_real_file_with_home(self):
"""Regression test for #558"""
self.fs.is_windows_fs = os.name != 'nt'
if self.fs.is_windows_fs:
self.fs.is_macos = False
self.fs.reset()
self.fs.add_real_file(__file__)
with open(__file__) as f:
self.assertTrue(f.read())
home = Path.home()
if sys.version_info < (3, 6):
# fspath support since Python 3.6
home = str(home)
os.chdir(home)
with open(__file__) as f:
self.assertTrue(f.read())
Expand Down Expand Up @@ -869,5 +871,13 @@ def test_drivelike_path(self):
self.assertTrue(os.path.exists(str(file_path.relative_to(folder))))


@unittest.skipIf(sys.platform != 'win32', 'Windows-specific behavior')
class TestAbsolutePathOnWindows(fake_filesystem_unittest.TestCase):
@patchfs
def test_is_absolute(self, fs):
# regression test for #673
self.assertTrue(pathlib.Path(".").absolute().is_absolute())


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion pyfakefs/tests/fake_filesystem_vs_real_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _get_errno(raised_error):

class TestCase(unittest.TestCase):
is_windows = sys.platform.startswith('win')
_FAKE_FS_BASE = sep('/fakefs')
_FAKE_FS_BASE = 'C:\\fakefs' if is_windows else '/fakefs'


class FakeFilesystemVsRealTest(TestCase):
Expand Down
4 changes: 2 additions & 2 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ def test_get_cwd(self):
self.skip_real_fs()
dirname = self.make_path('foo', 'bar')
self.create_dir(dirname)
self.assertEqual(self.os.getcwd(), self.os.path.sep)
self.assertEqual(self.filesystem.root_dir_name, self.os.getcwd())
self.os.chdir(dirname)
self.assertEqual(self.os.getcwd(), dirname)
self.assertEqual(dirname, self.os.getcwd())

def test_listdir(self):
self.assert_raises_os_error(
Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/tests/fake_tempfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_mkstemp_dir(self):
self.assertEqual(2, len(temporary))
self.assertEqual(next_fd, temporary[0])
self.assertTrue(temporary[1].startswith(
os.path.join(os.sep, 'dir', 'tmp')))
os.path.join(self.fs.root_dir_name, 'dir', 'tmp')))
self.assertTrue(self.fs.exists(temporary[1]))
mode = 0o666 if self.fs.is_windows_fs else 0o600
self.assertEqual(self.fs.get_object(temporary[1]).st_mode,
Expand Down

0 comments on commit cff98eb

Please sign in to comment.