Skip to content

Commit df1d3c6

Browse files
committed
Avoid EncodingWarning occurring in tests
- 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
1 parent 28a8f41 commit df1d3c6

14 files changed

+334
-310
lines changed

pyfakefs/fake_pathlib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ def touch(self, mode=0o666, exist_ok=True):
749749
else:
750750
self.filesystem.raise_os_error(errno.EEXIST, self._path())
751751
else:
752-
fake_file = self.open("w")
752+
fake_file = self.open("w", encoding="utf8")
753753
fake_file.close()
754754
self.chmod(mode)
755755

pyfakefs/tests/dynamic_patch_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_shutil_patch(self):
6060
def test_pathlib_path_patch(self):
6161
file_path = "test.txt"
6262
path = pathlib.Path(file_path)
63-
with path.open("w") as f:
63+
with path.open("w", encoding="utf8") as f:
6464
f.write("test")
6565

6666
self.assertTrue(self.fs.exists(file_path))

pyfakefs/tests/example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ def create_file(path):
6161
>>> create_file('/test/file.txt')
6262
>>> os.path.exists('/test/file.txt')
6363
True
64-
>>> with open('/test/file.txt') as f:
64+
>>> with open('/test/file.txt', encoding='utf8') as f:
6565
... f.readlines()
6666
["This is test file '/test/file.txt'.\\n", \
6767
'It was created using open().\\n']
6868
"""
69-
with open(path, "w") as f:
69+
with open(path, "w", encoding="utf8") as f:
7070
f.write("This is test file '{0}'.\n".format(path))
7171
f.write("It was created using open().\n")
7272

pyfakefs/tests/fake_filesystem_shutil_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_rmtree_with_open_file_posix(self):
138138
self.create_file(os.path.join(dir_path, "bar"))
139139
file_path = os.path.join(dir_path, "baz")
140140
self.create_file(file_path)
141-
with open(file_path):
141+
with open(file_path, encoding="utf8"):
142142
shutil.rmtree(dir_path)
143143
self.assertFalse(os.path.exists(file_path))
144144

@@ -149,7 +149,7 @@ def test_rmtree_with_open_file_fails_under_windows(self):
149149
self.create_file(os.path.join(dir_path, "bar"))
150150
file_path = os.path.join(dir_path, "baz")
151151
self.create_file(file_path)
152-
with open(file_path):
152+
with open(file_path, encoding="utf8"):
153153
with self.assertRaises(OSError):
154154
shutil.rmtree(dir_path)
155155
self.assertTrue(os.path.exists(dir_path))

pyfakefs/tests/fake_filesystem_test.py

+26-23
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def test_empty_file_created_for_none_contents(self):
605605
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
606606
path = "foo/bar/baz"
607607
self.filesystem.create_file(path, contents=None)
608-
with fake_open(path) as f:
608+
with fake_open(path, encoding="utf8") as f:
609609
self.assertEqual("", f.read())
610610

611611
def test_create_file_with_incorrect_mode_type(self):
@@ -1649,15 +1649,15 @@ def test_disk_usage_on_file_creation(self):
16491649
self.fs.add_mount_point("!mount", total_size)
16501650

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

16551655
with self.assertRaises(OSError):
16561656
create_too_large_file()
16571657

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

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

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

18841884
def test_diskusage_after_open_write(self):
1885-
with self.open("bar.txt", "w") as f:
1885+
with self.open("bar.txt", "w", encoding="utf8") as f:
18861886
f.write("a" * 60)
18871887
f.flush()
18881888
self.assertEqual(60, self.fs.get_disk_usage()[1])
18891889

18901890
def test_disk_full_after_reopened(self):
1891-
with self.open("bar.txt", "w") as f:
1891+
with self.open("bar.txt", "w", encoding="utf8") as f:
18921892
f.write("a" * 60)
1893-
with self.open("bar.txt") as f:
1893+
with self.open("bar.txt", encoding="utf8") as f:
18941894
self.assertEqual("a" * 60, f.read())
18951895
with self.raises_os_error(errno.ENOSPC):
1896-
with self.open("bar.txt", "w") as f:
1896+
with self.open("bar.txt", "w", encoding="utf8") as f:
18971897
f.write("b" * 110)
18981898
with self.raises_os_error(errno.ENOSPC):
18991899
f.flush()
1900-
with self.open("bar.txt") as f:
1900+
with self.open("bar.txt", encoding="utf8") as f:
19011901
self.assertEqual("", f.read())
19021902

19031903
def test_disk_full_append(self):
19041904
file_path = "bar.txt"
1905-
with self.open(file_path, "w") as f:
1905+
with self.open(file_path, "w", encoding="utf8") as f:
19061906
f.write("a" * 60)
1907-
with self.open(file_path) as f:
1907+
with self.open(file_path, encoding="utf8") as f:
19081908
self.assertEqual("a" * 60, f.read())
19091909
with self.raises_os_error(errno.ENOSPC):
1910-
with self.open(file_path, "a") as f:
1910+
with self.open(file_path, "a", encoding="utf8") as f:
19111911
f.write("b" * 41)
19121912
with self.raises_os_error(errno.ENOSPC):
19131913
f.flush()
1914-
with self.open("bar.txt") as f:
1914+
with self.open("bar.txt", encoding="utf8") as f:
19151915
self.assertEqual(f.read(), "a" * 60)
19161916

19171917
def test_disk_full_after_reopened_rplus_seek(self):
1918-
with self.open("bar.txt", "w") as f:
1918+
with self.open("bar.txt", "w", encoding="utf8") as f:
19191919
f.write("a" * 60)
1920-
with self.open("bar.txt") as f:
1920+
with self.open("bar.txt", encoding="utf8") as f:
19211921
self.assertEqual(f.read(), "a" * 60)
19221922
with self.raises_os_error(errno.ENOSPC):
1923-
with self.open("bar.txt", "r+") as f:
1923+
with self.open("bar.txt", "r+", encoding="utf8") as f:
19241924
f.seek(50)
19251925
f.write("b" * 60)
19261926
with self.raises_os_error(errno.ENOSPC):
19271927
f.flush()
1928-
with self.open("bar.txt") as f:
1928+
with self.open("bar.txt", encoding="utf8") as f:
19291929
self.assertEqual(f.read(), "a" * 60)
19301930

19311931

@@ -2055,11 +2055,13 @@ def create_real_paths(self):
20552055
for dir_name in ("foo", "bar"):
20562056
real_dir = os.path.join(real_dir_root, dir_name)
20572057
os.makedirs(real_dir, exist_ok=True)
2058-
with open(os.path.join(real_dir, "test.txt"), "w") as f:
2058+
with open(
2059+
os.path.join(real_dir, "test.txt"), "w", encoding="utf8"
2060+
) as f:
20592061
f.write("test")
20602062
sub_dir = os.path.join(real_dir, "sub")
20612063
os.makedirs(sub_dir, exist_ok=True)
2062-
with open(os.path.join(sub_dir, "sub.txt"), "w") as f:
2064+
with open(os.path.join(sub_dir, "sub.txt"), "w", encoding="utf8") as f:
20632065
f.write("sub")
20642066
yield real_dir_root
20652067
finally:
@@ -2203,7 +2205,7 @@ def test_write_to_real_file(self):
22032205
# regression test for #470
22042206
real_file_path = os.path.abspath(__file__)
22052207
self.filesystem.add_real_file(real_file_path, read_only=False)
2206-
with self.fake_open(real_file_path, "w") as f:
2208+
with self.fake_open(real_file_path, "w", encoding="utf8") as f:
22072209
f.write("foo")
22082210

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

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

2292-
with fake_open("/etc/something", "w") as f:
2294+
with fake_open("/etc/something", "w", encoding="utf8") as f:
22932295
f.write("good morning")
22942296

22952297
try:
@@ -2385,7 +2387,8 @@ def test_add_existing_real_directory_symlink(self):
23852387
"pyfakefs",
23862388
"tests",
23872389
"fixtures/symlink_file_absolute_outside",
2388-
)
2390+
),
2391+
encoding="utf8",
23892392
).read(),
23902393
"good morning",
23912394
)
@@ -2585,14 +2588,14 @@ def setUp(self):
25852588
def test_side_effect_called(self):
25862589
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
25872590
self.side_effect_called = False
2588-
with fake_open("/a/b/file_one", "w") as handle:
2591+
with fake_open("/a/b/file_one", "w", encoding="utf8") as handle:
25892592
handle.write("foo")
25902593
self.assertTrue(self.side_effect_called)
25912594

25922595
def test_side_effect_file_object(self):
25932596
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
25942597
self.side_effect_called = False
2595-
with fake_open("/a/b/file_one", "w") as handle:
2598+
with fake_open("/a/b/file_one", "w", encoding="utf8") as handle:
25962599
handle.write("foo")
25972600
self.assertEqual(self.side_effect_file_object_content, "foo")
25982601

pyfakefs/tests/fake_filesystem_unittest_test.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ class TestPatcher(TestCase):
5656
def test_context_manager(self):
5757
with Patcher() as patcher:
5858
patcher.fs.create_file("/foo/bar", contents="test")
59-
with open("/foo/bar") as f:
59+
with open("/foo/bar", encoding="utf8") as f:
6060
contents = f.read()
6161
self.assertEqual("test", contents)
6262

6363
@patchfs
6464
def test_context_decorator(self, fake_fs):
6565
fake_fs.create_file("/foo/bar", contents="test")
66-
with open("/foo/bar") as f:
66+
with open("/foo/bar", encoding="utf8") as f:
6767
contents = f.read()
6868
self.assertEqual("test", contents)
6969

@@ -73,7 +73,7 @@ class TestPatchfsArgumentOrder(TestCase):
7373
@mock.patch("os.system")
7474
def test_argument_order1(self, fake_fs, patched_system):
7575
fake_fs.create_file("/foo/bar", contents="test")
76-
with open("/foo/bar") as f:
76+
with open("/foo/bar", encoding="utf8") as f:
7777
contents = f.read()
7878
self.assertEqual("test", contents)
7979
os.system("foo")
@@ -83,7 +83,7 @@ def test_argument_order1(self, fake_fs, patched_system):
8383
@patchfs
8484
def test_argument_order2(self, patched_system, fake_fs):
8585
fake_fs.create_file("/foo/bar", contents="test")
86-
with open("/foo/bar") as f:
86+
with open("/foo/bar", encoding="utf8") as f:
8787
contents = f.read()
8888
self.assertEqual("test", contents)
8989
os.system("foo")
@@ -102,10 +102,10 @@ class TestPyfakefsUnittest(TestPyfakefsUnittestBase): # pylint: disable=R0904
102102
def test_open(self):
103103
"""Fake `open()` function is bound"""
104104
self.assertFalse(os.path.exists("/fake_file.txt"))
105-
with open("/fake_file.txt", "w") as f:
105+
with open("/fake_file.txt", "w", encoding="utf8") as f:
106106
f.write("This test file was created using the open() function.\n")
107107
self.assertTrue(self.fs.exists("/fake_file.txt"))
108-
with open("/fake_file.txt") as f:
108+
with open("/fake_file.txt", encoding="utf8") as f:
109109
content = f.read()
110110
self.assertEqual(
111111
"This test file was created using the " "open() function.\n",
@@ -115,10 +115,10 @@ def test_open(self):
115115
def test_io_open(self):
116116
"""Fake io module is bound"""
117117
self.assertFalse(os.path.exists("/fake_file.txt"))
118-
with io.open("/fake_file.txt", "w") as f:
118+
with io.open("/fake_file.txt", "w", encoding="utf8") as f:
119119
f.write("This test file was created using the" " io.open() function.\n")
120120
self.assertTrue(self.fs.exists("/fake_file.txt"))
121-
with open("/fake_file.txt") as f:
121+
with open("/fake_file.txt", encoding="utf8") as f:
122122
content = f.read()
123123
self.assertEqual(
124124
"This test file was created using the " "io.open() function.\n",
@@ -160,7 +160,7 @@ def test_shutil(self):
160160

161161
def test_fakepathlib(self):
162162
p = pathlib.Path("/fake_file.txt")
163-
with p.open("w") as f:
163+
with p.open("w", encoding="utf8") as f:
164164
f.write("text")
165165
is_windows = sys.platform.startswith("win")
166166
if is_windows:
@@ -532,7 +532,7 @@ def test_non_root_behavior(self):
532532
self.fs.create_file(file_path)
533533
os.chmod(file_path, 0o400)
534534
with self.assertRaises(OSError):
535-
open(file_path, "w")
535+
open(file_path, "w", encoding="utf8")
536536

537537

538538
class PauseResumeTest(fake_filesystem_unittest.TestCase):
@@ -824,11 +824,11 @@ def test_real_file_with_home(self):
824824
if self.fs.is_windows_fs:
825825
self.fs.is_macos = False
826826
self.fs.add_real_file(__file__)
827-
with open(__file__) as f:
827+
with open(__file__, encoding="utf8") as f:
828828
self.assertTrue(f.read())
829829
home = Path.home()
830830
os.chdir(home)
831-
with open(__file__) as f:
831+
with open(__file__, encoding="utf8") as f:
832832
self.assertTrue(f.read())
833833

834834
def test_windows(self):
@@ -916,7 +916,7 @@ def setUpClass(cls):
916916

917917
def test_using_fs_functions(self):
918918
self.assertTrue(os.path.exists("foo/bar"))
919-
with open("foo/bar") as f:
919+
with open("foo/bar", encoding="utf8") as f:
920920
contents = f.read()
921921
self.assertEqual("test", contents)
922922

pyfakefs/tests/fake_filesystem_vs_real_test.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def _create_test_file(self, file_type, path, contents=None):
6565
os.mkdir(real_path)
6666
self.fake_os.mkdir(fake_path)
6767
if file_type == "f":
68-
fh = open(real_path, "w")
68+
fh = open(real_path, "w", encoding="utf8")
6969
fh.write(contents or "")
7070
fh.close()
71-
fh = self.fake_open(fake_path, "w")
71+
fh = self.fake_open(fake_path, "w", encoding="utf8")
7272
fh.write(contents or "")
7373
fh.close()
7474
# b for binary file
@@ -318,8 +318,11 @@ def diff_open_method_behavior(
318318
Returns:
319319
A description of the difference in behavior, or None.
320320
"""
321-
with open(path, mode) as real_fh:
322-
with self.fake_open(path, mode) as fake_fh:
321+
kwargs = {}
322+
if "b" not in mode:
323+
kwargs["encoding"] = "utf8"
324+
with open(path, mode, **kwargs) as real_fh:
325+
with self.fake_open(path, mode, **kwargs) as fake_fh:
323326
return self._compare_behaviors(
324327
method_name, data, real_fh, fake_fh, method_returns_data
325328
)

0 commit comments

Comments
 (0)