diff options
Diffstat (limited to 'pyfakefs/tests/fake_os_test.py')
-rw-r--r-- | pyfakefs/tests/fake_os_test.py | 3653 |
1 files changed, 1939 insertions, 1714 deletions
diff --git a/pyfakefs/tests/fake_os_test.py b/pyfakefs/tests/fake_os_test.py index 7408d17..6b48bbf 100644 --- a/pyfakefs/tests/fake_os_test.py +++ b/pyfakefs/tests/fake_os_test.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright 2009 Google Inc. All Rights Reserved. # @@ -14,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Unit tests for fake_filesystem.FakeOpen.""" +"""Unit tests for fake_os.FakeOsModule.""" import errno import os @@ -22,12 +21,19 @@ import stat import sys import unittest -from pyfakefs.helpers import IN_DOCKER, IS_PYPY +from pyfakefs.helpers import IN_DOCKER, IS_PYPY, get_uid, get_gid -from pyfakefs import fake_filesystem -from pyfakefs.fake_filesystem import FakeFileOpen, is_root +from pyfakefs import fake_filesystem, fake_os, fake_open, fake_file +from pyfakefs.fake_filesystem import ( + FakeFileOpen, + is_root, + set_uid, + set_gid, +) from pyfakefs.extra_packages import ( - use_scandir, use_scandir_package, use_builtin_scandir + use_scandir, + use_scandir_package, + use_builtin_scandir, ) from pyfakefs.tests.test_utils import TestCase, RealFsTestCase @@ -59,62 +65,64 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chdir(self): """chdir should work on a directory.""" - directory = self.make_path('foo') + directory = self.make_path("foo") self.create_dir(directory) self.os.chdir(directory) def test_chdir_fails_non_exist(self): """chdir should raise OSError if the target does not exist.""" - directory = self.make_path('no', 'such', 'directory') + directory = self.make_path("no", "such", "directory") self.assert_raises_os_error(errno.ENOENT, self.os.chdir, directory) def test_chdir_fails_non_directory(self): """chdir should raise OSError if the target is not a directory.""" - filename = self.make_path('foo', 'bar') + filename = self.make_path("foo", "bar") self.create_file(filename) self.assert_raises_os_error(errno.ENOTDIR, self.os.chdir, filename) def test_consecutive_chdir(self): """Consecutive relative chdir calls should work.""" - dir1 = self.make_path('foo') - dir2 = 'bar' + dir1 = self.make_path("foo") + dir2 = "bar" full_dirname = self.os.path.join(dir1, dir2) self.create_dir(full_dirname) self.os.chdir(dir1) self.os.chdir(dir2) # use real path to handle symlink /var to /private/var in MacOs - self.assertEqual(os.path.realpath(self.os.getcwd()), - os.path.realpath(full_dirname)) + self.assertEqual( + os.path.realpath(self.os.getcwd()), os.path.realpath(full_dirname) + ) def test_backwards_chdir(self): """chdir into '..' should behave appropriately.""" # skipping real fs test - can't test root dir self.skip_real_fs() rootdir = self.os.getcwd() - dirname = 'foo' + dirname = "foo" abs_dirname = self.os.path.abspath(dirname) self.filesystem.create_dir(dirname) self.os.chdir(dirname) self.assertEqual(abs_dirname, self.os.getcwd()) - self.os.chdir('..') + self.os.chdir("..") self.assertEqual(rootdir, self.os.getcwd()) - self.os.chdir(self.os.path.join(dirname, '..')) + self.os.chdir(self.os.path.join(dirname, "..")) self.assertEqual(rootdir, self.os.getcwd()) def test_get_cwd(self): # skipping real fs test - can't test root dir self.skip_real_fs() - dirname = self.make_path('foo', 'bar') + 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( - errno.ENOENT, self.os.listdir, 'non_existing/fake_dir') - directory = self.make_path('xyzzy', 'plugh') - files = ['foo', 'bar', 'baz'] + errno.ENOENT, self.os.listdir, "non_existing/fake_dir" + ) + directory = self.make_path("xyzzy", "plugh") + files = ["foo", "bar", "baz"] for f in files: self.create_file(self.os.path.join(directory, f)) files.sort() @@ -125,8 +133,8 @@ class FakeOsModuleTest(FakeOsModuleTestBase): if os.listdir not in os.supports_fd: self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.listdir, 500) - dir_path = self.make_path('xyzzy', 'plugh') - files = ['foo', 'bar', 'baz'] + dir_path = self.make_path("xyzzy", "plugh") + files = ["foo", "bar", "baz"] for f in files: self.create_file(self.os.path.join(dir_path, f)) files.sort() @@ -135,43 +143,42 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.assertEqual(files, sorted(self.os.listdir(path_des))) def test_listdir_returns_list(self): - directory_root = self.make_path('xyzzy') + directory_root = self.make_path("xyzzy") self.os.mkdir(directory_root) - directory = self.os.path.join(directory_root, 'bug') + directory = self.os.path.join(directory_root, "bug") self.os.mkdir(directory) - self.create_file(self.make_path(directory, 'foo')) - self.assertEqual(['foo'], self.os.listdir(directory)) + self.create_file(self.make_path(directory, "foo")) + self.assertEqual(["foo"], self.os.listdir(directory)) def test_listdir_on_symlink(self): self.skip_if_symlink_not_supported() - directory = self.make_path('xyzzy') - files = ['foo', 'bar', 'baz'] + directory = self.make_path("xyzzy") + files = ["foo", "bar", "baz"] for f in files: self.create_file(self.make_path(directory, f)) - self.create_symlink(self.make_path('symlink'), self.make_path('xyzzy')) + self.create_symlink(self.make_path("symlink"), self.make_path("xyzzy")) files.sort() - self.assertEqual(files, - sorted(self.os.listdir(self.make_path('symlink')))) + self.assertEqual(files, sorted(self.os.listdir(self.make_path("symlink")))) def test_listdir_error(self): - file_path = self.make_path('foo', 'bar', 'baz') + file_path = self.make_path("foo", "bar", "baz") self.create_file(file_path) self.assert_raises_os_error(errno.ENOTDIR, self.os.listdir, file_path) def test_exists_current_dir(self): - self.assertTrue(self.os.path.exists('.')) + self.assertTrue(self.os.path.exists(".")) def test_listdir_current(self): - files = ['foo', 'bar', 'baz'] + files = ["foo", "bar", "baz"] for f in files: self.create_file(self.make_path(f)) files.sort() self.assertEqual(files, sorted(self.os.listdir(self.base_path))) def test_fdopen(self): - file_path1 = self.make_path('some_file1') - self.create_file(file_path1, contents='contents here1') - with self.open(file_path1, 'r') as fake_file1: + file_path1 = self.make_path("some_file1") + self.create_file(file_path1, contents="contents here1") + with self.open(file_path1, "r") as fake_file1: fileno = fake_file1.fileno() fake_file2 = self.os.fdopen(fileno) self.assertNotEqual(fake_file2, fake_file1) @@ -179,23 +186,23 @@ class FakeOsModuleTest(FakeOsModuleTestBase): with self.assertRaises(TypeError): self.os.fdopen(None) with self.assertRaises(TypeError): - self.os.fdopen('a string') + self.os.fdopen("a string") def test_out_of_range_fdopen(self): # test some file descriptor that is clearly out of range self.assert_raises_os_error(errno.EBADF, self.os.fdopen, 500) def test_closed_file_descriptor(self): - first_path = self.make_path('some_file1') - second_path = self.make_path('some_file2') - third_path = self.make_path('some_file3') - self.create_file(first_path, contents='contents here1') - self.create_file(second_path, contents='contents here2') - self.create_file(third_path, contents='contents here3') - - fake_file1 = self.open(first_path, 'r') - fake_file2 = self.open(second_path, 'r') - fake_file3 = self.open(third_path, 'r') + first_path = self.make_path("some_file1") + second_path = self.make_path("some_file2") + third_path = self.make_path("some_file3") + self.create_file(first_path, contents="contents here1") + self.create_file(second_path, contents="contents here2") + self.create_file(third_path, contents="contents here3") + + fake_file1 = self.open(first_path, "r") + fake_file2 = self.open(second_path, "r") + fake_file3 = self.open(third_path, "r") fileno1 = fake_file1.fileno() fileno2 = fake_file2.fileno() fileno3 = fake_file3.fileno() @@ -213,25 +220,25 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_fdopen_mode(self): self.skip_real_fs() - file_path1 = self.make_path('some_file1') - self.create_file(file_path1, contents='contents here1') + file_path1 = self.make_path("some_file1") + self.create_file(file_path1, contents="contents here1") self.os.chmod(file_path1, (stat.S_IFREG | 0o666) ^ stat.S_IWRITE) - fake_file1 = self.open(file_path1, 'r') + fake_file1 = self.open(file_path1, "r") fileno1 = fake_file1.fileno() self.os.fdopen(fileno1) - self.os.fdopen(fileno1, 'r') + self.os.fdopen(fileno1, "r") if not is_root(): with self.assertRaises(OSError): - self.os.fdopen(fileno1, 'w') + self.os.fdopen(fileno1, "w") else: - self.os.fdopen(fileno1, 'w') + self.os.fdopen(fileno1, "w") self.os.close(fileno1) def test_fstat(self): - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') - self.create_file(file_path, contents='ABCDE') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") + self.create_file(file_path, contents="ABCDE") with self.open(file_path) as file_obj: fileno = file_obj.fileno() self.assertTrue(stat.S_IFREG & self.os.fstat(fileno)[stat.ST_MODE]) @@ -239,20 +246,45 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.assertEqual(5, self.os.fstat(fileno)[stat.ST_SIZE]) def test_stat(self): - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') - self.create_file(file_path, contents='ABCDE') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") + self.create_file(file_path, contents="ABCDE") self.assertTrue(stat.S_IFDIR & self.os.stat(directory)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.stat(file_path)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.stat(file_path).st_mode) self.assertEqual(5, self.os.stat(file_path)[stat.ST_SIZE]) + def test_st_blocks(self): + self.check_posix_only() + file_path = self.make_path("foo1") + self.create_file(file_path, contents=b"") + self.assertEqual(0, self.os.stat(file_path).st_blocks) + file_path = self.make_path("foo2") + self.create_file(file_path, contents=b"t") + self.assertEqual(8, self.os.stat(file_path).st_blocks) + file_path = self.make_path("foo3") + self.create_file(file_path, contents=b"t" * 4095) + self.assertEqual(8, self.os.stat(file_path).st_blocks) + file_path = self.make_path("foo4") + self.create_file(file_path, contents=b"t" * 4096) + self.assertEqual(8, self.os.stat(file_path).st_blocks) + file_path = self.make_path("foo5") + self.create_file(file_path, contents=b"t" * 4097) + self.assertEqual(16, self.os.stat(file_path).st_blocks) + + def test_no_st_blocks_in_windows(self): + self.check_windows_only() + file_path = self.make_path("foo") + self.create_file(file_path, contents=b"") + with self.assertRaises(AttributeError): + self.os.stat(file_path).st_blocks + def test_stat_with_unc_path(self): self.skip_real_fs() self.check_windows_only() - directory = '//root/share/dir' - file_path = self.os.path.join(directory, 'plugh') - self.create_file(file_path, contents='ABCDE') + directory = "//root/share/dir" + file_path = self.os.path.join(directory, "plugh") + self.create_file(file_path, contents="ABCDE") self.assertTrue(stat.S_IFDIR & self.os.stat(directory)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.stat(file_path)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.stat(file_path).st_mode) @@ -261,9 +293,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_stat_with_drive(self): self.skip_real_fs() self.check_windows_only() - directory = 'C:/foo/dir' - file_path = self.os.path.join(directory, 'plugh') - self.create_file(file_path, contents='ABCDE') + directory = "C:/foo/dir" + file_path = self.os.path.join(directory, "plugh") + self.create_file(file_path, contents="ABCDE") self.assertTrue(stat.S_IFDIR & self.os.stat(directory)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.stat(file_path)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.stat(file_path).st_mode) @@ -272,141 +304,144 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_stat_uses_open_fd_as_path(self): self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.stat, 5) - file_path = self.make_path('foo', 'bar') + file_path = self.make_path("foo", "bar") self.create_file(file_path) with self.open(file_path) as f: - self.assertTrue( - stat.S_IFREG & self.os.stat(f.filedes)[stat.ST_MODE]) + self.assertTrue(stat.S_IFREG & self.os.stat(f.filedes)[stat.ST_MODE]) def test_stat_no_follow_symlinks_posix(self): """Test that stat with follow_symlinks=False behaves like lstat.""" self.check_posix_only() - directory = self.make_path('xyzzy') - base_name = 'plugh' - file_contents = 'frobozz' + directory = self.make_path("xyzzy") + base_name = "plugh" + file_contents = "frobozz" # Just make sure we didn't accidentally make our test data meaningless. self.assertNotEqual(len(base_name), len(file_contents)) file_path = self.os.path.join(directory, base_name) - link_path = self.os.path.join(directory, 'link') + link_path = self.os.path.join(directory, "link") self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, base_name) - self.assertEqual(len(file_contents), - self.os.stat(file_path, follow_symlinks=False)[ - stat.ST_SIZE]) - self.assertEqual(len(base_name), - self.os.stat(link_path, follow_symlinks=False)[ - stat.ST_SIZE]) + self.assertEqual( + len(file_contents), + self.os.stat(file_path, follow_symlinks=False)[stat.ST_SIZE], + ) + self.assertEqual( + len(base_name), + self.os.stat(link_path, follow_symlinks=False)[stat.ST_SIZE], + ) def test_stat_no_follow_symlinks_windows(self): """Test that stat with follow_symlinks=False behaves like lstat.""" self.check_windows_only() self.skip_if_symlink_not_supported() - directory = self.make_path('xyzzy') - base_name = 'plugh' - file_contents = 'frobozz' + directory = self.make_path("xyzzy") + base_name = "plugh" + file_contents = "frobozz" # Just make sure we didn't accidentally make our test data meaningless. self.assertNotEqual(len(base_name), len(file_contents)) file_path = self.os.path.join(directory, base_name) - link_path = self.os.path.join(directory, 'link') + link_path = self.os.path.join(directory, "link") self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, base_name) - self.assertEqual(len(file_contents), - self.os.stat(file_path, follow_symlinks=False)[ - stat.ST_SIZE]) - self.assertEqual(0, - self.os.stat(link_path, follow_symlinks=False)[ - stat.ST_SIZE]) + self.assertEqual( + len(file_contents), + self.os.stat(file_path, follow_symlinks=False)[stat.ST_SIZE], + ) + self.assertEqual( + 0, self.os.stat(link_path, follow_symlinks=False)[stat.ST_SIZE] + ) def test_lstat_size_posix(self): self.check_posix_only() - directory = self.make_path('xyzzy') - base_name = 'plugh' - file_contents = 'frobozz' + directory = self.make_path("xyzzy") + base_name = "plugh" + file_contents = "frobozz" # Just make sure we didn't accidentally make our test data meaningless. self.assertNotEqual(len(base_name), len(file_contents)) file_path = self.os.path.join(directory, base_name) - link_path = self.os.path.join(directory, 'link') + link_path = self.os.path.join(directory, "link") self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, base_name) - self.assertEqual(len(file_contents), - self.os.lstat(file_path)[stat.ST_SIZE]) - self.assertEqual(len(base_name), - self.os.lstat(link_path)[stat.ST_SIZE]) + self.assertEqual(len(file_contents), self.os.lstat(file_path)[stat.ST_SIZE]) + self.assertEqual(len(base_name), self.os.lstat(link_path)[stat.ST_SIZE]) def test_lstat_size_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - directory = self.make_path('xyzzy') - base_name = 'plugh' - file_contents = 'frobozz' + directory = self.make_path("xyzzy") + base_name = "plugh" + file_contents = "frobozz" # Just make sure we didn't accidentally make our test data meaningless. self.assertNotEqual(len(base_name), len(file_contents)) file_path = self.os.path.join(directory, base_name) - link_path = self.os.path.join(directory, 'link') + link_path = self.os.path.join(directory, "link") self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, base_name) - self.assertEqual(len(file_contents), - self.os.lstat(file_path)[stat.ST_SIZE]) - self.assertEqual(0, - self.os.lstat(link_path)[stat.ST_SIZE]) + self.assertEqual(len(file_contents), self.os.lstat(file_path)[stat.ST_SIZE]) + self.assertEqual(0, self.os.lstat(link_path)[stat.ST_SIZE]) def test_lstat_trailing_sep(self): # regression test for #342 stat_result = self.os.lstat(self.base_path) - self.assertEqual(stat_result, - self.os.lstat(self.base_path + self.path_separator())) - self.assertEqual(stat_result, self.os.lstat( - self.base_path + self.path_separator() + self.path_separator())) + self.assertEqual( + stat_result, self.os.lstat(self.base_path + self.path_separator()) + ) + self.assertEqual( + stat_result, + self.os.lstat( + self.base_path + self.path_separator() + self.path_separator() + ), + ) def test_stat_with_byte_string(self): stat_str = self.os.stat(self.base_path) - base_path_bytes = self.base_path.encode('utf8') + base_path_bytes = self.base_path.encode("utf8") stat_bytes = self.os.stat(base_path_bytes) self.assertEqual(stat_bytes, stat_str) def test_lstat_with_byte_string(self): stat_str = self.os.lstat(self.base_path) - base_path_bytes = self.base_path.encode('utf8') + base_path_bytes = self.base_path.encode("utf8") stat_bytes = self.os.lstat(base_path_bytes) self.assertEqual(stat_bytes, stat_str) def test_stat_with_current_dir(self): # regression test for #516 - stat_result = self.os.stat('.') - lstat_result = self.os.lstat('.') + stat_result = self.os.stat(".") + lstat_result = self.os.lstat(".") self.assertEqual(stat_result, lstat_result) def test_exists_with_trailing_sep(self): # regression test for #364 - file_path = self.make_path('alpha') + file_path = self.make_path("alpha") self.create_file(file_path) self.assertFalse(self.os.path.exists(file_path + self.os.sep)) def test_mkdir_with_trailing_sep(self): # regression test for #367 - dir_path = self.make_path('foo') + dir_path = self.make_path("foo") self.os.mkdir(dir_path + self.os.sep + self.os.sep) self.assertTrue(self.os.path.exists(dir_path)) def test_readlink_empty_path(self): self.check_posix_only() - self.assert_raises_os_error(errno.ENOENT, - self.os.readlink, '') + self.assert_raises_os_error(errno.ENOENT, self.os.readlink, "") def test_readlink_ending_with_sep_posix(self): # regression test for #359 self.check_posix_only() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) - self.assert_raises_os_error(errno.EINVAL, - self.os.readlink, link_path + self.os.sep) + self.assert_raises_os_error( + errno.EINVAL, self.os.readlink, link_path + self.os.sep + ) def test_lstat_symlink_with_trailing_sep_linux(self): # regression test for #366 self.check_linux_only() self.skip_if_symlink_not_supported() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) # used to raise self.assertTrue(self.os.lstat(link_path + self.os.sep).st_mode) @@ -415,7 +450,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # regression test for #366 self.check_macos_only() self.skip_if_symlink_not_supported() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) # used to raise self.assertTrue(self.os.lstat(link_path + self.os.sep).st_mode) @@ -423,35 +458,37 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_readlink_ending_with_sep_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) - self.assert_equal_paths(self.base_path, - self.os.readlink(link_path + self.os.sep)) + self.assert_equal_paths( + self.base_path, self.os.readlink(link_path + self.os.sep) + ) def test_islink_with_trailing_sep_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) self.assertTrue(self.os.path.islink(link_path + self.os.path.sep)) def test_islink_with_trailing_sep_linux(self): self.check_linux_only() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) self.assertFalse(self.os.path.islink(link_path + self.os.sep)) def test_islink_with_trailing_sep_macos(self): self.check_macos_only() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) self.assertFalse(self.os.path.islink(link_path + self.os.sep)) def check_getsize_raises_with_trailing_separator(self, error_nr): - file_path = self.make_path('bar') + file_path = self.make_path("bar") self.create_file(file_path) - self.assert_raises_os_error(error_nr, self.os.path.getsize, - file_path + self.os.sep) + self.assert_raises_os_error( + error_nr, self.os.path.getsize, file_path + self.os.sep + ) def test_getsize_raises_with_trailing_separator_posix(self): self.check_posix_only() @@ -463,10 +500,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def check_remove_link_ending_with_sep(self, error_nr): # regression test for #360 - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) - self.assert_raises_os_error(error_nr, - self.os.remove, link_path + self.os.sep) + self.assert_raises_os_error(error_nr, self.os.remove, link_path + self.os.sep) def test_remove_link_ending_with_sep_linux(self): self.check_linux_only() @@ -486,34 +522,36 @@ class FakeOsModuleTest(FakeOsModuleTestBase): if os.lstat not in os.supports_fd: self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.lstat, 5) - file_path = self.make_path('foo', 'bar') - link_path = self.make_path('foo', 'link') - file_contents = b'contents' + file_path = self.make_path("foo", "bar") + link_path = self.make_path("foo", "link") + file_contents = b"contents" self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, file_path) with self.open(file_path) as f: - self.assertEqual(len(file_contents), - self.os.lstat(f.filedes)[stat.ST_SIZE]) + self.assertEqual(len(file_contents), self.os.lstat(f.filedes)[stat.ST_SIZE]) def test_stat_non_existent_file(self): # set up - file_path = self.make_path('non', 'existent', 'file') + file_path = self.make_path("non", "existent", "file") self.assertFalse(self.os.path.exists(file_path)) # actual tests try: # Use try-catch to check exception attributes. self.os.stat(file_path) - self.fail('Exception is expected.') # COV_NF_LINE + self.fail("Exception is expected.") # COV_NF_LINE except OSError as os_error: self.assertEqual(errno.ENOENT, os_error.errno) self.assertEqual(file_path, os_error.filename) def check_open_raises_with_trailing_separator(self, error_nr): - file_path = self.make_path('bar') + self.os.sep - self.assert_raises_os_error(error_nr, self.os.open, - file_path, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC) + file_path = self.make_path("bar") + self.os.sep + self.assert_raises_os_error( + error_nr, + self.os.open, + file_path, + os.O_CREAT | os.O_WRONLY | os.O_TRUNC, + ) def test_open_raises_with_trailing_separator_linux(self): self.check_linux_only() @@ -530,55 +568,54 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_lexists_with_trailing_separator_linux_windows(self): self.check_linux_and_windows() self.skip_if_symlink_not_supported() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.os.symlink(file_path, file_path) self.assertFalse(self.os.path.lexists(file_path + self.os.sep)) def test_lexists_with_trailing_separator_macos(self): # regression test for #373 self.check_macos_only() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.os.symlink(file_path, file_path) self.assertTrue(self.os.path.lexists(file_path + self.os.sep)) def test_islink_with_trailing_separator_linux_windows(self): self.check_linux_and_windows() self.skip_if_symlink_not_supported() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.os.symlink(file_path, file_path) self.assertFalse(self.os.path.islink(file_path + self.os.sep)) def test_islink_with_trailing_separator_macos(self): # regression test for #373 self.check_macos_only() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.os.symlink(file_path, file_path) self.assertTrue(self.os.path.islink(file_path + self.os.sep)) def test_isfile_with_trailing_separator_linux_windows(self): self.check_linux_and_windows() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.create_file(file_path) self.assertFalse(self.os.path.isfile(file_path + self.os.sep)) def test_isfile_with_trailing_separator_macos(self): # regression test for #374 self.check_macos_only() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.create_file(file_path) self.assertFalse(self.os.path.isfile(file_path + self.os.sep)) def test_isfile_not_readable_file(self): - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.create_file(file_path, perm=0) self.assertTrue(self.os.path.isfile(file_path)) def check_stat_with_trailing_separator(self, error_nr): # regression test for #376 - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.create_file(file_path) - self.assert_raises_os_error(error_nr, self.os.stat, - file_path + self.os.sep) + self.assert_raises_os_error(error_nr, self.os.stat, file_path + self.os.sep) def test_stat_with_trailing_separator_posix(self): self.check_posix_only() @@ -590,10 +627,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def check_remove_with_trailing_separator(self, error_nr): # regression test for #377 - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.create_file(file_path) - self.assert_raises_os_error(error_nr, self.os.remove, - file_path + self.os.sep) + self.assert_raises_os_error(error_nr, self.os.remove, file_path + self.os.sep) def test_remove_with_trailing_separator_posix(self): self.check_posix_only() @@ -605,13 +641,13 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_readlink(self): self.skip_if_symlink_not_supported() - link_path = self.make_path('foo', 'bar', 'baz') - target = self.make_path('tarJAY') + link_path = self.make_path("foo", "bar", "baz") + target = self.make_path("tarJAY") self.create_symlink(link_path, target) self.assert_equal_paths(self.os.readlink(link_path), target) def check_readlink_raises_if_path_is_not_a_link(self): - file_path = self.make_path('foo', 'bar', 'eleventyone') + file_path = self.make_path("foo", "bar", "eleventyone") self.create_file(file_path) self.assert_raises_os_error(errno.EINVAL, self.os.readlink, file_path) @@ -625,10 +661,10 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.check_readlink_raises_if_path_is_not_a_link() def check_readlink_raises_if_path_has_file(self, error_subtype): - self.create_file(self.make_path('a_file')) - file_path = self.make_path('a_file', 'foo') + self.create_file(self.make_path("a_file")) + file_path = self.make_path("a_file", "foo") self.assert_raises_os_error(error_subtype, self.os.readlink, file_path) - file_path = self.make_path('a_file', 'foo', 'bar') + file_path = self.make_path("a_file", "foo", "bar") self.assert_raises_os_error(error_subtype, self.os.readlink, file_path) def test_readlink_raises_if_path_has_file_windows(self): @@ -642,8 +678,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_readlink_raises_if_path_does_not_exist(self): self.skip_if_symlink_not_supported() - self.assert_raises_os_error(errno.ENOENT, self.os.readlink, - '/this/path/does/not/exist') + self.assert_raises_os_error( + errno.ENOENT, self.os.readlink, "/this/path/does/not/exist" + ) def test_readlink_raises_if_path_is_none(self): self.skip_if_symlink_not_supported() @@ -652,43 +689,50 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_broken_symlink_with_trailing_separator_linux(self): self.check_linux_only() - file_path = self.make_path('foo') - link_path = self.make_path('link') + file_path = self.make_path("foo") + link_path = self.make_path("link") self.os.symlink(file_path, link_path) - self.assert_raises_os_error(errno.EEXIST, self.os.symlink, - link_path + self.os.sep, - link_path + self.os.sep) + self.assert_raises_os_error( + errno.EEXIST, + self.os.symlink, + link_path + self.os.sep, + link_path + self.os.sep, + ) def test_broken_symlink_with_trailing_separator_macos(self): # regression test for #371 self.check_macos_only() - file_path = self.make_path('foo') - link_path = self.make_path('link') + file_path = self.make_path("foo") + link_path = self.make_path("link") self.os.symlink(file_path, link_path) self.os.symlink(link_path + self.os.sep, link_path + self.os.sep) def test_broken_symlink_with_trailing_separator_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - file_path = self.make_path('foo') - link_path = self.make_path('link') + file_path = self.make_path("foo") + link_path = self.make_path("link") self.os.symlink(file_path, link_path) - self.assert_raises_os_error(errno.EINVAL, self.os.symlink, - link_path + self.os.sep, - link_path + self.os.sep) + self.assert_raises_os_error( + errno.EINVAL, + self.os.symlink, + link_path + self.os.sep, + link_path + self.os.sep, + ) def test_circular_readlink_with_trailing_separator_linux(self): # Regression test for #372 self.check_linux_only() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.os.symlink(file_path, file_path) - self.assert_raises_os_error(errno.ELOOP, self.os.readlink, - file_path + self.os.sep) + self.assert_raises_os_error( + errno.ELOOP, self.os.readlink, file_path + self.os.sep + ) def test_circular_readlink_with_trailing_separator_macos(self): # Regression test for #372 self.check_macos_only() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.os.symlink(file_path, file_path) self.os.readlink(file_path + self.os.sep) @@ -696,38 +740,43 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # Regression test for #372 self.check_windows_only() self.skip_if_symlink_not_supported() - file_path = self.make_path('foo') + file_path = self.make_path("foo") self.os.symlink(file_path, file_path) - self.assert_raises_os_error(errno.EINVAL, self.os.readlink, - file_path + self.os.sep) + self.assert_raises_os_error( + errno.EINVAL, self.os.readlink, file_path + self.os.sep + ) def test_readlink_with_links_in_path(self): self.skip_if_symlink_not_supported() - self.create_symlink(self.make_path('meyer', 'lemon', 'pie'), - self.make_path('yum')) - self.create_symlink(self.make_path('geo', 'metro'), - self.make_path('meyer')) - self.assert_equal_paths(self.make_path('yum'), - self.os.readlink( - self.make_path('geo', 'metro', - 'lemon', 'pie'))) + self.create_symlink( + self.make_path("meyer", "lemon", "pie"), self.make_path("yum") + ) + self.create_symlink(self.make_path("geo", "metro"), self.make_path("meyer")) + self.assert_equal_paths( + self.make_path("yum"), + self.os.readlink(self.make_path("geo", "metro", "lemon", "pie")), + ) def test_readlink_with_chained_links_in_path(self): self.skip_if_symlink_not_supported() - self.create_symlink(self.make_path( - 'eastern', 'european', 'wolfhounds', 'chase'), - self.make_path('cats')) - self.create_symlink(self.make_path('russian'), - self.make_path('eastern', 'european')) - self.create_symlink(self.make_path('dogs'), - self.make_path('russian', 'wolfhounds')) - self.assert_equal_paths(self.make_path('cats'), - self.os.readlink( - self.make_path('dogs', 'chase'))) + self.create_symlink( + self.make_path("eastern", "european", "wolfhounds", "chase"), + self.make_path("cats"), + ) + self.create_symlink( + self.make_path("russian"), self.make_path("eastern", "european") + ) + self.create_symlink( + self.make_path("dogs"), self.make_path("russian", "wolfhounds") + ) + self.assert_equal_paths( + self.make_path("cats"), + self.os.readlink(self.make_path("dogs", "chase")), + ) def check_remove_dir(self, dir_error): - directory = self.make_path('xyzzy') - dir_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + dir_path = self.os.path.join(directory, "plugh") self.create_dir(dir_path) self.assertTrue(self.os.path.exists(dir_path)) self.assert_raises_os_error(dir_error, self.os.remove, dir_path) @@ -735,7 +784,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.os.chdir(directory) self.assert_raises_os_error(dir_error, self.os.remove, dir_path) self.assertTrue(self.os.path.exists(dir_path)) - self.assert_raises_os_error(errno.ENOENT, self.os.remove, '/plugh') + self.assert_raises_os_error(errno.ENOENT, self.os.remove, "/plugh") def test_remove_dir_linux(self): self.check_linux_only() @@ -753,21 +802,21 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # regression test for issue #337 self.check_windows_only() self.skip_real_fs() - dir_path = self.os.path.join('C:', 'test') + dir_path = self.os.path.join("C:", "test") self.filesystem.create_dir(dir_path) self.assert_raises_os_error(errno.EACCES, self.os.remove, dir_path) def test_remove_file(self): - directory = self.make_path('zzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("zzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) self.os.remove(file_path) self.assertFalse(self.os.path.exists(file_path)) def test_remove_file_no_directory(self): - directory = self.make_path('zzy') - file_name = 'plugh' + directory = self.make_path("zzy") + file_name = "plugh" file_path = self.os.path.join(directory, file_name) self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) @@ -777,7 +826,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_remove_file_with_read_permission_raises_in_windows(self): self.check_windows_only() - path = self.make_path('foo', 'bar') + path = self.make_path("foo", "bar") self.create_file(path) self.os.chmod(path, 0o444) self.assert_raises_os_error(errno.EACCES, self.os.remove, path) @@ -785,7 +834,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_remove_file_with_read_permission_shall_succeed_in_posix(self): self.check_posix_only() - path = self.make_path('foo', 'bar') + path = self.make_path("foo", "bar") self.create_file(path) self.os.chmod(path, 0o444) self.os.remove(path) @@ -793,8 +842,8 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_remove_file_without_parent_permission_raises_in_posix(self): self.check_posix_only() - parent_dir = self.make_path('foo') - path = self.os.path.join(parent_dir, 'bar') + parent_dir = self.make_path("foo") + path = self.os.path.join(parent_dir, "bar") self.create_file(path) self.os.chmod(parent_dir, 0o666) # missing execute permission if not is_root(): @@ -816,28 +865,28 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_remove_open_file_fails_under_windows(self): self.check_windows_only() - path = self.make_path('foo', 'bar') + path = self.make_path("foo", "bar") self.create_file(path) - with self.open(path, 'r'): + with self.open(path, "r"): self.assert_raises_os_error(errno.EACCES, self.os.remove, path) self.assertTrue(self.os.path.exists(path)) def test_remove_open_file_possible_under_posix(self): self.check_posix_only() - path = self.make_path('foo', 'bar') + path = self.make_path("foo", "bar") self.create_file(path) - self.open(path, 'r') + self.open(path, "r") self.os.remove(path) self.assertFalse(self.os.path.exists(path)) def test_remove_file_relative_path(self): self.skip_real_fs() original_dir = self.os.getcwd() - directory = self.make_path('zzy') - subdirectory = self.os.path.join(directory, 'zzy') - file_name = 'plugh' + directory = self.make_path("zzy") + subdirectory = self.os.path.join(directory, "zzy") + file_name = "plugh" file_path = self.os.path.join(directory, file_name) - file_path_relative = self.os.path.join('..', file_name) + file_path_relative = self.os.path.join("..", file_name) self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) self.create_dir(subdirectory) @@ -849,7 +898,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.assertFalse(self.os.path.exists(file_path)) def check_remove_dir_raises_error(self, dir_error): - directory = self.make_path('zzy') + directory = self.make_path("zzy") self.create_dir(directory) self.assert_raises_os_error(dir_error, self.os.remove, directory) @@ -867,8 +916,8 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_remove_symlink_to_dir(self): self.skip_if_symlink_not_supported() - directory = self.make_path('zzy') - link = self.make_path('link_to_dir') + directory = self.make_path("zzy") + link = self.make_path("link_to_dir") self.create_dir(directory) self.os.symlink(directory, link) self.assertTrue(self.os.path.exists(directory)) @@ -878,48 +927,46 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.assertFalse(self.os.path.exists(link)) def test_unlink_raises_if_not_exist(self): - file_path = self.make_path('file', 'does', 'not', 'exist') + file_path = self.make_path("file", "does", "not", "exist") self.assertFalse(self.os.path.exists(file_path)) self.assert_raises_os_error(errno.ENOENT, self.os.unlink, file_path) def test_rename_to_nonexistent_file(self): """Can rename a file to an unused name.""" - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') - self.create_file(old_file_path, contents='test contents') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") + self.create_file(old_file_path, contents="test contents") self.assertTrue(self.os.path.exists(old_file_path)) self.assertFalse(self.os.path.exists(new_file_path)) self.os.rename(old_file_path, new_file_path) self.assertFalse(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) - self.check_contents(new_file_path, 'test contents') + self.check_contents(new_file_path, "test contents") def test_rename_dir_to_symlink_posix(self): self.check_posix_only() - link_path = self.make_path('link') - dir_path = self.make_path('dir') - link_target = self.os.path.join(dir_path, 'link_target') + link_path = self.make_path("link") + dir_path = self.make_path("dir") + link_target = self.os.path.join(dir_path, "link_target") self.create_dir(dir_path) self.os.symlink(link_target, link_path) - self.assert_raises_os_error(errno.ENOTDIR, self.os.rename, dir_path, - link_path) + self.assert_raises_os_error(errno.ENOTDIR, self.os.rename, dir_path, link_path) def test_rename_dir_to_symlink_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - link_path = self.make_path('link') - dir_path = self.make_path('dir') - link_target = self.os.path.join(dir_path, 'link_target') + link_path = self.make_path("link") + dir_path = self.make_path("dir") + link_target = self.os.path.join(dir_path, "link_target") self.create_dir(dir_path) self.os.symlink(link_target, link_path) - self.assert_raises_os_error(errno.EEXIST, self.os.rename, dir_path, - link_path) + self.assert_raises_os_error(errno.EEXIST, self.os.rename, dir_path, link_path) def test_rename_file_to_symlink(self): self.check_posix_only() - link_path = self.make_path('file_link') - file_path = self.make_path('file') + link_path = self.make_path("file_link") + file_path = self.make_path("file") self.os.symlink(file_path, link_path) self.create_file(file_path) self.os.rename(file_path, link_path) @@ -929,10 +976,10 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_symlink_to_symlink(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') + base_path = self.make_path("foo", "bar") self.create_dir(base_path) - link_path1 = self.os.path.join(base_path, 'link1') - link_path2 = self.os.path.join(base_path, 'link2') + link_path1 = self.os.path.join(base_path, "link1") + link_path2 = self.os.path.join(base_path, "link2") self.os.symlink(base_path, link_path1) self.os.symlink(base_path, link_path2) self.os.rename(link_path1, link_path2) @@ -941,72 +988,77 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_symlink_to_symlink_for_parent_raises(self): self.check_posix_only() - dir_link = self.make_path('dir_link') - dir_path = self.make_path('dir') - dir_in_dir_path = self.os.path.join(dir_link, 'inner_dir') + dir_link = self.make_path("dir_link") + dir_path = self.make_path("dir") + dir_in_dir_path = self.os.path.join(dir_link, "inner_dir") self.create_dir(dir_path) self.os.symlink(dir_path, dir_link) self.create_dir(dir_in_dir_path) - self.assert_raises_os_error(errno.EINVAL, self.os.rename, dir_path, - dir_in_dir_path) + self.assert_raises_os_error( + errno.EINVAL, self.os.rename, dir_path, dir_in_dir_path + ) def check_rename_case_with_symlink(self, result): self.skip_if_symlink_not_supported() self.check_case_insensitive_fs() - dir_path_lower = self.make_path('beta') + dir_path_lower = self.make_path("beta") self.create_dir(dir_path_lower) - link_path = self.make_path('b') + link_path = self.make_path("b") self.os.symlink(self.base_path, link_path) - path1 = self.os.path.join(link_path, 'Beta') - dir_path_upper = self.make_path('Beta') + path1 = self.os.path.join(link_path, "Beta") + dir_path_upper = self.make_path("Beta") self.os.rename(path1, dir_path_upper) self.assertEqual(result, sorted(self.os.listdir(self.base_path))) def test_rename_case_with_symlink_mac(self): # Regression test for #322 self.check_macos_only() - self.check_rename_case_with_symlink(['b', 'beta']) + self.check_rename_case_with_symlink(["b", "beta"]) def test_rename_case_with_symlink_windows(self): self.check_windows_only() - self.check_rename_case_with_symlink(['Beta', 'b']) + self.check_rename_case_with_symlink(["Beta", "b"]) def test_recursive_rename_raises(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') + base_path = self.make_path("foo", "bar") self.create_dir(base_path) - new_path = self.os.path.join(base_path, 'new_dir') - self.assert_raises_os_error(errno.EINVAL, self.os.rename, base_path, - new_path) + new_path = self.os.path.join(base_path, "new_dir") + self.assert_raises_os_error(errno.EINVAL, self.os.rename, base_path, new_path) def test_rename_file_to_parent_dir_file(self): # Regression test for issue 230 - dir_path = self.make_path('dir') + dir_path = self.make_path("dir") self.create_dir(dir_path) - file_path = self.make_path('old_file') - new_file_path = self.os.path.join(dir_path, 'new_file') + file_path = self.make_path("old_file") + new_file_path = self.os.path.join(dir_path, "new_file") self.create_file(file_path) self.os.rename(file_path, new_file_path) def test_rename_with_target_parent_file_raises_posix(self): self.check_posix_only() - file_path = self.make_path('foo', 'baz') + file_path = self.make_path("foo", "baz") self.create_file(file_path) - self.assert_raises_os_error(errno.ENOTDIR, self.os.rename, file_path, - file_path + '/new') + self.assert_raises_os_error( + errno.ENOTDIR, self.os.rename, file_path, file_path + "/new" + ) def test_rename_with_target_parent_file_raises_windows(self): self.check_windows_only() - file_path = self.make_path('foo', 'baz') + file_path = self.make_path("foo", "baz") self.create_file(file_path) - self.assert_raises_os_error(errno.EACCES, self.os.rename, file_path, - self.os.path.join(file_path, 'new')) + self.assert_raises_os_error( + errno.EACCES, + self.os.rename, + file_path, + self.os.path.join(file_path, "new"), + ) def test_rename_symlink_to_source(self): self.check_posix_only() - base_path = self.make_path('foo') - link_path = self.os.path.join(base_path, 'slink') - file_path = self.os.path.join(base_path, 'file') + base_path = self.make_path("foo") + link_path = self.os.path.join(base_path, "slink") + file_path = self.os.path.join(base_path, "file") self.create_file(file_path) self.os.symlink(file_path, link_path) self.os.rename(link_path, file_path) @@ -1014,20 +1066,19 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_symlink_to_dir_raises(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(base_path, 'dir_link') - dir_path = self.os.path.join(base_path, 'dir') + base_path = self.make_path("foo", "bar") + link_path = self.os.path.join(base_path, "dir_link") + dir_path = self.os.path.join(base_path, "dir") self.create_dir(dir_path) self.os.symlink(dir_path, link_path) - self.assert_raises_os_error(errno.EISDIR, self.os.rename, link_path, - dir_path) + self.assert_raises_os_error(errno.EISDIR, self.os.rename, link_path, dir_path) def test_rename_broken_symlink(self): self.check_posix_only() - base_path = self.make_path('foo') + base_path = self.make_path("foo") self.create_dir(base_path) - link_path = self.os.path.join(base_path, 'slink') - file_path = self.os.path.join(base_path, 'file') + link_path = self.os.path.join(base_path, "slink") + file_path = self.os.path.join(base_path, "file") self.os.symlink(file_path, link_path) self.os.rename(link_path, file_path) self.assertFalse(self.os.path.exists(file_path)) @@ -1036,28 +1087,25 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_directory(self): """Can rename a directory to an unused name.""" - for old_path, new_path in [('wxyyw', 'xyzzy'), ('abccb', 'cdeed')]: + for old_path, new_path in [("wxyyw", "xyzzy"), ("abccb", "cdeed")]: old_path = self.make_path(old_path) new_path = self.make_path(new_path) - self.create_file(self.os.path.join(old_path, 'plugh'), - contents='test') + self.create_file(self.os.path.join(old_path, "plugh"), contents="test") self.assertTrue(self.os.path.exists(old_path)) self.assertFalse(self.os.path.exists(new_path)) self.os.rename(old_path, new_path) self.assertFalse(self.os.path.exists(old_path)) self.assertTrue(self.os.path.exists(new_path)) - self.check_contents(self.os.path.join(new_path, 'plugh'), 'test') + self.check_contents(self.os.path.join(new_path, "plugh"), "test") if not self.use_real_fs(): - self.assertEqual(3, - self.filesystem.get_object(new_path).st_nlink) + self.assertEqual(3, self.filesystem.get_object(new_path).st_nlink) def check_rename_directory_to_existing_file_raises(self, error_nr): - dir_path = self.make_path('dir') - file_path = self.make_path('file') + dir_path = self.make_path("dir") + file_path = self.make_path("file") self.create_dir(dir_path) self.create_file(file_path) - self.assert_raises_os_error(error_nr, self.os.rename, dir_path, - file_path) + self.assert_raises_os_error(error_nr, self.os.rename, dir_path, file_path) def test_rename_directory_to_existing_file_raises_posix(self): self.check_posix_only() @@ -1070,19 +1118,18 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_to_existing_directory_should_raise_under_windows(self): """Renaming to an existing directory raises OSError under Windows.""" self.check_windows_only() - old_path = self.make_path('foo', 'bar') - new_path = self.make_path('foo', 'baz') + old_path = self.make_path("foo", "bar") + new_path = self.make_path("foo", "baz") self.create_dir(old_path) self.create_dir(new_path) - self.assert_raises_os_error(errno.EEXIST, self.os.rename, old_path, - new_path) + self.assert_raises_os_error(errno.EEXIST, self.os.rename, old_path, new_path) def test_rename_to_a_hardlink_of_same_file_should_do_nothing(self): self.skip_real_fs_failure(skip_posix=False) self.skip_if_symlink_not_supported() - file_path = self.make_path('dir', 'file') + file_path = self.make_path("dir", "file") self.create_file(file_path) - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.link(file_path, link_path) self.os.rename(file_path, link_path) self.assertTrue(self.os.path.exists(file_path)) @@ -1090,13 +1137,13 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_hardlink_works_with_symlink(self): self.skip_if_symlink_not_supported() - base_path = self.make_path('foo') + base_path = self.make_path("foo") self.create_dir(base_path) - symlink_path = self.os.path.join(base_path, 'slink') + symlink_path = self.os.path.join(base_path, "slink") self.os.symlink(base_path, symlink_path) - file_path = self.os.path.join(base_path, 'slink', 'beta') + file_path = self.os.path.join(base_path, "slink", "beta") self.create_file(file_path) - link_path = self.os.path.join(base_path, 'slink', 'gamma') + link_path = self.os.path.join(base_path, "slink", "gamma") self.os.link(file_path, link_path) self.assertTrue(self.os.path.exists(link_path)) self.assertFalse(self.os.path.islink(link_path)) @@ -1104,43 +1151,40 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_replace_existing_directory_should_raise_under_windows(self): """Renaming to an existing directory raises OSError under Windows.""" self.check_windows_only() - old_path = self.make_path('foo', 'bar') - new_path = self.make_path('foo', 'baz') + old_path = self.make_path("foo", "bar") + new_path = self.make_path("foo", "baz") self.create_dir(old_path) self.create_dir(new_path) - self.assert_raises_os_error(errno.EACCES, self.os.replace, old_path, - new_path) + self.assert_raises_os_error(errno.EACCES, self.os.replace, old_path, new_path) def test_rename_to_existing_directory_under_posix(self): """Renaming to an existing directory changes the existing directory under Posix.""" self.check_posix_only() - old_path = self.make_path('foo', 'bar') - new_path = self.make_path('xyzzy') - self.create_dir(self.os.path.join(old_path, 'sub')) + old_path = self.make_path("foo", "bar") + new_path = self.make_path("xyzzy") + self.create_dir(self.os.path.join(old_path, "sub")) self.create_dir(new_path) self.os.rename(old_path, new_path) - self.assertTrue( - self.os.path.exists(self.os.path.join(new_path, 'sub'))) + self.assertTrue(self.os.path.exists(self.os.path.join(new_path, "sub"))) self.assertFalse(self.os.path.exists(old_path)) def test_rename_file_to_existing_directory_raises_under_posix(self): self.check_posix_only() - file_path = self.make_path('foo', 'bar', 'baz') - new_path = self.make_path('xyzzy') + file_path = self.make_path("foo", "bar", "baz") + new_path = self.make_path("xyzzy") self.create_file(file_path) self.create_dir(new_path) - self.assert_raises_os_error(errno.EISDIR, self.os.rename, file_path, - new_path) + self.assert_raises_os_error(errno.EISDIR, self.os.rename, file_path, new_path) def test_rename_to_existing_dir_under_posix_raises_if_not_empty(self): """Renaming to an existing directory changes the existing directory under Posix.""" self.check_posix_only() - old_path = self.make_path('foo', 'bar') - new_path = self.make_path('foo', 'baz') - self.create_dir(self.os.path.join(old_path, 'sub')) - self.create_dir(self.os.path.join(new_path, 'sub')) + old_path = self.make_path("foo", "bar") + new_path = self.make_path("foo", "baz") + self.create_dir(self.os.path.join(old_path, "sub")) + self.create_dir(self.os.path.join(new_path, "sub")) # not testing specific subtype: # raises errno.ENOTEMPTY under Ubuntu 16.04, MacOS and pyfakefs @@ -1151,112 +1195,115 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_to_another_device_should_raise(self): """Renaming to another filesystem device raises OSError.""" self.skip_real_fs() - self.filesystem.add_mount_point('/mount') - old_path = '/foo/bar' - new_path = '/mount/bar' + self.filesystem.add_mount_point("/mount") + old_path = "/foo/bar" + new_path = "/mount/bar" self.filesystem.create_file(old_path) - self.assert_raises_os_error(errno.EXDEV, self.os.rename, old_path, - new_path) + self.assert_raises_os_error(errno.EXDEV, self.os.rename, old_path, new_path) def test_rename_to_existent_file_posix(self): """Can rename a file to a used name under Unix.""" self.check_posix_only() - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') - self.create_file(old_file_path, contents='test contents 1') - self.create_file(new_file_path, contents='test contents 2') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") + self.create_file(old_file_path, contents="test contents 1") + self.create_file(new_file_path, contents="test contents 2") self.assertTrue(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) self.os.rename(old_file_path, new_file_path) self.assertFalse(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) - self.check_contents(new_file_path, 'test contents 1') + self.check_contents(new_file_path, "test contents 1") def test_rename_to_existent_file_windows(self): """Renaming a file to a used name raises OSError under Windows.""" self.check_windows_only() - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') - self.create_file(old_file_path, contents='test contents 1') - self.create_file(new_file_path, contents='test contents 2') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") + self.create_file(old_file_path, contents="test contents 1") + self.create_file(new_file_path, contents="test contents 2") self.assertTrue(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) self.assert_raises_os_error( - errno.EEXIST, self.os.rename, old_file_path, new_file_path) + errno.EEXIST, self.os.rename, old_file_path, new_file_path + ) def test_replace_to_existent_file(self): """Replaces an existing file (does not work with `rename()` under Windows).""" - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') - self.create_file(old_file_path, contents='test contents 1') - self.create_file(new_file_path, contents='test contents 2') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") + self.create_file(old_file_path, contents="test contents 1") + self.create_file(new_file_path, contents="test contents 2") self.assertTrue(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) self.os.replace(old_file_path, new_file_path) self.assertFalse(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) - self.check_contents(new_file_path, 'test contents 1') + self.check_contents(new_file_path, "test contents 1") def test_rename_to_nonexistent_dir(self): """Can rename a file to a name in a nonexistent dir.""" - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join( - directory, 'no_such_path', 'plugh_new') - self.create_file(old_file_path, contents='test contents') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "no_such_path", "plugh_new") + self.create_file(old_file_path, contents="test contents") self.assertTrue(self.os.path.exists(old_file_path)) self.assertFalse(self.os.path.exists(new_file_path)) self.assert_raises_os_error( - errno.ENOENT, self.os.rename, old_file_path, new_file_path) + errno.ENOENT, self.os.rename, old_file_path, new_file_path + ) self.assertTrue(self.os.path.exists(old_file_path)) self.assertFalse(self.os.path.exists(new_file_path)) - self.check_contents(old_file_path, 'test contents') + self.check_contents(old_file_path, "test contents") def test_rename_nonexistent_file_should_raise_error(self): """Can't rename a file that doesn't exist.""" - self.assert_raises_os_error(errno.ENOENT, self.os.rename, - 'nonexistent-foo', 'doesn\'t-matter-bar') + self.assert_raises_os_error( + errno.ENOENT, + self.os.rename, + "nonexistent-foo", + "doesn't-matter-bar", + ) def test_rename_empty_dir(self): """Test a rename of an empty directory.""" - directory = self.make_path('xyzzy') - before_dir = self.os.path.join(directory, 'empty') - after_dir = self.os.path.join(directory, 'unused') + directory = self.make_path("xyzzy") + before_dir = self.os.path.join(directory, "empty") + after_dir = self.os.path.join(directory, "unused") self.create_dir(before_dir) - self.assertTrue( - self.os.path.exists(self.os.path.join(before_dir, '.'))) + self.assertTrue(self.os.path.exists(self.os.path.join(before_dir, "."))) self.assertFalse(self.os.path.exists(after_dir)) self.os.rename(before_dir, after_dir) self.assertFalse(self.os.path.exists(before_dir)) - self.assertTrue(self.os.path.exists(self.os.path.join(after_dir, '.'))) + self.assertTrue(self.os.path.exists(self.os.path.join(after_dir, "."))) def test_rename_symlink(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') + base_path = self.make_path("foo", "bar") self.create_dir(base_path) - link_path = self.os.path.join(base_path, 'link') + link_path = self.os.path.join(base_path, "link") self.os.symlink(base_path, link_path) - file_path = self.os.path.join(link_path, 'file') - new_file_path = self.os.path.join(link_path, 'new') + file_path = self.os.path.join(link_path, "file") + new_file_path = self.os.path.join(link_path, "new") self.create_file(file_path) self.os.rename(file_path, new_file_path) self.assertFalse(self.os.path.exists(file_path)) self.assertTrue(self.os.path.exists(new_file_path)) def check_append_mode_tell_after_truncate(self, tell_result): - file_path = self.make_path('baz') - with self.open(file_path, 'w') as f0: - with self.open(file_path, 'a') as f1: - f1.write('abcde') + file_path = self.make_path("baz") + with self.open(file_path, "w") as f0: + with self.open(file_path, "a") as f1: + f1.write("abcde") f0.seek(2) f0.truncate() self.assertEqual(tell_result, f1.tell()) - with self.open(file_path, mode='rb') as f: - self.assertEqual(b'\0\0abcde', f.read()) + with self.open(file_path, mode="rb") as f: + self.assertEqual(b"\0\0abcde", f.read()) def test_append_mode_tell_linux_windows(self): # Regression test for #300 @@ -1270,16 +1317,16 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_tell_after_seek_in_append_mode(self): # Regression test for #363 - file_path = self.make_path('foo') - with self.open(file_path, 'a') as f: + file_path = self.make_path("foo") + with self.open(file_path, "a") as f: f.seek(1) self.assertEqual(1, f.tell()) def test_tell_after_seekback_in_append_mode(self): # Regression test for #414 - file_path = self.make_path('foo') - with self.open(file_path, 'a') as f: - f.write('aa') + file_path = self.make_path("foo") + with self.open(file_path, "a") as f: + f.write("aa") f.seek(1) self.assertEqual(1, f.tell()) @@ -1288,10 +1335,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.assertTrue(self, self.os.path.isdir(self.base_path + self.os.sep)) def check_rename_dir_with_trailing_sep(self, error): - dir_path = self.make_path('dir') + self.os.sep + dir_path = self.make_path("dir") + self.os.sep self.os.mkdir(dir_path) - self.assert_raises_os_error(error, - self.os.rename, dir_path, self.base_path) + self.assert_raises_os_error(error, self.os.rename, dir_path, self.base_path) def test_rename_dir_with_trailing_sep_posix(self): # regression test for #406 @@ -1304,13 +1350,13 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_dir(self): """Test a rename of a directory.""" - directory = self.make_path('xyzzy') - before_dir = self.os.path.join(directory, 'before') - before_file = self.os.path.join(directory, 'before', 'file') - after_dir = self.os.path.join(directory, 'after') - after_file = self.os.path.join(directory, 'after', 'file') + directory = self.make_path("xyzzy") + before_dir = self.os.path.join(directory, "before") + before_file = self.os.path.join(directory, "before", "file") + after_dir = self.os.path.join(directory, "after") + after_file = self.os.path.join(directory, "after", "file") self.create_dir(before_dir) - self.create_file(before_file, contents='payload') + self.create_file(before_file, contents="payload") self.assertTrue(self.os.path.exists(before_dir)) self.assertTrue(self.os.path.exists(before_file)) self.assertFalse(self.os.path.exists(after_dir)) @@ -1320,15 +1366,15 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.assertFalse(self.os.path.exists(before_file)) self.assertTrue(self.os.path.exists(after_dir)) self.assertTrue(self.os.path.exists(after_file)) - self.check_contents(after_file, 'payload') + self.check_contents(after_file, "payload") def test_rename_preserves_stat(self): """Test if rename preserves mtime.""" self.check_posix_only() self.skip_real_fs() - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") self.create_file(old_file_path) old_file = self.filesystem.get_object(old_file_path) old_file.st_mtime = old_file.st_mtime - 3600 @@ -1338,8 +1384,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): new_file = self.filesystem.get_object(new_file_path) self.assertNotEqual(new_file.st_mtime, old_file.st_mtime) self.os.rename(old_file_path, new_file_path) - new_file = self.filesystem.get_object( - new_file_path, check_read_perm=False) + new_file = self.filesystem.get_object(new_file_path, check_read_perm=False) self.assertEqual(new_file.st_mtime, old_file.st_mtime) self.assertEqual(new_file.st_mode, old_file.st_mode) self.assertEqual(new_file.st_uid, old_file.st_uid) @@ -1347,18 +1392,18 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_same_filenames(self): """Test renaming when old and new names are the same.""" - directory = self.make_path('xyzzy') - file_contents = 'Spam eggs' - file_path = self.os.path.join(directory, 'eggs') + directory = self.make_path("xyzzy") + file_contents = "Spam eggs" + file_path = self.os.path.join(directory, "eggs") self.create_file(file_path, contents=file_contents) self.os.rename(file_path, file_path) self.check_contents(file_path, file_contents) def test_rmdir(self): """Can remove a directory.""" - directory = self.make_path('xyzzy') - sub_dir = self.make_path('xyzzy', 'abccd') - other_dir = self.make_path('xyzzy', 'cdeed') + directory = self.make_path("xyzzy") + sub_dir = self.make_path("xyzzy", "abccd") + other_dir = self.make_path("xyzzy", "cdeed") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) self.os.rmdir(directory) @@ -1366,28 +1411,28 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.create_dir(sub_dir) self.create_dir(other_dir) self.os.chdir(sub_dir) - self.os.rmdir('../cdeed') + self.os.rmdir("../cdeed") self.assertFalse(self.os.path.exists(other_dir)) - self.os.chdir('..') - self.os.rmdir('abccd') + self.os.chdir("..") + self.os.rmdir("abccd") self.assertFalse(self.os.path.exists(sub_dir)) def test_rmdir_raises_if_not_empty(self): """Raises an exception if the target directory is not empty.""" - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) self.assert_raises_os_error(errno.ENOTEMPTY, self.os.rmdir, directory) def check_rmdir_raises_if_not_directory(self, error_nr): """Raises an exception if the target is not a directory.""" - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) self.assert_raises_os_error(errno.ENOTDIR, self.os.rmdir, file_path) - self.assert_raises_os_error(error_nr, self.os.rmdir, '.') + self.assert_raises_os_error(error_nr, self.os.rmdir, ".") def test_rmdir_raises_if_not_directory_posix(self): self.check_posix_only() @@ -1399,19 +1444,19 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rmdir_raises_if_not_exist(self): """Raises an exception if the target does not exist.""" - directory = self.make_path('xyzzy') + directory = self.make_path("xyzzy") self.assertFalse(self.os.path.exists(directory)) self.assert_raises_os_error(errno.ENOENT, self.os.rmdir, directory) def test_rmdir_via_symlink(self): self.check_windows_only() self.skip_if_symlink_not_supported() - base_path = self.make_path('foo', 'bar') - dir_path = self.os.path.join(base_path, 'alpha') + base_path = self.make_path("foo", "bar") + dir_path = self.os.path.join(base_path, "alpha") self.create_dir(dir_path) - link_path = self.os.path.join(base_path, 'beta') + link_path = self.os.path.join(base_path, "beta") self.os.symlink(base_path, link_path) - self.os.rmdir(link_path + '/alpha') + self.os.rmdir(link_path + "/alpha") self.assertFalse(self.os.path.exists(dir_path)) def remove_dirs_check(self, directory): @@ -1422,15 +1467,21 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_removedirs(self): # no exception raised self.skip_real_fs() - data = ['test1', ('test1', 'test2'), ('test1', 'extra'), - ('test1', 'test2', 'test3')] + data = [ + "test1", + ("test1", "test2"), + ("test1", "extra"), + ("test1", "test2", "test3"), + ] for directory in data: self.create_dir(self.make_path(directory)) self.assertTrue(self.os.path.exists(self.make_path(directory))) - self.assert_raises_os_error(errno.ENOTEMPTY, self.remove_dirs_check, - self.make_path(data[0])) - self.assert_raises_os_error(errno.ENOTEMPTY, self.remove_dirs_check, - self.make_path(data[1])) + self.assert_raises_os_error( + errno.ENOTEMPTY, self.remove_dirs_check, self.make_path(data[0]) + ) + self.assert_raises_os_error( + errno.ENOTEMPTY, self.remove_dirs_check, self.make_path(data[1]) + ) self.assertTrue(self.remove_dirs_check(self.make_path(data[3]))) self.assertTrue(self.os.path.exists(self.make_path(data[0]))) @@ -1439,26 +1490,25 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # Should raise because '/test1/extra' is all that is left, and # removedirs('/test1/extra') will eventually try to rmdir('/'). - self.assert_raises_os_error(errno.EBUSY, self.remove_dirs_check, - self.make_path(data[2])) + self.assert_raises_os_error( + errno.EBUSY, self.remove_dirs_check, self.make_path(data[2]) + ) # However, it will still delete '/test1') in the process. self.assertFalse(self.os.path.exists(self.make_path(data[0]))) - self.create_dir(self.make_path('test1', 'test2')) + self.create_dir(self.make_path("test1", "test2")) # Add this to the root directory to avoid raising an exception. - self.filesystem.create_dir(self.make_path('test3')) - self.assertTrue( - self.remove_dirs_check(self.make_path('test1', 'test2'))) - self.assertFalse(self.os.path.exists(self.make_path('test1', 'test2'))) - self.assertFalse(self.os.path.exists(self.make_path('test1'))) + self.filesystem.create_dir(self.make_path("test3")) + self.assertTrue(self.remove_dirs_check(self.make_path("test1", "test2"))) + self.assertFalse(self.os.path.exists(self.make_path("test1", "test2"))) + self.assertFalse(self.os.path.exists(self.make_path("test1"))) def test_removedirs_raises_if_removing_root(self): """Raises exception if asked to remove '/'.""" self.skip_real_fs() self.os.rmdir(self.base_path) - directory = self.os.path.splitdrive( - self.base_path)[0] + self.os.path.sep + directory = self.os.path.splitdrive(self.base_path)[0] + self.os.path.sep self.assertTrue(self.os.path.exists(directory)) self.assert_raises_os_error(errno.EBUSY, self.os.removedirs, directory) @@ -1469,7 +1519,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): All of other directories should still be removed, though. """ self.skip_real_fs() - directory = self.make_path('foo', 'bar') + directory = self.make_path("foo", "bar") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) self.assert_raises_os_error(errno.EBUSY, self.os.removedirs, directory) @@ -1481,8 +1531,8 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_removedirs_with_trailing_slash(self): """removedirs works on directory names with trailing slashes.""" # separate this case from the removing-root-directory case - self.create_dir(self.make_path('baz')) - directory = self.make_path('foo', 'bar') + self.create_dir(self.make_path("baz")) + directory = self.make_path("foo", "bar") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) self.os.removedirs(directory) @@ -1490,20 +1540,19 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_remove_dirs_with_top_symlink_fails(self): self.check_posix_only() - dir_path = self.make_path('dir') - dir_link = self.make_path('dir_link') + dir_path = self.make_path("dir") + dir_link = self.make_path("dir_link") self.create_dir(dir_path) self.os.symlink(dir_path, dir_link) - self.assert_raises_os_error(errno.ENOTDIR, - self.os.removedirs, dir_link) + self.assert_raises_os_error(errno.ENOTDIR, self.os.removedirs, dir_link) def test_remove_dirs_with_non_top_symlink_succeeds(self): self.check_posix_only() - dir_path = self.make_path('dir') - dir_link = self.make_path('dir_link') + dir_path = self.make_path("dir") + dir_link = self.make_path("dir_link") self.create_dir(dir_path) self.os.symlink(dir_path, dir_link) - dir_in_dir = self.os.path.join(dir_link, 'dir2') + dir_in_dir = self.os.path.join(dir_link, "dir2") self.create_dir(dir_in_dir) self.os.removedirs(dir_in_dir) self.assertFalse(self.os.path.exists(dir_in_dir)) @@ -1513,43 +1562,42 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_mkdir(self): """mkdir can create a relative directory.""" self.skip_real_fs() - directory = 'xyzzy' + directory = "xyzzy" self.assertFalse(self.filesystem.exists(directory)) self.os.mkdir(directory) - self.assertTrue(self.filesystem.exists('/%s' % directory)) + self.assertTrue(self.filesystem.exists("/%s" % directory)) self.os.chdir(directory) self.os.mkdir(directory) - self.assertTrue( - self.filesystem.exists('/%s/%s' % (directory, directory))) + self.assertTrue(self.filesystem.exists("/%s/%s" % (directory, directory))) self.os.chdir(directory) - self.os.mkdir('../abccb') - self.assertTrue(self.os.path.exists('/%s/abccb' % directory)) + self.os.mkdir("../abccb") + self.assertTrue(self.os.path.exists("/%s/abccb" % directory)) def test_mkdir_with_trailing_slash(self): """mkdir can create a directory named with a trailing slash.""" - directory = self.make_path('foo') + directory = self.make_path("foo") self.assertFalse(self.os.path.exists(directory)) self.os.mkdir(directory) self.assertTrue(self.os.path.exists(directory)) - self.assertTrue(self.os.path.exists(self.make_path('foo'))) + self.assertTrue(self.os.path.exists(self.make_path("foo"))) def test_mkdir_raises_if_empty_directory_name(self): - """mkdir raises exeption if creating directory named ''.""" - directory = '' + """mkdir raises exception if creating directory named ''.""" + directory = "" self.assert_raises_os_error(errno.ENOENT, self.os.mkdir, directory) def test_mkdir_raises_if_no_parent(self): """mkdir raises exception if parent directory does not exist.""" - parent = 'xyzzy' - directory = '%s/foo' % (parent,) + parent = "xyzzy" + directory = "%s/foo" % (parent,) self.assertFalse(self.os.path.exists(parent)) self.assert_raises_os_error(errno.ENOENT, self.os.mkdir, directory) def test_mkdir_raises_on_symlink_in_posix(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(base_path, 'link_to_dir') - dir_path = self.os.path.join(base_path, 'dir') + base_path = self.make_path("foo", "bar") + link_path = self.os.path.join(base_path, "link_to_dir") + dir_path = self.os.path.join(base_path, "dir") self.create_dir(dir_path) self.os.symlink(dir_path, link_path) self.assert_raises_os_error(errno.ENOTDIR, self.os.rmdir, link_path) @@ -1557,9 +1605,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_mkdir_removes_symlink_in_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - base_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(base_path, 'link_to_dir') - dir_path = self.os.path.join(base_path, 'dir') + base_path = self.make_path("foo", "bar") + link_path = self.os.path.join(base_path, "link_to_dir") + dir_path = self.os.path.join(base_path, "dir") self.create_dir(dir_path) self.os.symlink(dir_path, link_path) self.os.rmdir(link_path) @@ -1568,26 +1616,27 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_mkdir_raises_if_directory_exists(self): """mkdir raises exception if directory already exists.""" - directory = self.make_path('xyzzy') + directory = self.make_path("xyzzy") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, directory) def test_mkdir_raises_if_file_exists(self): """mkdir raises exception if name already exists as a file.""" - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, file_path) def check_mkdir_raises_if_parent_is_file(self, error_type): """mkdir raises exception if name already exists as a file.""" - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) - self.assert_raises_os_error(error_type, self.os.mkdir, - self.os.path.join(file_path, 'ff')) + self.assert_raises_os_error( + error_type, self.os.mkdir, self.os.path.join(file_path, "ff") + ) def test_mkdir_raises_if_parent_is_file_posix(self): self.check_posix_only() @@ -1600,64 +1649,59 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_mkdir_raises_with_slash_dot_posix(self): """mkdir raises exception if mkdir foo/. (trailing /.).""" self.check_posix_only() - self.assert_raises_os_error(errno.EEXIST, - self.os.mkdir, self.os.sep + '.') - directory = self.make_path('xyzzy', '.') + self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, self.os.sep + ".") + directory = self.make_path("xyzzy", ".") self.assert_raises_os_error(errno.ENOENT, self.os.mkdir, directory) - self.create_dir(self.make_path('xyzzy')) + self.create_dir(self.make_path("xyzzy")) self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, directory) def test_mkdir_raises_with_slash_dot_windows(self): """mkdir raises exception if mkdir foo/. (trailing /.).""" self.check_windows_only() - self.assert_raises_os_error(errno.EACCES, - self.os.mkdir, self.os.sep + '.') - directory = self.make_path('xyzzy', '.') + self.assert_raises_os_error(errno.EACCES, self.os.mkdir, self.os.sep + ".") + directory = self.make_path("xyzzy", ".") self.os.mkdir(directory) - self.create_dir(self.make_path('xyzzy')) + self.create_dir(self.make_path("xyzzy")) self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, directory) def test_mkdir_raises_with_double_dots_posix(self): """mkdir raises exception if mkdir foo/foo2/../foo3.""" self.check_posix_only() - self.assert_raises_os_error(errno.EEXIST, - self.os.mkdir, self.os.sep + '..') - directory = self.make_path('xyzzy', 'dir1', 'dir2', '..', '..', 'dir3') + self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, self.os.sep + "..") + directory = self.make_path("xyzzy", "dir1", "dir2", "..", "..", "dir3") self.assert_raises_os_error(errno.ENOENT, self.os.mkdir, directory) - self.create_dir(self.make_path('xyzzy')) + self.create_dir(self.make_path("xyzzy")) self.assert_raises_os_error(errno.ENOENT, self.os.mkdir, directory) - self.create_dir(self.make_path('xyzzy', 'dir1')) + self.create_dir(self.make_path("xyzzy", "dir1")) self.assert_raises_os_error(errno.ENOENT, self.os.mkdir, directory) - self.create_dir(self.make_path('xyzzy', 'dir1', 'dir2')) + self.create_dir(self.make_path("xyzzy", "dir1", "dir2")) self.os.mkdir(directory) self.assertTrue(self.os.path.exists(directory)) - directory = self.make_path('xyzzy', 'dir1', '..') + directory = self.make_path("xyzzy", "dir1", "..") self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, directory) def test_mkdir_raises_with_double_dots_windows(self): """mkdir raises exception if mkdir foo/foo2/../foo3.""" self.check_windows_only() - self.assert_raises_os_error(errno.EACCES, - self.os.mkdir, self.os.sep + '..') - directory = self.make_path( - 'xyzzy', 'dir1', 'dir2', '..', '..', 'dir3') + self.assert_raises_os_error(errno.EACCES, self.os.mkdir, self.os.sep + "..") + directory = self.make_path("xyzzy", "dir1", "dir2", "..", "..", "dir3") self.assert_raises_os_error(errno.ENOENT, self.os.mkdir, directory) - self.create_dir(self.make_path('xyzzy')) + self.create_dir(self.make_path("xyzzy")) self.os.mkdir(directory) self.assertTrue(self.os.path.exists(directory)) - directory = self.make_path('xyzzy', 'dir1', '..') + directory = self.make_path("xyzzy", "dir1", "..") self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, directory) def test_mkdir_raises_if_parent_is_read_only(self): """mkdir raises exception if parent is read only.""" self.check_posix_only() - directory = self.make_path('a') + directory = self.make_path("a") self.os.mkdir(directory) # Change directory permissions to be read only. self.os.chmod(directory, 0o400) - directory = self.make_path('a', 'b') + directory = self.make_path("a", "b") if not is_root(): self.assert_raises_os_error(errno.EACCES, self.os.mkdir, directory) else: @@ -1666,26 +1710,26 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_mkdir_with_with_symlink_parent(self): self.check_posix_only() - dir_path = self.make_path('foo', 'bar') + dir_path = self.make_path("foo", "bar") self.create_dir(dir_path) - link_path = self.make_path('foo', 'link') + link_path = self.make_path("foo", "link") self.os.symlink(dir_path, link_path) - new_dir = self.os.path.join(link_path, 'new_dir') + new_dir = self.os.path.join(link_path, "new_dir") self.os.mkdir(new_dir) self.assertTrue(self.os.path.exists(new_dir)) def test_makedirs(self): """makedirs can create a directory even if parent does not exist.""" - parent = self.make_path('xyzzy') - directory = self.os.path.join(parent, 'foo') + parent = self.make_path("xyzzy") + directory = self.os.path.join(parent, "foo") self.assertFalse(self.os.path.exists(parent)) self.os.makedirs(directory) self.assertTrue(self.os.path.exists(directory)) def check_makedirs_raises_if_parent_is_file(self, error_type): """makedirs raises exception if a parent component exists as a file.""" - file_path = self.make_path('xyzzy') - directory = self.os.path.join(file_path, 'plugh') + file_path = self.make_path("xyzzy") + directory = self.os.path.join(file_path, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) self.assert_raises_os_error(error_type, self.os.makedirs, directory) @@ -1700,40 +1744,43 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_makedirs_raises_if_parent_is_broken_link(self): self.check_posix_only() - link_path = self.make_path('broken_link') - self.os.symlink(self.make_path('bogus'), link_path) - self.assert_raises_os_error(errno.ENOENT, self.os.makedirs, - self.os.path.join(link_path, 'newdir')) + link_path = self.make_path("broken_link") + self.os.symlink(self.make_path("bogus"), link_path) + self.assert_raises_os_error( + errno.ENOENT, + self.os.makedirs, + self.os.path.join(link_path, "newdir"), + ) def test_makedirs_raises_if_parent_is_looping_link(self): self.skip_if_symlink_not_supported() - link_path = self.make_path('link') - link_target = self.os.path.join(link_path, 'link') + link_path = self.make_path("link") + link_target = self.os.path.join(link_path, "link") self.os.symlink(link_target, link_path) self.assert_raises_os_error(errno.EEXIST, self.os.makedirs, link_path) def test_makedirs_if_parent_is_symlink(self): self.check_posix_only() - base_dir = self.make_path('foo', 'bar') + base_dir = self.make_path("foo", "bar") self.create_dir(base_dir) - link_dir = self.os.path.join(base_dir, 'linked') + link_dir = self.os.path.join(base_dir, "linked") self.os.symlink(base_dir, link_dir) - new_dir = self.os.path.join(link_dir, 'f') + new_dir = self.os.path.join(link_dir, "f") self.os.makedirs(name=new_dir) self.assertTrue(self.os.path.exists(new_dir)) def test_makedirs_raises_if_access_denied(self): """makedirs raises exception if access denied.""" self.check_posix_only() - directory = self.make_path('a') + directory = self.make_path("a") self.os.mkdir(directory) # Change directory permissions to be read only. self.os.chmod(directory, 0o400) - directory = self.make_path('a', 'b') + directory = self.make_path("a", "b") if not is_root(): - with self.assertRaises(Exception): + with self.assertRaises(OSError): self.os.makedirs(directory) else: self.os.makedirs(directory) @@ -1741,7 +1788,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_makedirs_exist_ok(self): """makedirs uses the exist_ok argument""" - directory = self.make_path('xyzzy', 'foo') + directory = self.make_path("xyzzy", "foo") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) @@ -1751,23 +1798,23 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_makedirs_in_write_protected_dir(self): self.check_posix_only() - directory = self.make_path('foo') + directory = self.make_path("foo") self.os.mkdir(directory, mode=0o555) - subdir = self.os.path.join(directory, 'bar') + subdir = self.os.path.join(directory, "bar") if not is_root(): - self.assert_raises_os_error(errno.EACCES, self.os.makedirs, - subdir, exist_ok=True) - self.assert_raises_os_error(errno.EACCES, self.os.makedirs, - subdir, exist_ok=False) + self.assert_raises_os_error( + errno.EACCES, self.os.makedirs, subdir, exist_ok=True + ) + self.assert_raises_os_error( + errno.EACCES, self.os.makedirs, subdir, exist_ok=False + ) else: self.os.makedirs(subdir) self.assertTrue(self.os.path.exists(subdir)) def test_makedirs_raises_on_empty_path(self): - self.assert_raises_os_error( - errno.ENOENT, self.os.makedirs, '', exist_ok=False) - self.assert_raises_os_error( - errno.ENOENT, self.os.makedirs, '', exist_ok=True) + self.assert_raises_os_error(errno.ENOENT, self.os.makedirs, "", exist_ok=False) + self.assert_raises_os_error(errno.ENOENT, self.os.makedirs, "", exist_ok=True) # test fsync and fdatasync def test_fsync_raises_on_non_int(self): @@ -1789,48 +1836,45 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_fsync_pass_posix(self): self.check_posix_only() - test_file_path = self.make_path('test_file') - self.create_file(test_file_path, contents='dummy file contents') - with self.open(test_file_path, 'r') as test_file: + test_file_path = self.make_path("test_file") + self.create_file(test_file_path, contents="dummy file contents") + with self.open(test_file_path, "r") as test_file: test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fsync(test_fd) # And just for sanity, double-check that this still raises - self.assert_raises_os_error(errno.EBADF, - self.os.fsync, test_fd + 500) + self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 500) def test_fsync_pass_windows(self): self.check_windows_only() - test_file_path = self.make_path('test_file') - self.create_file(test_file_path, contents='dummy file contents') - with self.open(test_file_path, 'r+') as test_file: + test_file_path = self.make_path("test_file") + self.create_file(test_file_path, contents="dummy file contents") + with self.open(test_file_path, "r+") as test_file: test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fsync(test_fd) # And just for sanity, double-check that this still raises - self.assert_raises_os_error(errno.EBADF, - self.os.fsync, test_fd + 500) - with self.open(test_file_path, 'r') as test_file: + self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 500) + with self.open(test_file_path, "r") as test_file: test_fd = test_file.fileno() self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd) def test_fdatasync_pass(self): # setup self.check_linux_only() - test_file_path = self.make_path('test_file') - self.create_file(test_file_path, contents='dummy file contents') - test_file = self.open(test_file_path, 'r') + test_file_path = self.make_path("test_file") + self.create_file(test_file_path, contents="dummy file contents") + test_file = self.open(test_file_path, "r") test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fdatasync(test_fd) # And just for sanity, double-check that this still raises - self.assert_raises_os_error(errno.EBADF, - self.os.fdatasync, test_fd + 500) + self.assert_raises_os_error(errno.EBADF, self.os.fdatasync, test_fd + 500) def test_access700(self): # set up self.check_posix_only() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) self.os.chmod(path, 0o700) self.assert_mode_equal(0o700, self.os.stat(path).st_mode) @@ -1844,7 +1888,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_access600(self): # set up self.check_posix_only() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) self.os.chmod(path, 0o600) self.assert_mode_equal(0o600, self.os.stat(path).st_mode) @@ -1859,7 +1903,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_access400(self): # set up self.check_posix_only() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) self.os.chmod(path, 0o400) self.assert_mode_equal(0o400, self.os.stat(path).st_mode) @@ -1878,9 +1922,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_access_symlink(self): self.skip_if_symlink_not_supported() self.skip_real_fs() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) - link_path = self.make_path('link_to_some_file') + link_path = self.make_path("link_to_some_file") self.create_symlink(link_path, path) self.os.chmod(link_path, 0o400) @@ -1897,22 +1941,16 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.assertFalse(self.os.access(link_path, self.rwx)) # test link itself - self.assertTrue( - self.os.access(link_path, self.os.F_OK, follow_symlinks=False)) - self.assertTrue( - self.os.access(link_path, self.os.R_OK, follow_symlinks=False)) - self.assertTrue( - self.os.access(link_path, self.os.W_OK, follow_symlinks=False)) - self.assertTrue( - self.os.access(link_path, self.os.X_OK, follow_symlinks=False)) - self.assertTrue( - self.os.access(link_path, self.rwx, follow_symlinks=False)) - self.assertTrue( - self.os.access(link_path, self.rw, follow_symlinks=False)) + self.assertTrue(self.os.access(link_path, self.os.F_OK, follow_symlinks=False)) + self.assertTrue(self.os.access(link_path, self.os.R_OK, follow_symlinks=False)) + self.assertTrue(self.os.access(link_path, self.os.W_OK, follow_symlinks=False)) + self.assertTrue(self.os.access(link_path, self.os.X_OK, follow_symlinks=False)) + self.assertTrue(self.os.access(link_path, self.rwx, follow_symlinks=False)) + self.assertTrue(self.os.access(link_path, self.rw, follow_symlinks=False)) def test_access_non_existent_file(self): # set up - path = self.make_path('non', 'existent', 'file') + path = self.make_path("non", "existent", "file") self.assertFalse(self.os.path.exists(path)) # actual tests self.assertFalse(self.os.access(path, self.os.F_OK)) @@ -1924,7 +1962,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_effective_ids_not_supported_under_windows(self): self.check_windows_only() - path = self.make_path('foo', 'bar') + path = self.make_path("foo", "bar") with self.assertRaises(NotImplementedError): self.os.access(path, self.os.F_OK, effective_ids=True) @@ -1932,7 +1970,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # set up self.check_posix_only() self.skip_real_fs() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) # actual tests self.os.chmod(path, 0o6543) @@ -1945,7 +1983,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.check_posix_only() self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.chmod, 5, 0o6543) - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) with self.open(path) as f: @@ -1955,9 +1993,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chmod_follow_symlink(self): self.check_posix_only() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) - link_path = self.make_path('link_to_some_file') + link_path = self.make_path("link_to_some_file") self.create_symlink(link_path, path) self.os.chmod(link_path, 0o6543) @@ -1969,11 +2007,11 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chmod_no_follow_symlink(self): self.check_posix_only() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) - link_path = self.make_path('link_to_some_file') + link_path = self.make_path("link_to_some_file") self.create_symlink(link_path, path) - if os.chmod not in os.supports_follow_symlinks or IS_PYPY: + if self.os.chmod not in self.os.supports_follow_symlinks or IS_PYPY: with self.assertRaises(NotImplementedError): self.os.chmod(link_path, 0o6543, follow_symlinks=False) else: @@ -1987,9 +2025,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): """lchmod shall behave like chmod with follow_symlinks=True.""" self.check_posix_only() self.skip_real_fs() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) - link_path = self.make_path('link_to_some_file') + link_path = self.make_path("link_to_some_file") self.create_symlink(link_path, path) self.os.lchmod(link_path, 0o6543) @@ -2002,7 +2040,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # set up self.check_posix_only() self.skip_real_fs() - path = self.make_path('some_dir') + path = self.make_path("some_dir") self.createTestDirectory(path) # actual tests self.os.chmod(path, 0o1434) @@ -2013,13 +2051,13 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chmod_non_existent(self): # set up - path = self.make_path('non', 'existent', 'file') + path = self.make_path("non", "existent", "file") self.assertFalse(self.os.path.exists(path)) # actual tests try: # Use try-catch to check exception attributes. self.os.chmod(path, 0o777) - self.fail('Exception is expected.') # COV_NF_LINE + self.fail("Exception is expected.") # COV_NF_LINE except OSError as os_error: self.assertEqual(errno.ENOENT, os_error.errno) self.assertEqual(path, os_error.filename) @@ -2027,7 +2065,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chown_existing_file(self): # set up self.skip_real_fs() - file_path = self.make_path('some_file') + file_path = self.make_path("some_file") self.create_file(file_path) # first set it make sure it's set self.os.chown(file_path, 100, 101) @@ -2049,7 +2087,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.check_posix_only() self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.chown, 5, 100, 101) - file_path = self.make_path('foo', 'bar') + file_path = self.make_path("foo", "bar") self.create_file(file_path) with self.open(file_path) as f: @@ -2059,9 +2097,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chown_follow_symlink(self): self.skip_real_fs() - file_path = self.make_path('some_file') + file_path = self.make_path("some_file") self.create_file(file_path) - link_path = self.make_path('link_to_some_file') + link_path = self.make_path("link_to_some_file") self.create_symlink(link_path, file_path) self.os.chown(link_path, 100, 101) @@ -2074,9 +2112,9 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chown_no_follow_symlink(self): self.skip_real_fs() - file_path = self.make_path('some_file') + file_path = self.make_path("some_file") self.create_file(file_path) - link_path = self.make_path('link_to_some_file') + link_path = self.make_path("link_to_some_file") self.create_symlink(link_path, file_path) self.os.chown(link_path, 100, 101, follow_symlinks=False) @@ -2090,23 +2128,22 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_chown_bad_arguments(self): """os.chown() with bad args (Issue #30)""" self.check_posix_only() - file_path = self.make_path('some_file') + file_path = self.make_path("some_file") self.create_file(file_path) - self.assertRaises(TypeError, self.os.chown, file_path, 'username', -1) - self.assertRaises(TypeError, self.os.chown, file_path, -1, 'groupname') + self.assertRaises(TypeError, self.os.chown, file_path, "username", -1) + self.assertRaises(TypeError, self.os.chown, file_path, -1, "groupname") def test_chown_nonexisting_file_should_raise_os_error(self): self.check_posix_only() - file_path = self.make_path('some_file') + file_path = self.make_path("some_file") self.assertFalse(self.os.path.exists(file_path)) - self.assert_raises_os_error( - errno.ENOENT, self.os.chown, file_path, 100, 100) + self.assert_raises_os_error(errno.ENOENT, self.os.chown, file_path, 100, 100) def test_classify_directory_contents(self): """Directory classification should work correctly.""" - root_directory = self.make_path('foo') - test_directories = ['bar1', 'baz2'] - test_files = ['baz1', 'bar2', 'baz3'] + root_directory = self.make_path("foo") + test_directories = ["bar1", "baz2"] + test_files = ["baz1", "bar2", "baz3"] self.create_dir(root_directory) for directory in test_directories: directory = self.os.path.join(root_directory, directory) @@ -2129,7 +2166,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # so we test it under Linux only def test_mk_nod_can_create_a_file(self): self.check_linux_only() - filename = self.make_path('foo') + filename = self.make_path("foo") self.assertFalse(self.os.path.exists(filename)) self.os.mknod(filename) self.assertTrue(self.os.path.exists(filename)) @@ -2137,48 +2174,48 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_mk_nod_raises_if_empty_file_name(self): self.check_linux_only() - filename = '' + filename = "" self.assert_raises_os_error(errno.ENOENT, self.os.mknod, filename) def test_mk_nod_raises_if_parent_dir_doesnt_exist(self): self.check_linux_only() - parent = self.make_path('xyzzy') - filename = self.os.path.join(parent, 'foo') + parent = self.make_path("xyzzy") + filename = self.os.path.join(parent, "foo") self.assertFalse(self.os.path.exists(parent)) self.assert_raises_os_error(errno.ENOENT, self.os.mknod, filename) def test_mk_nod_raises_if_file_exists(self): self.check_linux_only() - filename = self.make_path('tmp', 'foo') + filename = self.make_path("tmp", "foo") self.create_file(filename) self.assertTrue(self.os.path.exists(filename)) self.assert_raises_os_error(errno.EEXIST, self.os.mknod, filename) def test_mk_nod_raises_if_filename_is_dot(self): self.check_linux_only() - filename = self.make_path('tmp', '.') + filename = self.make_path("tmp", ".") self.assert_raises_os_error(errno.ENOENT, self.os.mknod, filename) def test_mk_nod_raises_if_filename_is_double_dot(self): self.check_linux_only() - filename = self.make_path('tmp', '..') + filename = self.make_path("tmp", "..") self.assert_raises_os_error(errno.ENOENT, self.os.mknod, filename) def test_mknod_empty_tail_for_existing_file_raises(self): self.check_linux_only() - filename = self.make_path('foo') + filename = self.make_path("foo") self.create_file(filename) self.assertTrue(self.os.path.exists(filename)) self.assert_raises_os_error(errno.EEXIST, self.os.mknod, filename) def test_mknod_empty_tail_for_nonexistent_file_raises(self): self.check_linux_only() - filename = self.make_path('tmp', 'foo') + filename = self.make_path("tmp", "foo") self.assert_raises_os_error(errno.ENOENT, self.os.mknod, filename) def test_mknod_raises_if_filename_is_empty_string(self): self.check_linux_only() - filename = '' + filename = "" self.assert_raises_os_error(errno.ENOENT, self.os.mknod, filename) def test_mknod_raises_if_unsupported_options(self): @@ -2186,119 +2223,129 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # behavior seems to have changed in ubuntu-20.04, version 20210606.1 # skipping real fs tests for now self.skip_real_fs() - filename = 'abcde' + filename = "abcde" if not is_root(): - self.assert_raises_os_error(errno.EPERM, self.os.mknod, filename, - stat.S_IFCHR) + self.assert_raises_os_error( + errno.EPERM, self.os.mknod, filename, stat.S_IFCHR + ) else: self.os.mknod(filename, stat.S_IFCHR) self.os.remove(filename) def test_mknod_raises_if_parent_is_not_a_directory(self): self.check_linux_only() - filename1 = self.make_path('foo') + filename1 = self.make_path("foo") self.create_file(filename1) self.assertTrue(self.os.path.exists(filename1)) - filename2 = self.make_path('foo', 'bar') + filename2 = self.make_path("foo", "bar") self.assert_raises_os_error(errno.ENOTDIR, self.os.mknod, filename2) def test_symlink(self): self.skip_if_symlink_not_supported() - file_path = self.make_path('foo', 'bar', 'baz') - self.create_dir(self.make_path('foo', 'bar')) - self.os.symlink('bogus', file_path) + file_path = self.make_path("foo", "bar", "baz") + self.create_dir(self.make_path("foo", "bar")) + self.os.symlink("bogus", file_path) self.assertTrue(self.os.path.lexists(file_path)) self.assertFalse(self.os.path.exists(file_path)) - self.create_file(self.make_path('foo', 'bar', 'bogus')) + self.create_file(self.make_path("foo", "bar", "bogus")) self.assertTrue(self.os.path.lexists(file_path)) self.assertTrue(self.os.path.exists(file_path)) def test_symlink_on_nonexisting_path_raises(self): self.check_posix_only() - dir_path = self.make_path('bar') - link_path = self.os.path.join(dir_path, 'bar') - self.assert_raises_os_error(errno.ENOENT, self.os.symlink, link_path, - link_path) - self.assert_raises_os_error(errno.ENOENT, self.os.symlink, dir_path, - link_path) + dir_path = self.make_path("bar") + link_path = self.os.path.join(dir_path, "bar") + self.assert_raises_os_error(errno.ENOENT, self.os.symlink, link_path, link_path) + self.assert_raises_os_error(errno.ENOENT, self.os.symlink, dir_path, link_path) def test_symlink_with_path_ending_with_sep_in_posix(self): self.check_posix_only() - dir_path = self.make_path('dir') + dir_path = self.make_path("dir") self.create_dir(dir_path) - self.assert_raises_os_error(errno.EEXIST, self.os.symlink, - self.base_path, dir_path + self.os.sep) + self.assert_raises_os_error( + errno.EEXIST, + self.os.symlink, + self.base_path, + dir_path + self.os.sep, + ) - dir_path = self.make_path('bar') - self.assert_raises_os_error(errno.ENOENT, self.os.symlink, - self.base_path, dir_path + self.os.sep) + dir_path = self.make_path("bar") + self.assert_raises_os_error( + errno.ENOENT, + self.os.symlink, + self.base_path, + dir_path + self.os.sep, + ) def test_symlink_with_path_ending_with_sep_in_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - dir_path = self.make_path('dir') + dir_path = self.make_path("dir") self.create_dir(dir_path) - self.assert_raises_os_error(errno.EEXIST, self.os.symlink, - self.base_path, dir_path + self.os.sep) + self.assert_raises_os_error( + errno.EEXIST, + self.os.symlink, + self.base_path, + dir_path + self.os.sep, + ) - dir_path = self.make_path('bar') + dir_path = self.make_path("bar") # does not raise under Windows self.os.symlink(self.base_path, dir_path + self.os.sep) def test_broken_symlink_with_trailing_sep_posix(self): # Regression test for #390 self.check_linux_only() - path0 = self.make_path('foo') + self.os.sep - self.assert_raises_os_error( - errno.ENOENT, self.os.symlink, path0, path0) + path0 = self.make_path("foo") + self.os.sep + self.assert_raises_os_error(errno.ENOENT, self.os.symlink, path0, path0) def test_broken_symlink_with_trailing_sep_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - path0 = self.make_path('foo') + self.os.sep - self.assert_raises_os_error( - errno.EINVAL, self.os.symlink, path0, path0) + path0 = self.make_path("foo") + self.os.sep + self.assert_raises_os_error(errno.EINVAL, self.os.symlink, path0, path0) def test_rename_symlink_with_trailing_sep_linux(self): # Regression test for #391 self.check_linux_only() - path = self.make_path('foo') + path = self.make_path("foo") self.os.symlink(self.base_path, path) - self.assert_raises_os_error(errno.ENOTDIR, self.os.rename, - path + self.os.sep, self.base_path) + self.assert_raises_os_error( + errno.ENOTDIR, self.os.rename, path + self.os.sep, self.base_path + ) def test_rename_symlink_with_trailing_sep_macos(self): # Regression test for #391 self.check_macos_only() - path = self.make_path('foo') + path = self.make_path("foo") self.os.symlink(self.base_path, path) self.os.rename(path + self.os.sep, self.base_path) def test_rename_symlink_with_trailing_sep_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - path = self.make_path('foo') + path = self.make_path("foo") self.os.symlink(self.base_path, path) - self.assert_raises_os_error(errno.EEXIST, self.os.rename, - path + self.os.sep, self.base_path) + self.assert_raises_os_error( + errno.EEXIST, self.os.rename, path + self.os.sep, self.base_path + ) def test_rename_symlink_to_other_case(self): # Regression test for #389 self.skip_if_symlink_not_supported() - link_path = self.make_path('foo') + link_path = self.make_path("foo") self.os.symlink(self.base_path, link_path) - link_to_link_path = self.make_path('BAR') + link_to_link_path = self.make_path("BAR") self.os.symlink(link_path, link_to_link_path) - new_link_to_link_path = self.os.path.join(link_path, 'bar') + new_link_to_link_path = self.os.path.join(link_path, "bar") self.os.rename(link_to_link_path, new_link_to_link_path) - self.assertEqual(['bar', 'foo'], - sorted(self.os.listdir(new_link_to_link_path))) + self.assertEqual(["bar", "foo"], sorted(self.os.listdir(new_link_to_link_path))) def create_broken_link_path_with_trailing_sep(self): # Regression tests for #396 self.skip_if_symlink_not_supported() - link_path = self.make_path('link') - target_path = self.make_path('target') + link_path = self.make_path("link") + target_path = self.make_path("target") self.os.symlink(target_path, link_path) link_path += self.os.sep return link_path @@ -2353,19 +2400,22 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.check_linux_only() link_path = self.create_broken_link_path_with_trailing_sep() self.assert_raises_os_error( - errno.ENOTDIR, self.os.rename, link_path, self.make_path('target')) + errno.ENOTDIR, self.os.rename, link_path, self.make_path("target") + ) def test_rename_broken_link_with_trailing_sep_macos(self): self.check_macos_only() link_path = self.create_broken_link_path_with_trailing_sep() self.assert_raises_os_error( - errno.ENOENT, self.os.rename, link_path, self.make_path('target')) + errno.ENOENT, self.os.rename, link_path, self.make_path("target") + ) def test_rename_broken_link_with_trailing_sep_windows(self): self.check_windows_only() link_path = self.create_broken_link_path_with_trailing_sep() self.assert_raises_os_error( - errno.EINVAL, self.os.rename, link_path, self.make_path('target')) + errno.EINVAL, self.os.rename, link_path, self.make_path("target") + ) def test_readlink_broken_link_with_trailing_sep_posix(self): self.check_posix_only() @@ -2388,26 +2438,27 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rename_link_with_trailing_sep_to_self_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - path = self.make_path('foo') + path = self.make_path("foo") self.os.symlink(self.base_path, path) self.os.rename(path + self.os.sep, path) # no error def test_rename_link_with_trailing_sep_to_self_posix(self): # Regression test for #395 self.check_posix_only() - path = self.make_path('foo') + path = self.make_path("foo") self.os.symlink(self.base_path, path) self.assert_raises_os_error( - errno.ENOTDIR, self.os.rename, path + self.os.sep, path) + errno.ENOTDIR, self.os.rename, path + self.os.sep, path + ) def check_open_broken_symlink_to_path_with_trailing_sep(self, error): # Regression tests for #397 self.skip_if_symlink_not_supported() - target_path = self.make_path('target') + self.os.sep - link_path = self.make_path('link') + target_path = self.make_path("target") + self.os.sep + link_path = self.make_path("link") self.os.symlink(target_path, link_path) - self.assert_raises_os_error(error, self.open, link_path, 'a') - self.assert_raises_os_error(error, self.open, link_path, 'w') + self.assert_raises_os_error(error, self.open, link_path, "a") + self.assert_raises_os_error(error, self.open, link_path, "w") def test_open_broken_symlink_to_path_with_trailing_sep_linux(self): self.check_linux_only() @@ -2424,11 +2475,12 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def check_link_path_ending_with_sep(self, error): # Regression tests for #399 self.skip_if_symlink_not_supported() - file_path = self.make_path('foo') - link_path = self.make_path('link') - with self.open(file_path, 'w'): + file_path = self.make_path("foo") + link_path = self.make_path("link") + with self.open(file_path, "w"): self.assert_raises_os_error( - error, self.os.link, file_path + self.os.sep, link_path) + error, self.os.link, file_path + self.os.sep, link_path + ) def test_link_path_ending_with_sep_posix(self): self.check_posix_only() @@ -2441,27 +2493,27 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_link_to_path_ending_with_sep_posix(self): # regression test for #407 self.check_posix_only() - path0 = self.make_path('foo') + self.os.sep - path1 = self.make_path('bar') - with self.open(path1, 'w'): - self.assert_raises_os_error(errno.ENOENT, - self.os.link, path1, path0) + path0 = self.make_path("foo") + self.os.sep + path1 = self.make_path("bar") + with self.open(path1, "w"): + self.assert_raises_os_error(errno.ENOENT, self.os.link, path1, path0) def test_link_to_path_ending_with_sep_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - path0 = self.make_path('foo') + self.os.sep - path1 = self.make_path('bar') - with self.open(path1, 'w'): + path0 = self.make_path("foo") + self.os.sep + path1 = self.make_path("bar") + with self.open(path1, "w"): self.os.link(path1, path0) self.assertTrue(self.os.path.exists(path1)) def check_rename_to_path_ending_with_sep(self, error): # Regression tests for #400 - file_path = self.make_path('foo') - with self.open(file_path, 'w'): + file_path = self.make_path("foo") + with self.open(file_path, "w"): self.assert_raises_os_error( - error, self.os.rename, file_path + self.os.sep, file_path) + error, self.os.rename, file_path + self.os.sep, file_path + ) def test_rename_to_path_ending_with_sep_posix(self): self.check_posix_only() @@ -2473,19 +2525,20 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rmdir_link_with_trailing_sep_linux(self): self.check_linux_only() - dir_path = self.make_path('foo') + dir_path = self.make_path("foo") self.os.mkdir(dir_path) - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.symlink(dir_path, link_path) self.assert_raises_os_error( - errno.ENOTDIR, self.os.rmdir, link_path + self.os.sep) + errno.ENOTDIR, self.os.rmdir, link_path + self.os.sep + ) def test_rmdir_link_with_trailing_sep_macos(self): # Regression test for #398 self.check_macos_only() - dir_path = self.make_path('foo') + dir_path = self.make_path("foo") self.os.mkdir(dir_path) - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.symlink(dir_path, link_path) self.os.rmdir(link_path + self.os.sep) self.assertFalse(self.os.path.exists(link_path)) @@ -2493,27 +2546,26 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_rmdir_link_with_trailing_sep_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - dir_path = self.make_path('foo') + dir_path = self.make_path("foo") self.os.mkdir(dir_path) - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.symlink(dir_path, link_path) self.os.rmdir(link_path + self.os.sep) self.assertFalse(self.os.path.exists(link_path)) def test_readlink_circular_link_with_trailing_sep_linux(self): self.check_linux_only() - path1 = self.make_path('foo') - path0 = self.make_path('bar') + path1 = self.make_path("foo") + path0 = self.make_path("bar") self.os.symlink(path0, path1) self.os.symlink(path1, path0) - self.assert_raises_os_error( - errno.ELOOP, self.os.readlink, path0 + self.os.sep) + self.assert_raises_os_error(errno.ELOOP, self.os.readlink, path0 + self.os.sep) def test_readlink_circular_link_with_trailing_sep_macos(self): # Regression test for #392 self.check_macos_only() - path1 = self.make_path('foo') - path0 = self.make_path('bar') + path1 = self.make_path("foo") + path0 = self.make_path("bar") self.os.symlink(path0, path1) self.os.symlink(path1, path0) self.assertEqual(path0, self.os.readlink(path0 + self.os.sep)) @@ -2521,27 +2573,26 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_readlink_circular_link_with_trailing_sep_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - path1 = self.make_path('foo') - path0 = self.make_path('bar') + path1 = self.make_path("foo") + path0 = self.make_path("bar") self.os.symlink(path0, path1) self.os.symlink(path1, path0) - self.assert_raises_os_error( - errno.EINVAL, self.os.readlink, path0 + self.os.sep) + self.assert_raises_os_error(errno.EINVAL, self.os.readlink, path0 + self.os.sep) # hard link related tests def test_link_bogus(self): # trying to create a link from a non-existent file should fail self.skip_if_symlink_not_supported() - self.assert_raises_os_error(errno.ENOENT, - self.os.link, '/nonexistent_source', - '/link_dest') + self.assert_raises_os_error( + errno.ENOENT, self.os.link, "/nonexistent_source", "/link_dest" + ) def test_link_delete(self): self.skip_if_symlink_not_supported() - file1_path = self.make_path('test_file1') - file2_path = self.make_path('test_file2') - contents1 = 'abcdef' + file1_path = self.make_path("test_file1") + file2_path = self.make_path("test_file2") + contents1 = "abcdef" # Create file self.create_file(file1_path, contents=contents1) # link to second file @@ -2556,10 +2607,10 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_link_update(self): self.skip_if_symlink_not_supported() - file1_path = self.make_path('test_file1') - file2_path = self.make_path('test_file2') - contents1 = 'abcdef' - contents2 = 'ghijkl' + file1_path = self.make_path("test_file1") + file2_path = self.make_path("test_file2") + contents1 = "abcdef" + contents2 = "ghijkl" # Create file and link self.create_file(file1_path, contents=contents1) self.os.link(file1_path, file2_path) @@ -2567,7 +2618,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): with self.open(file2_path) as f: self.assertEqual(f.read(), contents1) # update the first file - with self.open(file1_path, 'w') as f: + with self.open(file1_path, "w") as f: f.write(contents2) # assert that second file contains contents2 with self.open(file2_path) as f: @@ -2575,46 +2626,44 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_link_non_existent_parent(self): self.skip_if_symlink_not_supported() - file1_path = self.make_path('test_file1') - breaking_link_path = self.make_path('nonexistent', 'test_file2') - contents1 = 'abcdef' + file1_path = self.make_path("test_file1") + breaking_link_path = self.make_path("nonexistent", "test_file2") + contents1 = "abcdef" # Create file and link self.create_file(file1_path, contents=contents1) # trying to create a link under a non-existent directory should fail self.assert_raises_os_error( - errno.ENOENT, self.os.link, file1_path, breaking_link_path) + errno.ENOENT, self.os.link, file1_path, breaking_link_path + ) def test_link_is_existing_file(self): self.skip_if_symlink_not_supported() - file_path = self.make_path('foo', 'bar') + file_path = self.make_path("foo", "bar") self.create_file(file_path) - self.assert_raises_os_error(errno.EEXIST, self.os.link, file_path, - file_path) + self.assert_raises_os_error(errno.EEXIST, self.os.link, file_path, file_path) def test_link_target_is_dir_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - dir_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(dir_path, 'link') + dir_path = self.make_path("foo", "bar") + link_path = self.os.path.join(dir_path, "link") self.create_dir(dir_path) - self.assert_raises_os_error(errno.EACCES, self.os.link, dir_path, - link_path) + self.assert_raises_os_error(errno.EACCES, self.os.link, dir_path, link_path) def test_link_target_is_dir_posix(self): self.check_posix_only() - dir_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(dir_path, 'link') + dir_path = self.make_path("foo", "bar") + link_path = self.os.path.join(dir_path, "link") self.create_dir(dir_path) - self.assert_raises_os_error(errno.EPERM, self.os.link, dir_path, - link_path) + self.assert_raises_os_error(errno.EPERM, self.os.link, dir_path, link_path) def test_link_count1(self): """Test that hard link counts are updated correctly.""" self.skip_if_symlink_not_supported() - file1_path = self.make_path('test_file1') - file2_path = self.make_path('test_file2') - file3_path = self.make_path('test_file3') + file1_path = self.make_path("test_file1") + file2_path = self.make_path("test_file2") + file3_path = self.make_path("test_file3") self.create_file(file1_path) # initial link count should be one self.assertEqual(self.os.stat(file1_path).st_nlink, 1) @@ -2637,15 +2686,15 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_nlink_for_directories(self): self.skip_real_fs() - self.create_dir(self.make_path('foo', 'bar')) - self.create_file(self.make_path('foo', 'baz')) - self.assertEqual(2, self.filesystem.get_object( - self.make_path('foo', 'bar')).st_nlink) - self.assertEqual(4, self.filesystem.get_object( - self.make_path('foo')).st_nlink) - self.create_file(self.make_path('foo', 'baz2')) - self.assertEqual(5, self.filesystem.get_object( - self.make_path('foo')).st_nlink) + self.create_dir(self.make_path("foo", "bar")) + self.create_file(self.make_path("foo", "baz")) + self.assertEqual( + 2, + self.filesystem.get_object(self.make_path("foo", "bar")).st_nlink, + ) + self.assertEqual(4, self.filesystem.get_object(self.make_path("foo")).st_nlink) + self.create_file(self.make_path("foo", "baz2")) + self.assertEqual(5, self.filesystem.get_object(self.make_path("foo")).st_nlink) def test_umask(self): self.check_posix_only() @@ -2657,11 +2706,11 @@ class FakeOsModuleTest(FakeOsModuleTestBase): """mkdir creates a directory with umask applied.""" self.check_posix_only() self.os.umask(0o22) - dir1 = self.make_path('dir1') + dir1 = self.make_path("dir1") self.os.mkdir(dir1) self.assert_mode_equal(0o755, self.os.stat(dir1).st_mode) self.os.umask(0o67) - dir2 = self.make_path('dir2') + dir2 = self.make_path("dir2") self.os.mkdir(dir2) self.assert_mode_equal(0o710, self.os.stat(dir2).st_mode) @@ -2669,28 +2718,28 @@ class FakeOsModuleTest(FakeOsModuleTestBase): """makedirs creates a directories with umask applied.""" self.check_posix_only() self.os.umask(0o22) - self.os.makedirs(self.make_path('p1', 'dir1')) + self.os.makedirs(self.make_path("p1", "dir1")) + self.assert_mode_equal(0o755, self.os.stat(self.make_path("p1")).st_mode) self.assert_mode_equal( - 0o755, self.os.stat(self.make_path('p1')).st_mode) - self.assert_mode_equal( - 0o755, self.os.stat(self.make_path('p1', 'dir1')).st_mode) + 0o755, self.os.stat(self.make_path("p1", "dir1")).st_mode + ) self.os.umask(0o67) - self.os.makedirs(self.make_path('p2', 'dir2')) - self.assert_mode_equal( - 0o710, self.os.stat(self.make_path('p2')).st_mode) + self.os.makedirs(self.make_path("p2", "dir2")) + self.assert_mode_equal(0o710, self.os.stat(self.make_path("p2")).st_mode) self.assert_mode_equal( - 0o710, self.os.stat(self.make_path('p2', 'dir2')).st_mode) + 0o710, self.os.stat(self.make_path("p2", "dir2")).st_mode + ) def test_mknod_umask_applied(self): """mkdir creates a device with umask applied.""" # skipping MacOs due to mknod permission issues self.check_linux_only() self.os.umask(0o22) - node1 = self.make_path('nod1') + node1 = self.make_path("nod1") self.os.mknod(node1, stat.S_IFREG | 0o666) self.assert_mode_equal(0o644, self.os.stat(node1).st_mode) self.os.umask(0o27) - node2 = self.make_path('nod2') + node2 = self.make_path("nod2") self.os.mknod(node2, stat.S_IFREG | 0o666) self.assert_mode_equal(0o640, self.os.stat(node2).st_mode) @@ -2698,12 +2747,12 @@ class FakeOsModuleTest(FakeOsModuleTestBase): """open creates a file with umask applied.""" self.check_posix_only() self.os.umask(0o22) - file1 = self.make_path('file1') - self.open(file1, 'w').close() + file1 = self.make_path("file1") + self.open(file1, "w").close() self.assert_mode_equal(0o644, self.os.stat(file1).st_mode) self.os.umask(0o27) - file2 = self.make_path('file2') - self.open(file2, 'w').close() + file2 = self.make_path("file2") + self.open(file2, "w").close() self.assert_mode_equal(0o640, self.os.stat(file2).st_mode) def test_open_pipe(self): @@ -2712,7 +2761,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): self.os.close(write_fd) def test_open_pipe_with_existing_fd(self): - file1 = self.make_path('file1') + file1 = self.make_path("file1") fd = self.os.open(file1, os.O_CREAT) read_fd, write_fd = self.os.pipe() self.assertGreater(read_fd, fd) @@ -2722,7 +2771,7 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_open_file_with_existing_pipe(self): read_fd, write_fd = self.os.pipe() - file1 = self.make_path('file1') + file1 = self.make_path("file1") fd = self.os.open(file1, os.O_CREAT) self.assertGreater(fd, write_fd) self.os.close(read_fd) @@ -2731,8 +2780,8 @@ class FakeOsModuleTest(FakeOsModuleTestBase): def test_read_write_pipe(self): read_fd, write_fd = self.os.pipe() - self.assertEqual(4, self.os.write(write_fd, b'test')) - self.assertEqual(b'test', self.os.read(read_fd, 4)) + self.assertEqual(4, self.os.write(write_fd, b"test")) + self.assertEqual(b"test", self.os.read(read_fd, 4)) self.os.close(read_fd) self.os.close(write_fd) @@ -2741,78 +2790,96 @@ class FakeOsModuleTest(FakeOsModuleTestBase): # are out of sync (see #581) fds = [] for i in range(5): - path = self.make_path('file' + str(i)) + path = self.make_path("file" + str(i)) fds.append(self.os.open(path, os.O_CREAT)) - file_path = self.make_path('file.txt') + file_path = self.make_path("file.txt") self.create_file(file_path) with self.open(file_path): read_fd, write_fd = self.os.pipe() - with self.open(write_fd, 'wb') as f: - self.assertEqual(4, f.write(b'test')) - with self.open(read_fd, 'rb') as f: - self.assertEqual(b'test', f.read()) + with self.open(write_fd, "wb") as f: + self.assertEqual(4, f.write(b"test")) + with self.open(read_fd, "rb") as f: + self.assertEqual(b"test", f.read()) for fd in fds: self.os.close(fd) def test_write_to_pipe(self): read_fd, write_fd = self.os.pipe() - self.os.write(write_fd, b'test') - self.assertEqual(b'test', self.os.read(read_fd, 4)) + self.os.write(write_fd, b"test") + self.assertEqual(b"test", self.os.read(read_fd, 4)) self.os.close(read_fd) self.os.close(write_fd) + @unittest.skipIf( + sys.platform not in ("win32", "darwin", "linux"), + "Pipe implementation may differ on other platforms", + ) def test_write_to_read_fd(self): read_fd, write_fd = self.os.pipe() - self.assert_raises_os_error(errno.EBADF, - self.os.write, read_fd, b'test') + self.assert_raises_os_error(errno.EBADF, self.os.write, read_fd, b"test") self.os.close(read_fd) self.os.close(write_fd) def test_truncate(self): - file_path = self.make_path('foo', 'bar') - self.create_file(file_path, contents='012345678901234567') + file_path = self.make_path("foo", "bar") + self.create_file(file_path, contents="012345678901234567") self.os.truncate(file_path, 10) with self.open(file_path) as f: - self.assertEqual('0123456789', f.read()) + self.assertEqual("0123456789", f.read()) def test_truncate_non_existing(self): - self.assert_raises_os_error(errno.ENOENT, self.os.truncate, 'foo', 10) + self.assert_raises_os_error(errno.ENOENT, self.os.truncate, "foo", 10) def test_truncate_to_larger(self): - file_path = self.make_path('foo', 'bar') - self.create_file(file_path, contents='0123456789') + file_path = self.make_path("foo", "bar") + self.create_file(file_path, contents="0123456789") fd = self.os.open(file_path, os.O_RDWR) self.os.truncate(fd, 20) self.assertEqual(20, self.os.stat(file_path).st_size) with self.open(file_path) as f: - self.assertEqual('0123456789' + '\0' * 10, f.read()) + self.assertEqual("0123456789" + "\0" * 10, f.read()) def test_truncate_with_fd(self): if os.truncate not in os.supports_fd: self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.ftruncate, 50, 10) - file_path = self.make_path('some_file') - self.create_file(file_path, contents='01234567890123456789') + file_path = self.make_path("some_file") + self.create_file(file_path, contents="01234567890123456789") fd = self.os.open(file_path, os.O_RDWR) self.os.truncate(fd, 10) self.assertEqual(10, self.os.stat(file_path).st_size) with self.open(file_path) as f: - self.assertEqual('0123456789', f.read()) + self.assertEqual("0123456789", f.read()) def test_ftruncate(self): if self.is_pypy: # not correctly supported self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.ftruncate, 50, 10) - file_path = self.make_path('some_file') - self.create_file(file_path, contents='0123456789012345') + file_path = self.make_path("some_file") + self.create_file(file_path, contents="0123456789012345") fd = self.os.open(file_path, os.O_RDWR) self.os.truncate(fd, 10) self.assertEqual(10, self.os.stat(file_path).st_size) with self.open(file_path) as f: - self.assertEqual('0123456789', f.read()) + self.assertEqual("0123456789", f.read()) + + def test_capabilities(self): + """Make sure that the fake capabilities are the same as the real ones.""" + self.assertEqual( + self.os.stat in self.os.supports_follow_symlinks, + os.stat in os.supports_follow_symlinks, + ) + self.assertEqual(self.os.stat in self.os.supports_fd, os.stat in os.supports_fd) + self.assertEqual( + self.os.stat in self.os.supports_dir_fd, os.stat in os.supports_dir_fd + ) + self.assertEqual( + self.os.stat in self.os.supports_effective_ids, + os.stat in os.supports_effective_ids, + ) class RealOsModuleTest(FakeOsModuleTest): @@ -2829,55 +2896,54 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_chdir_fails_non_directory(self): """chdir should raise OSError if the target is not a directory.""" - filename = self.make_path('foo', 'bar') + filename = self.make_path("foo", "bar") self.create_file(filename) - filename1 = self.make_path('Foo', 'Bar') + filename1 = self.make_path("Foo", "Bar") self.assert_raises_os_error(errno.ENOTDIR, self.os.chdir, filename1) def test_listdir_returns_list(self): - directory_root = self.make_path('xyzzy') + directory_root = self.make_path("xyzzy") self.os.mkdir(directory_root) - directory = self.os.path.join(directory_root, 'bug') + directory = self.os.path.join(directory_root, "bug") self.os.mkdir(directory) - directory_upper = self.make_path('XYZZY', 'BUG') - self.create_file(self.make_path(directory, 'foo')) - self.assertEqual(['foo'], self.os.listdir(directory_upper)) + directory_upper = self.make_path("XYZZY", "BUG") + self.create_file(self.make_path(directory, "foo")) + self.assertEqual(["foo"], self.os.listdir(directory_upper)) def test_listdir_on_symlink(self): self.skip_if_symlink_not_supported() - directory = self.make_path('xyzzy') - files = ['foo', 'bar', 'baz'] + directory = self.make_path("xyzzy") + files = ["foo", "bar", "baz"] for f in files: self.create_file(self.make_path(directory, f)) - self.create_symlink(self.make_path('symlink'), self.make_path('xyzzy')) + self.create_symlink(self.make_path("symlink"), self.make_path("xyzzy")) files.sort() - self.assertEqual(files, - sorted(self.os.listdir(self.make_path('SymLink')))) + self.assertEqual(files, sorted(self.os.listdir(self.make_path("SymLink")))) def test_fdopen_mode(self): self.skip_real_fs() - file_path1 = self.make_path('some_file1') - file_path2 = self.make_path('Some_File1') - file_path3 = self.make_path('SOME_file1') - self.create_file(file_path1, contents='contents here1') + file_path1 = self.make_path("some_file1") + file_path2 = self.make_path("Some_File1") + file_path3 = self.make_path("SOME_file1") + self.create_file(file_path1, contents="contents here1") self.os.chmod(file_path2, (stat.S_IFREG | 0o666) ^ stat.S_IWRITE) - fake_file1 = self.open(file_path3, 'r') + fake_file1 = self.open(file_path3, "r") fileno1 = fake_file1.fileno() self.os.fdopen(fileno1) - self.os.fdopen(fileno1, 'r') + self.os.fdopen(fileno1, "r") if not is_root(): - self.assertRaises(OSError, self.os.fdopen, fileno1, 'w') + self.assertRaises(OSError, self.os.fdopen, fileno1, "w") else: - self.os.fdopen(fileno1, 'w') + self.os.fdopen(fileno1, "w") def test_stat(self): - directory = self.make_path('xyzzy') - directory1 = self.make_path('XYZZY') - file_path = self.os.path.join(directory, 'plugh') - self.create_file(file_path, contents='ABCDE') + directory = self.make_path("xyzzy") + directory1 = self.make_path("XYZZY") + file_path = self.os.path.join(directory, "plugh") + self.create_file(file_path, contents="ABCDE") self.assertTrue(stat.S_IFDIR & self.os.stat(directory1)[stat.ST_MODE]) - file_path1 = self.os.path.join(directory1, 'Plugh') + file_path1 = self.os.path.join(directory1, "Plugh") self.assertTrue(stat.S_IFREG & self.os.stat(file_path1)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.stat(file_path1).st_mode) self.assertEqual(5, self.os.stat(file_path1)[stat.ST_SIZE]) @@ -2885,48 +2951,51 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_stat_no_follow_symlinks_posix(self): """Test that stat with follow_symlinks=False behaves like lstat.""" self.check_posix_only() - directory = self.make_path('xyzzy') - base_name = 'plugh' - file_contents = 'frobozz' + directory = self.make_path("xyzzy") + base_name = "plugh" + file_contents = "frobozz" # Just make sure we didn't accidentally make our test data meaningless. self.assertNotEqual(len(base_name), len(file_contents)) file_path = self.os.path.join(directory, base_name) - link_path = self.os.path.join(directory, 'link') + link_path = self.os.path.join(directory, "link") self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, base_name) - self.assertEqual(len(file_contents), self.os.stat( - file_path.upper(), follow_symlinks=False)[stat.ST_SIZE]) - self.assertEqual(len(base_name), self.os.stat( - link_path.upper(), follow_symlinks=False)[stat.ST_SIZE]) + self.assertEqual( + len(file_contents), + self.os.stat(file_path.upper(), follow_symlinks=False)[stat.ST_SIZE], + ) + self.assertEqual( + len(base_name), + self.os.stat(link_path.upper(), follow_symlinks=False)[stat.ST_SIZE], + ) def test_lstat_posix(self): self.check_posix_only() - directory = self.make_path('xyzzy') - base_name = 'plugh' - file_contents = 'frobozz' + directory = self.make_path("xyzzy") + base_name = "plugh" + file_contents = "frobozz" # Just make sure we didn't accidentally make our test data meaningless. self.assertNotEqual(len(base_name), len(file_contents)) file_path = self.os.path.join(directory, base_name) - link_path = self.os.path.join(directory, 'link') + link_path = self.os.path.join(directory, "link") self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, base_name) - self.assertEqual(len(file_contents), - self.os.lstat(file_path.upper())[stat.ST_SIZE]) - self.assertEqual(len(base_name), - self.os.lstat(link_path.upper())[stat.ST_SIZE]) + self.assertEqual( + len(file_contents), self.os.lstat(file_path.upper())[stat.ST_SIZE] + ) + self.assertEqual(len(base_name), self.os.lstat(link_path.upper())[stat.ST_SIZE]) def test_readlink(self): self.skip_if_symlink_not_supported() - link_path = self.make_path('foo', 'bar', 'baz') - target = self.make_path('tarJAY') + link_path = self.make_path("foo", "bar", "baz") + target = self.make_path("tarJAY") self.create_symlink(link_path, target) self.assert_equal_paths(self.os.readlink(link_path.upper()), target) def check_readlink_raises_if_path_not_a_link(self): - file_path = self.make_path('foo', 'bar', 'eleventyone') + file_path = self.make_path("foo", "bar", "eleventyone") self.create_file(file_path) - self.assert_raises_os_error(errno.EINVAL, - self.os.readlink, file_path.upper()) + self.assert_raises_os_error(errno.EINVAL, self.os.readlink, file_path.upper()) def test_readlink_raises_if_path_not_a_link_windows(self): self.check_windows_only() @@ -2938,13 +3007,11 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): self.check_readlink_raises_if_path_not_a_link() def check_readlink_raises_if_path_has_file(self, error_subtype): - self.create_file(self.make_path('a_file')) - file_path = self.make_path('a_file', 'foo') - self.assert_raises_os_error(error_subtype, - self.os.readlink, file_path.upper()) - file_path = self.make_path('a_file', 'foo', 'bar') - self.assert_raises_os_error(error_subtype, - self.os.readlink, file_path.upper()) + self.create_file(self.make_path("a_file")) + file_path = self.make_path("a_file", "foo") + self.assert_raises_os_error(error_subtype, self.os.readlink, file_path.upper()) + file_path = self.make_path("a_file", "foo", "bar") + self.assert_raises_os_error(error_subtype, self.os.readlink, file_path.upper()) def test_readlink_raises_if_path_has_file_windows(self): self.check_windows_only() @@ -2957,31 +3024,35 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_readlink_with_links_in_path(self): self.skip_if_symlink_not_supported() - self.create_symlink(self.make_path('meyer', 'lemon', 'pie'), - self.make_path('yum')) - self.create_symlink(self.make_path('geo', 'metro'), - self.make_path('Meyer')) - self.assert_equal_paths(self.make_path('yum'), - self.os.readlink( - self.make_path('Geo', 'Metro', - 'Lemon', 'Pie'))) + self.create_symlink( + self.make_path("meyer", "lemon", "pie"), self.make_path("yum") + ) + self.create_symlink(self.make_path("geo", "metro"), self.make_path("Meyer")) + self.assert_equal_paths( + self.make_path("yum"), + self.os.readlink(self.make_path("Geo", "Metro", "Lemon", "Pie")), + ) def test_readlink_with_chained_links_in_path(self): self.skip_if_symlink_not_supported() - self.create_symlink(self.make_path( - 'eastern', 'european', 'wolfhounds', 'chase'), - self.make_path('cats')) - self.create_symlink(self.make_path('russian'), - self.make_path('Eastern', 'European')) - self.create_symlink(self.make_path('dogs'), - self.make_path('Russian', 'Wolfhounds')) - self.assert_equal_paths(self.make_path('cats'), - self.os.readlink( - self.make_path('DOGS', 'Chase'))) + self.create_symlink( + self.make_path("eastern", "european", "wolfhounds", "chase"), + self.make_path("cats"), + ) + self.create_symlink( + self.make_path("russian"), self.make_path("Eastern", "European") + ) + self.create_symlink( + self.make_path("dogs"), self.make_path("Russian", "Wolfhounds") + ) + self.assert_equal_paths( + self.make_path("cats"), + self.os.readlink(self.make_path("DOGS", "Chase")), + ) def check_remove_dir(self, dir_error): - directory = self.make_path('xyzzy') - dir_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + dir_path = self.os.path.join(directory, "plugh") self.create_dir(dir_path) dir_path = dir_path.upper() self.assertTrue(self.os.path.exists(dir_path.upper())) @@ -2990,7 +3061,7 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): self.os.chdir(directory) self.assert_raises_os_error(dir_error, self.os.remove, dir_path) self.assertTrue(self.os.path.exists(dir_path)) - self.assert_raises_os_error(errno.ENOENT, self.os.remove, '/Plugh') + self.assert_raises_os_error(errno.ENOENT, self.os.remove, "/Plugh") def test_remove_dir_mac_os(self): self.check_macos_only() @@ -3001,16 +3072,16 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): self.check_remove_dir(errno.EACCES) def test_remove_file(self): - directory = self.make_path('zzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("zzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path.upper())) self.os.remove(file_path.upper()) self.assertFalse(self.os.path.exists(file_path)) def test_remove_file_no_directory(self): - directory = self.make_path('zzy') - file_name = 'plugh' + directory = self.make_path("zzy") + file_name = "plugh" file_path = self.os.path.join(directory, file_name) self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) @@ -3020,29 +3091,28 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_remove_open_file_fails_under_windows(self): self.check_windows_only() - path = self.make_path('foo', 'bar') + path = self.make_path("foo", "bar") self.create_file(path) - with self.open(path, 'r'): - self.assert_raises_os_error(errno.EACCES, - self.os.remove, path.upper()) + with self.open(path, "r"): + self.assert_raises_os_error(errno.EACCES, self.os.remove, path.upper()) self.assertTrue(self.os.path.exists(path)) def test_remove_open_file_possible_under_posix(self): self.check_posix_only() - path = self.make_path('foo', 'bar') + path = self.make_path("foo", "bar") self.create_file(path) - self.open(path, 'r') + self.open(path, "r") self.os.remove(path.upper()) self.assertFalse(self.os.path.exists(path)) def test_remove_file_relative_path(self): self.skip_real_fs() original_dir = self.os.getcwd() - directory = self.make_path('zzy') - subdirectory = self.os.path.join(directory, 'zzy') - file_name = 'plugh' + directory = self.make_path("zzy") + subdirectory = self.os.path.join(directory, "zzy") + file_name = "plugh" file_path = self.os.path.join(directory, file_name) - file_path_relative = self.os.path.join('..', file_name) + file_path_relative = self.os.path.join("..", file_name) self.create_file(file_path.upper()) self.assertTrue(self.os.path.exists(file_path)) self.create_dir(subdirectory) @@ -3054,10 +3124,9 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): self.assertFalse(self.os.path.exists(file_path)) def check_remove_dir_raises_error(self, dir_error): - directory = self.make_path('zzy') + directory = self.make_path("zzy") self.create_dir(directory) - self.assert_raises_os_error(dir_error, - self.os.remove, directory.upper()) + self.assert_raises_os_error(dir_error, self.os.remove, directory.upper()) def test_remove_dir_raises_error_mac_os(self): self.check_macos_only() @@ -3069,8 +3138,8 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_remove_symlink_to_dir(self): self.skip_if_symlink_not_supported() - directory = self.make_path('zzy') - link = self.make_path('link_to_dir') + directory = self.make_path("zzy") + link = self.make_path("link_to_dir") self.create_dir(directory) self.os.symlink(directory, link) self.assertTrue(self.os.path.exists(directory)) @@ -3081,42 +3150,40 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_rename_dir_to_symlink_posix(self): self.check_posix_only() - link_path = self.make_path('link') - dir_path = self.make_path('dir') - link_target = self.os.path.join(dir_path, 'link_target') + link_path = self.make_path("link") + dir_path = self.make_path("dir") + link_target = self.os.path.join(dir_path, "link_target") self.create_dir(dir_path) self.os.symlink(link_target.upper(), link_path.upper()) - self.assert_raises_os_error(errno.ENOTDIR, self.os.rename, dir_path, - link_path) + self.assert_raises_os_error(errno.ENOTDIR, self.os.rename, dir_path, link_path) def test_rename_dir_to_symlink_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - link_path = self.make_path('link') - dir_path = self.make_path('dir') - link_target = self.os.path.join(dir_path, 'link_target') + link_path = self.make_path("link") + dir_path = self.make_path("dir") + link_target = self.os.path.join(dir_path, "link_target") self.create_dir(dir_path) self.os.symlink(link_target.upper(), link_path.upper()) - self.assert_raises_os_error(errno.EEXIST, self.os.rename, dir_path, - link_path) + self.assert_raises_os_error(errno.EEXIST, self.os.rename, dir_path, link_path) def test_rename_dir_to_existing_dir(self): # Regression test for #317 self.check_posix_only() - dest_dir_path = self.make_path('Dest') + dest_dir_path = self.make_path("Dest") # seems to behave differently under different MacOS versions self.skip_real_fs() - new_dest_dir_path = self.make_path('dest') + new_dest_dir_path = self.make_path("dest") self.os.mkdir(dest_dir_path) - source_dir_path = self.make_path('src') + source_dir_path = self.make_path("src") self.os.mkdir(source_dir_path) self.os.rename(source_dir_path, new_dest_dir_path) - self.assertEqual(['dest'], self.os.listdir(self.base_path)) + self.assertEqual(["dest"], self.os.listdir(self.base_path)) def test_rename_file_to_symlink(self): self.check_posix_only() - link_path = self.make_path('file_link') - file_path = self.make_path('file') + link_path = self.make_path("file_link") + file_path = self.make_path("file") self.os.symlink(file_path, link_path) self.create_file(file_path) self.os.rename(file_path.upper(), link_path) @@ -3126,10 +3193,10 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_rename_symlink_to_symlink(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') + base_path = self.make_path("foo", "bar") self.create_dir(base_path) - link_path1 = self.os.path.join(base_path, 'link1') - link_path2 = self.os.path.join(base_path, 'link2') + link_path1 = self.os.path.join(base_path, "link1") + link_path2 = self.os.path.join(base_path, "link2") self.os.symlink(base_path.upper(), link_path1) self.os.symlink(base_path, link_path2) self.os.rename(link_path1.upper(), link_path2.upper()) @@ -3138,64 +3205,72 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_rename_symlink_to_symlink_for_parent_raises(self): self.check_posix_only() - dir_link = self.make_path('dir_link') - dir_path = self.make_path('dir') - dir_in_dir_path = self.os.path.join(dir_link, 'inner_dir') + dir_link = self.make_path("dir_link") + dir_path = self.make_path("dir") + dir_in_dir_path = self.os.path.join(dir_link, "inner_dir") self.create_dir(dir_path) self.os.symlink(dir_path.upper(), dir_link) self.create_dir(dir_in_dir_path) - self.assert_raises_os_error(errno.EINVAL, self.os.rename, dir_path, - dir_in_dir_path.upper()) + self.assert_raises_os_error( + errno.EINVAL, self.os.rename, dir_path, dir_in_dir_path.upper() + ) def test_rename_directory_to_linked_dir(self): # Regression test for #314 self.skip_if_symlink_not_supported() - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.symlink(self.base_path, link_path) - link_subdir = self.os.path.join(link_path, 'dir') - dir_path = self.make_path('Dir') + link_subdir = self.os.path.join(link_path, "dir") + dir_path = self.make_path("Dir") self.os.mkdir(dir_path) self.os.rename(dir_path, link_subdir) - self.assertEqual(['dir', 'link'], - sorted(self.os.listdir(self.base_path))) + self.assertEqual(["dir", "link"], sorted(self.os.listdir(self.base_path))) def test_recursive_rename_raises(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') + base_path = self.make_path("foo", "bar") self.create_dir(base_path) - new_path = self.os.path.join(base_path, 'new_dir') - self.assert_raises_os_error(errno.EINVAL, self.os.rename, - base_path.upper(), new_path) + new_path = self.os.path.join(base_path, "new_dir") + self.assert_raises_os_error( + errno.EINVAL, self.os.rename, base_path.upper(), new_path + ) def test_rename_with_target_parent_file_raises_posix(self): self.check_posix_only() - file_path = self.make_path('foo', 'baz') + file_path = self.make_path("foo", "baz") self.create_file(file_path) - self.assert_raises_os_error(errno.ENOTDIR, self.os.rename, file_path, - file_path.upper() + '/new') + self.assert_raises_os_error( + errno.ENOTDIR, + self.os.rename, + file_path, + file_path.upper() + "/new", + ) def test_rename_with_target_parent_file_raises_windows(self): self.check_windows_only() - file_path = self.make_path('foo', 'baz') + file_path = self.make_path("foo", "baz") self.create_file(file_path) self.assert_raises_os_error( - errno.EACCES, self.os.rename, file_path, - self.os.path.join(file_path.upper(), 'new')) + errno.EACCES, + self.os.rename, + file_path, + self.os.path.join(file_path.upper(), "new"), + ) def test_rename_looping_symlink(self): # Regression test for #315 self.skip_if_symlink_not_supported() - path_lower = self.make_path('baz') - path_upper = self.make_path('BAZ') + path_lower = self.make_path("baz") + path_upper = self.make_path("BAZ") self.os.symlink(path_lower, path_upper) self.os.rename(path_upper, path_lower) - self.assertEqual(['baz'], self.os.listdir(self.base_path)) + self.assertEqual(["baz"], self.os.listdir(self.base_path)) def test_rename_symlink_to_source(self): self.check_posix_only() - base_path = self.make_path('foo') - link_path = self.os.path.join(base_path, 'slink') - file_path = self.os.path.join(base_path, 'file') + base_path = self.make_path("foo") + link_path = self.os.path.join(base_path, "slink") + file_path = self.os.path.join(base_path, "file") self.create_file(file_path) self.os.symlink(file_path, link_path) self.os.rename(link_path.upper(), file_path.upper()) @@ -3203,20 +3278,21 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_rename_symlink_to_dir_raises(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(base_path, 'dir_link') - dir_path = self.os.path.join(base_path, 'dir') + base_path = self.make_path("foo", "bar") + link_path = self.os.path.join(base_path, "dir_link") + dir_path = self.os.path.join(base_path, "dir") self.create_dir(dir_path) self.os.symlink(dir_path, link_path.upper()) - self.assert_raises_os_error(errno.EISDIR, self.os.rename, link_path, - dir_path.upper()) + self.assert_raises_os_error( + errno.EISDIR, self.os.rename, link_path, dir_path.upper() + ) def test_rename_broken_symlink(self): self.check_posix_only() - base_path = self.make_path('foo') + base_path = self.make_path("foo") self.create_dir(base_path) - link_path = self.os.path.join(base_path, 'slink') - file_path = self.os.path.join(base_path, 'file') + link_path = self.os.path.join(base_path, "slink") + file_path = self.os.path.join(base_path, "file") self.os.symlink(file_path.upper(), link_path) self.os.rename(link_path.upper(), file_path) self.assertFalse(self.os.path.exists(file_path)) @@ -3225,49 +3301,48 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_change_case_in_case_insensitive_file_system(self): """Can use `rename()` to change filename case in a case-insensitive - file system.""" - old_file_path = self.make_path('fileName') - new_file_path = self.make_path('FileNAME') - self.create_file(old_file_path, contents='test contents') - self.assertEqual(['fileName'], self.os.listdir(self.base_path)) + file system.""" + old_file_path = self.make_path("fileName") + new_file_path = self.make_path("FileNAME") + self.create_file(old_file_path, contents="test contents") + self.assertEqual(["fileName"], self.os.listdir(self.base_path)) self.os.rename(old_file_path, new_file_path) self.assertTrue(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) - self.assertEqual(['FileNAME'], self.os.listdir(self.base_path)) + self.assertEqual(["FileNAME"], self.os.listdir(self.base_path)) def test_rename_symlink_with_changed_case(self): # Regression test for #313 self.skip_if_symlink_not_supported() - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.symlink(self.base_path, link_path) - link_path = self.os.path.join(link_path, 'link') - link_path_upper = self.make_path('link', 'LINK') + link_path = self.os.path.join(link_path, "link") + link_path_upper = self.make_path("link", "LINK") self.os.rename(link_path_upper, link_path) def test_rename_directory(self): """Can rename a directory to an unused name.""" - for old_path, new_path in [('wxyyw', 'xyzzy'), ('abccb', 'cdeed')]: + for old_path, new_path in [("wxyyw", "xyzzy"), ("abccb", "cdeed")]: old_path = self.make_path(old_path) new_path = self.make_path(new_path) - self.create_file(self.os.path.join(old_path, 'plugh'), - contents='test') + self.create_file(self.os.path.join(old_path, "plugh"), contents="test") self.assertTrue(self.os.path.exists(old_path)) self.assertFalse(self.os.path.exists(new_path)) self.os.rename(old_path.upper(), new_path.upper()) self.assertFalse(self.os.path.exists(old_path)) self.assertTrue(self.os.path.exists(new_path)) - self.check_contents(self.os.path.join(new_path, 'plugh'), 'test') + self.check_contents(self.os.path.join(new_path, "plugh"), "test") if not self.use_real_fs(): - self.assertEqual(3, - self.filesystem.get_object(new_path).st_nlink) + self.assertEqual(3, self.filesystem.get_object(new_path).st_nlink) def check_rename_directory_to_existing_file_raises(self, error_nr): - dir_path = self.make_path('dir') - file_path = self.make_path('file') + dir_path = self.make_path("dir") + file_path = self.make_path("file") self.create_dir(dir_path) self.create_file(file_path) - self.assert_raises_os_error(error_nr, self.os.rename, dir_path, - file_path.upper()) + self.assert_raises_os_error( + error_nr, self.os.rename, dir_path, file_path.upper() + ) def test_rename_directory_to_existing_file_raises_posix(self): self.check_posix_only() @@ -3280,20 +3355,20 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_rename_to_existing_directory_should_raise_under_windows(self): """Renaming to an existing directory raises OSError under Windows.""" self.check_windows_only() - old_path = self.make_path('foo', 'bar') - new_path = self.make_path('foo', 'baz') + old_path = self.make_path("foo", "bar") + new_path = self.make_path("foo", "baz") self.create_dir(old_path) self.create_dir(new_path) - self.assert_raises_os_error(errno.EEXIST, self.os.rename, - old_path.upper(), - new_path.upper()) + self.assert_raises_os_error( + errno.EEXIST, self.os.rename, old_path.upper(), new_path.upper() + ) def test_rename_to_a_hardlink_of_same_file_should_do_nothing(self): self.skip_real_fs_failure(skip_posix=False) self.skip_if_symlink_not_supported() - file_path = self.make_path('dir', 'file') + file_path = self.make_path("dir", "file") self.create_file(file_path) - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.link(file_path.upper(), link_path) self.os.rename(file_path, link_path.upper()) self.assertTrue(self.os.path.exists(file_path)) @@ -3301,9 +3376,9 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_rename_with_incorrect_source_case(self): # Regression test for #308 - base_path = self.make_path('foo') - path0 = self.os.path.join(base_path, 'bar') - path1 = self.os.path.join(base_path, 'Bar') + base_path = self.make_path("foo") + path0 = self.os.path.join(base_path, "bar") + path1 = self.os.path.join(base_path, "Bar") self.create_dir(path0) self.os.rename(path1, path0) self.assertTrue(self.os.path.exists(path0)) @@ -3316,7 +3391,7 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): path0 = self.make_path("beta", "Beta") path1 = self.make_path("Beta") self.os.rename(path0, path1) - self.assertEqual(['beta'], sorted(self.os.listdir(path0))) + self.assertEqual(["beta"], sorted(self.os.listdir(path0))) def test_rename_symlink_to_other_case_works_in_windows(self): self.check_windows_only() @@ -3326,134 +3401,160 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): path0 = self.make_path("beta", "Beta") path1 = self.make_path("Beta") self.os.rename(path0, path1) - self.assertEqual(['Beta'], sorted(self.os.listdir(path0))) + self.assertEqual(["Beta"], sorted(self.os.listdir(path0))) + + def test_renames_creates_missing_dirs(self): + old_path = self.make_path("foo.txt") + self.create_file(old_path) + new_path = self.make_path("new", "dir", "bar.txt") + self.os.renames(old_path, new_path) + self.assertTrue(self.os.path.exists(new_path)) + self.assertFalse(self.os.path.exists(old_path)) + + def test_renames_removes_empty_dirs(self): + old_base_path = self.make_path("old") + old_path = self.make_path("old", "dir1", "dir2", "foo.txt") + other_file = self.os.path.join(old_base_path, "foo.png") + self.create_file(old_path) + self.create_file(other_file) + new_path = self.make_path("new", "bar.txt") + self.os.renames(old_path, new_path) + self.assertTrue(self.os.path.exists(new_path)) + self.assertFalse(self.os.path.exists(old_path)) + self.assertTrue(self.os.path.exists(old_base_path)) + removed_path = self.os.path.join(old_base_path, "dir1") + self.assertFalse(self.os.path.exists(removed_path)) def test_stat_with_mixed_case(self): # Regression test for #310 self.skip_if_symlink_not_supported() - base_path = self.make_path('foo') - path = self.os.path.join(base_path, 'bar') + base_path = self.make_path("foo") + path = self.os.path.join(base_path, "bar") self.create_dir(path) - path = self.os.path.join(path, 'Bar') + path = self.os.path.join(path, "Bar") self.os.symlink(base_path, path) - path = self.os.path.join(path, 'Bar') + path = self.os.path.join(path, "Bar") # used to raise self.os.stat(path) def test_hardlink_works_with_symlink(self): self.skip_if_symlink_not_supported() - base_path = self.make_path('foo') + base_path = self.make_path("foo") self.create_dir(base_path) - symlink_path = self.os.path.join(base_path, 'slink') + symlink_path = self.os.path.join(base_path, "slink") self.os.symlink(base_path.upper(), symlink_path) - file_path = self.os.path.join(base_path, 'slink', 'beta') + file_path = self.os.path.join(base_path, "slink", "beta") self.create_file(file_path) - link_path = self.os.path.join(base_path, 'Slink', 'gamma') + link_path = self.os.path.join(base_path, "Slink", "gamma") self.os.link(file_path, link_path) self.assertTrue(self.os.path.exists(link_path)) def test_replace_existing_directory_should_raise_under_windows(self): """Renaming to an existing directory raises OSError under Windows.""" self.check_windows_only() - old_path = self.make_path('foo', 'bar') - new_path = self.make_path('foo', 'baz') + old_path = self.make_path("foo", "bar") + new_path = self.make_path("foo", "baz") self.create_dir(old_path) self.create_dir(new_path) - self.assert_raises_os_error(errno.EACCES, self.os.replace, old_path, - new_path.upper()) + self.assert_raises_os_error( + errno.EACCES, self.os.replace, old_path, new_path.upper() + ) def test_rename_to_existing_directory_under_posix(self): """Renaming to an existing directory changes the existing directory under Posix.""" self.check_posix_only() - old_path = self.make_path('foo', 'bar') - new_path = self.make_path('xyzzy') - self.create_dir(self.os.path.join(old_path, 'sub')) + old_path = self.make_path("foo", "bar") + new_path = self.make_path("xyzzy") + self.create_dir(self.os.path.join(old_path, "sub")) self.create_dir(new_path) self.os.rename(old_path.upper(), new_path.upper()) - self.assertTrue( - self.os.path.exists(self.os.path.join(new_path, 'sub'))) + self.assertTrue(self.os.path.exists(self.os.path.join(new_path, "sub"))) self.assertFalse(self.os.path.exists(old_path)) def test_rename_file_to_existing_directory_raises_under_posix(self): self.check_posix_only() - file_path = self.make_path('foo', 'bar', 'baz') - new_path = self.make_path('xyzzy') + file_path = self.make_path("foo", "bar", "baz") + new_path = self.make_path("xyzzy") self.create_file(file_path) self.create_dir(new_path) - self.assert_raises_os_error(errno.EISDIR, self.os.rename, - file_path.upper(), - new_path.upper()) + self.assert_raises_os_error( + errno.EISDIR, self.os.rename, file_path.upper(), new_path.upper() + ) def test_rename_to_existent_file_posix(self): """Can rename a file to a used name under Unix.""" self.check_posix_only() - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') - self.create_file(old_file_path, contents='test contents 1') - self.create_file(new_file_path, contents='test contents 2') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") + self.create_file(old_file_path, contents="test contents 1") + self.create_file(new_file_path, contents="test contents 2") self.assertTrue(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) self.os.rename(old_file_path.upper(), new_file_path.upper()) self.assertFalse(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) - self.check_contents(new_file_path, 'test contents 1') + self.check_contents(new_file_path, "test contents 1") def test_rename_to_existent_file_windows(self): """Renaming a file to a used name raises OSError under Windows.""" self.check_windows_only() - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') - self.create_file(old_file_path, contents='test contents 1') - self.create_file(new_file_path, contents='test contents 2') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") + self.create_file(old_file_path, contents="test contents 1") + self.create_file(new_file_path, contents="test contents 2") self.assertTrue(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) - self.assert_raises_os_error(errno.EEXIST, self.os.rename, - old_file_path.upper(), - new_file_path.upper()) + self.assert_raises_os_error( + errno.EEXIST, + self.os.rename, + old_file_path.upper(), + new_file_path.upper(), + ) def test_replace_to_existent_file(self): """Replaces an existing file (does not work with `rename()` under Windows).""" - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join(directory, 'plugh_new') - self.create_file(old_file_path, contents='test contents 1') - self.create_file(new_file_path, contents='test contents 2') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "plugh_new") + self.create_file(old_file_path, contents="test contents 1") + self.create_file(new_file_path, contents="test contents 2") self.assertTrue(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) self.os.replace(old_file_path.upper(), new_file_path.upper()) self.assertFalse(self.os.path.exists(old_file_path)) self.assertTrue(self.os.path.exists(new_file_path)) - self.check_contents(new_file_path, 'test contents 1') + self.check_contents(new_file_path, "test contents 1") def test_rename_to_nonexistent_dir(self): """Can rename a file to a name in a nonexistent dir.""" - directory = self.make_path('xyzzy') - old_file_path = self.os.path.join(directory, 'plugh_old') - new_file_path = self.os.path.join( - directory, 'no_such_path', 'plugh_new') - self.create_file(old_file_path, contents='test contents') + directory = self.make_path("xyzzy") + old_file_path = self.os.path.join(directory, "plugh_old") + new_file_path = self.os.path.join(directory, "no_such_path", "plugh_new") + self.create_file(old_file_path, contents="test contents") self.assertTrue(self.os.path.exists(old_file_path)) self.assertFalse(self.os.path.exists(new_file_path)) - self.assert_raises_os_error(errno.ENOENT, self.os.rename, - old_file_path.upper(), - new_file_path.upper()) + self.assert_raises_os_error( + errno.ENOENT, + self.os.rename, + old_file_path.upper(), + new_file_path.upper(), + ) self.assertTrue(self.os.path.exists(old_file_path)) self.assertFalse(self.os.path.exists(new_file_path)) - self.check_contents(old_file_path, 'test contents') + self.check_contents(old_file_path, "test contents") def check_rename_case_only_with_symlink_parent(self): # Regression test for #319 - self.os.symlink(self.base_path, self.make_path('link')) - dir_upper = self.make_path('link', 'Alpha') + self.os.symlink(self.base_path, self.make_path("link")) + dir_upper = self.make_path("link", "Alpha") self.os.mkdir(dir_upper) - dir_lower = self.make_path('alpha') + dir_lower = self.make_path("alpha") self.os.rename(dir_upper, dir_lower) - self.assertEqual(['alpha', 'link'], - sorted(self.os.listdir(self.base_path))) + self.assertEqual(["alpha", "link"], sorted(self.os.listdir(self.base_path))) def test_rename_case_only_with_symlink_parent_windows(self): self.check_windows_only() @@ -3466,13 +3567,13 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_rename_dir(self): """Test a rename of a directory.""" - directory = self.make_path('xyzzy') - before_dir = self.os.path.join(directory, 'before') - before_file = self.os.path.join(directory, 'before', 'file') - after_dir = self.os.path.join(directory, 'after') - after_file = self.os.path.join(directory, 'after', 'file') + directory = self.make_path("xyzzy") + before_dir = self.os.path.join(directory, "before") + before_file = self.os.path.join(directory, "before", "file") + after_dir = self.os.path.join(directory, "after") + after_file = self.os.path.join(directory, "after", "file") self.create_dir(before_dir) - self.create_file(before_file, contents='payload') + self.create_file(before_file, contents="payload") self.assertTrue(self.os.path.exists(before_dir.upper())) self.assertTrue(self.os.path.exists(before_file.upper())) self.assertFalse(self.os.path.exists(after_dir.upper())) @@ -3482,22 +3583,22 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): self.assertFalse(self.os.path.exists(before_file.upper())) self.assertTrue(self.os.path.exists(after_dir.upper())) self.assertTrue(self.os.path.exists(after_file.upper())) - self.check_contents(after_file, 'payload') + self.check_contents(after_file, "payload") def test_rename_same_filenames(self): """Test renaming when old and new names are the same.""" - directory = self.make_path('xyzzy') - file_contents = 'Spam eggs' - file_path = self.os.path.join(directory, 'eggs') + directory = self.make_path("xyzzy") + file_contents = "Spam eggs" + file_path = self.os.path.join(directory, "eggs") self.create_file(file_path, contents=file_contents) self.os.rename(file_path, file_path.upper()) self.check_contents(file_path, file_contents) def test_rmdir(self): """Can remove a directory.""" - directory = self.make_path('xyzzy') - sub_dir = self.make_path('xyzzy', 'abccd') - other_dir = self.make_path('xyzzy', 'cdeed') + directory = self.make_path("xyzzy") + sub_dir = self.make_path("xyzzy", "abccd") + other_dir = self.make_path("xyzzy", "cdeed") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) self.os.rmdir(directory) @@ -3505,30 +3606,30 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): self.create_dir(sub_dir) self.create_dir(other_dir) self.os.chdir(sub_dir) - self.os.rmdir('../CDEED') + self.os.rmdir("../CDEED") self.assertFalse(self.os.path.exists(other_dir)) - self.os.chdir('..') - self.os.rmdir('AbcCd') + self.os.chdir("..") + self.os.rmdir("AbcCd") self.assertFalse(self.os.path.exists(sub_dir)) def test_rmdir_via_symlink(self): self.check_windows_only() self.skip_if_symlink_not_supported() - base_path = self.make_path('foo', 'bar') - dir_path = self.os.path.join(base_path, 'alpha') + base_path = self.make_path("foo", "bar") + dir_path = self.os.path.join(base_path, "alpha") self.create_dir(dir_path) - link_path = self.os.path.join(base_path, 'beta') + link_path = self.os.path.join(base_path, "beta") self.os.symlink(base_path, link_path) - self.os.rmdir(link_path + '/Alpha') + self.os.rmdir(link_path + "/Alpha") self.assertFalse(self.os.path.exists(dir_path)) def test_remove_dirs_with_non_top_symlink_succeeds(self): self.check_posix_only() - dir_path = self.make_path('dir') - dir_link = self.make_path('dir_link') + dir_path = self.make_path("dir") + dir_link = self.make_path("dir_link") self.create_dir(dir_path) self.os.symlink(dir_path, dir_link) - dir_in_dir = self.os.path.join(dir_link, 'dir2') + dir_in_dir = self.os.path.join(dir_link, "dir2") self.create_dir(dir_in_dir) self.os.removedirs(dir_in_dir.upper()) self.assertFalse(self.os.path.exists(dir_in_dir)) @@ -3537,9 +3638,9 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_mkdir_raises_on_symlink_in_posix(self): self.check_posix_only() - base_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(base_path, 'link_to_dir') - dir_path = self.os.path.join(base_path, 'dir') + base_path = self.make_path("foo", "bar") + link_path = self.os.path.join(base_path, "link_to_dir") + dir_path = self.os.path.join(base_path, "dir") self.create_dir(dir_path) self.os.symlink(dir_path.upper(), link_path.upper()) self.assert_raises_os_error(errno.ENOTDIR, self.os.rmdir, link_path) @@ -3547,9 +3648,9 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_mkdir_removes_symlink_in_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - base_path = self.make_path('foo', 'bar') - link_path = self.os.path.join(base_path, 'link_to_dir') - dir_path = self.os.path.join(base_path, 'dir') + base_path = self.make_path("foo", "bar") + link_path = self.os.path.join(base_path, "link_to_dir") + dir_path = self.os.path.join(base_path, "dir") self.create_dir(dir_path) self.os.symlink(dir_path.upper(), link_path.upper()) self.os.rmdir(link_path) @@ -3558,37 +3659,37 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_mkdir_raises_if_directory_exists(self): """mkdir raises exception if directory already exists.""" - directory = self.make_path('xyzzy') + directory = self.make_path("xyzzy") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) - self.assert_raises_os_error(errno.EEXIST, - self.os.mkdir, directory.upper()) + self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, directory.upper()) def test_mkdir_raises_if_file_exists(self): """mkdir raises exception if name already exists as a file.""" - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) - self.assert_raises_os_error(errno.EEXIST, - self.os.mkdir, file_path.upper()) + self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, file_path.upper()) def test_mkdir_raises_if_symlink_exists(self): # Regression test for #309 self.skip_if_symlink_not_supported() - path1 = self.make_path('baz') + path1 = self.make_path("baz") self.os.symlink(path1, path1) - path2 = self.make_path('Baz') + path2 = self.make_path("Baz") self.assert_raises_os_error(errno.EEXIST, self.os.mkdir, path2) def check_mkdir_raises_if_parent_is_file(self, error_type): """mkdir raises exception if name already exists as a file.""" - directory = self.make_path('xyzzy') - file_path = self.os.path.join(directory, 'plugh') + directory = self.make_path("xyzzy") + file_path = self.os.path.join(directory, "plugh") self.create_file(file_path) - self.assert_raises_os_error(error_type, self.os.mkdir, - self.os.path.join(file_path.upper(), - 'ff')) + self.assert_raises_os_error( + error_type, + self.os.mkdir, + self.os.path.join(file_path.upper(), "ff"), + ) def test_mkdir_raises_if_parent_is_file_posix(self): self.check_posix_only() @@ -3600,20 +3701,19 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_makedirs(self): """makedirs can create a directory even if parent does not exist.""" - parent = self.make_path('xyzzy') - directory = self.os.path.join(parent, 'foo') + parent = self.make_path("xyzzy") + directory = self.os.path.join(parent, "foo") self.assertFalse(self.os.path.exists(parent)) self.os.makedirs(directory.upper()) self.assertTrue(self.os.path.exists(directory)) def check_makedirs_raises_if_parent_is_file(self, error_type): """makedirs raises exception if a parent component exists as a file.""" - file_path = self.make_path('xyzzy') - directory = self.os.path.join(file_path, 'plugh') + file_path = self.make_path("xyzzy") + directory = self.os.path.join(file_path, "plugh") self.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) - self.assert_raises_os_error(error_type, self.os.makedirs, - directory.upper()) + self.assert_raises_os_error(error_type, self.os.makedirs, directory.upper()) def test_makedirs_raises_if_parent_is_file_posix(self): self.check_posix_only() @@ -3625,28 +3725,29 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_makedirs_raises_if_parent_is_broken_link(self): self.check_posix_only() - link_path = self.make_path('broken_link') - self.os.symlink(self.make_path('bogus'), link_path) - self.assert_raises_os_error(errno.ENOENT, self.os.makedirs, - self.os.path.join(link_path.upper(), - 'newdir')) + link_path = self.make_path("broken_link") + self.os.symlink(self.make_path("bogus"), link_path) + self.assert_raises_os_error( + errno.ENOENT, + self.os.makedirs, + self.os.path.join(link_path.upper(), "newdir"), + ) def test_makedirs_exist_ok(self): """makedirs uses the exist_ok argument""" - directory = self.make_path('xyzzy', 'foo') + directory = self.make_path("xyzzy", "foo") self.create_dir(directory) self.assertTrue(self.os.path.exists(directory)) - self.assert_raises_os_error(errno.EEXIST, self.os.makedirs, - directory.upper()) + self.assert_raises_os_error(errno.EEXIST, self.os.makedirs, directory.upper()) self.os.makedirs(directory.upper(), exist_ok=True) self.assertTrue(self.os.path.exists(directory)) # test fsync and fdatasync def test_fsync_pass(self): - test_file_path = self.make_path('test_file') - self.create_file(test_file_path, contents='dummy file contents') - test_file = self.open(test_file_path.upper(), 'r+') + test_file_path = self.make_path("test_file") + self.create_file(test_file_path, contents="dummy file contents") + test_file = self.open(test_file_path.upper(), "r+") test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fsync(test_fd) @@ -3658,7 +3759,7 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): # set up self.check_posix_only() self.skip_real_fs() - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) # actual tests self.os.chmod(path.upper(), 0o6543) @@ -3669,12 +3770,12 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_symlink(self): self.skip_if_symlink_not_supported() - file_path = self.make_path('foo', 'bar', 'baz') - self.create_dir(self.make_path('foo', 'bar')) - self.os.symlink('bogus', file_path.upper()) + file_path = self.make_path("foo", "bar", "baz") + self.create_dir(self.make_path("foo", "bar")) + self.os.symlink("bogus", file_path.upper()) self.assertTrue(self.os.path.lexists(file_path)) self.assertFalse(self.os.path.exists(file_path)) - self.create_file(self.make_path('Foo', 'Bar', 'Bogus')) + self.create_file(self.make_path("Foo", "Bar", "Bogus")) self.assertTrue(self.os.path.lexists(file_path)) self.assertTrue(self.os.path.exists(file_path)) @@ -3682,9 +3783,9 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_link_delete(self): self.skip_if_symlink_not_supported() - file1_path = self.make_path('test_file1') - file2_path = self.make_path('test_file2') - contents1 = 'abcdef' + file1_path = self.make_path("test_file1") + file2_path = self.make_path("test_file2") + contents1 = "abcdef" # Create file self.create_file(file1_path, contents=contents1) # link to second file @@ -3698,30 +3799,30 @@ class FakeOsModuleTestCaseInsensitiveFS(FakeOsModuleTestBase): def test_link_is_existing_file(self): self.skip_if_symlink_not_supported() - file_path = self.make_path('foo', 'bar') + file_path = self.make_path("foo", "bar") self.create_file(file_path) - self.assert_raises_os_error(errno.EEXIST, self.os.link, - file_path.upper(), file_path.upper()) + self.assert_raises_os_error( + errno.EEXIST, self.os.link, file_path.upper(), file_path.upper() + ) def test_link_is_broken_symlink(self): # Regression test for #311 self.skip_if_symlink_not_supported() self.check_case_insensitive_fs() - file_path = self.make_path('baz') + file_path = self.make_path("baz") self.create_file(file_path) - path_lower = self.make_path('foo') + path_lower = self.make_path("foo") self.os.symlink(path_lower, path_lower) - path_upper = self.make_path('Foo') - self.assert_raises_os_error(errno.EEXIST, - self.os.link, file_path, path_upper) + path_upper = self.make_path("Foo") + self.assert_raises_os_error(errno.EEXIST, self.os.link, file_path, path_upper) def test_link_with_changed_case(self): # Regression test for #312 self.skip_if_symlink_not_supported() self.check_case_insensitive_fs() - link_path = self.make_path('link') + link_path = self.make_path("link") self.os.symlink(self.base_path, link_path) - link_path = self.os.path.join(link_path, 'Link') + link_path = self.os.path.join(link_path, "Link") self.assertTrue(self.os.lstat(link_path)) @@ -3733,7 +3834,7 @@ class RealOsModuleTestCaseInsensitiveFS(FakeOsModuleTestCaseInsensitiveFS): class FakeOsModuleTimeTest(FakeOsModuleTestBase): def test_chmod_st_ctime(self): with self.mock_time(start=200): - file_path = 'some_file' + file_path = "some_file" self.filesystem.create_file(file_path) self.assertTrue(self.os.path.exists(file_path)) @@ -3745,7 +3846,7 @@ class FakeOsModuleTimeTest(FakeOsModuleTestBase): self.assertEqual(220, st.st_ctime) def test_utime_sets_current_time_if_args_is_none(self): - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) with self.mock_time(start=200): @@ -3754,88 +3855,9 @@ class FakeOsModuleTimeTest(FakeOsModuleTestBase): self.assertEqual(200, st.st_atime) self.assertEqual(200, st.st_mtime) - def test_utime_sets_current_time_if_args_is_none_with_floats(self): - # we set os.stat_float_times() to False, so atime/ctime/mtime - # are converted as ints (seconds since epoch) - stat_float_times = fake_filesystem.FakeOsModule.stat_float_times() - fake_filesystem.FakeOsModule.stat_float_times(False) - try: - with self.mock_time(start=200.9124): - path = '/some_file' - self.createTestFile(path) - - st = self.os.stat(path) - # 200 is the current time established above - # (if converted to int) - self.assertEqual(200, st.st_atime) - self.assertTrue(isinstance(st.st_atime, int)) - self.assertEqual(220, st.st_mtime) - self.assertTrue(isinstance(st.st_mtime, int)) - - self.assertEqual(200912400000, st.st_atime_ns) - self.assertEqual(220912400000, st.st_mtime_ns) - - self.assertEqual(220, st.st_mtime) - self.assertEqual(240, st.st_ctime) - # actual tests - self.os.utime(path, times=None) - st = self.os.stat(path) - self.assertEqual(260, st.st_atime) - self.assertTrue(isinstance(st.st_atime, int)) - self.assertEqual(260, st.st_mtime) - self.assertTrue(isinstance(st.st_mtime, int)) - self.assertEqual(260912400000, st.st_atime_ns) - self.assertEqual(260912400000, st.st_mtime_ns) - finally: - fake_filesystem.FakeOsModule.stat_float_times(stat_float_times) - - def test_utime_sets_current_time_if_args_is_none_with_floats_n_sec(self): - stat_float_times = fake_filesystem.FakeOsModule.stat_float_times() - fake_filesystem.FakeOsModule.stat_float_times(False) - try: - with self.mock_time(start=200.9123): - path = self.make_path('some_file') - self.createTestFile(path) - test_file = self.filesystem.get_object(path) - - st = self.os.stat(path) - self.assertEqual(200, st.st_atime) - self.assertEqual(220, st.st_mtime) - self.assertEqual(240, st.st_ctime) - self.assertEqual(240, test_file.st_ctime) - self.assertTrue(isinstance(st.st_ctime, int)) - self.assertTrue(isinstance(test_file.st_ctime, int)) - - self.os.stat_float_times(True) # first time float time - self.assertEqual(240, st.st_ctime) # st does not change - self.assertEqual(240.9123, test_file.st_ctime) # but the file - self.assertTrue(isinstance(st.st_ctime, int)) - self.assertTrue(isinstance(test_file.st_ctime, float)) - - self.os.stat_float_times(False) # reverting to int - self.assertEqual(240, test_file.st_ctime) - self.assertTrue(isinstance(test_file.st_ctime, int)) - - self.assertEqual(240, st.st_ctime) - self.assertTrue(isinstance(st.st_ctime, int)) - - self.os.stat_float_times(True) - st = self.os.stat(path) - # float time not converted to int - self.assertAlmostEqual(200.9123, st.st_atime) - self.assertAlmostEqual(220.9123, st.st_mtime) - self.assertAlmostEqual(240.9123, test_file.st_ctime, - st.st_ctime) - self.os.utime(path, times=None) - st = self.os.stat(path) - self.assertAlmostEqual(260.9123, st.st_atime) - self.assertAlmostEqual(260.9123, st.st_mtime) - finally: - fake_filesystem.FakeOsModule.stat_float_times(stat_float_times) - def test_utime_sets_specified_time(self): # set up - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) self.os.stat(path) # actual tests @@ -3846,7 +3868,7 @@ class FakeOsModuleTimeTest(FakeOsModuleTestBase): def test_utime_dir(self): # set up - path = '/some_dir' + path = "/some_dir" self.createTestDirectory(path) # actual tests self.os.utime(path, times=(1.0, 2.0)) @@ -3855,9 +3877,9 @@ class FakeOsModuleTimeTest(FakeOsModuleTestBase): self.assertEqual(2.0, st.st_mtime) def test_utime_follow_symlinks(self): - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) - link_path = '/link_to_some_file' + link_path = "/link_to_some_file" self.filesystem.create_symlink(link_path, path) self.os.utime(link_path, times=(1, 2)) @@ -3866,9 +3888,9 @@ class FakeOsModuleTimeTest(FakeOsModuleTestBase): self.assertEqual(2, st.st_mtime) def test_utime_no_follow_symlinks(self): - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) - link_path = '/link_to_some_file' + link_path = "/link_to_some_file" self.filesystem.create_symlink(link_path, path) self.os.utime(link_path, times=(1, 2), follow_symlinks=False) @@ -3880,22 +3902,22 @@ class FakeOsModuleTimeTest(FakeOsModuleTestBase): self.assertEqual(2, st.st_mtime) def test_utime_non_existent(self): - path = '/non/existent/file' + path = "/non/existent/file" self.assertFalse(self.os.path.exists(path)) self.assert_raises_os_error(errno.ENOENT, self.os.utime, path, (1, 2)) def test_utime_invalid_times_arg_raises(self): - path = '/some_dir' + path = "/some_dir" self.createTestDirectory(path) # the error message differs with different Python versions # we don't expect the same message here self.assertRaises(TypeError, self.os.utime, path, (1, 2, 3)) - self.assertRaises(TypeError, self.os.utime, path, (1, 'str')) + self.assertRaises(TypeError, self.os.utime, path, (1, "str")) def test_utime_sets_specified_time_in_ns(self): # set up - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) self.os.stat(path) @@ -3906,19 +3928,20 @@ class FakeOsModuleTimeTest(FakeOsModuleTestBase): self.assertEqual(0.4, st.st_mtime) def test_utime_incorrect_ns_argument_raises(self): - file_path = 'some_file' + file_path = "some_file" self.filesystem.create_file(file_path) self.assertRaises(TypeError, self.os.utime, file_path, ns=200000000) - self.assertRaises(TypeError, self.os.utime, file_path, ns=('a', 'b')) - self.assertRaises(ValueError, self.os.utime, file_path, times=(1, 2), - ns=(100, 200)) + self.assertRaises(TypeError, self.os.utime, file_path, ns=("a", "b")) + self.assertRaises( + ValueError, self.os.utime, file_path, times=(1, 2), ns=(100, 200) + ) def test_utime_uses_open_fd_as_path(self): if os.utime not in os.supports_fd: self.skip_real_fs() self.assert_raises_os_error(errno.EBADF, self.os.utime, 5, (1, 2)) - path = self.make_path('some_file') + path = self.make_path("some_file") self.createTestFile(path) with FakeFileOpen(self.filesystem)(path) as f: @@ -3936,47 +3959,45 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): super(FakeOsModuleLowLevelFileOpTest, self).setUp() def test_open_read_only(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_RDONLY) - self.assertEqual(b'contents', self.os.read(file_des, 8)) - self.assert_raises_os_error(errno.EBADF, - self.os.write, file_des, b'test') + self.assertEqual(b"contents", self.os.read(file_des, 8)) + self.assert_raises_os_error(errno.EBADF, self.os.write, file_des, b"test") self.os.close(file_des) def test_open_read_only_write_zero_bytes_posix(self): self.check_posix_only() - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_RDONLY) - self.assert_raises_os_error(errno.EBADF, - self.os.write, file_des, b'test') + self.assert_raises_os_error(errno.EBADF, self.os.write, file_des, b"test") self.os.close(file_des) def test_open_read_only_write_zero_bytes_windows(self): # under Windows, writing an empty string to a read only file # is not an error self.check_windows_only() - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_RDONLY) - self.assertEqual(0, self.os.write(file_des, b'')) + self.assertEqual(0, self.os.write(file_des, b"")) self.os.close(file_des) def test_open_write_only(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_WRONLY) - self.assertEqual(4, self.os.write(file_des, b'test')) - self.check_contents(file_path, b'testents') + self.assertEqual(4, self.os.write(file_des, b"test")) + self.check_contents(file_path, b"testents") self.os.close(file_des) def test_open_write_only_raises_on_read(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_WRONLY) self.assert_raises_os_error(errno.EBADF, self.os.read, file_des, 5) @@ -3984,18 +4005,17 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): file_des = self.os.open(file_path, os.O_WRONLY | os.O_TRUNC) self.assert_raises_os_error(errno.EBADF, self.os.read, file_des, 5) self.os.close(file_des) - file_path2 = self.make_path('file2') + file_path2 = self.make_path("file2") file_des = self.os.open(file_path2, os.O_CREAT | os.O_WRONLY) self.assert_raises_os_error(errno.EBADF, self.os.read, file_des, 5) self.os.close(file_des) - file_des = self.os.open(file_path2, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC) + file_des = self.os.open(file_path2, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) self.assert_raises_os_error(errno.EBADF, self.os.read, file_des, 5) self.os.close(file_des) def test_open_write_only_read_zero_bytes_posix(self): self.check_posix_only() - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_CREAT | os.O_WRONLY) self.assert_raises_os_error(errno.EBADF, self.os.read, file_des, 0) self.os.close(file_des) @@ -4003,129 +4023,135 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): def test_open_write_only_read_zero_bytes_windows(self): # under Windows, reading 0 bytes from a write only file is not an error self.check_windows_only() - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_CREAT | os.O_WRONLY) - self.assertEqual(b'', self.os.read(file_des, 0)) + self.assertEqual(b"", self.os.read(file_des, 0)) self.os.close(file_des) def test_open_read_write(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_RDWR) - self.assertEqual(4, self.os.write(file_des, b'test')) - self.check_contents(file_path, b'testents') + self.assertEqual(4, self.os.write(file_des, b"test")) + self.check_contents(file_path, b"testents") self.os.close(file_des) def test_open_create_is_read_only(self): - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_CREAT) - self.assertEqual(b'', self.os.read(file_des, 1)) - self.assert_raises_os_error(errno.EBADF, - self.os.write, file_des, b'foo') + self.assertEqual(b"", self.os.read(file_des, 1)) + self.assert_raises_os_error(errno.EBADF, self.os.write, file_des, b"foo") self.os.close(file_des) def test_open_create_truncate_is_read_only(self): - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_CREAT | os.O_TRUNC) - self.assertEqual(b'', self.os.read(file_des, 1)) - self.assert_raises_os_error(errno.EBADF, - self.os.write, file_des, b'foo') + self.assertEqual(b"", self.os.read(file_des, 1)) + self.assert_raises_os_error(errno.EBADF, self.os.write, file_des, b"foo") self.os.close(file_des) def test_open_raises_if_does_not_exist(self): - file_path = self.make_path('file1') - self.assert_raises_os_error(errno.ENOENT, self.os.open, file_path, - os.O_RDONLY) - self.assert_raises_os_error(errno.ENOENT, self.os.open, file_path, - os.O_WRONLY) - self.assert_raises_os_error(errno.ENOENT, self.os.open, file_path, - os.O_RDWR) + file_path = self.make_path("file1") + self.assert_raises_os_error(errno.ENOENT, self.os.open, file_path, os.O_RDONLY) + self.assert_raises_os_error(errno.ENOENT, self.os.open, file_path, os.O_WRONLY) + self.assert_raises_os_error(errno.ENOENT, self.os.open, file_path, os.O_RDWR) def test_exclusive_open_raises_without_create_mode(self): self.skip_real_fs() - file_path = self.make_path('file1') - self.assertRaises(NotImplementedError, self.os.open, file_path, - os.O_EXCL) - self.assertRaises(NotImplementedError, self.os.open, file_path, - os.O_EXCL | os.O_WRONLY) - self.assertRaises(NotImplementedError, self.os.open, file_path, - os.O_EXCL | os.O_RDWR) - self.assertRaises(NotImplementedError, self.os.open, file_path, - os.O_EXCL | os.O_TRUNC | os.O_APPEND) + file_path = self.make_path("file1") + self.assertRaises(NotImplementedError, self.os.open, file_path, os.O_EXCL) + self.assertRaises( + NotImplementedError, + self.os.open, + file_path, + os.O_EXCL | os.O_WRONLY, + ) + self.assertRaises( + NotImplementedError, self.os.open, file_path, os.O_EXCL | os.O_RDWR + ) + self.assertRaises( + NotImplementedError, + self.os.open, + file_path, + os.O_EXCL | os.O_TRUNC | os.O_APPEND, + ) def test_open_raises_if_parent_does_not_exist(self): - path = self.make_path('alpha', 'alpha') - self.assert_raises_os_error(errno.ENOENT, self.os.open, path, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC) + path = self.make_path("alpha", "alpha") + self.assert_raises_os_error( + errno.ENOENT, + self.os.open, + path, + os.O_CREAT | os.O_WRONLY | os.O_TRUNC, + ) def test_open_truncate(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_RDWR | os.O_TRUNC) - self.assertEqual(b'', self.os.read(file_des, 8)) - self.assertEqual(4, self.os.write(file_des, b'test')) - self.check_contents(file_path, b'test') + self.assertEqual(b"", self.os.read(file_des, 8)) + self.assertEqual(4, self.os.write(file_des, b"test")) + self.check_contents(file_path, b"test") self.os.close(file_des) - @unittest.skipIf(not TestCase.is_windows, - 'O_TEMPORARY only present in Windows') + @unittest.skipIf(not TestCase.is_windows, "O_TEMPORARY only present in Windows") def test_temp_file(self): - file_path = self.make_path('file1') + file_path = self.make_path("file1") fd = self.os.open(file_path, os.O_CREAT | os.O_RDWR | os.O_TEMPORARY) self.assertTrue(self.os.path.exists(file_path)) self.os.close(fd) self.assertFalse(self.os.path.exists(file_path)) def test_open_append(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") file_des = self.os.open(file_path, os.O_WRONLY | os.O_APPEND) - self.assertEqual(4, self.os.write(file_des, b'test')) - self.check_contents(file_path, b'contentstest') + self.assertEqual(4, self.os.write(file_des, b"test")) + self.check_contents(file_path, b"contentstest") self.os.close(file_des) def test_open_create(self): - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_RDWR | os.O_CREAT) self.assertTrue(self.os.path.exists(file_path)) - self.assertEqual(4, self.os.write(file_des, b'test')) - self.check_contents(file_path, 'test') + self.assertEqual(4, self.os.write(file_des, b"test")) + self.check_contents(file_path, "test") self.os.close(file_des) def test_can_read_after_create_exclusive(self): self.check_posix_only() - path1 = self.make_path('alpha') + path1 = self.make_path("alpha") file_des = self.os.open(path1, os.O_CREAT | os.O_EXCL) - self.assertEqual(b'', self.os.read(file_des, 0)) - self.assert_raises_os_error(errno.EBADF, self.os.write, file_des, b'') + self.assertEqual(b"", self.os.read(file_des, 0)) + self.assert_raises_os_error(errno.EBADF, self.os.write, file_des, b"") self.os.close(file_des) def test_open_create_mode_posix(self): self.check_posix_only() - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o700) self.assertTrue(self.os.path.exists(file_path)) self.assert_raises_os_error(errno.EBADF, self.os.read, file_des, 5) - self.assertEqual(4, self.os.write(file_des, b'test')) + self.assertEqual(4, self.os.write(file_des, b"test")) self.assert_mode_equal(0o700, self.os.stat(file_path).st_mode) self.os.close(file_des) def test_open_create_mode_windows(self): self.check_windows_only() - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o700) self.assertTrue(self.os.path.exists(file_path)) self.assert_raises_os_error(errno.EBADF, self.os.read, file_des, 5) - self.assertEqual(4, self.os.write(file_des, b'test')) + self.assertEqual(4, self.os.write(file_des, b"test")) self.assert_mode_equal(0o666, self.os.stat(file_path).st_mode) self.os.close(file_des) def testOpenCreateMode444Windows(self): self.check_windows_only() - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o442) self.assert_mode_equal(0o444, self.os.stat(file_path).st_mode) self.os.close(file_des) @@ -4133,68 +4159,73 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): def testOpenCreateMode666Windows(self): self.check_windows_only() - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o224) self.assert_mode_equal(0o666, self.os.stat(file_path).st_mode) self.os.close(file_des) def test_open_exclusive(self): - file_path = self.make_path('file1') + file_path = self.make_path("file1") file_des = self.os.open(file_path, os.O_RDWR | os.O_EXCL | os.O_CREAT) self.assertTrue(self.os.path.exists(file_path)) self.os.close(file_des) def test_open_exclusive_raises_if_file_exists(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'contents') - self.assert_raises_os_error(errno.EEXIST, self.os.open, file_path, - os.O_RDWR | os.O_EXCL | os.O_CREAT) - self.assert_raises_os_error(errno.EEXIST, self.os.open, file_path, - os.O_RDWR | os.O_EXCL | os.O_CREAT) + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"contents") + self.assert_raises_os_error( + errno.EEXIST, + self.os.open, + file_path, + os.O_RDWR | os.O_EXCL | os.O_CREAT, + ) + self.assert_raises_os_error( + errno.EEXIST, + self.os.open, + file_path, + os.O_RDWR | os.O_EXCL | os.O_CREAT, + ) def test_open_exclusive_raises_if_symlink_exists_in_posix(self): self.check_posix_only() - link_path = self.make_path('link') - link_target = self.make_path('link_target') + link_path = self.make_path("link") + link_target = self.make_path("link_target") self.os.symlink(link_target, link_path) self.assert_raises_os_error( - errno.EEXIST, self.os.open, link_path, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC | os.O_EXCL) + errno.EEXIST, + self.os.open, + link_path, + os.O_CREAT | os.O_WRONLY | os.O_TRUNC | os.O_EXCL, + ) def test_open_exclusive_if_symlink_exists_works_in_windows(self): self.check_windows_only() self.skip_if_symlink_not_supported() - link_path = self.make_path('link') - link_target = self.make_path('link_target') + link_path = self.make_path("link") + link_target = self.make_path("link_target") self.os.symlink(link_target, link_path) - fd = self.os.open(link_path, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC | os.O_EXCL) + fd = self.os.open(link_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC | os.O_EXCL) self.os.close(fd) def test_open_directory_raises_under_windows(self): self.check_windows_only() - dir_path = self.make_path('dir') + dir_path = self.make_path("dir") self.create_dir(dir_path) - self.assert_raises_os_error(errno.EACCES, self.os.open, dir_path, - os.O_RDONLY) - self.assert_raises_os_error(errno.EACCES, self.os.open, dir_path, - os.O_WRONLY) - self.assert_raises_os_error(errno.EACCES, self.os.open, dir_path, - os.O_RDWR) + self.assert_raises_os_error(errno.EACCES, self.os.open, dir_path, os.O_RDONLY) + self.assert_raises_os_error(errno.EACCES, self.os.open, dir_path, os.O_WRONLY) + self.assert_raises_os_error(errno.EACCES, self.os.open, dir_path, os.O_RDWR) def test_open_directory_for_writing_raises_under_posix(self): self.check_posix_only() - dir_path = self.make_path('dir') + dir_path = self.make_path("dir") self.create_dir(dir_path) - self.assert_raises_os_error(errno.EISDIR, self.os.open, dir_path, - os.O_WRONLY) - self.assert_raises_os_error(errno.EISDIR, self.os.open, dir_path, - os.O_RDWR) + self.assert_raises_os_error(errno.EISDIR, self.os.open, dir_path, os.O_WRONLY) + self.assert_raises_os_error(errno.EISDIR, self.os.open, dir_path, os.O_RDWR) def test_open_directory_read_only_under_posix(self): self.check_posix_only() self.skip_real_fs() - dir_path = self.make_path('dir') + dir_path = self.make_path("dir") self.create_dir(dir_path) file_des = self.os.open(dir_path, os.O_RDONLY) self.assertEqual(3, file_des) @@ -4204,29 +4235,26 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): self.check_linux_only() dir_path = self.make_path("alpha") self.os.mkdir(dir_path) - self.assert_raises_os_error(errno.EISDIR, - self.os.open, dir_path, os.O_CREAT) + self.assert_raises_os_error(errno.EISDIR, self.os.open, dir_path, os.O_CREAT) def test_writing_to_existing_directory(self): self.check_macos_only() dir_path = self.make_path("alpha") self.os.mkdir(dir_path) fd = self.os.open(dir_path, os.O_CREAT) - self.assert_raises_os_error(errno.EBADF, self.os.write, fd, b'') + self.assert_raises_os_error(errno.EBADF, self.os.write, fd, b"") def test_opening_existing_directory_in_write_mode(self): self.check_posix_only() dir_path = self.make_path("alpha") self.os.mkdir(dir_path) - self.assert_raises_os_error(errno.EISDIR, - self.os.open, dir_path, os.O_WRONLY) + self.assert_raises_os_error(errno.EISDIR, self.os.open, dir_path, os.O_WRONLY) def test_open_mode_posix(self): self.check_posix_only() self.skip_real_fs() - file_path = self.make_path('baz') - file_des = self.os.open(file_path, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC) + file_path = self.make_path("baz") + file_des = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) stat0 = self.os.fstat(file_des) # not a really good test as this replicates the code, # but we don't know the umask at the test system @@ -4235,103 +4263,104 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): def test_open_mode_windows(self): self.check_windows_only() - file_path = self.make_path('baz') - file_des = self.os.open(file_path, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC) + file_path = self.make_path("baz") + file_des = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) stat0 = self.os.fstat(file_des) self.assertEqual(0o100666, stat0.st_mode) self.os.close(file_des) def test_write_read(self): - file_path = self.make_path('file1') - self.create_file(file_path, contents=b'orig contents') - new_contents = b'1234567890abcdef' + file_path = self.make_path("file1") + self.create_file(file_path, contents=b"orig contents") + new_contents = b"1234567890abcdef" - with self.open(file_path, 'wb') as fh: + with self.open(file_path, "wb") as fh: fileno = fh.fileno() - self.assertEqual(len(new_contents), - self.os.write(fileno, new_contents)) + self.assertEqual(len(new_contents), self.os.write(fileno, new_contents)) self.check_contents(file_path, new_contents) - with self.open(file_path, 'rb') as fh: + with self.open(file_path, "rb") as fh: fileno = fh.fileno() - self.assertEqual(b'', self.os.read(fileno, 0)) + self.assertEqual(b"", self.os.read(fileno, 0)) self.assertEqual(new_contents[0:2], self.os.read(fileno, 2)) self.assertEqual(new_contents[2:10], self.os.read(fileno, 8)) self.assertEqual(new_contents[10:], self.os.read(fileno, 100)) - self.assertEqual(b'', self.os.read(fileno, 10)) + self.assertEqual(b"", self.os.read(fileno, 10)) - self.assert_raises_os_error(errno.EBADF, self.os.write, fileno, - new_contents) + self.assert_raises_os_error(errno.EBADF, self.os.write, fileno, new_contents) self.assert_raises_os_error(errno.EBADF, self.os.read, fileno, 10) def test_write_from_different_f_ds(self): # Regression test for #211 - file_path = self.make_path('baz') + file_path = self.make_path("baz") fd0 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) fd1 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) - self.os.write(fd0, b'aaaa') - self.os.write(fd1, b'bb') + self.os.write(fd0, b"aaaa") + self.os.write(fd1, b"bb") self.assertEqual(4, self.os.path.getsize(file_path)) - self.check_contents(file_path, b'bbaa') + self.check_contents(file_path, b"bbaa") self.os.close(fd1) self.os.close(fd0) def test_write_from_different_fds_with_append(self): # Regression test for #268 - file_path = self.make_path('baz') + file_path = self.make_path("baz") fd0 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) fd1 = self.os.open(file_path, os.O_WRONLY | os.O_APPEND) - self.os.write(fd0, b'aaa') - self.os.write(fd1, b'bbb') + self.os.write(fd0, b"aaa") + self.os.write(fd1, b"bbb") self.assertEqual(6, self.os.path.getsize(file_path)) - self.check_contents(file_path, b'aaabbb') + self.check_contents(file_path, b"aaabbb") self.os.close(fd1) self.os.close(fd0) def test_read_only_read_after_write(self): # Regression test for #269 self.check_posix_only() - file_path = self.make_path('foo', 'bar', 'baz') - self.create_file(file_path, contents=b'test') + file_path = self.make_path("foo", "bar", "baz") + self.create_file(file_path, contents=b"test") fd0 = self.os.open(file_path, os.O_CREAT) fd1 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) - self.assertEqual(b'', self.os.read(fd0, 0)) + self.assertEqual(b"", self.os.read(fd0, 0)) self.os.close(fd1) self.os.close(fd0) def test_read_after_closing_write_descriptor(self): # Regression test for #271 - file_path = self.make_path('baz') + file_path = self.make_path("baz") fd0 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) fd1 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) fd2 = self.os.open(file_path, os.O_CREAT) - self.os.write(fd1, b'abc') + self.os.write(fd1, b"abc") self.os.close(fd0) - self.assertEqual(b'abc', self.os.read(fd2, 3)) + self.assertEqual(b"abc", self.os.read(fd2, 3)) self.os.close(fd2) self.os.close(fd1) def test_writing_behind_end_of_file(self): # Regression test for #273 - file_path = self.make_path('baz') + file_path = self.make_path("baz") fd1 = self.os.open(file_path, os.O_CREAT) fd2 = self.os.open(file_path, os.O_RDWR) - self.os.write(fd2, b'm') + self.os.write(fd2, b"m") fd3 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) - self.assertEqual(b'', self.os.read(fd2, 1)) - self.os.write(fd2, b'm') - self.assertEqual(b'\x00m', self.os.read(fd1, 2)) + self.assertEqual(b"", self.os.read(fd2, 1)) + self.os.write(fd2, b"m") + self.assertEqual(b"\x00m", self.os.read(fd1, 2)) self.os.close(fd1) self.os.close(fd2) self.os.close(fd3) def test_devnull_posix(self): self.check_posix_only() + # make sure os.devnull is correctly set after changing the filesystem + self.setup_fake_fs() self.assertTrue(self.os.path.exists(self.os.devnull)) def test_devnull_windows(self): self.check_windows_only() + # make sure os.devnull is correctly set after changing the filesystem + self.setup_fake_fs() if sys.version_info < (3, 8): self.assertFalse(self.os.path.exists(self.os.devnull)) else: @@ -4339,31 +4368,29 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): def test_write_devnull(self): fd = self.os.open(self.os.devnull, os.O_RDWR) - self.assertEqual(4, self.os.write(fd, b'test')) - self.assertEqual(b'', self.os.read(fd, 4)) + self.assertEqual(4, self.os.write(fd, b"test")) + self.assertEqual(b"", self.os.read(fd, 4)) self.os.close(fd) fd = self.os.open(self.os.devnull, os.O_RDONLY) - self.assertEqual(b'', self.os.read(fd, 4)) + self.assertEqual(b"", self.os.read(fd, 4)) self.os.close(fd) def test_sendfile_with_invalid_fd(self): self.check_linux_only() - self.assert_raises_os_error(errno.EBADF, self.os.sendfile, - 100, 101, 0, 100) - src_file_path = self.make_path('foo') - dst_file_path = self.make_path('bar') - self.create_file(src_file_path, 'testcontent') + self.assert_raises_os_error(errno.EBADF, self.os.sendfile, 100, 101, 0, 100) + src_file_path = self.make_path("foo") + dst_file_path = self.make_path("bar") + self.create_file(src_file_path, "testcontent") self.create_file(dst_file_path) fd1 = self.os.open(src_file_path, os.O_RDONLY) fd2 = self.os.open(dst_file_path, os.O_RDONLY) - self.assert_raises_os_error(errno.EBADF, self.os.sendfile, - fd2, fd1, 0, 4) + self.assert_raises_os_error(errno.EBADF, self.os.sendfile, fd2, fd1, 0, 4) def test_sendfile_no_offset(self): self.check_linux_only() - src_file_path = self.make_path('foo') - dst_file_path = self.make_path('bar') - self.create_file(src_file_path, 'testcontent') + src_file_path = self.make_path("foo") + dst_file_path = self.make_path("bar") + self.create_file(src_file_path, "testcontent") self.create_file(dst_file_path) fd1 = self.os.open(src_file_path, os.O_RDONLY) fd2 = self.os.open(dst_file_path, os.O_RDWR) @@ -4371,13 +4398,13 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): self.os.close(fd2) self.os.close(fd1) with self.open(dst_file_path) as f: - self.assertEqual('tes', f.read()) + self.assertEqual("tes", f.read()) def test_sendfile_with_offset(self): self.check_linux_only() - src_file_path = self.make_path('foo') - dst_file_path = self.make_path('bar') - self.create_file(src_file_path, 'testcontent') + src_file_path = self.make_path("foo") + dst_file_path = self.make_path("bar") + self.create_file(src_file_path, "testcontent") self.create_file(dst_file_path) fd1 = self.os.open(src_file_path, os.O_RDONLY) fd2 = self.os.open(dst_file_path, os.O_RDWR) @@ -4385,13 +4412,13 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): self.os.close(fd2) self.os.close(fd1) with self.open(dst_file_path) as f: - self.assertEqual('cont', f.read()) + self.assertEqual("cont", f.read()) def test_sendfile_twice(self): self.check_linux_only() - src_file_path = self.make_path('foo') - dst_file_path = self.make_path('bar') - self.create_file(src_file_path, 'testcontent') + src_file_path = self.make_path("foo") + dst_file_path = self.make_path("bar") + self.create_file(src_file_path, "testcontent") self.create_file(dst_file_path) fd1 = self.os.open(src_file_path, os.O_RDONLY) fd2 = self.os.open(dst_file_path, os.O_RDWR) @@ -4400,13 +4427,13 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): self.os.close(fd2) self.os.close(fd1) with self.open(dst_file_path) as f: - self.assertEqual('contcont', f.read()) + self.assertEqual("contcont", f.read()) def test_sendfile_offset_none(self): self.check_linux_only() - src_file_path = self.make_path('foo') - dst_file_path = self.make_path('bar') - self.create_file(src_file_path, 'testcontent') + src_file_path = self.make_path("foo") + dst_file_path = self.make_path("bar") + self.create_file(src_file_path, "testcontent") self.create_file(dst_file_path) fd1 = self.os.open(src_file_path, os.O_RDONLY) fd2 = self.os.open(dst_file_path, os.O_RDWR) @@ -4415,13 +4442,13 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase): self.os.close(fd2) self.os.close(fd1) with self.open(dst_file_path) as f: - self.assertEqual('testcon', f.read()) + self.assertEqual("testcon", f.read()) - @unittest.skipIf(not TestCase.is_macos, 'Testing MacOs only behavior') + @unittest.skipIf(not TestCase.is_macos, "Testing MacOs only behavior") def test_no_sendfile_to_regular_file_under_macos(self): - src_file_path = self.make_path('foo') - dst_file_path = self.make_path('bar') - self.create_file(src_file_path, 'testcontent') + src_file_path = self.make_path("foo") + dst_file_path = self.make_path("bar") + self.create_file(src_file_path, "testcontent") self.create_file(dst_file_path) fd1 = self.os.open(src_file_path, os.O_RDONLY) fd2 = self.os.open(dst_file_path, os.O_RDWR) @@ -4437,12 +4464,12 @@ class RealOsModuleLowLevelFileOpTest(FakeOsModuleLowLevelFileOpTest): class FakeOsModuleWalkTest(FakeOsModuleTestBase): - def assertWalkResults(self, expected, top, topdown=True, - followlinks=False): + def assertWalkResults(self, expected, top, topdown=True, followlinks=False): # as the result of walk is unsorted, we have to check against # sorted results - result = [step for step in self.os.walk( - top, topdown=topdown, followlinks=followlinks)] + result = list( + step for step in self.os.walk(top, topdown=topdown, followlinks=followlinks) + ) result = sorted(result, key=lambda lst: lst[0]) expected = sorted(expected, key=lambda lst: lst[0]) self.assertEqual(len(expected), len(result)) @@ -4465,46 +4492,46 @@ class FakeOsModuleWalkTest(FakeOsModuleTestBase): def test_walk_top_down(self): """Walk down ordering is correct.""" - base_dir = self.make_path('foo') - self.create_file(self.os.path.join(base_dir, '1.txt')) - self.create_file(self.os.path.join(base_dir, 'bar1', '2.txt')) - self.create_file(self.os.path.join(base_dir, 'bar1', 'baz', '3.txt')) - self.create_file(self.os.path.join(base_dir, 'bar2', '4.txt')) + base_dir = self.make_path("foo") + self.create_file(self.os.path.join(base_dir, "1.txt")) + self.create_file(self.os.path.join(base_dir, "bar1", "2.txt")) + self.create_file(self.os.path.join(base_dir, "bar1", "baz", "3.txt")) + self.create_file(self.os.path.join(base_dir, "bar2", "4.txt")) expected = [ - (base_dir, ['bar1', 'bar2'], ['1.txt']), - (self.os.path.join(base_dir, 'bar1'), ['baz'], ['2.txt']), - (self.os.path.join(base_dir, 'bar1', 'baz'), [], ['3.txt']), - (self.os.path.join(base_dir, 'bar2'), [], ['4.txt']), + (base_dir, ["bar1", "bar2"], ["1.txt"]), + (self.os.path.join(base_dir, "bar1"), ["baz"], ["2.txt"]), + (self.os.path.join(base_dir, "bar1", "baz"), [], ["3.txt"]), + (self.os.path.join(base_dir, "bar2"), [], ["4.txt"]), ] self.assertWalkResults(expected, base_dir) def test_walk_bottom_up(self): """Walk up ordering is correct.""" - base_dir = self.make_path('foo') - self.create_file(self.os.path.join(base_dir, 'bar1', 'baz', '1.txt')) - self.create_file(self.os.path.join(base_dir, 'bar1', '2.txt')) - self.create_file(self.os.path.join(base_dir, 'bar2', '3.txt')) - self.create_file(self.os.path.join(base_dir, '4.txt')) + base_dir = self.make_path("foo") + self.create_file(self.os.path.join(base_dir, "bar1", "baz", "1.txt")) + self.create_file(self.os.path.join(base_dir, "bar1", "2.txt")) + self.create_file(self.os.path.join(base_dir, "bar2", "3.txt")) + self.create_file(self.os.path.join(base_dir, "4.txt")) expected = [ - (self.os.path.join(base_dir, 'bar1', 'baz'), [], ['1.txt']), - (self.os.path.join(base_dir, 'bar1'), ['baz'], ['2.txt']), - (self.os.path.join(base_dir, 'bar2'), [], ['3.txt']), - (base_dir, ['bar1', 'bar2'], ['4.txt']), + (self.os.path.join(base_dir, "bar1", "baz"), [], ["1.txt"]), + (self.os.path.join(base_dir, "bar1"), ["baz"], ["2.txt"]), + (self.os.path.join(base_dir, "bar2"), [], ["3.txt"]), + (base_dir, ["bar1", "bar2"], ["4.txt"]), ] - self.assertWalkResults(expected, self.make_path('foo'), topdown=False) + self.assertWalkResults(expected, self.make_path("foo"), topdown=False) def test_walk_raises_if_non_existent(self): """Raises an exception when attempting to walk - non-existent directory.""" - directory = self.make_path('foo', 'bar') + non-existent directory.""" + directory = self.make_path("foo", "bar") self.assertEqual(False, self.os.path.exists(directory)) generator = self.os.walk(directory) self.assertRaises(StopIteration, next, generator) def test_walk_raises_if_not_directory(self): """Raises an exception when attempting to walk a non-directory.""" - filename = self.make_path('foo', 'bar') + filename = self.make_path("foo", "bar") self.create_file(filename) generator = self.os.walk(filename) self.assertRaises(StopIteration, next, generator) @@ -4513,7 +4540,7 @@ class FakeOsModuleWalkTest(FakeOsModuleTestBase): """Calls onerror with correct errno when walking non-existent directory.""" self.ResetErrno() - directory = self.make_path('foo', 'bar') + directory = self.make_path("foo", "bar") self.assertEqual(False, self.os.path.exists(directory)) # Calling os.walk on a non-existent directory should trigger # a call to the onerror method. @@ -4525,7 +4552,7 @@ class FakeOsModuleWalkTest(FakeOsModuleTestBase): def test_walk_calls_on_error_if_not_directory(self): """Calls onerror with correct errno when walking non-directory.""" self.ResetErrno() - filename = self.make_path('foo' 'bar') + filename = self.make_path("foo" "bar") self.create_file(filename) self.assertEqual(True, self.os.path.exists(filename)) # Calling `os.walk` on a file should trigger a call to the @@ -4537,16 +4564,16 @@ class FakeOsModuleWalkTest(FakeOsModuleTestBase): def test_walk_skips_removed_directories(self): """Caller can modify list of directories to visit while walking.""" - root = self.make_path('foo') - visit = 'visit' - no_visit = 'no_visit' - self.create_file(self.os.path.join(root, 'bar')) - self.create_file(self.os.path.join(root, visit, '1.txt')) - self.create_file(self.os.path.join(root, visit, '2.txt')) - self.create_file(self.os.path.join(root, no_visit, '3.txt')) - self.create_file(self.os.path.join(root, no_visit, '4.txt')) - - generator = self.os.walk(self.make_path('foo')) + root = self.make_path("foo") + visit = "visit" + no_visit = "no_visit" + self.create_file(self.os.path.join(root, "bar")) + self.create_file(self.os.path.join(root, visit, "1.txt")) + self.create_file(self.os.path.join(root, visit, "2.txt")) + self.create_file(self.os.path.join(root, no_visit, "3.txt")) + self.create_file(self.os.path.join(root, no_visit, "4.txt")) + + generator = self.os.walk(self.make_path("foo")) root_contents = next(generator) root_contents[1].remove(no_visit) @@ -4561,84 +4588,83 @@ class FakeOsModuleWalkTest(FakeOsModuleTestBase): def test_walk_followsymlink_disabled(self): self.check_posix_only() - base_dir = self.make_path('foo') - link_dir = self.make_path('linked') - self.create_file(self.os.path.join(link_dir, 'subfile')) - self.create_file(self.os.path.join(base_dir, 'bar', 'baz')) - self.create_file(self.os.path.join(base_dir, 'bar', 'xyzzy', 'plugh')) - self.create_symlink( - self.os.path.join(base_dir, 'created_link'), link_dir) + base_dir = self.make_path("foo") + link_dir = self.make_path("linked") + self.create_file(self.os.path.join(link_dir, "subfile")) + self.create_file(self.os.path.join(base_dir, "bar", "baz")) + self.create_file(self.os.path.join(base_dir, "bar", "xyzzy", "plugh")) + self.create_symlink(self.os.path.join(base_dir, "created_link"), link_dir) expected = [ - (base_dir, ['bar', 'created_link'], []), - (self.os.path.join(base_dir, 'bar'), ['xyzzy'], ['baz']), - (self.os.path.join(base_dir, 'bar', 'xyzzy'), [], ['plugh']), + (base_dir, ["bar", "created_link"], []), + (self.os.path.join(base_dir, "bar"), ["xyzzy"], ["baz"]), + (self.os.path.join(base_dir, "bar", "xyzzy"), [], ["plugh"]), ] self.assertWalkResults(expected, base_dir, followlinks=False) - expected = [(self.os.path.join(base_dir, 'created_link'), - [], ['subfile'])] - self.assertWalkResults(expected, - self.os.path.join(base_dir, 'created_link'), - followlinks=False) + expected = [(self.os.path.join(base_dir, "created_link"), [], ["subfile"])] + self.assertWalkResults( + expected, + self.os.path.join(base_dir, "created_link"), + followlinks=False, + ) def test_walk_followsymlink_enabled(self): self.check_posix_only() - base_dir = self.make_path('foo') - link_dir = self.make_path('linked') - self.create_file(self.os.path.join(link_dir, 'subfile')) - self.create_file(self.os.path.join(base_dir, 'bar', 'baz')) - self.create_file(self.os.path.join(base_dir, 'bar', 'xyzzy', 'plugh')) - self.create_symlink(self.os.path.join(base_dir, 'created_link'), - self.os.path.join(link_dir)) + base_dir = self.make_path("foo") + link_dir = self.make_path("linked") + self.create_file(self.os.path.join(link_dir, "subfile")) + self.create_file(self.os.path.join(base_dir, "bar", "baz")) + self.create_file(self.os.path.join(base_dir, "bar", "xyzzy", "plugh")) + self.create_symlink( + self.os.path.join(base_dir, "created_link"), + self.os.path.join(link_dir), + ) expected = [ - (base_dir, ['bar', 'created_link'], []), - (self.os.path.join(base_dir, 'bar'), ['xyzzy'], ['baz']), - (self.os.path.join(base_dir, 'bar', 'xyzzy'), [], ['plugh']), - (self.os.path.join(base_dir, 'created_link'), [], ['subfile']), + (base_dir, ["bar", "created_link"], []), + (self.os.path.join(base_dir, "bar"), ["xyzzy"], ["baz"]), + (self.os.path.join(base_dir, "bar", "xyzzy"), [], ["plugh"]), + (self.os.path.join(base_dir, "created_link"), [], ["subfile"]), ] self.assertWalkResults(expected, base_dir, followlinks=True) - expected = [(self.os.path.join(base_dir, 'created_link'), - [], ['subfile'])] - self.assertWalkResults(expected, - self.os.path.join(base_dir, 'created_link'), - followlinks=True) + expected = [(self.os.path.join(base_dir, "created_link"), [], ["subfile"])] + self.assertWalkResults( + expected, + self.os.path.join(base_dir, "created_link"), + followlinks=True, + ) def test_walk_linked_file_in_subdir(self): # regression test for #559 (tested for link on incomplete path) self.check_posix_only() # need to have a top-level link to reproduce the bug - skip real fs self.skip_real_fs() - file_path = '/foo/bar/baz' + file_path = "/foo/bar/baz" self.create_file(file_path) - self.create_symlink('bar', file_path) - expected = [ - ('/foo', ['bar'], []), - ('/foo/bar', [], ['baz']) - ] - self.assertWalkResults(expected, '/foo') + self.create_symlink("bar", file_path) + expected = [("/foo", ["bar"], []), ("/foo/bar", [], ["baz"])] + self.assertWalkResults(expected, "/foo") def test_base_dirpath(self): # regression test for #512 - file_path = self.make_path('foo', 'bar', 'baz') + file_path = self.make_path("foo", "bar", "baz") self.create_file(file_path) variants = [ - self.make_path('foo', 'bar'), - self.make_path('foo', '..', 'foo', 'bar'), - self.make_path('foo', '..', 'foo', 'bar') + - self.os.path.sep * 3, - self.make_path('foo') + self.os.path.sep * 3 + 'bar' + self.make_path("foo", "bar"), + self.make_path("foo", "..", "foo", "bar"), + self.make_path("foo", "..", "foo", "bar") + self.os.path.sep * 3, + self.make_path("foo") + self.os.path.sep * 3 + "bar", ] for base_dir in variants: - for dirpath, dirnames, filenames in self.os.walk(base_dir): + for dirpath, _dirnames, _filenames in self.os.walk(base_dir): self.assertEqual(dirpath, base_dir) - file_path = self.make_path('foo', 'bar', 'dir', 'baz') + file_path = self.make_path("foo", "bar", "dir", "baz") self.create_file(file_path) for base_dir in variants: - for dirpath, dirnames, filenames in self.os.walk(base_dir): + for dirpath, _dirnames, _filenames in self.os.walk(base_dir): self.assertTrue(dirpath.startswith(base_dir)) @@ -4650,289 +4676,338 @@ class RealOsModuleWalkTest(FakeOsModuleWalkTest): class FakeOsModuleDirFdTest(FakeOsModuleTestBase): def setUp(self): super(FakeOsModuleDirFdTest, self).setUp() - self.os.supports_dir_fd = set() + self.os.supports_dir_fd.clear() self.filesystem.is_windows_fs = False - self.filesystem.create_dir('/foo/bar') - self.dir_fd = self.os.open('/foo', os.O_RDONLY) - self.filesystem.create_file('/foo/baz') + self.filesystem.create_dir("/foo/bar") + self.dir_fd = self.os.open("/foo", os.O_RDONLY) + self.filesystem.create_file("/foo/baz") def test_access(self): self.assertRaises( - NotImplementedError, self.os.access, 'baz', self.os.F_OK, - dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.access) - self.assertTrue( - self.os.access('baz', self.os.F_OK, dir_fd=self.dir_fd)) + NotImplementedError, + self.os.access, + "baz", + self.os.F_OK, + dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.access) + self.assertTrue(self.os.access("baz", self.os.F_OK, dir_fd=self.dir_fd)) def test_chmod(self): self.assertRaises( - NotImplementedError, self.os.chmod, 'baz', 0o6543, - dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.chmod) - self.os.chmod('baz', 0o6543, dir_fd=self.dir_fd) - st = self.os.stat('/foo/baz') + NotImplementedError, + self.os.chmod, + "baz", + 0o6543, + dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.chmod) + self.os.chmod("baz", 0o6543, dir_fd=self.dir_fd) + st = self.os.stat("/foo/baz") self.assert_mode_equal(0o6543, st.st_mode) - @unittest.skipIf(not hasattr(os, 'chown'), - 'chown not on all platforms available') + @unittest.skipIf(not hasattr(os, "chown"), "chown not on all platforms available") def test_chown(self): self.assertRaises( - NotImplementedError, self.os.chown, 'baz', 100, 101, - dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.chown) - self.os.chown('baz', 100, 101, dir_fd=self.dir_fd) - st = self.os.stat('/foo/baz') + NotImplementedError, + self.os.chown, + "baz", + 100, + 101, + dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.chown) + self.os.chown("baz", 100, 101, dir_fd=self.dir_fd) + st = self.os.stat("/foo/baz") self.assertEqual(st[stat.ST_UID], 100) self.assertEqual(st[stat.ST_GID], 101) def test_link_src_fd(self): self.assertRaises( - NotImplementedError, self.os.link, 'baz', '/bat', - src_dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.link) - self.os.link('baz', '/bat', src_dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/bat')) + NotImplementedError, + self.os.link, + "baz", + "/bat", + src_dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.link) + self.os.link("baz", "/bat", src_dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/bat")) def test_link_dst_fd(self): self.assertRaises( - NotImplementedError, self.os.link, 'baz', '/bat', - dst_dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.link) - self.os.link('/foo/baz', 'bat', dst_dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/foo/bat')) + NotImplementedError, + self.os.link, + "baz", + "/bat", + dst_dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.link) + self.os.link("/foo/baz", "bat", dst_dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/foo/bat")) def test_symlink(self): self.assertRaises( - NotImplementedError, self.os.symlink, 'baz', '/bat', - dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.symlink) - self.os.symlink('baz', '/bat', dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/bat')) + NotImplementedError, + self.os.symlink, + "baz", + "/bat", + dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.symlink) + self.os.symlink("baz", "/bat", dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/bat")) def test_readlink(self): self.skip_if_symlink_not_supported() - self.filesystem.create_symlink('/meyer/lemon/pie', '/foo/baz') - self.filesystem.create_symlink('/geo/metro', '/meyer') + self.filesystem.create_symlink("/meyer/lemon/pie", "/foo/baz") + self.filesystem.create_symlink("/geo/metro", "/meyer") self.assertRaises( - NotImplementedError, self.os.readlink, '/geo/metro/lemon/pie', - dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.readlink) - self.assertEqual('/foo/baz', self.os.readlink( - '/geo/metro/lemon/pie', dir_fd=self.dir_fd)) + NotImplementedError, + self.os.readlink, + "/geo/metro/lemon/pie", + dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.readlink) + self.assertEqual( + "/foo/baz", + self.os.readlink("/geo/metro/lemon/pie", dir_fd=self.dir_fd), + ) def test_stat(self): - self.assertRaises( - NotImplementedError, self.os.stat, 'baz', dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.stat) - st = self.os.stat('baz', dir_fd=self.dir_fd) + self.assertRaises(NotImplementedError, self.os.stat, "baz", dir_fd=self.dir_fd) + self.os.supports_dir_fd.add(self.os.stat) + st = self.os.stat("baz", dir_fd=self.dir_fd) self.assertEqual(st.st_mode, 0o100666) def test_lstat(self): - self.assertRaises( - NotImplementedError, self.os.lstat, 'baz', dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.lstat) - st = self.os.lstat('baz', dir_fd=self.dir_fd) + self.assertRaises(NotImplementedError, self.os.lstat, "baz", dir_fd=self.dir_fd) + self.os.supports_dir_fd.add(self.os.lstat) + st = self.os.lstat("baz", dir_fd=self.dir_fd) self.assertEqual(st.st_mode, 0o100666) def test_mkdir(self): self.assertRaises( - NotImplementedError, self.os.mkdir, 'newdir', dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.mkdir) - self.os.mkdir('newdir', dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/foo/newdir')) + NotImplementedError, self.os.mkdir, "newdir", dir_fd=self.dir_fd + ) + self.os.supports_dir_fd.add(self.os.mkdir) + self.os.mkdir("newdir", dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/foo/newdir")) def test_rmdir(self): - self.assertRaises( - NotImplementedError, self.os.rmdir, 'bar', dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.rmdir) - self.os.rmdir('bar', dir_fd=self.dir_fd) - self.assertFalse(self.os.path.exists('/foo/bar')) + self.assertRaises(NotImplementedError, self.os.rmdir, "bar", dir_fd=self.dir_fd) + self.os.supports_dir_fd.add(self.os.rmdir) + self.os.rmdir("bar", dir_fd=self.dir_fd) + self.assertFalse(self.os.path.exists("/foo/bar")) - @unittest.skipIf(not hasattr(os, 'mknod'), - 'mknod not on all platforms available') + @unittest.skipIf(not hasattr(os, "mknod"), "mknod not on all platforms available") def test_mknod(self): self.assertRaises( - NotImplementedError, self.os.mknod, 'newdir', dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.mknod) - self.os.mknod('newdir', dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/foo/newdir')) + NotImplementedError, self.os.mknod, "newdir", dir_fd=self.dir_fd + ) + self.os.supports_dir_fd.add(self.os.mknod) + self.os.mknod("newdir", dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/foo/newdir")) def test_rename_src_fd(self): self.assertRaises( - NotImplementedError, self.os.rename, 'baz', '/foo/batz', - src_dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.rename) - self.os.rename('bar', '/foo/batz', src_dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/foo/batz')) + NotImplementedError, + self.os.rename, + "baz", + "/foo/batz", + src_dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.rename) + self.os.rename("bar", "/foo/batz", src_dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/foo/batz")) def test_rename_dst_fd(self): self.assertRaises( - NotImplementedError, self.os.rename, 'baz', '/foo/batz', - dst_dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.rename) - self.os.rename('/foo/bar', 'batz', dst_dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/foo/batz')) + NotImplementedError, + self.os.rename, + "baz", + "/foo/batz", + dst_dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.rename) + self.os.rename("/foo/bar", "batz", dst_dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/foo/batz")) def test_replace_src_fd(self): self.assertRaises( - NotImplementedError, self.os.rename, 'baz', '/foo/batz', - src_dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.rename) - self.os.replace('bar', '/foo/batz', src_dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/foo/batz')) + NotImplementedError, + self.os.rename, + "baz", + "/foo/batz", + src_dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.rename) + self.os.replace("bar", "/foo/batz", src_dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/foo/batz")) def test_replace_dst_fd(self): self.assertRaises( - NotImplementedError, self.os.rename, 'baz', '/foo/batz', - dst_dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.rename) - self.os.replace('/foo/bar', 'batz', dst_dir_fd=self.dir_fd) - self.assertTrue(self.os.path.exists('/foo/batz')) + NotImplementedError, + self.os.rename, + "baz", + "/foo/batz", + dst_dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.rename) + self.os.replace("/foo/bar", "batz", dst_dir_fd=self.dir_fd) + self.assertTrue(self.os.path.exists("/foo/batz")) def test_remove(self): self.assertRaises( - NotImplementedError, self.os.remove, 'baz', dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.remove) - self.os.remove('baz', dir_fd=self.dir_fd) - self.assertFalse(self.os.path.exists('/foo/baz')) + NotImplementedError, self.os.remove, "baz", dir_fd=self.dir_fd + ) + self.os.supports_dir_fd.add(self.os.remove) + self.os.remove("baz", dir_fd=self.dir_fd) + self.assertFalse(self.os.path.exists("/foo/baz")) def test_unlink(self): self.assertRaises( - NotImplementedError, self.os.unlink, 'baz', dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.unlink) - self.os.unlink('baz', dir_fd=self.dir_fd) - self.assertFalse(self.os.path.exists('/foo/baz')) + NotImplementedError, self.os.unlink, "baz", dir_fd=self.dir_fd + ) + self.os.supports_dir_fd.add(self.os.unlink) + self.os.unlink("baz", dir_fd=self.dir_fd) + self.assertFalse(self.os.path.exists("/foo/baz")) def test_utime(self): self.assertRaises( - NotImplementedError, self.os.utime, 'baz', (1, 2), - dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.utime) - self.os.utime('baz', times=(1, 2), dir_fd=self.dir_fd) - st = self.os.stat('/foo/baz') + NotImplementedError, + self.os.utime, + "baz", + (1, 2), + dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.utime) + self.os.utime("baz", times=(1, 2), dir_fd=self.dir_fd) + st = self.os.stat("/foo/baz") self.assertEqual(1, st.st_atime) self.assertEqual(2, st.st_mtime) def test_open(self): self.assertRaises( - NotImplementedError, self.os.open, 'baz', os.O_RDONLY, - dir_fd=self.dir_fd) - self.os.supports_dir_fd.add(os.open) - fd = self.os.open('baz', os.O_RDONLY, dir_fd=self.dir_fd) + NotImplementedError, + self.os.open, + "baz", + os.O_RDONLY, + dir_fd=self.dir_fd, + ) + self.os.supports_dir_fd.add(self.os.open) + fd = self.os.open("baz", os.O_RDONLY, dir_fd=self.dir_fd) self.assertLess(0, fd) class StatPropagationTest(TestCase): def setUp(self): - self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') - self.os = fake_filesystem.FakeOsModule(self.filesystem) - self.open = fake_filesystem.FakeFileOpen(self.filesystem) + self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/") + self.os = fake_os.FakeOsModule(self.filesystem) + self.open = fake_open.FakeFileOpen(self.filesystem) def test_file_size_updated_via_close(self): """test that file size gets updated via close().""" - file_dir = 'xyzzy' - file_path = 'xyzzy/close' - content = 'This is a test.' + file_dir = "xyzzy" + file_path = "xyzzy/close" + content = "This is a test." self.os.mkdir(file_dir) - fh = self.open(file_path, 'w') + fh = self.open(file_path, "w") self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual('', self.filesystem.get_object(file_path).contents) + self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.write(content) self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual('', self.filesystem.get_object(file_path).contents) + self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.close() self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual(content, - self.filesystem.get_object(file_path).contents) + self.assertEqual(content, self.filesystem.get_object(file_path).contents) def test_file_size_not_reset_after_close(self): - file_dir = 'xyzzy' - file_path = 'xyzzy/close' + file_dir = "xyzzy" + file_path = "xyzzy/close" self.os.mkdir(file_dir) size = 1234 # The file has size, but no content. When the file is opened for # reading, its size should be preserved. self.filesystem.create_file(file_path, st_size=size) - fh = self.open(file_path, 'r') + fh = self.open(file_path, "r") fh.close() - self.assertEqual(size, self.open(file_path, 'r').size()) + self.assertEqual(size, self.open(file_path, "r").size()) def test_file_size_after_write(self): - file_path = 'test_file' - original_content = 'abcdef' + file_path = "test_file" + original_content = "abcdef" original_size = len(original_content) self.filesystem.create_file(file_path, contents=original_content) - added_content = 'foo bar' + added_content = "foo bar" expected_size = original_size + len(added_content) - fh = self.open(file_path, 'a') + fh = self.open(file_path, "a") fh.write(added_content) self.assertEqual(original_size, fh.size()) fh.close() - self.assertEqual(expected_size, self.open(file_path, 'r').size()) + self.assertEqual(expected_size, self.open(file_path, "r").size()) def test_large_file_size_after_write(self): - file_path = 'test_file' - original_content = 'abcdef' + file_path = "test_file" + original_content = "abcdef" original_size = len(original_content) self.filesystem.create_file(file_path, st_size=original_size) - added_content = 'foo bar' - fh = self.open(file_path, 'a') - self.assertRaises(fake_filesystem.FakeLargeFileIoException, - lambda: fh.write(added_content)) + added_content = "foo bar" + fh = self.open(file_path, "a") + self.assertRaises( + fake_file.FakeLargeFileIoException, + lambda: fh.write(added_content), + ) def test_file_size_updated_via_flush(self): """test that file size gets updated via flush().""" - file_dir = 'xyzzy' - file_name = 'flush' + file_dir = "xyzzy" + file_name = "flush" file_path = self.os.path.join(file_dir, file_name) - content = 'This might be a test.' + content = "This might be a test." self.os.mkdir(file_dir) - fh = self.open(file_path, 'w') + fh = self.open(file_path, "w") self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual('', self.filesystem.get_object(file_path).contents) + self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.write(content) self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual('', self.filesystem.get_object(file_path).contents) + self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.flush() self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual(content, - self.filesystem.get_object(file_path).contents) + self.assertEqual(content, self.filesystem.get_object(file_path).contents) fh.close() self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual(content, - self.filesystem.get_object(file_path).contents) + self.assertEqual(content, self.filesystem.get_object(file_path).contents) def test_file_size_truncation(self): """test that file size gets updated via open().""" - file_dir = 'xyzzy' - file_path = 'xyzzy/truncation' - content = 'AAA content.' + file_dir = "xyzzy" + file_path = "xyzzy/truncation" + content = "AAA content." # pre-create file with content self.os.mkdir(file_dir) - fh = self.open(file_path, 'w') + fh = self.open(file_path, "w") fh.write(content) fh.close() self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual(content, - self.filesystem.get_object(file_path).contents) + self.assertEqual(content, self.filesystem.get_object(file_path).contents) # test file truncation - fh = self.open(file_path, 'w') + fh = self.open(file_path, "w") self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) - self.assertEqual('', self.filesystem.get_object(file_path).contents) + self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.close() -@unittest.skipIf(not use_scandir, 'only run if scandir is available') +@unittest.skipIf(not use_scandir, "only run if scandir is available") class FakeScandirTest(FakeOsModuleTestBase): FILE_SIZE = 50 LINKED_FILE_SIZE = 10 def setUp(self): super(FakeScandirTest, self).setUp() - self.supports_symlinks = (not self.is_windows or - not self.use_real_fs()) + self.supports_symlinks = not self.is_windows or not self.use_real_fs() if use_scandir_package: if self.use_real_fs(): @@ -4948,35 +5023,34 @@ class FakeScandirTest(FakeOsModuleTestBase): scandir = self.os.scandir self.scandir = scandir - self.directory = self.make_path('xyzzy', 'plugh') - link_dir = self.make_path('linked', 'plugh') - self.linked_file_path = self.os.path.join(link_dir, 'file') - self.linked_dir_path = self.os.path.join(link_dir, 'dir') - self.rel_linked_dir_path = ( - self.os.path.join('..', '..', 'linked', 'plugh', 'dir')) - self.rel_linked_file_path = ( - self.os.path.join('..', '..', 'linked', 'plugh', 'file')) - self.dir_path = self.os.path.join(self.directory, 'dir') - self.file_path = self.os.path.join(self.directory, 'file') - self.file_link_path = self.os.path.join(self.directory, 'link_file') - self.dir_link_path = self.os.path.join(self.directory, 'link_dir') - self.file_rel_link_path = self.os.path.join(self.directory, - 'rel_link_file') - self.dir_rel_link_path = self.os.path.join(self.directory, - 'rel_link_dir') + self.directory = self.make_path("xyzzy", "plugh") + link_dir = self.make_path("linked", "plugh") + self.linked_file_path = self.os.path.join(link_dir, "file") + self.linked_dir_path = self.os.path.join(link_dir, "dir") + self.rel_linked_dir_path = self.os.path.join( + "..", "..", "linked", "plugh", "dir" + ) + self.rel_linked_file_path = self.os.path.join( + "..", "..", "linked", "plugh", "file" + ) + self.dir_path = self.os.path.join(self.directory, "dir") + self.file_path = self.os.path.join(self.directory, "file") + self.file_link_path = self.os.path.join(self.directory, "link_file") + self.dir_link_path = self.os.path.join(self.directory, "link_dir") + self.file_rel_link_path = self.os.path.join(self.directory, "rel_link_file") + self.dir_rel_link_path = self.os.path.join(self.directory, "rel_link_dir") self.create_dir(self.dir_path) - self.create_file(self.file_path, contents=b'b' * self.FILE_SIZE) + self.create_file(self.file_path, contents=b"b" * self.FILE_SIZE) if self.supports_symlinks: self.create_dir(self.linked_dir_path) - self.create_file(self.linked_file_path, - contents=b'a' * self.LINKED_FILE_SIZE), + self.create_file( + self.linked_file_path, contents=b"a" * self.LINKED_FILE_SIZE + ), self.create_symlink(self.dir_link_path, self.linked_dir_path) self.create_symlink(self.file_link_path, self.linked_file_path) - self.create_symlink(self.dir_rel_link_path, - self.rel_linked_dir_path) - self.create_symlink(self.file_rel_link_path, - self.rel_linked_file_path) + self.create_symlink(self.dir_rel_link_path, self.rel_linked_dir_path) + self.create_symlink(self.file_rel_link_path, self.rel_linked_file_path) # Changing the working directory below is to make sure relative paths # to the files and directories created above are reasonable. @@ -5004,35 +5078,31 @@ class FakeScandirTest(FakeOsModuleTestBase): return self.directory def test_paths(self): - sorted_names = ['dir', 'file'] + sorted_names = ["dir", "file"] if self.supports_symlinks: - sorted_names.extend(['link_dir', 'link_file', 'rel_link_dir', - 'rel_link_file']) + sorted_names.extend( + ["link_dir", "link_file", "rel_link_dir", "rel_link_file"] + ) self.assertEqual(len(sorted_names), len(self.dir_entries)) - self.assertEqual(sorted_names, - [entry.name for entry in self.dir_entries]) - sorted_paths = [self.os.path.join(self.scandir_path(), name) - for name in sorted_names] - self.assertEqual(sorted_paths, - [entry.path for entry in self.dir_entries]) + self.assertEqual(sorted_names, [entry.name for entry in self.dir_entries]) + sorted_paths = [ + self.os.path.join(self.scandir_path(), name) for name in sorted_names + ] + self.assertEqual(sorted_paths, [entry.path for entry in self.dir_entries]) def test_isfile(self): self.assertFalse(self.dir_entries[0].is_file()) self.assertTrue(self.dir_entries[1].is_file()) if self.supports_symlinks: self.assertFalse(self.dir_entries[2].is_file()) - self.assertFalse( - self.dir_entries[2].is_file(follow_symlinks=False)) + self.assertFalse(self.dir_entries[2].is_file(follow_symlinks=False)) self.assertTrue(self.dir_entries[3].is_file()) - self.assertFalse( - self.dir_entries[3].is_file(follow_symlinks=False)) + self.assertFalse(self.dir_entries[3].is_file(follow_symlinks=False)) self.assertFalse(self.dir_entries[4].is_file()) - self.assertFalse( - self.dir_entries[4].is_file(follow_symlinks=False)) + self.assertFalse(self.dir_entries[4].is_file(follow_symlinks=False)) self.assertTrue(self.dir_entries[5].is_file()) - self.assertFalse( - self.dir_entries[5].is_file(follow_symlinks=False)) + self.assertFalse(self.dir_entries[5].is_file(follow_symlinks=False)) def test_isdir(self): self.assertTrue(self.dir_entries[0].is_dir()) @@ -5059,34 +5129,44 @@ class FakeScandirTest(FakeOsModuleTestBase): def test_path_links_not_resolved(self): # regression test for #350 self.skip_if_symlink_not_supported() - dir_path = self.make_path('A', 'B', 'C') - self.os.makedirs(self.os.path.join(dir_path, 'D')) - link_path = self.make_path('A', 'C') + dir_path = self.make_path("A", "B", "C") + self.os.makedirs(self.os.path.join(dir_path, "D")) + link_path = self.make_path("A", "C") self.os.symlink(dir_path, link_path) - self.assertEqual([self.os.path.join(link_path, 'D')], - [f.path for f in self.scandir(link_path)]) + self.assertEqual( + [self.os.path.join(link_path, "D")], + [f.path for f in self.scandir(link_path)], + ) def test_inode(self): if use_scandir and self.use_real_fs(): if self.is_windows: - self.skipTest( - 'inode seems not to work in scandir module under Windows') + self.skipTest("inode seems not to work in scandir module under Windows") if IN_DOCKER: - self.skipTest( - 'inode seems not to work in a Docker container') - self.assertEqual(self.os.stat(self.dir_path).st_ino, - self.dir_entries[0].inode()) - self.assertEqual(self.os.stat(self.file_path).st_ino, - self.dir_entries[1].inode()) + self.skipTest("inode seems not to work in a Docker container") + self.assertEqual( + self.os.stat(self.dir_path).st_ino, self.dir_entries[0].inode() + ) + self.assertEqual( + self.os.stat(self.file_path).st_ino, self.dir_entries[1].inode() + ) if self.supports_symlinks: - self.assertEqual(self.os.lstat(self.dir_link_path).st_ino, - self.dir_entries[2].inode()) - self.assertEqual(self.os.lstat(self.file_link_path).st_ino, - self.dir_entries[3].inode()) - self.assertEqual(self.os.lstat(self.dir_rel_link_path).st_ino, - self.dir_entries[4].inode()) - self.assertEqual(self.os.lstat(self.file_rel_link_path).st_ino, - self.dir_entries[5].inode()) + self.assertEqual( + self.os.lstat(self.dir_link_path).st_ino, + self.dir_entries[2].inode(), + ) + self.assertEqual( + self.os.lstat(self.file_link_path).st_ino, + self.dir_entries[3].inode(), + ) + self.assertEqual( + self.os.lstat(self.dir_rel_link_path).st_ino, + self.dir_entries[4].inode(), + ) + self.assertEqual( + self.os.lstat(self.file_rel_link_path).st_ino, + self.dir_entries[5].inode(), + ) def test_scandir_stat_nlink(self): # regression test for #350 @@ -5102,75 +5182,103 @@ class FakeScandirTest(FakeOsModuleTestBase): self.assertEqual(1, scandir_stat_nlink) self.assertEqual(1, self.os.stat(self.file_path).st_nlink) - def check_stat(self, absolute_symlink_expected_size, - relative_symlink_expected_size): + @unittest.skipIf(not hasattr(os, "O_DIRECTORY"), "opening directory not supported") + @unittest.skipIf(sys.version_info < (3, 7), "fd not supported for scandir") + def test_scandir_with_fd(self): + # regression test for #723 + temp_dir = self.make_path("tmp", "dir") + self.create_dir(temp_dir) + self.create_file(self.os.path.join(temp_dir, "file1")) + self.create_file(self.os.path.join(temp_dir, "file2")) + self.create_dir(self.os.path.join(temp_dir, "subdir")) + self.os.chdir(temp_dir) + fd = self.os.open(temp_dir, flags=os.O_RDONLY | os.O_DIRECTORY) + children = [dir_entry.name for dir_entry in self.os.scandir(fd)] + assert sorted(children) == ["file1", "file2", "subdir"] + + def check_stat( + self, absolute_symlink_expected_size, relative_symlink_expected_size + ): self.assertEqual(self.FILE_SIZE, self.dir_entries[1].stat().st_size) - self.assertEqual( - int(self.os.stat(self.dir_path).st_ctime), - int(self.dir_entries[0].stat().st_ctime)) + if not self.is_windows_fs or sys.version_info < (3, 12): + # behavior of st_ctime changed in 3.12, to be adapted later + self.assertEqual( + int(self.os.stat(self.dir_path).st_ctime), + int(self.dir_entries[0].stat().st_ctime), + ) if self.supports_symlinks: - self.assertEqual(self.LINKED_FILE_SIZE, - self.dir_entries[3].stat().st_size) - self.assertEqual(absolute_symlink_expected_size, - self.dir_entries[3].stat( - follow_symlinks=False).st_size) + self.assertEqual(self.LINKED_FILE_SIZE, self.dir_entries[3].stat().st_size) + self.assertEqual( + absolute_symlink_expected_size, + self.dir_entries[3].stat(follow_symlinks=False).st_size, + ) self.assertEqual( int(self.os.stat(self.linked_dir_path).st_mtime), - int(self.dir_entries[2].stat().st_mtime)) - self.assertEqual(self.LINKED_FILE_SIZE, - self.dir_entries[5].stat().st_size) - self.assertEqual(relative_symlink_expected_size, - self.dir_entries[5].stat( - follow_symlinks=False).st_size) + int(self.dir_entries[2].stat().st_mtime), + ) + self.assertEqual(self.LINKED_FILE_SIZE, self.dir_entries[5].stat().st_size) + self.assertEqual( + relative_symlink_expected_size, + self.dir_entries[5].stat(follow_symlinks=False).st_size, + ) self.assertEqual( int(self.os.stat(self.linked_dir_path).st_mtime), - int(self.dir_entries[4].stat().st_mtime)) + int(self.dir_entries[4].stat().st_mtime), + ) - @unittest.skipIf(TestCase.is_windows, 'POSIX specific behavior') + @unittest.skipIf(TestCase.is_windows, "POSIX specific behavior") def test_stat_posix(self): - self.check_stat(len(self.linked_file_path), - len(self.rel_linked_file_path)) + self.check_stat(len(self.linked_file_path), len(self.rel_linked_file_path)) - @unittest.skipIf(not TestCase.is_windows, 'Windows specific behavior') + @unittest.skipIf(not TestCase.is_windows, "Windows specific behavior") def test_stat_windows(self): self.check_stat(0, 0) def test_index_access_to_stat_times_returns_int(self): - self.assertEqual(self.os.stat(self.dir_path)[stat.ST_CTIME], - int(self.dir_entries[0].stat().st_ctime)) + if not self.is_windows_fs or sys.version_info < (3, 12): + # behavior of st_ctime changed in 3.12, to be adapted later + self.assertEqual( + self.os.stat(self.dir_path)[stat.ST_CTIME], + int(self.dir_entries[0].stat().st_ctime), + ) if self.supports_symlinks: - self.assertEqual(self.os.stat(self.linked_dir_path)[stat.ST_MTIME], - int(self.dir_entries[2].stat().st_mtime)) - self.assertEqual(self.os.stat(self.linked_dir_path)[stat.ST_MTIME], - int(self.dir_entries[4].stat().st_mtime)) + self.assertEqual( + self.os.stat(self.linked_dir_path)[stat.ST_MTIME], + int(self.dir_entries[2].stat().st_mtime), + ) + self.assertEqual( + self.os.stat(self.linked_dir_path)[stat.ST_MTIME], + int(self.dir_entries[4].stat().st_mtime), + ) def test_stat_ino_dev(self): if self.supports_symlinks: file_stat = self.os.stat(self.linked_file_path) - self.assertEqual(file_stat.st_ino, - self.dir_entries[3].stat().st_ino) - self.assertEqual(file_stat.st_dev, - self.dir_entries[3].stat().st_dev) - self.assertEqual(file_stat.st_ino, - self.dir_entries[5].stat().st_ino) - self.assertEqual(file_stat.st_dev, - self.dir_entries[5].stat().st_dev) - - @unittest.skipIf(sys.version_info < (3, 6) or not use_builtin_scandir, - 'Path-like objects have been introduced in Python 3.6') + self.assertEqual(file_stat.st_ino, self.dir_entries[3].stat().st_ino) + self.assertEqual(file_stat.st_dev, self.dir_entries[3].stat().st_dev) + self.assertEqual(file_stat.st_ino, self.dir_entries[5].stat().st_ino) + self.assertEqual(file_stat.st_dev, self.dir_entries[5].stat().st_dev) + + @unittest.skipIf( + sys.version_info < (3, 6) or not use_builtin_scandir, + "Path-like objects have been introduced in Python 3.6", + ) def test_path_like(self): self.assertTrue(isinstance(self.dir_entries[0], os.PathLike)) - self.assertEqual(self.os.path.join(self.scandir_path(), 'dir'), - os.fspath(self.dir_entries[0])) - self.assertEqual(self.os.path.join(self.scandir_path(), 'file'), - os.fspath(self.dir_entries[1])) + self.assertEqual( + self.os.path.join(self.scandir_path(), "dir"), + os.fspath(self.dir_entries[0]), + ) + self.assertEqual( + self.os.path.join(self.scandir_path(), "file"), + os.fspath(self.dir_entries[1]), + ) def test_non_existing_dir(self): # behaves differently in different systems, so we skip the real fs test self.skip_real_fs() - self.assert_raises_os_error( - errno.ENOENT, self.scandir, 'non_existing/fake_dir') + self.assert_raises_os_error(errno.ENOENT, self.scandir, "non_existing/fake_dir") class RealScandirTest(FakeScandirTest): @@ -5193,9 +5301,8 @@ class RealScandirRelTest(FakeScandirRelTest): return True -@unittest.skipIf(sys.version_info < (3, 7) or TestCase.is_windows or - use_scandir_package, - 'dir_fd support for os.scandir was introduced in Python 3.7') +@unittest.skipIf(TestCase.is_windows, "dir_fd not supported for os.scandir in Windows") +@unittest.skipIf(use_scandir_package, "no dir_fd support for scandir package") class FakeScandirFdTest(FakeScandirTest): def tearDown(self): self.os.close(self.dir_fd) @@ -5204,7 +5311,7 @@ class FakeScandirFdTest(FakeScandirTest): def scandir_path(self): # When scandir is called with a filedescriptor, only the name of the # entry is returned in the path attribute of the DirEntry objects. - return '' + return "" def do_scandir(self): self.dir_fd = self.os.open(self.directory, os.O_RDONLY) @@ -5218,8 +5325,7 @@ class RealScandirFdTest(FakeScandirFdTest): class FakeScandirFdRelTest(FakeScandirFdTest): def do_scandir(self): - self.dir_fd = self.os.open(self.os.path.relpath(self.directory), - os.O_RDONLY) + self.dir_fd = self.os.open(self.os.path.relpath(self.directory), os.O_RDONLY) return self.scandir(self.dir_fd) @@ -5232,8 +5338,8 @@ class FakeExtendedAttributeTest(FakeOsModuleTestBase): def setUp(self): super(FakeExtendedAttributeTest, self).setUp() self.check_linux_only() - self.dir_path = self.make_path('foo') - self.file_path = self.os.path.join(self.dir_path, 'bar') + self.dir_path = self.make_path("foo") + self.file_path = self.os.path.join(self.dir_path, "bar") self.create_file(self.file_path) def test_empty_xattr(self): @@ -5241,63 +5347,182 @@ class FakeExtendedAttributeTest(FakeOsModuleTestBase): self.assertEqual([], self.os.listxattr(self.file_path)) def test_setxattr(self): - self.assertRaises(TypeError, self.os.setxattr, - self.file_path, 'test', 'value') - self.assert_raises_os_error(errno.EEXIST, self.os.setxattr, - self.file_path, 'test', b'value', - self.os.XATTR_REPLACE) - self.os.setxattr(self.file_path, 'test', b'value') - self.assertEqual(b'value', self.os.getxattr(self.file_path, 'test')) - self.assert_raises_os_error(errno.ENODATA, self.os.setxattr, - self.file_path, 'test', b'value', - self.os.XATTR_CREATE) + self.assertRaises(TypeError, self.os.setxattr, self.file_path, "test", "value") + self.assert_raises_os_error( + errno.EEXIST, + self.os.setxattr, + self.file_path, + "test", + b"value", + self.os.XATTR_REPLACE, + ) + self.os.setxattr(self.file_path, "test", b"value") + self.assertEqual(b"value", self.os.getxattr(self.file_path, "test")) + self.assert_raises_os_error( + errno.ENODATA, + self.os.setxattr, + self.file_path, + "test", + b"value", + self.os.XATTR_CREATE, + ) def test_removeattr(self): - self.os.removexattr(self.file_path, 'test') + self.os.removexattr(self.file_path, "test") self.assertEqual([], self.os.listxattr(self.file_path)) - self.os.setxattr(self.file_path, b'test', b'value') - self.assertEqual(['test'], self.os.listxattr(self.file_path)) - self.assertEqual(b'value', self.os.getxattr(self.file_path, 'test')) - self.os.removexattr(self.file_path, 'test') + self.os.setxattr(self.file_path, b"test", b"value") + self.assertEqual(["test"], self.os.listxattr(self.file_path)) + self.assertEqual(b"value", self.os.getxattr(self.file_path, "test")) + self.os.removexattr(self.file_path, "test") self.assertEqual([], self.os.listxattr(self.file_path)) - self.assertIsNone(self.os.getxattr(self.file_path, 'test')) + self.assertIsNone(self.os.getxattr(self.file_path, "test")) def test_default_path(self): self.os.chdir(self.dir_path) - self.os.setxattr(self.dir_path, b'test', b'value') - self.assertEqual(['test'], self.os.listxattr()) - self.assertEqual(b'value', self.os.getxattr(self.dir_path, 'test')) + self.os.setxattr(self.dir_path, b"test", b"value") + self.assertEqual(["test"], self.os.listxattr()) + self.assertEqual(b"value", self.os.getxattr(self.dir_path, "test")) class FakeOsUnreadableDirTest(FakeOsModuleTestBase): def setUp(self): if self.use_real_fs(): - # make sure no dir is created if skipped + # unreadable dirs in Windows are only simulated + # and cannot be created in the real OS using file system + # functions only self.check_posix_only() super(FakeOsUnreadableDirTest, self).setUp() - self.check_posix_only() - self.dir_path = self.make_path('some_dir') - self.file_path = self.os.path.join(self.dir_path, 'some_file') + self.dir_path = self.make_path("some_dir") + self.file_path = self.os.path.join(self.dir_path, "some_file") self.create_file(self.file_path) - self.os.chmod(self.dir_path, 0o000) + self.chmod(self.dir_path, 0o000) + + def chmod(self, path, mode): + if self.is_windows_fs: + self.filesystem.chmod(path, mode, force_unix_mode=True) + else: + self.os.chmod(path, mode) + + def test_getuid(self): + self.skip_real_fs() # won't change user in real fs + self.check_posix_only() + uid = self.os.getuid() + set_uid(uid + 10) + self.assertEqual(uid + 10, self.os.getuid()) + self.assertEqual(uid + 10, get_uid()) + set_uid(uid) + self.assertEqual(uid, self.os.getuid()) + + def test_getgid(self): + self.skip_real_fs() # won't change group in real fs + self.check_posix_only() + gid = self.os.getgid() + set_gid(gid + 10) + self.assertEqual(gid + 10, self.os.getgid()) + self.assertEqual(gid + 10, get_gid()) + set_gid(gid) + self.assertEqual(gid, self.os.getgid()) def test_listdir_unreadable_dir(self): if not is_root(): - self.assert_raises_os_error( - errno.EACCES, self.os.listdir, self.dir_path) + self.assert_raises_os_error(errno.EACCES, self.os.listdir, self.dir_path) else: - self.assertEqual(['some_file'], self.os.listdir(self.dir_path)) + self.assertEqual(["some_file"], self.os.listdir(self.dir_path)) + + def test_listdir_user_readable_dir(self): + self.chmod(self.dir_path, 0o600) + self.assertEqual(["some_file"], self.os.listdir(self.dir_path)) + self.chmod(self.dir_path, 0o000) + + def test_listdir_user_readable_dir_from_other_user(self): + self.skip_real_fs() # won't change user in real fs + self.check_posix_only() + user_id = get_uid() + set_uid(user_id + 1) + dir_path = self.make_path("dir1") + self.create_dir(dir_path, perm=0o600) + self.assertTrue(self.os.path.exists(dir_path)) + set_uid(user_id) + if not is_root(): + with self.assertRaises(PermissionError): + self.os.listdir(dir_path) + else: + self.assertEqual(["some_file"], self.os.listdir(self.dir_path)) + + def test_listdir_group_readable_dir_from_other_user(self): + self.skip_real_fs() # won't change user in real fs + user_id = get_uid() + set_uid(user_id + 1) + dir_path = self.make_path("dir1") + self.create_dir(dir_path, perm=0o660) + self.assertTrue(self.os.path.exists(dir_path)) + set_uid(user_id) + self.assertEqual([], self.os.listdir(dir_path)) + + def test_listdir_group_readable_dir_from_other_group(self): + self.skip_real_fs() # won't change user in real fs + self.check_posix_only() + group_id = self.os.getgid() + set_gid(group_id + 1) + dir_path = self.make_path("dir1") + self.create_dir(dir_path, perm=0o060) + self.assertTrue(self.os.path.exists(dir_path)) + set_gid(group_id) + if not is_root(): + with self.assertRaises(PermissionError): + self.os.listdir(dir_path) + else: + self.assertEqual([], self.os.listdir(dir_path)) + + def test_listdir_other_readable_dir_from_other_group(self): + self.skip_real_fs() # won't change user in real fs + group_id = get_gid() + set_gid(group_id + 1) + dir_path = self.make_path("dir1") + self.create_dir(dir_path, perm=0o004) + self.assertTrue(self.os.path.exists(dir_path)) + set_gid(group_id) + self.assertEqual([], self.os.listdir(dir_path)) def test_stat_unreadable_dir(self): self.assertEqual(0, self.os.stat(self.dir_path).st_mode & 0o666) + def test_chmod_unreadable_dir(self): + self.chmod(self.dir_path, 0o666) + self.assertEqual(0o666, self.os.stat(self.dir_path).st_mode & 0o666) + self.chmod(self.dir_path, 0o000) + self.assertEqual(0, self.os.stat(self.dir_path).st_mode & 0o666) + def test_stat_file_in_unreadable_dir(self): if not is_root(): - self.assert_raises_os_error( - errno.EACCES, self.os.stat, self.file_path) + self.assert_raises_os_error(errno.EACCES, self.os.stat, self.file_path) else: self.assertEqual(0, self.os.stat(self.file_path).st_size) + def test_remove_unreadable_dir(self): + self.check_posix_only() + dir_path = self.make_path("dir1") + self.create_dir(dir_path, perm=0o000) + self.assertTrue(self.os.path.exists(dir_path)) + self.os.rmdir(dir_path) + self.assertFalse(self.os.path.exists(dir_path)) + + def test_remove_unreadable_dir_from_other_user(self): + self.skip_real_fs() # won't change user in real fs + user_id = get_uid() + set_uid(user_id + 1) + dir_path = self.make_path("dir1") + self.create_dir(dir_path, perm=0o000) + self.assertTrue(self.os.path.exists(dir_path)) + set_uid(user_id) + if not is_root(): + with self.assertRaises(PermissionError): + self.os.rmdir(dir_path) + self.assertTrue(self.os.path.exists(dir_path)) + else: + self.os.rmdir(dir_path) + self.assertFalse(self.os.path.exists(dir_path)) + class RealOsUnreadableDirTest(FakeOsUnreadableDirTest): def use_real_fs(self): |