Skip to content

Commit

Permalink
Avoid EncodingWarning occurring in tests
Browse files Browse the repository at this point in the history
- we have switched the warnings on, so we try to avoid them
  as much as possible in our own tests
- use utf8 encoding in all tests with no explicit encoding
  • Loading branch information
mrbean-bremen committed Apr 4, 2024
1 parent 28a8f41 commit 3b1a99a
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 310 deletions.
2 changes: 1 addition & 1 deletion pyfakefs/fake_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def touch(self, mode=0o666, exist_ok=True):
else:
self.filesystem.raise_os_error(errno.EEXIST, self._path())
else:
fake_file = self.open("w")
fake_file = self.open("w", encoding="utf8")
fake_file.close()
self.chmod(mode)

Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/tests/dynamic_patch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_shutil_patch(self):
def test_pathlib_path_patch(self):
file_path = "test.txt"
path = pathlib.Path(file_path)
with path.open("w") as f:
with path.open("w", encoding="utf8") as f:
f.write("test")

self.assertTrue(self.fs.exists(file_path))
Expand Down
4 changes: 2 additions & 2 deletions pyfakefs/tests/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def create_file(path):
>>> create_file('/test/file.txt')
>>> os.path.exists('/test/file.txt')
True
>>> with open('/test/file.txt') as f:
>>> with open('/test/file.txt', encoding='utf8') as f:
... f.readlines()
["This is test file '/test/file.txt'.\\n", \
'It was created using open().\\n']
"""
with open(path, "w") as f:
with open(path, "w", encoding="utf8") as f:
f.write("This is test file '{0}'.\n".format(path))
f.write("It was created using open().\n")

Expand Down
4 changes: 2 additions & 2 deletions pyfakefs/tests/fake_filesystem_shutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_rmtree_with_open_file_posix(self):
self.create_file(os.path.join(dir_path, "bar"))
file_path = os.path.join(dir_path, "baz")
self.create_file(file_path)
with open(file_path):
with open(file_path, encoding="utf8"):
shutil.rmtree(dir_path)
self.assertFalse(os.path.exists(file_path))

Expand All @@ -149,7 +149,7 @@ def test_rmtree_with_open_file_fails_under_windows(self):
self.create_file(os.path.join(dir_path, "bar"))
file_path = os.path.join(dir_path, "baz")
self.create_file(file_path)
with open(file_path):
with open(file_path, encoding="utf8"):
with self.assertRaises(OSError):
shutil.rmtree(dir_path)
self.assertTrue(os.path.exists(dir_path))
Expand Down
49 changes: 26 additions & 23 deletions pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def test_empty_file_created_for_none_contents(self):
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
path = "foo/bar/baz"
self.filesystem.create_file(path, contents=None)
with fake_open(path) as f:
with fake_open(path, encoding="utf8") as f:
self.assertEqual("", f.read())

def test_create_file_with_incorrect_mode_type(self):
Expand Down Expand Up @@ -1649,15 +1649,15 @@ def test_disk_usage_on_file_creation(self):
self.fs.add_mount_point("!mount", total_size)

def create_too_large_file():
with self.open("!mount!file", "w") as dest:
with self.open("!mount!file", "w", encoding="utf8") as dest:
dest.write("a" * (total_size + 1))

with self.assertRaises(OSError):
create_too_large_file()

self.assertEqual(0, self.fs.get_disk_usage("!mount").used)

with self.open("!mount!file", "w") as dest:
with self.open("!mount!file", "w", encoding="utf8") as dest:
dest.write("a" * total_size)

self.assertEqual(total_size, self.fs.get_disk_usage("!mount").used)
Expand Down Expand Up @@ -1882,50 +1882,50 @@ def test_copying_preserves_byte_contents(self):
self.assertEqual(dest_file.contents, source_file.contents)

def test_diskusage_after_open_write(self):
with self.open("bar.txt", "w") as f:
with self.open("bar.txt", "w", encoding="utf8") as f:
f.write("a" * 60)
f.flush()
self.assertEqual(60, self.fs.get_disk_usage()[1])

def test_disk_full_after_reopened(self):
with self.open("bar.txt", "w") as f:
with self.open("bar.txt", "w", encoding="utf8") as f:
f.write("a" * 60)
with self.open("bar.txt") as f:
with self.open("bar.txt", encoding="utf8") as f:
self.assertEqual("a" * 60, f.read())
with self.raises_os_error(errno.ENOSPC):
with self.open("bar.txt", "w") as f:
with self.open("bar.txt", "w", encoding="utf8") as f:
f.write("b" * 110)
with self.raises_os_error(errno.ENOSPC):
f.flush()
with self.open("bar.txt") as f:
with self.open("bar.txt", encoding="utf8") as f:
self.assertEqual("", f.read())

def test_disk_full_append(self):
file_path = "bar.txt"
with self.open(file_path, "w") as f:
with self.open(file_path, "w", encoding="utf8") as f:
f.write("a" * 60)
with self.open(file_path) as f:
with self.open(file_path, encoding="utf8") as f:
self.assertEqual("a" * 60, f.read())
with self.raises_os_error(errno.ENOSPC):
with self.open(file_path, "a") as f:
with self.open(file_path, "a", encoding="utf8") as f:
f.write("b" * 41)
with self.raises_os_error(errno.ENOSPC):
f.flush()
with self.open("bar.txt") as f:
with self.open("bar.txt", encoding="utf8") as f:
self.assertEqual(f.read(), "a" * 60)

def test_disk_full_after_reopened_rplus_seek(self):
with self.open("bar.txt", "w") as f:
with self.open("bar.txt", "w", encoding="utf8") as f:
f.write("a" * 60)
with self.open("bar.txt") as f:
with self.open("bar.txt", encoding="utf8") as f:
self.assertEqual(f.read(), "a" * 60)
with self.raises_os_error(errno.ENOSPC):
with self.open("bar.txt", "r+") as f:
with self.open("bar.txt", "r+", encoding="utf8") as f:
f.seek(50)
f.write("b" * 60)
with self.raises_os_error(errno.ENOSPC):
f.flush()
with self.open("bar.txt") as f:
with self.open("bar.txt", encoding="utf8") as f:
self.assertEqual(f.read(), "a" * 60)


Expand Down Expand Up @@ -2055,11 +2055,13 @@ def create_real_paths(self):
for dir_name in ("foo", "bar"):
real_dir = os.path.join(real_dir_root, dir_name)
os.makedirs(real_dir, exist_ok=True)
with open(os.path.join(real_dir, "test.txt"), "w") as f:
with open(
os.path.join(real_dir, "test.txt"), "w", encoding="utf8"
) as f:
f.write("test")
sub_dir = os.path.join(real_dir, "sub")
os.makedirs(sub_dir, exist_ok=True)
with open(os.path.join(sub_dir, "sub.txt"), "w") as f:
with open(os.path.join(sub_dir, "sub.txt"), "w", encoding="utf8") as f:
f.write("sub")
yield real_dir_root
finally:
Expand Down Expand Up @@ -2203,7 +2205,7 @@ def test_write_to_real_file(self):
# regression test for #470
real_file_path = os.path.abspath(__file__)
self.filesystem.add_real_file(real_file_path, read_only=False)
with self.fake_open(real_file_path, "w") as f:
with self.fake_open(real_file_path, "w", encoding="utf8") as f:
f.write("foo")

with self.fake_open(real_file_path, "rb") as f:
Expand Down Expand Up @@ -2289,7 +2291,7 @@ def test_add_existing_real_directory_symlink(self):

self.filesystem.create_file("/etc/something")

with fake_open("/etc/something", "w") as f:
with fake_open("/etc/something", "w", encoding="utf8") as f:
f.write("good morning")

try:
Expand Down Expand Up @@ -2385,7 +2387,8 @@ def test_add_existing_real_directory_symlink(self):
"pyfakefs",
"tests",
"fixtures/symlink_file_absolute_outside",
)
),
encoding="utf8",
).read(),
"good morning",
)
Expand Down Expand Up @@ -2585,14 +2588,14 @@ def setUp(self):
def test_side_effect_called(self):
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
self.side_effect_called = False
with fake_open("/a/b/file_one", "w") as handle:
with fake_open("/a/b/file_one", "w", encoding="utf8") as handle:
handle.write("foo")
self.assertTrue(self.side_effect_called)

def test_side_effect_file_object(self):
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
self.side_effect_called = False
with fake_open("/a/b/file_one", "w") as handle:
with fake_open("/a/b/file_one", "w", encoding="utf8") as handle:
handle.write("foo")
self.assertEqual(self.side_effect_file_object_content, "foo")

Expand Down
26 changes: 13 additions & 13 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ 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:
with open("/foo/bar", encoding="utf8") as f:
contents = f.read()
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:
with open("/foo/bar", encoding="utf8") as f:
contents = f.read()
self.assertEqual("test", contents)

Expand All @@ -73,7 +73,7 @@ class TestPatchfsArgumentOrder(TestCase):
@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:
with open("/foo/bar", encoding="utf8") as f:
contents = f.read()
self.assertEqual("test", contents)
os.system("foo")
Expand All @@ -83,7 +83,7 @@ def test_argument_order1(self, fake_fs, patched_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:
with open("/foo/bar", encoding="utf8") as f:
contents = f.read()
self.assertEqual("test", contents)
os.system("foo")
Expand All @@ -102,10 +102,10 @@ 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:
with open("/fake_file.txt", "w", encoding="utf8") 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:
with open("/fake_file.txt", encoding="utf8") as f:
content = f.read()
self.assertEqual(
"This test file was created using the " "open() function.\n",
Expand All @@ -115,10 +115,10 @@ def test_open(self):
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:
with io.open("/fake_file.txt", "w", encoding="utf8") 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:
with open("/fake_file.txt", encoding="utf8") as f:
content = f.read()
self.assertEqual(
"This test file was created using the " "io.open() function.\n",
Expand Down Expand Up @@ -160,7 +160,7 @@ def test_shutil(self):

def test_fakepathlib(self):
p = pathlib.Path("/fake_file.txt")
with p.open("w") as f:
with p.open("w", encoding="utf8") as f:
f.write("text")
is_windows = sys.platform.startswith("win")
if is_windows:
Expand Down Expand Up @@ -532,7 +532,7 @@ def test_non_root_behavior(self):
self.fs.create_file(file_path)
os.chmod(file_path, 0o400)
with self.assertRaises(OSError):
open(file_path, "w")
open(file_path, "w", encoding="utf8")


class PauseResumeTest(fake_filesystem_unittest.TestCase):
Expand Down Expand Up @@ -824,11 +824,11 @@ def test_real_file_with_home(self):
if self.fs.is_windows_fs:
self.fs.is_macos = False
self.fs.add_real_file(__file__)
with open(__file__) as f:
with open(__file__, encoding="utf8") as f:
self.assertTrue(f.read())
home = Path.home()
os.chdir(home)
with open(__file__) as f:
with open(__file__, encoding="utf8") as f:
self.assertTrue(f.read())

def test_windows(self):
Expand Down Expand Up @@ -916,7 +916,7 @@ def setUpClass(cls):

def test_using_fs_functions(self):
self.assertTrue(os.path.exists("foo/bar"))
with open("foo/bar") as f:
with open("foo/bar", encoding="utf8") as f:
contents = f.read()
self.assertEqual("test", contents)

Expand Down
11 changes: 7 additions & 4 deletions pyfakefs/tests/fake_filesystem_vs_real_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def _create_test_file(self, file_type, path, contents=None):
os.mkdir(real_path)
self.fake_os.mkdir(fake_path)
if file_type == "f":
fh = open(real_path, "w")
fh = open(real_path, "w", encoding="utf8")
fh.write(contents or "")
fh.close()
fh = self.fake_open(fake_path, "w")
fh = self.fake_open(fake_path, "w", encoding="utf8")
fh.write(contents or "")
fh.close()
# b for binary file
Expand Down Expand Up @@ -318,8 +318,11 @@ def diff_open_method_behavior(
Returns:
A description of the difference in behavior, or None.
"""
with open(path, mode) as real_fh:
with self.fake_open(path, mode) as fake_fh:
kwargs = {}
if "b" not in mode:
kwargs["encoding"] = "utf8"
with open(path, mode, **kwargs) as real_fh:
with self.fake_open(path, mode, **kwargs) as fake_fh:
return self._compare_behaviors(
method_name, data, real_fh, fake_fh, method_returns_data
)
Expand Down

0 comments on commit 3b1a99a

Please sign in to comment.