Skip to content

Commit 661b938

Browse files
committed
Add encoding in more tests
1 parent 7e51076 commit 661b938

27 files changed

+213
-144
lines changed

scripts/towncrier-draft-to-file.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ def main():
77
Platform agnostic wrapper script for towncrier.
88
Fixes the issue (#7251) where windows users are unable to natively run tox -e docs to build pytest docs.
99
"""
10-
with open("doc/en/_changelog_towncrier_draft.rst", "w") as draft_file:
10+
with open(
11+
"doc/en/_changelog_towncrier_draft.rst", "w", encoding="utf-8"
12+
) as draft_file:
1113
return call(("towncrier", "--draft"), stdout=draft_file)
1214

1315

testing/_py/test_local.py

+59-39
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
import contextlib
12
import multiprocessing
23
import os
34
import sys
45
import time
6+
import warnings
57
from unittest import mock
68

79
import pytest
810
from py import error
911
from py.path import local
1012

1113

14+
@contextlib.contextmanager
15+
def ignore_encoding_warning():
16+
with warnings.catch_warnings():
17+
with contextlib.suppress(NameError): # new in 3.10
18+
warnings.simplefilter("ignore", EncodingWarning)
19+
yield
20+
21+
1222
class CommonFSTests:
1323
def test_constructor_equality(self, path1):
1424
p = path1.__class__(path1)
@@ -223,7 +233,8 @@ def test_cmp(self, path1):
223233
assert not (path1 < path1)
224234

225235
def test_simple_read(self, path1):
226-
x = path1.join("samplefile").read("r")
236+
with ignore_encoding_warning():
237+
x = path1.join("samplefile").read("r")
227238
assert x == "samplefile\n"
228239

229240
def test_join_div_operator(self, path1):
@@ -265,12 +276,14 @@ def test_newext(self, path1):
265276

266277
def test_readlines(self, path1):
267278
fn = path1.join("samplefile")
268-
contents = fn.readlines()
279+
with ignore_encoding_warning():
280+
contents = fn.readlines()
269281
assert contents == ["samplefile\n"]
270282

271283
def test_readlines_nocr(self, path1):
272284
fn = path1.join("samplefile")
273-
contents = fn.readlines(cr=0)
285+
with ignore_encoding_warning():
286+
contents = fn.readlines(cr=0)
274287
assert contents == ["samplefile", ""]
275288

276289
def test_file(self, path1):
@@ -362,8 +375,8 @@ def test_copy_file(self, path1):
362375
initpy.copy(copied)
363376
try:
364377
assert copied.check()
365-
s1 = initpy.read()
366-
s2 = copied.read()
378+
s1 = initpy.read_text(encoding="utf-8")
379+
s2 = copied.read_text(encoding="utf-8")
367380
assert s1 == s2
368381
finally:
369382
if copied.check():
@@ -376,8 +389,8 @@ def test_copy_dir(self, path1):
376389
otherdir.copy(copied)
377390
assert copied.check(dir=1)
378391
assert copied.join("__init__.py").check(file=1)
379-
s1 = otherdir.join("__init__.py").read()
380-
s2 = copied.join("__init__.py").read()
392+
s1 = otherdir.join("__init__.py").read_text(encoding="utf-8")
393+
s2 = copied.join("__init__.py").read_text(encoding="utf-8")
381394
assert s1 == s2
382395
finally:
383396
if copied.check(dir=1):
@@ -463,13 +476,13 @@ def setuptestfs(path):
463476
return
464477
# print "setting up test fs for", repr(path)
465478
samplefile = path.ensure("samplefile")
466-
samplefile.write("samplefile\n")
479+
samplefile.write_text("samplefile\n", encoding="utf-8")
467480

468481
execfile = path.ensure("execfile")
469-
execfile.write("x=42")
482+
execfile.write_text("x=42", encoding="utf-8")
470483

471484
execfilepy = path.ensure("execfile.py")
472-
execfilepy.write("x=42")
485+
execfilepy.write_text("x=42", encoding="utf-8")
473486

474487
d = {1: 2, "hello": "world", "answer": 42}
475488
path.ensure("samplepickle").dump(d)
@@ -481,22 +494,24 @@ def setuptestfs(path):
481494
otherdir.ensure("__init__.py")
482495

483496
module_a = otherdir.ensure("a.py")
484-
module_a.write("from .b import stuff as result\n")
497+
module_a.write_text("from .b import stuff as result\n", encoding="utf-8")
485498
module_b = otherdir.ensure("b.py")
486-
module_b.write('stuff="got it"\n')
499+
module_b.write_text('stuff="got it"\n', encoding="utf-8")
487500
module_c = otherdir.ensure("c.py")
488-
module_c.write(
501+
module_c.write_text(
489502
"""import py;
490503
import otherdir.a
491504
value = otherdir.a.result
492-
"""
505+
""",
506+
encoding="utf-8",
493507
)
494508
module_d = otherdir.ensure("d.py")
495-
module_d.write(
509+
module_d.write_text(
496510
"""import py;
497511
from otherdir import a
498512
value2 = a.result
499-
"""
513+
""",
514+
encoding="utf-8",
500515
)
501516

502517

@@ -534,9 +549,11 @@ def batch_make_numbered_dirs(rootdir, repeats):
534549
for i in range(repeats):
535550
dir_ = local.make_numbered_dir(prefix="repro-", rootdir=rootdir)
536551
file_ = dir_.join("foo")
537-
file_.write("%s" % i)
538-
actual = int(file_.read())
539-
assert actual == i, f"int(file_.read()) is {actual} instead of {i}"
552+
file_.write_text("%s" % i, encoding="utf-8")
553+
actual = int(file_.read_text(encoding="utf-8"))
554+
assert (
555+
actual == i
556+
), f"int(file_.read_text(encoding='utf-8')) is {actual} instead of {i}"
540557
dir_.join(".lock").remove(ignore_errors=True)
541558
return True
542559

@@ -692,14 +709,14 @@ def test_gt_with_strings(self, path1):
692709

693710
def test_open_and_ensure(self, path1):
694711
p = path1.join("sub1", "sub2", "file")
695-
with p.open("w", ensure=1) as f:
712+
with p.open("w", ensure=1, encoding="utf-8") as f:
696713
f.write("hello")
697-
assert p.read() == "hello"
714+
assert p.read_text(encoding="utf-8") == "hello"
698715

699716
def test_write_and_ensure(self, path1):
700717
p = path1.join("sub1", "sub2", "file")
701-
p.write("hello", ensure=1)
702-
assert p.read() == "hello"
718+
p.write_text("hello", ensure=1, encoding="utf-8")
719+
assert p.read_text(encoding="utf-8") == "hello"
703720

704721
@pytest.mark.parametrize("bin", (False, True))
705722
def test_dump(self, tmpdir, bin):
@@ -770,9 +787,9 @@ def test_ensure_filepath_withdir(self, tmpdir):
770787
newfile = tmpdir.join("test1", "test")
771788
newfile.ensure()
772789
assert newfile.check(file=1)
773-
newfile.write("42")
790+
newfile.write_text("42", encoding="utf-8")
774791
newfile.ensure()
775-
s = newfile.read()
792+
s = newfile.read_text(encoding="utf-8")
776793
assert s == "42"
777794

778795
def test_ensure_filepath_withoutdir(self, tmpdir):
@@ -806,9 +823,9 @@ def test_long_filenames(self, tmpdir):
806823
newfilename = "/test" * 60 # type:ignore[unreachable]
807824
l1 = tmpdir.join(newfilename)
808825
l1.ensure(file=True)
809-
l1.write("foo")
826+
l1.write_text("foo", encoding="utf-8")
810827
l2 = tmpdir.join(newfilename)
811-
assert l2.read() == "foo"
828+
assert l2.read_text(encoding="utf-8") == "foo"
812829

813830
def test_visit_depth_first(self, tmpdir):
814831
tmpdir.ensure("a", "1")
@@ -1278,22 +1295,22 @@ class TestPOSIXLocalPath:
12781295
def test_hardlink(self, tmpdir):
12791296
linkpath = tmpdir.join("test")
12801297
filepath = tmpdir.join("file")
1281-
filepath.write("Hello")
1298+
filepath.write_text("Hello", encoding="utf-8")
12821299
nlink = filepath.stat().nlink
12831300
linkpath.mklinkto(filepath)
12841301
assert filepath.stat().nlink == nlink + 1
12851302

12861303
def test_symlink_are_identical(self, tmpdir):
12871304
filepath = tmpdir.join("file")
1288-
filepath.write("Hello")
1305+
filepath.write_text("Hello", encoding="utf-8")
12891306
linkpath = tmpdir.join("test")
12901307
linkpath.mksymlinkto(filepath)
12911308
assert linkpath.readlink() == str(filepath)
12921309

12931310
def test_symlink_isfile(self, tmpdir):
12941311
linkpath = tmpdir.join("test")
12951312
filepath = tmpdir.join("file")
1296-
filepath.write("")
1313+
filepath.write_text("", encoding="utf-8")
12971314
linkpath.mksymlinkto(filepath)
12981315
assert linkpath.check(file=1)
12991316
assert not linkpath.check(link=0, file=1)
@@ -1302,10 +1319,12 @@ def test_symlink_isfile(self, tmpdir):
13021319
def test_symlink_relative(self, tmpdir):
13031320
linkpath = tmpdir.join("test")
13041321
filepath = tmpdir.join("file")
1305-
filepath.write("Hello")
1322+
filepath.write_text("Hello", encoding="utf-8")
13061323
linkpath.mksymlinkto(filepath, absolute=False)
13071324
assert linkpath.readlink() == "file"
1308-
assert filepath.read() == linkpath.read()
1325+
assert filepath.read_text(encoding="utf-8") == linkpath.read_text(
1326+
encoding="utf-8"
1327+
)
13091328

13101329
def test_symlink_not_existing(self, tmpdir):
13111330
linkpath = tmpdir.join("testnotexisting")
@@ -1338,7 +1357,7 @@ def test_symlink_remove(self, tmpdir):
13381357
def test_realpath_file(self, tmpdir):
13391358
linkpath = tmpdir.join("test")
13401359
filepath = tmpdir.join("file")
1341-
filepath.write("")
1360+
filepath.write_text("", encoding="utf-8")
13421361
linkpath.mksymlinkto(filepath)
13431362
realpath = linkpath.realpath()
13441363
assert realpath.basename == "file"
@@ -1383,7 +1402,7 @@ def test_atime(self, tmpdir):
13831402
atime1 = path.atime()
13841403
# we could wait here but timer resolution is very
13851404
# system dependent
1386-
path.read()
1405+
path.read_binary()
13871406
time.sleep(ATIME_RESOLUTION)
13881407
atime2 = path.atime()
13891408
time.sleep(ATIME_RESOLUTION)
@@ -1467,7 +1486,7 @@ def test_copy_stat_dir(self, tmpdir):
14671486
test_files = ["a", "b", "c"]
14681487
src = tmpdir.join("src")
14691488
for f in test_files:
1470-
src.join(f).write(f, ensure=True)
1489+
src.join(f).write_text(f, ensure=True, encoding="utf-8")
14711490
dst = tmpdir.join("dst")
14721491
# a small delay before the copy
14731492
time.sleep(ATIME_RESOLUTION)
@@ -1521,10 +1540,11 @@ def test_listdir(self, tmpdir):
15211540
def test_read_write(self, tmpdir):
15221541
x = tmpdir.join("hello")
15231542
part = "hällo"
1524-
x.write(part)
1525-
assert x.read() == part
1526-
x.write(part.encode(sys.getdefaultencoding()))
1527-
assert x.read() == part.encode(sys.getdefaultencoding())
1543+
with ignore_encoding_warning():
1544+
x.write(part)
1545+
assert x.read() == part
1546+
x.write(part.encode(sys.getdefaultencoding()))
1547+
assert x.read() == part.encode(sys.getdefaultencoding())
15281548

15291549

15301550
class TestBinaryAndTextMethods:

testing/acceptance_test.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def test_pyargs_only_imported_once(self, pytester: Pytester) -> None:
613613

614614
def test_pyargs_filename_looks_like_module(self, pytester: Pytester) -> None:
615615
pytester.path.joinpath("conftest.py").touch()
616-
pytester.path.joinpath("t.py").write_text("def test(): pass")
616+
pytester.path.joinpath("t.py").write_text("def test(): pass", encoding="utf-8")
617617
result = pytester.runpytest("--pyargs", "t.py")
618618
assert result.ret == ExitCode.OK
619619

@@ -622,8 +622,12 @@ def test_cmdline_python_package(self, pytester: Pytester, monkeypatch) -> None:
622622

623623
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
624624
path = pytester.mkpydir("tpkg")
625-
path.joinpath("test_hello.py").write_text("def test_hello(): pass")
626-
path.joinpath("test_world.py").write_text("def test_world(): pass")
625+
path.joinpath("test_hello.py").write_text(
626+
"def test_hello(): pass", encoding="utf-8"
627+
)
628+
path.joinpath("test_world.py").write_text(
629+
"def test_world(): pass", encoding="utf-8"
630+
)
627631
result = pytester.runpytest("--pyargs", "tpkg")
628632
assert result.ret == 0
629633
result.stdout.fnmatch_lines(["*2 passed*"])
@@ -662,13 +666,15 @@ def test_cmdline_python_namespace_package(
662666
ns = d.joinpath("ns_pkg")
663667
ns.mkdir()
664668
ns.joinpath("__init__.py").write_text(
665-
"__import__('pkg_resources').declare_namespace(__name__)"
669+
"__import__('pkg_resources').declare_namespace(__name__)",
670+
encoding="utf-8",
666671
)
667672
lib = ns.joinpath(dirname)
668673
lib.mkdir()
669674
lib.joinpath("__init__.py").touch()
670675
lib.joinpath(f"test_{dirname}.py").write_text(
671-
f"def test_{dirname}(): pass\ndef test_other():pass"
676+
f"def test_{dirname}(): pass\ndef test_other():pass",
677+
encoding="utf-8",
672678
)
673679

674680
# The structure of the test directory is now:
@@ -754,10 +760,10 @@ def test_cmdline_python_package_symlink(
754760
lib.mkdir()
755761
lib.joinpath("__init__.py").touch()
756762
lib.joinpath("test_bar.py").write_text(
757-
"def test_bar(): pass\ndef test_other(a_fixture):pass"
763+
"def test_bar(): pass\ndef test_other(a_fixture):pass", encoding="utf-8"
758764
)
759765
lib.joinpath("conftest.py").write_text(
760-
"import pytest\n@pytest.fixture\ndef a_fixture():pass"
766+
"import pytest\n@pytest.fixture\ndef a_fixture():pass", encoding="utf-8"
761767
)
762768

763769
d_local = pytester.mkdir("symlink_root")
@@ -1276,8 +1282,7 @@ def test_simple():
12761282
result.stderr.fnmatch_lines(["*@this is stderr@*"])
12771283

12781284
# now ensure the output is in the junitxml
1279-
with open(pytester.path.joinpath("output.xml")) as f:
1280-
fullXml = f.read()
1285+
fullXml = pytester.path.joinpath("output.xml").read_text(encoding="utf-8")
12811286
assert "@this is stdout@\n" in fullXml
12821287
assert "@this is stderr@\n" in fullXml
12831288

testing/code/test_excinfo.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def test_excinfo_no_sourcecode():
374374

375375
def test_excinfo_no_python_sourcecode(tmp_path: Path) -> None:
376376
# XXX: simplified locally testable version
377-
tmp_path.joinpath("test.txt").write_text("{{ h()}}:")
377+
tmp_path.joinpath("test.txt").write_text("{{ h()}}:", encoding="utf-8")
378378

379379
jinja2 = pytest.importorskip("jinja2")
380380
loader = jinja2.FileSystemLoader(str(tmp_path))
@@ -451,7 +451,7 @@ def importasmod(source):
451451
source = textwrap.dedent(source)
452452
modpath = tmp_path.joinpath("mod.py")
453453
tmp_path.joinpath("__init__.py").touch()
454-
modpath.write_text(source)
454+
modpath.write_text(source, encoding="utf-8")
455455
importlib.invalidate_caches()
456456
return import_path(modpath, root=tmp_path)
457457

@@ -1023,7 +1023,7 @@ def f():
10231023
"""
10241024
)
10251025
excinfo = pytest.raises(ValueError, mod.f)
1026-
tmp_path.joinpath("mod.py").write_text("asdf")
1026+
tmp_path.joinpath("mod.py").write_text("asdf", encoding="utf-8")
10271027
excinfo.traceback = excinfo.traceback.filter(excinfo)
10281028
repr = excinfo.getrepr()
10291029
repr.toterminal(tw_mock)

testing/code/test_source.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def method(self):
294294
"""
295295
)
296296
path = tmp_path.joinpath("a.py")
297-
path.write_text(str(source))
297+
path.write_text(str(source), encoding="utf-8")
298298
mod: Any = import_path(path, root=tmp_path)
299299
s2 = Source(mod.A)
300300
assert str(source).strip() == str(s2).strip()

0 commit comments

Comments
 (0)