diff options
Diffstat (limited to 'pyfakefs/tests/fake_filesystem_unittest_test.py')
-rw-r--r-- | pyfakefs/tests/fake_filesystem_unittest_test.py | 608 |
1 files changed, 328 insertions, 280 deletions
diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index f4da663..86f1ae6 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -28,7 +28,6 @@ import sys import tempfile import unittest import warnings -from distutils.dir_util import copy_tree, remove_tree from pathlib import Path from unittest import TestCase, mock @@ -37,45 +36,52 @@ import pyfakefs.tests.logsio from pyfakefs import fake_filesystem_unittest, fake_filesystem from pyfakefs.fake_filesystem import OSType from pyfakefs.fake_filesystem_unittest import ( - Patcher, Pause, patchfs, PatchMode + Patcher, + Pause, + patchfs, + PatchMode, ) from pyfakefs.tests.fixtures import module_with_attributes +if sys.version_info < (3, 12): + # distutils removed in Python 3.12 + from distutils.dir_util import copy_tree, remove_tree + class TestPatcher(TestCase): def test_context_manager(self): with Patcher() as patcher: - patcher.fs.create_file('/foo/bar', contents='test') - with open('/foo/bar') as f: + patcher.fs.create_file("/foo/bar", contents="test") + with open("/foo/bar") as f: contents = f.read() - self.assertEqual('test', contents) + self.assertEqual("test", contents) @patchfs def test_context_decorator(self, fake_fs): - fake_fs.create_file('/foo/bar', contents='test') - with open('/foo/bar') as f: + fake_fs.create_file("/foo/bar", contents="test") + with open("/foo/bar") as f: contents = f.read() - self.assertEqual('test', contents) + self.assertEqual("test", contents) class TestPatchfsArgumentOrder(TestCase): @patchfs - @mock.patch('os.system') + @mock.patch("os.system") def test_argument_order1(self, fake_fs, patched_system): - fake_fs.create_file('/foo/bar', contents='test') - with open('/foo/bar') as f: + fake_fs.create_file("/foo/bar", contents="test") + with open("/foo/bar") as f: contents = f.read() - self.assertEqual('test', contents) + self.assertEqual("test", contents) os.system("foo") patched_system.assert_called_with("foo") - @mock.patch('os.system') + @mock.patch("os.system") @patchfs def test_argument_order2(self, patched_system, fake_fs): - fake_fs.create_file('/foo/bar', contents='test') - with open('/foo/bar') as f: + fake_fs.create_file("/foo/bar", contents="test") + with open("/foo/bar") as f: contents = f.read() - self.assertEqual('test', contents) + self.assertEqual("test", contents) os.system("foo") patched_system.assert_called_with("foo") @@ -91,136 +97,150 @@ class TestPyfakefsUnittest(TestPyfakefsUnittestBase): # pylint: disable=R0904 def test_open(self): """Fake `open()` function is bound""" - self.assertFalse(os.path.exists('/fake_file.txt')) - with open('/fake_file.txt', 'w') as f: + self.assertFalse(os.path.exists("/fake_file.txt")) + with open("/fake_file.txt", "w") as f: f.write("This test file was created using the open() function.\n") - self.assertTrue(self.fs.exists('/fake_file.txt')) - with open('/fake_file.txt') as f: + self.assertTrue(self.fs.exists("/fake_file.txt")) + with open("/fake_file.txt") as f: content = f.read() - self.assertEqual('This test file was created using the ' - 'open() function.\n', content) + self.assertEqual( + "This test file was created using the " "open() function.\n", + content, + ) def test_io_open(self): """Fake io module is bound""" - self.assertFalse(os.path.exists('/fake_file.txt')) - with io.open('/fake_file.txt', 'w') as f: - f.write("This test file was created using the" - " io.open() function.\n") - self.assertTrue(self.fs.exists('/fake_file.txt')) - with open('/fake_file.txt') as f: + self.assertFalse(os.path.exists("/fake_file.txt")) + with io.open("/fake_file.txt", "w") as f: + f.write("This test file was created using the" " io.open() function.\n") + self.assertTrue(self.fs.exists("/fake_file.txt")) + with open("/fake_file.txt") as f: content = f.read() - self.assertEqual('This test file was created using the ' - 'io.open() function.\n', content) + self.assertEqual( + "This test file was created using the " "io.open() function.\n", + content, + ) def test_os(self): """Fake os module is bound""" - self.assertFalse(self.fs.exists('/test/dir1/dir2')) - os.makedirs('/test/dir1/dir2') - self.assertTrue(self.fs.exists('/test/dir1/dir2')) + self.assertFalse(self.fs.exists("/test/dir1/dir2")) + os.makedirs("/test/dir1/dir2") + self.assertTrue(self.fs.exists("/test/dir1/dir2")) def test_glob(self): """Fake glob module is bound""" - is_windows = sys.platform.startswith('win') - self.assertEqual([], glob.glob('/test/dir1/dir*')) - self.fs.create_dir('/test/dir1/dir2a') - matching_paths = glob.glob('/test/dir1/dir*') + is_windows = sys.platform.startswith("win") + self.assertEqual([], glob.glob("/test/dir1/dir*")) + self.fs.create_dir("/test/dir1/dir2a") + matching_paths = glob.glob("/test/dir1/dir*") if is_windows: - self.assertEqual([r'/test/dir1\dir2a'], matching_paths) + self.assertEqual([r"/test/dir1\dir2a"], matching_paths) else: - self.assertEqual(['/test/dir1/dir2a'], matching_paths) - self.fs.create_dir('/test/dir1/dir2b') - matching_paths = sorted(glob.glob('/test/dir1/dir*')) + self.assertEqual(["/test/dir1/dir2a"], matching_paths) + self.fs.create_dir("/test/dir1/dir2b") + matching_paths = sorted(glob.glob("/test/dir1/dir*")) if is_windows: - self.assertEqual([r'/test/dir1\dir2a', r'/test/dir1\dir2b'], - matching_paths) + self.assertEqual([r"/test/dir1\dir2a", r"/test/dir1\dir2b"], matching_paths) else: - self.assertEqual(['/test/dir1/dir2a', '/test/dir1/dir2b'], - matching_paths) + self.assertEqual(["/test/dir1/dir2a", "/test/dir1/dir2b"], matching_paths) def test_shutil(self): """Fake shutil module is bound""" - self.fs.create_dir('/test/dir1/dir2a') - self.fs.create_dir('/test/dir1/dir2b') - self.assertTrue(self.fs.exists('/test/dir1/dir2b')) - self.assertTrue(self.fs.exists('/test/dir1/dir2a')) + self.fs.create_dir("/test/dir1/dir2a") + self.fs.create_dir("/test/dir1/dir2b") + self.assertTrue(self.fs.exists("/test/dir1/dir2b")) + self.assertTrue(self.fs.exists("/test/dir1/dir2a")) - shutil.rmtree('/test/dir1') - self.assertFalse(self.fs.exists('/test/dir1')) + shutil.rmtree("/test/dir1") + self.assertFalse(self.fs.exists("/test/dir1")) def test_fakepathlib(self): - with pathlib.Path('/fake_file.txt') as p: - with p.open('w') as f: - f.write('text') - is_windows = sys.platform.startswith('win') + p = pathlib.Path("/fake_file.txt") + with p.open("w") as f: + f.write("text") + is_windows = sys.platform.startswith("win") if is_windows: - self.assertTrue(self.fs.exists(r'\fake_file.txt')) + self.assertTrue(self.fs.exists(r"\fake_file.txt")) else: - self.assertTrue(self.fs.exists('/fake_file.txt')) + self.assertTrue(self.fs.exists("/fake_file.txt")) class TestPatchingImports(TestPyfakefsUnittestBase): def test_import_as_other_name(self): - file_path = '/foo/bar/baz' + file_path = "/foo/bar/baz" self.fs.create_file(file_path) self.assertTrue(self.fs.exists(file_path)) - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists1(file_path)) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists1(file_path)) def test_import_path_from_os(self): """Make sure `from os import path` patches `path`.""" - file_path = '/foo/bar/baz' + file_path = "/foo/bar/baz" self.fs.create_file(file_path) self.assertTrue(self.fs.exists(file_path)) - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists2(file_path)) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists2(file_path)) def test_import_path_from_pathlib(self): - file_path = '/foo/bar' + file_path = "/foo/bar" + self.fs.create_dir(file_path) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists3(file_path)) + + def test_import_exists_from_os_path(self): + file_path = "/foo/bar" self.fs.create_dir(file_path) - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists3(file_path)) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists5(file_path)) + + def test_import_isfile_from_os_path(self): + file_path = "/foo/bar" + self.fs.create_file(file_path) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_isfile(file_path)) - def test_import_function_from_os_path(self): - file_path = '/foo/bar' + def test_import_isdir_from_os_path(self): + file_path = "/foo/bar" self.fs.create_dir(file_path) - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists5(file_path)) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_isdir(file_path)) + + def test_import_islink_from_os_path(self): + file_path = "/foo/bar" + link_path = "/foo/link" + self.fs.create_file(file_path) + self.fs.create_symlink(link_path, file_path) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_islink(link_path)) def test_import_function_from_os_path_as_other_name(self): - file_path = '/foo/bar' + file_path = "/foo/bar" self.fs.create_dir(file_path) - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists6(file_path)) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists6(file_path)) def test_import_pathlib_path(self): - file_path = '/foo/bar' + file_path = "/foo/bar" self.fs.create_dir(file_path) - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists7(file_path)) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists7(file_path)) def test_import_function_from_os(self): - file_path = '/foo/bar' - self.fs.create_file(file_path, contents=b'abc') + file_path = "/foo/bar" + self.fs.create_file(file_path, contents=b"abc") stat_result = pyfakefs.tests.import_as_example.file_stat1(file_path) self.assertEqual(3, stat_result.st_size) def test_import_function_from_os_as_other_name(self): - file_path = '/foo/bar' - self.fs.create_file(file_path, contents=b'abc') + file_path = "/foo/bar" + self.fs.create_file(file_path, contents=b"abc") stat_result = pyfakefs.tests.import_as_example.file_stat2(file_path) self.assertEqual(3, stat_result.st_size) + @unittest.skipIf(sys.version_info >= (3, 12), "Currently not working in 3.12") def test_import_open_as_other_name(self): - file_path = '/foo/bar' - self.fs.create_file(file_path, contents=b'abc') + file_path = "/foo/bar" + self.fs.create_file(file_path, contents=b"abc") contents = pyfakefs.tests.import_as_example.file_contents1(file_path) - self.assertEqual('abc', contents) + self.assertEqual("abc", contents) + @unittest.skipIf(sys.version_info >= (3, 12), "Currently not working in 3.12") def test_import_io_open_as_other_name(self): - file_path = '/foo/bar' - self.fs.create_file(file_path, contents=b'abc') + file_path = "/foo/bar" + self.fs.create_file(file_path, contents=b"abc") contents = pyfakefs.tests.import_as_example.file_contents2(file_path) - self.assertEqual('abc', contents) + self.assertEqual("abc", contents) class TestPatchingDefaultArgs(fake_filesystem_unittest.TestCase): @@ -228,21 +248,19 @@ class TestPatchingDefaultArgs(fake_filesystem_unittest.TestCase): self.setUpPyfakefs(patch_default_args=True) def test_path_exists_as_default_arg_in_function(self): - file_path = '/foo/bar' + file_path = "/foo/bar" self.fs.create_dir(file_path) - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists4(file_path)) + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists4(file_path)) def test_path_exists_as_default_arg_in_method(self): - file_path = '/foo/bar' + file_path = "/foo/bar" self.fs.create_dir(file_path) sut = pyfakefs.tests.import_as_example.TestDefaultArg() self.assertTrue(sut.check_if_exists(file_path)) def test_fake_path_exists4(self): - self.fs.create_file('foo') - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists4('foo')) + self.fs.create_file("foo") + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists4("foo")) class TestAttributesWithFakeModuleNames(TestPyfakefsUnittestBase): @@ -252,13 +270,11 @@ class TestAttributesWithFakeModuleNames(TestPyfakefsUnittestBase): def test_attributes(self): """Attributes of module under test are not patched""" - self.assertEqual(module_with_attributes.os, 'os attribute value') - self.assertEqual(module_with_attributes.path, 'path attribute value') - self.assertEqual(module_with_attributes.pathlib, - 'pathlib attribute value') - self.assertEqual(module_with_attributes.shutil, - 'shutil attribute value') - self.assertEqual(module_with_attributes.io, 'io attribute value') + self.assertEqual(module_with_attributes.os, "os attribute value") + self.assertEqual(module_with_attributes.path, "path attribute value") + self.assertEqual(module_with_attributes.pathlib, "pathlib attribute value") + self.assertEqual(module_with_attributes.shutil, "shutil attribute value") + self.assertEqual(module_with_attributes.io, "io attribute value") import math as path # noqa: E402 wanted import not at top @@ -266,8 +282,8 @@ import math as path # noqa: E402 wanted import not at top class TestPathNotPatchedIfNotOsPath(TestPyfakefsUnittestBase): """Tests that `path` is not patched if it is not `os.path`. - An own path module (in this case an alias to math) can be imported - and used. + An own path module (in this case an alias to math) can be imported + and used. """ def test_own_path_module(self): @@ -281,10 +297,11 @@ class FailedPatchingTest(TestPyfakefsUnittestBase): @unittest.expectedFailure def test_system_stat(self): - file_path = '/foo/bar' - self.fs.create_file(file_path, contents=b'test') + file_path = "/foo/bar" + self.fs.create_file(file_path, contents=b"test") self.assertEqual( - 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) + 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size + ) class ReloadModuleTest(fake_filesystem_unittest.TestCase): @@ -294,50 +311,42 @@ class ReloadModuleTest(fake_filesystem_unittest.TestCase): def setUp(self): """Set up the fake file system""" - self.setUpPyfakefs( - modules_to_reload=[pyfakefs.tests.import_as_example]) + self.setUpPyfakefs(modules_to_reload=[pyfakefs.tests.import_as_example]) class NoSkipNamesTest(fake_filesystem_unittest.TestCase): """Reference test for additional_skip_names tests: - make sure that the module is patched by default.""" + make sure that the module is patched by default.""" def setUp(self): self.setUpPyfakefs() def test_path_exists(self): - self.assertFalse( - pyfakefs.tests.import_as_example.exists_this_file()) + self.assertFalse(pyfakefs.tests.import_as_example.exists_this_file()) def test_fake_path_exists1(self): - self.fs.create_file('foo') - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists1('foo')) + self.fs.create_file("foo") + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists1("foo")) def test_fake_path_exists2(self): - self.fs.create_file('foo') - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists2('foo')) + self.fs.create_file("foo") + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists2("foo")) def test_fake_path_exists3(self): - self.fs.create_file('foo') - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists3('foo')) + self.fs.create_file("foo") + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists3("foo")) def test_fake_path_exists5(self): - self.fs.create_file('foo') - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists5('foo')) + self.fs.create_file("foo") + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists5("foo")) def test_fake_path_exists6(self): - self.fs.create_file('foo') - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists6('foo')) + self.fs.create_file("foo") + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists6("foo")) def test_fake_path_exists7(self): - self.fs.create_file('foo') - self.assertTrue( - pyfakefs.tests.import_as_example.check_if_exists7('foo')) + self.fs.create_file("foo") + self.assertTrue(pyfakefs.tests.import_as_example.check_if_exists7("foo")) def test_open_fails(self): with self.assertRaises(OSError): @@ -345,10 +354,10 @@ class NoSkipNamesTest(fake_filesystem_unittest.TestCase): def test_open_patched_in_module_ending_with_io(self): # regression test for #569 - file_path = '/foo/bar' - self.fs.create_file(file_path, contents=b'abc') + file_path = "/foo/bar" + self.fs.create_file(file_path, contents=b"abc") contents = pyfakefs.tests.logsio.file_contents(file_path) - self.assertEqual(b'abc', contents) + self.assertEqual(b"abc", contents) class AdditionalSkipNamesTest(fake_filesystem_unittest.TestCase): @@ -356,48 +365,43 @@ class AdditionalSkipNamesTest(fake_filesystem_unittest.TestCase): Passes module name to `additional_skip_names`.""" def setUp(self): - self.setUpPyfakefs( - additional_skip_names=['pyfakefs.tests.import_as_example']) + self.setUpPyfakefs(additional_skip_names=["pyfakefs.tests.import_as_example"]) def test_path_exists(self): - self.assertTrue( - pyfakefs.tests.import_as_example.exists_this_file()) + self.assertTrue(pyfakefs.tests.import_as_example.exists_this_file()) def test_fake_path_does_not_exist1(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists1('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists1("foo")) def test_fake_path_does_not_exist2(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists2('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists2("foo")) def test_fake_path_does_not_exist3(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists3('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists3("foo")) def test_fake_path_does_not_exist4(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists4('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists4("foo")) def test_fake_path_does_not_exist5(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists5('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists5("foo")) def test_fake_path_does_not_exist6(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists6('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists6("foo")) def test_fake_path_does_not_exist7(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists7('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists7("foo")) + @unittest.skipIf( + sys.version_info >= (3, 12), + "Skip modules currently not working for open in 3.12", + ) def test_open_succeeds(self): pyfakefs.tests.import_as_example.open_this_file() @@ -410,48 +414,43 @@ class AdditionalSkipNamesModuleTest(fake_filesystem_unittest.TestCase): Passes module to `additional_skip_names`.""" def setUp(self): - self.setUpPyfakefs( - additional_skip_names=[pyfakefs.tests.import_as_example]) + self.setUpPyfakefs(additional_skip_names=[pyfakefs.tests.import_as_example]) def test_path_exists(self): - self.assertTrue( - pyfakefs.tests.import_as_example.exists_this_file()) + self.assertTrue(pyfakefs.tests.import_as_example.exists_this_file()) def test_fake_path_does_not_exist1(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists1('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists1("foo")) def test_fake_path_does_not_exist2(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists2('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists2("foo")) def test_fake_path_does_not_exist3(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists3('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists3("foo")) def test_fake_path_does_not_exist4(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists4('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists4("foo")) def test_fake_path_does_not_exist5(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists5('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists5("foo")) def test_fake_path_does_not_exist6(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists6('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists6("foo")) def test_fake_path_does_not_exist7(self): - self.fs.create_file('foo') - self.assertFalse( - pyfakefs.tests.import_as_example.check_if_exists7('foo')) + self.fs.create_file("foo") + self.assertFalse(pyfakefs.tests.import_as_example.check_if_exists7("foo")) + @unittest.skipIf( + sys.version_info >= (3, 12), + "Skip modules currently not working for open in 3.12", + ) def test_open_succeeds(self): pyfakefs.tests.import_as_example.open_this_file() @@ -462,6 +461,7 @@ class AdditionalSkipNamesModuleTest(fake_filesystem_unittest.TestCase): class FakeExampleModule: """Used to patch a function that uses system-specific functions that cannot be patched automatically.""" + _orig_module = pyfakefs.tests.import_as_example def __init__(self, fs): @@ -483,14 +483,15 @@ class PatchModuleTest(fake_filesystem_unittest.TestCase): def setUp(self): """Set up the fake file system""" self.setUpPyfakefs( - modules_to_patch={ - 'pyfakefs.tests.import_as_example': FakeExampleModule}) + modules_to_patch={"pyfakefs.tests.import_as_example": FakeExampleModule} + ) def test_system_stat(self): - file_path = '/foo/bar' - self.fs.create_file(file_path, contents=b'test') + file_path = "/foo/bar" + self.fs.create_file(file_path, contents=b"test") self.assertEqual( - 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) + 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size + ) class PatchModuleTestUsingDecorator(unittest.TestCase): @@ -501,18 +502,19 @@ class PatchModuleTestUsingDecorator(unittest.TestCase): @patchfs @unittest.expectedFailure def test_system_stat_failing(self, fake_fs): - file_path = '/foo/bar' - fake_fs.create_file(file_path, contents=b'test') + file_path = "/foo/bar" + fake_fs.create_file(file_path, contents=b"test") self.assertEqual( - 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) + 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size + ) - @patchfs(modules_to_patch={ - 'pyfakefs.tests.import_as_example': FakeExampleModule}) + @patchfs(modules_to_patch={"pyfakefs.tests.import_as_example": FakeExampleModule}) def test_system_stat(self, fake_fs): - file_path = '/foo/bar' - fake_fs.create_file(file_path, contents=b'test') + file_path = "/foo/bar" + fake_fs.create_file(file_path, contents=b"test") self.assertEqual( - 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) + 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size + ) class NoRootUserTest(fake_filesystem_unittest.TestCase): @@ -526,17 +528,17 @@ class NoRootUserTest(fake_filesystem_unittest.TestCase): user rights. """ self.fs.is_windows_fs = False - dir_path = '/foo/bar' + dir_path = "/foo/bar" self.fs.create_dir(dir_path, perm_bits=0o555) - file_path = dir_path + 'baz' + file_path = dir_path + "baz" with self.assertRaises(OSError): self.fs.create_file(file_path) - file_path = '/baz' + file_path = "/baz" self.fs.create_file(file_path) os.chmod(file_path, 0o400) with self.assertRaises(OSError): - open(file_path, 'w') + open(file_path, "w") class PauseResumeTest(fake_filesystem_unittest.TestCase): @@ -654,13 +656,27 @@ class TestPyfakefsTestCase(unittest.TestCase): def runTest(self): pass - self.test_case = TestTestCase('runTest') + self.test_case = TestTestCase("runTest") def test_test_case_type(self): self.assertIsInstance(self.test_case, unittest.TestCase) - self.assertIsInstance(self.test_case, - fake_filesystem_unittest.TestCaseMixin) + self.assertIsInstance(self.test_case, fake_filesystem_unittest.TestCaseMixin) + + +class TestTempDirCreation(fake_filesystem_unittest.TestCase): + """Test that the temp directory exists at test start.""" + + def setUp(self): + self.setUpPyfakefs() + + def test_tempdir_exists(self): + self.assertTrue(os.path.exists(tempfile.gettempdir())) + + @unittest.skipIf(sys.platform == "win32", "POSIX only test") + def test_tmp_exists(self): + # directory or link under Linux, link under macOS + self.assertTrue(os.path.exists("/tmp")) class TestTempFileReload(unittest.TestCase): @@ -669,19 +685,20 @@ class TestTempFileReload(unittest.TestCase): def test_fakefs(self): with Patcher() as patcher: - patcher.fs.create_file('/mytempfile', contents='abcd') + patcher.fs.create_file("/mytempfile", contents="abcd") def test_value(self): - v = multiprocessing.Value('I', 0) + v = multiprocessing.Value("I", 0) self.assertEqual(v.value, 0) -class TestPyfakefsTestCaseMixin(unittest.TestCase, - fake_filesystem_unittest.TestCaseMixin): +class TestPyfakefsTestCaseMixin( + unittest.TestCase, fake_filesystem_unittest.TestCaseMixin +): def test_set_up_pyfakefs(self): self.setUpPyfakefs() - self.assertTrue(hasattr(self, 'fs')) + self.assertTrue(hasattr(self, "fs")) self.assertIsInstance(self.fs, fake_filesystem.FakeFilesystem) @@ -690,41 +707,43 @@ class TestShutilWithZipfile(fake_filesystem_unittest.TestCase): def setUp(self): self.setUpPyfakefs() - self.fs.create_file('foo/bar') + self.fs.create_file("foo/bar") def test_a(self): - shutil.make_archive('archive', 'zip', root_dir='foo') + shutil.make_archive("archive", "zip", root_dir="foo") def test_b(self): # used to fail because 'bar' could not be found - shutil.make_archive('archive', 'zip', root_dir='foo') + shutil.make_archive("archive", "zip", root_dir="foo") -class TestDistutilsCopyTree(fake_filesystem_unittest.TestCase): - """Regression test for #501.""" +if sys.version_info < (3, 12): - def setUp(self): - self.setUpPyfakefs() - self.fs.create_dir("./test/subdir/") - self.fs.create_dir("./test/subdir2/") - self.fs.create_file("./test2/subdir/1.txt") + class TestDistutilsCopyTree(fake_filesystem_unittest.TestCase): + """Regression test for #501.""" + + def setUp(self): + self.setUpPyfakefs() + self.fs.create_dir("./test/subdir/") + self.fs.create_dir("./test/subdir2/") + self.fs.create_file("./test2/subdir/1.txt") - def test_file_copied(self): - copy_tree("./test2/", "./test/") - remove_tree("./test2/") + def test_file_copied(self): + copy_tree("./test2/", "./test/") + remove_tree("./test2/") - self.assertTrue(os.path.isfile('./test/subdir/1.txt')) - self.assertFalse(os.path.isdir('./test2/')) + self.assertTrue(os.path.isfile("./test/subdir/1.txt")) + self.assertFalse(os.path.isdir("./test2/")) - def test_file_copied_again(self): - # used to fail because 'test2' could not be found - self.assertTrue(os.path.isfile('./test2/subdir/1.txt')) + def test_file_copied_again(self): + # used to fail because 'test2' could not be found + self.assertTrue(os.path.isfile("./test2/subdir/1.txt")) - copy_tree("./test2/", "./test/") - remove_tree("./test2/") + copy_tree("./test2/", "./test/") + remove_tree("./test2/") - self.assertTrue(os.path.isfile('./test/subdir/1.txt')) - self.assertFalse(os.path.isdir('./test2/')) + self.assertTrue(os.path.isfile("./test/subdir/1.txt")) + self.assertFalse(os.path.isdir("./test2/")) class PathlibTest(TestCase): @@ -733,22 +752,27 @@ class PathlibTest(TestCase): @patchfs def test_cwd(self, fs): """Make sure fake file system is used for os in pathlib""" - self.assertEqual(os.path.sep, str(pathlib.Path.cwd())) + is_windows = sys.platform.startswith("win") + root_dir = "C:" + os.path.sep if is_windows else os.path.sep + self.assertEqual(root_dir, str(pathlib.Path.cwd())) dot_abs = pathlib.Path(".").absolute() - self.assertEqual(os.path.sep, str(dot_abs)) + self.assertEqual(root_dir, str(dot_abs)) self.assertTrue(dot_abs.exists()) class TestDeprecationSuppression(fake_filesystem_unittest.TestCase): - @unittest.skipIf(sys.version_info[1] == 6, - 'Test fails for Python 3.6 for unknown reason') + @unittest.skipIf( + sys.version_info[1] == 6, + "Test fails for Python 3.6 for unknown reason", + ) def test_no_deprecation_warning(self): """Ensures that deprecation warnings are suppressed during module lookup, see #542. """ - from pyfakefs.tests.fixtures.deprecated_property import \ - DeprecationTest # noqa: F401 + from pyfakefs.tests.fixtures.deprecated_property import ( # noqa: F401 + DeprecationTest, + ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("error", DeprecationWarning) @@ -757,7 +781,7 @@ class TestDeprecationSuppression(fake_filesystem_unittest.TestCase): def load_configs(configs): - """ Helper code for patching open_code in auto mode, see issue #554. """ + """Helper code for patching open_code in auto mode, see issue #554.""" retval = [] for config in configs: if len(config) > 3 and config[-3:] == ".py": @@ -768,16 +792,14 @@ def load_configs(configs): class AutoPatchOpenCodeTestCase(fake_filesystem_unittest.TestCase): - """ Test patching open_code in auto mode, see issue #554.""" + """Test patching open_code in auto mode, see issue #554.""" def setUp(self): self.setUpPyfakefs(patch_open_code=PatchMode.AUTO) - self.configpy = 'configpy.py' - self.fs.create_file( - self.configpy, - contents="configurable_value='yup'") - self.config_module = 'pyfakefs.tests.fixtures.config_module' + self.configpy = "configpy.py" + self.fs.create_file(self.configpy, contents="configurable_value='yup'") + self.config_module = "pyfakefs.tests.fixtures.config_module" def test_both(self): load_configs([self.configpy, self.config_module]) @@ -793,81 +815,107 @@ class TestOtherFS(fake_filesystem_unittest.TestCase): def setUp(self): self.setUpPyfakefs() + @mock.patch.dict(os.environ, {"HOME": "/home/john"}) def test_real_file_with_home(self): """Regression test for #558""" - self.fs.is_windows_fs = os.name != 'nt' + self.fs.is_windows_fs = os.name != "nt" + if self.fs.is_windows_fs: + self.fs.is_macos = False self.fs.add_real_file(__file__) with open(__file__) as f: self.assertTrue(f.read()) home = Path.home() - if sys.version_info < (3, 6): - # fspath support since Python 3.6 - home = str(home) os.chdir(home) with open(__file__) as f: self.assertTrue(f.read()) def test_windows(self): self.fs.os = OSType.WINDOWS - path = r'C:\foo\bar' - self.assertEqual(path, os.path.join('C:\\', 'foo', 'bar')) - self.assertEqual(('C:', r'\foo\bar'), os.path.splitdrive(path)) + path = r"C:\foo\bar" + self.assertEqual(path, os.path.join("C:\\", "foo", "bar")) + self.assertEqual(("C:", r"\foo\bar"), os.path.splitdrive(path)) self.fs.create_file(path) self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.exists(path.upper())) - self.assertTrue(os.path.ismount(r'\\share\foo')) - self.assertTrue(os.path.ismount(r'C:')) - self.assertEqual('\\', os.sep) - self.assertEqual('\\', os.path.sep) - self.assertEqual('/', os.altsep) - self.assertEqual(';', os.pathsep) - self.assertEqual('\r\n', os.linesep) - self.assertEqual('nul', os.devnull) + self.assertTrue(os.path.ismount(r"\\share\foo")) + self.assertTrue(os.path.ismount(r"C:")) + self.assertEqual("\\", os.sep) + self.assertEqual("\\", os.path.sep) + self.assertEqual("/", os.altsep) + self.assertEqual(";", os.pathsep) + self.assertEqual("\r\n", os.linesep) + self.assertEqual("nul", os.devnull) def test_linux(self): self.fs.os = OSType.LINUX - path = '/foo/bar' - self.assertEqual(path, os.path.join('/', 'foo', 'bar')) - self.assertEqual(('', 'C:/foo/bar'), os.path.splitdrive('C:/foo/bar')) + path = "/foo/bar" + self.assertEqual(path, os.path.join("/", "foo", "bar")) + self.assertEqual(("", "C:/foo/bar"), os.path.splitdrive("C:/foo/bar")) self.fs.create_file(path) self.assertTrue(os.path.exists(path)) self.assertFalse(os.path.exists(path.upper())) - self.assertTrue(os.path.ismount('/')) - self.assertFalse(os.path.ismount('//share/foo')) - self.assertEqual('/', os.sep) - self.assertEqual('/', os.path.sep) + self.assertTrue(os.path.ismount("/")) + self.assertFalse(os.path.ismount("//share/foo")) + self.assertEqual("/", os.sep) + self.assertEqual("/", os.path.sep) self.assertEqual(None, os.altsep) - self.assertEqual(':', os.pathsep) - self.assertEqual('\n', os.linesep) - self.assertEqual('/dev/null', os.devnull) + self.assertEqual(":", os.pathsep) + self.assertEqual("\n", os.linesep) + self.assertEqual("/dev/null", os.devnull) def test_macos(self): self.fs.os = OSType.MACOS - path = '/foo/bar' - self.assertEqual(path, os.path.join('/', 'foo', 'bar')) - self.assertEqual(('', 'C:/foo/bar'), os.path.splitdrive('C:/foo/bar')) + path = "/foo/bar" + self.assertEqual(path, os.path.join("/", "foo", "bar")) + self.assertEqual(("", "C:/foo/bar"), os.path.splitdrive("C:/foo/bar")) self.fs.create_file(path) self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.exists(path.upper())) - self.assertTrue(os.path.ismount('/')) - self.assertFalse(os.path.ismount('//share/foo')) - self.assertEqual('/', os.sep) - self.assertEqual('/', os.path.sep) + self.assertTrue(os.path.ismount("/")) + self.assertFalse(os.path.ismount("//share/foo")) + self.assertEqual("/", os.sep) + self.assertEqual("/", os.path.sep) self.assertEqual(None, os.altsep) - self.assertEqual(':', os.pathsep) - self.assertEqual('\n', os.linesep) - self.assertEqual('/dev/null', os.devnull) + self.assertEqual(":", os.pathsep) + self.assertEqual("\n", os.linesep) + self.assertEqual("/dev/null", os.devnull) def test_drivelike_path(self): self.fs.os = OSType.LINUX - folder = Path('/test') - file_path = folder / 'C:/testfile' + folder = Path("/test") + file_path = folder / "C:/testfile" file_path.parent.mkdir(parents=True) file_path.touch() - # use str() to be Python 3.5 compatible - os.chdir(str(folder)) + os.chdir(folder) self.assertTrue(os.path.exists(str(file_path.relative_to(folder)))) +@unittest.skipIf(sys.platform != "win32", "Windows-specific behavior") +class TestAbsolutePathOnWindows(fake_filesystem_unittest.TestCase): + @patchfs + def test_is_absolute(self, fs): + # regression test for #673 + self.assertTrue(pathlib.Path(".").absolute().is_absolute()) + + +@unittest.skipIf(sys.version_info < (3, 8), "Not available before Python 3.8") +class TestClassSetup(fake_filesystem_unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.setUpClassPyfakefs() + cls.fake_fs().create_file("foo/bar", contents="test") + + def test_using_fs_functions(self): + self.assertTrue(os.path.exists("foo/bar")) + with open("foo/bar") as f: + contents = f.read() + self.assertEqual("test", contents) + + def test_using_fakefs(self): + self.assertTrue(self.fs.exists("foo/bar")) + f = self.fs.get_object("foo/bar") + self.assertEqual("test", f.contents) + + if __name__ == "__main__": unittest.main() |