Add encoding in more tests

This commit is contained in:
Zac Hatfield-Dodds 2023-06-20 04:55:40 -07:00
parent 7e510769b4
commit 661b938fca
27 changed files with 213 additions and 144 deletions

View File

@ -7,7 +7,9 @@ def main():
Platform agnostic wrapper script for towncrier. Platform agnostic wrapper script for towncrier.
Fixes the issue (#7251) where windows users are unable to natively run tox -e docs to build pytest docs. Fixes the issue (#7251) where windows users are unable to natively run tox -e docs to build pytest docs.
""" """
with open("doc/en/_changelog_towncrier_draft.rst", "w") as draft_file: with open(
"doc/en/_changelog_towncrier_draft.rst", "w", encoding="utf-8"
) as draft_file:
return call(("towncrier", "--draft"), stdout=draft_file) return call(("towncrier", "--draft"), stdout=draft_file)

View File

@ -1,7 +1,9 @@
import contextlib
import multiprocessing import multiprocessing
import os import os
import sys import sys
import time import time
import warnings
from unittest import mock from unittest import mock
import pytest import pytest
@ -9,6 +11,14 @@ from py import error
from py.path import local from py.path import local
@contextlib.contextmanager
def ignore_encoding_warning():
with warnings.catch_warnings():
with contextlib.suppress(NameError): # new in 3.10
warnings.simplefilter("ignore", EncodingWarning)
yield
class CommonFSTests: class CommonFSTests:
def test_constructor_equality(self, path1): def test_constructor_equality(self, path1):
p = path1.__class__(path1) p = path1.__class__(path1)
@ -223,6 +233,7 @@ class CommonFSTests:
assert not (path1 < path1) assert not (path1 < path1)
def test_simple_read(self, path1): def test_simple_read(self, path1):
with ignore_encoding_warning():
x = path1.join("samplefile").read("r") x = path1.join("samplefile").read("r")
assert x == "samplefile\n" assert x == "samplefile\n"
@ -265,11 +276,13 @@ class CommonFSTests:
def test_readlines(self, path1): def test_readlines(self, path1):
fn = path1.join("samplefile") fn = path1.join("samplefile")
with ignore_encoding_warning():
contents = fn.readlines() contents = fn.readlines()
assert contents == ["samplefile\n"] assert contents == ["samplefile\n"]
def test_readlines_nocr(self, path1): def test_readlines_nocr(self, path1):
fn = path1.join("samplefile") fn = path1.join("samplefile")
with ignore_encoding_warning():
contents = fn.readlines(cr=0) contents = fn.readlines(cr=0)
assert contents == ["samplefile", ""] assert contents == ["samplefile", ""]
@ -362,8 +375,8 @@ class CommonFSTests:
initpy.copy(copied) initpy.copy(copied)
try: try:
assert copied.check() assert copied.check()
s1 = initpy.read() s1 = initpy.read_text(encoding="utf-8")
s2 = copied.read() s2 = copied.read_text(encoding="utf-8")
assert s1 == s2 assert s1 == s2
finally: finally:
if copied.check(): if copied.check():
@ -376,8 +389,8 @@ class CommonFSTests:
otherdir.copy(copied) otherdir.copy(copied)
assert copied.check(dir=1) assert copied.check(dir=1)
assert copied.join("__init__.py").check(file=1) assert copied.join("__init__.py").check(file=1)
s1 = otherdir.join("__init__.py").read() s1 = otherdir.join("__init__.py").read_text(encoding="utf-8")
s2 = copied.join("__init__.py").read() s2 = copied.join("__init__.py").read_text(encoding="utf-8")
assert s1 == s2 assert s1 == s2
finally: finally:
if copied.check(dir=1): if copied.check(dir=1):
@ -463,13 +476,13 @@ def setuptestfs(path):
return return
# print "setting up test fs for", repr(path) # print "setting up test fs for", repr(path)
samplefile = path.ensure("samplefile") samplefile = path.ensure("samplefile")
samplefile.write("samplefile\n") samplefile.write_text("samplefile\n", encoding="utf-8")
execfile = path.ensure("execfile") execfile = path.ensure("execfile")
execfile.write("x=42") execfile.write_text("x=42", encoding="utf-8")
execfilepy = path.ensure("execfile.py") execfilepy = path.ensure("execfile.py")
execfilepy.write("x=42") execfilepy.write_text("x=42", encoding="utf-8")
d = {1: 2, "hello": "world", "answer": 42} d = {1: 2, "hello": "world", "answer": 42}
path.ensure("samplepickle").dump(d) path.ensure("samplepickle").dump(d)
@ -481,22 +494,24 @@ def setuptestfs(path):
otherdir.ensure("__init__.py") otherdir.ensure("__init__.py")
module_a = otherdir.ensure("a.py") module_a = otherdir.ensure("a.py")
module_a.write("from .b import stuff as result\n") module_a.write_text("from .b import stuff as result\n", encoding="utf-8")
module_b = otherdir.ensure("b.py") module_b = otherdir.ensure("b.py")
module_b.write('stuff="got it"\n') module_b.write_text('stuff="got it"\n', encoding="utf-8")
module_c = otherdir.ensure("c.py") module_c = otherdir.ensure("c.py")
module_c.write( module_c.write_text(
"""import py; """import py;
import otherdir.a import otherdir.a
value = otherdir.a.result value = otherdir.a.result
""" """,
encoding="utf-8",
) )
module_d = otherdir.ensure("d.py") module_d = otherdir.ensure("d.py")
module_d.write( module_d.write_text(
"""import py; """import py;
from otherdir import a from otherdir import a
value2 = a.result value2 = a.result
""" """,
encoding="utf-8",
) )
@ -534,9 +549,11 @@ def batch_make_numbered_dirs(rootdir, repeats):
for i in range(repeats): for i in range(repeats):
dir_ = local.make_numbered_dir(prefix="repro-", rootdir=rootdir) dir_ = local.make_numbered_dir(prefix="repro-", rootdir=rootdir)
file_ = dir_.join("foo") file_ = dir_.join("foo")
file_.write("%s" % i) file_.write_text("%s" % i, encoding="utf-8")
actual = int(file_.read()) actual = int(file_.read_text(encoding="utf-8"))
assert actual == i, f"int(file_.read()) is {actual} instead of {i}" assert (
actual == i
), f"int(file_.read_text(encoding='utf-8')) is {actual} instead of {i}"
dir_.join(".lock").remove(ignore_errors=True) dir_.join(".lock").remove(ignore_errors=True)
return True return True
@ -692,14 +709,14 @@ class TestLocalPath(CommonFSTests):
def test_open_and_ensure(self, path1): def test_open_and_ensure(self, path1):
p = path1.join("sub1", "sub2", "file") p = path1.join("sub1", "sub2", "file")
with p.open("w", ensure=1) as f: with p.open("w", ensure=1, encoding="utf-8") as f:
f.write("hello") f.write("hello")
assert p.read() == "hello" assert p.read_text(encoding="utf-8") == "hello"
def test_write_and_ensure(self, path1): def test_write_and_ensure(self, path1):
p = path1.join("sub1", "sub2", "file") p = path1.join("sub1", "sub2", "file")
p.write("hello", ensure=1) p.write_text("hello", ensure=1, encoding="utf-8")
assert p.read() == "hello" assert p.read_text(encoding="utf-8") == "hello"
@pytest.mark.parametrize("bin", (False, True)) @pytest.mark.parametrize("bin", (False, True))
def test_dump(self, tmpdir, bin): def test_dump(self, tmpdir, bin):
@ -770,9 +787,9 @@ class TestLocalPath(CommonFSTests):
newfile = tmpdir.join("test1", "test") newfile = tmpdir.join("test1", "test")
newfile.ensure() newfile.ensure()
assert newfile.check(file=1) assert newfile.check(file=1)
newfile.write("42") newfile.write_text("42", encoding="utf-8")
newfile.ensure() newfile.ensure()
s = newfile.read() s = newfile.read_text(encoding="utf-8")
assert s == "42" assert s == "42"
def test_ensure_filepath_withoutdir(self, tmpdir): def test_ensure_filepath_withoutdir(self, tmpdir):
@ -806,9 +823,9 @@ class TestLocalPath(CommonFSTests):
newfilename = "/test" * 60 # type:ignore[unreachable] newfilename = "/test" * 60 # type:ignore[unreachable]
l1 = tmpdir.join(newfilename) l1 = tmpdir.join(newfilename)
l1.ensure(file=True) l1.ensure(file=True)
l1.write("foo") l1.write_text("foo", encoding="utf-8")
l2 = tmpdir.join(newfilename) l2 = tmpdir.join(newfilename)
assert l2.read() == "foo" assert l2.read_text(encoding="utf-8") == "foo"
def test_visit_depth_first(self, tmpdir): def test_visit_depth_first(self, tmpdir):
tmpdir.ensure("a", "1") tmpdir.ensure("a", "1")
@ -1278,14 +1295,14 @@ class TestPOSIXLocalPath:
def test_hardlink(self, tmpdir): def test_hardlink(self, tmpdir):
linkpath = tmpdir.join("test") linkpath = tmpdir.join("test")
filepath = tmpdir.join("file") filepath = tmpdir.join("file")
filepath.write("Hello") filepath.write_text("Hello", encoding="utf-8")
nlink = filepath.stat().nlink nlink = filepath.stat().nlink
linkpath.mklinkto(filepath) linkpath.mklinkto(filepath)
assert filepath.stat().nlink == nlink + 1 assert filepath.stat().nlink == nlink + 1
def test_symlink_are_identical(self, tmpdir): def test_symlink_are_identical(self, tmpdir):
filepath = tmpdir.join("file") filepath = tmpdir.join("file")
filepath.write("Hello") filepath.write_text("Hello", encoding="utf-8")
linkpath = tmpdir.join("test") linkpath = tmpdir.join("test")
linkpath.mksymlinkto(filepath) linkpath.mksymlinkto(filepath)
assert linkpath.readlink() == str(filepath) assert linkpath.readlink() == str(filepath)
@ -1293,7 +1310,7 @@ class TestPOSIXLocalPath:
def test_symlink_isfile(self, tmpdir): def test_symlink_isfile(self, tmpdir):
linkpath = tmpdir.join("test") linkpath = tmpdir.join("test")
filepath = tmpdir.join("file") filepath = tmpdir.join("file")
filepath.write("") filepath.write_text("", encoding="utf-8")
linkpath.mksymlinkto(filepath) linkpath.mksymlinkto(filepath)
assert linkpath.check(file=1) assert linkpath.check(file=1)
assert not linkpath.check(link=0, file=1) assert not linkpath.check(link=0, file=1)
@ -1302,10 +1319,12 @@ class TestPOSIXLocalPath:
def test_symlink_relative(self, tmpdir): def test_symlink_relative(self, tmpdir):
linkpath = tmpdir.join("test") linkpath = tmpdir.join("test")
filepath = tmpdir.join("file") filepath = tmpdir.join("file")
filepath.write("Hello") filepath.write_text("Hello", encoding="utf-8")
linkpath.mksymlinkto(filepath, absolute=False) linkpath.mksymlinkto(filepath, absolute=False)
assert linkpath.readlink() == "file" assert linkpath.readlink() == "file"
assert filepath.read() == linkpath.read() assert filepath.read_text(encoding="utf-8") == linkpath.read_text(
encoding="utf-8"
)
def test_symlink_not_existing(self, tmpdir): def test_symlink_not_existing(self, tmpdir):
linkpath = tmpdir.join("testnotexisting") linkpath = tmpdir.join("testnotexisting")
@ -1338,7 +1357,7 @@ class TestPOSIXLocalPath:
def test_realpath_file(self, tmpdir): def test_realpath_file(self, tmpdir):
linkpath = tmpdir.join("test") linkpath = tmpdir.join("test")
filepath = tmpdir.join("file") filepath = tmpdir.join("file")
filepath.write("") filepath.write_text("", encoding="utf-8")
linkpath.mksymlinkto(filepath) linkpath.mksymlinkto(filepath)
realpath = linkpath.realpath() realpath = linkpath.realpath()
assert realpath.basename == "file" assert realpath.basename == "file"
@ -1383,7 +1402,7 @@ class TestPOSIXLocalPath:
atime1 = path.atime() atime1 = path.atime()
# we could wait here but timer resolution is very # we could wait here but timer resolution is very
# system dependent # system dependent
path.read() path.read_binary()
time.sleep(ATIME_RESOLUTION) time.sleep(ATIME_RESOLUTION)
atime2 = path.atime() atime2 = path.atime()
time.sleep(ATIME_RESOLUTION) time.sleep(ATIME_RESOLUTION)
@ -1467,7 +1486,7 @@ class TestPOSIXLocalPath:
test_files = ["a", "b", "c"] test_files = ["a", "b", "c"]
src = tmpdir.join("src") src = tmpdir.join("src")
for f in test_files: for f in test_files:
src.join(f).write(f, ensure=True) src.join(f).write_text(f, ensure=True, encoding="utf-8")
dst = tmpdir.join("dst") dst = tmpdir.join("dst")
# a small delay before the copy # a small delay before the copy
time.sleep(ATIME_RESOLUTION) time.sleep(ATIME_RESOLUTION)
@ -1521,6 +1540,7 @@ class TestUnicodePy2Py3:
def test_read_write(self, tmpdir): def test_read_write(self, tmpdir):
x = tmpdir.join("hello") x = tmpdir.join("hello")
part = "hällo" part = "hällo"
with ignore_encoding_warning():
x.write(part) x.write(part)
assert x.read() == part assert x.read() == part
x.write(part.encode(sys.getdefaultencoding())) x.write(part.encode(sys.getdefaultencoding()))

View File

@ -613,7 +613,7 @@ class TestInvocationVariants:
def test_pyargs_filename_looks_like_module(self, pytester: Pytester) -> None: def test_pyargs_filename_looks_like_module(self, pytester: Pytester) -> None:
pytester.path.joinpath("conftest.py").touch() pytester.path.joinpath("conftest.py").touch()
pytester.path.joinpath("t.py").write_text("def test(): pass") pytester.path.joinpath("t.py").write_text("def test(): pass", encoding="utf-8")
result = pytester.runpytest("--pyargs", "t.py") result = pytester.runpytest("--pyargs", "t.py")
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
@ -622,8 +622,12 @@ class TestInvocationVariants:
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
path = pytester.mkpydir("tpkg") path = pytester.mkpydir("tpkg")
path.joinpath("test_hello.py").write_text("def test_hello(): pass") path.joinpath("test_hello.py").write_text(
path.joinpath("test_world.py").write_text("def test_world(): pass") "def test_hello(): pass", encoding="utf-8"
)
path.joinpath("test_world.py").write_text(
"def test_world(): pass", encoding="utf-8"
)
result = pytester.runpytest("--pyargs", "tpkg") result = pytester.runpytest("--pyargs", "tpkg")
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines(["*2 passed*"]) result.stdout.fnmatch_lines(["*2 passed*"])
@ -662,13 +666,15 @@ class TestInvocationVariants:
ns = d.joinpath("ns_pkg") ns = d.joinpath("ns_pkg")
ns.mkdir() ns.mkdir()
ns.joinpath("__init__.py").write_text( ns.joinpath("__init__.py").write_text(
"__import__('pkg_resources').declare_namespace(__name__)" "__import__('pkg_resources').declare_namespace(__name__)",
encoding="utf-8",
) )
lib = ns.joinpath(dirname) lib = ns.joinpath(dirname)
lib.mkdir() lib.mkdir()
lib.joinpath("__init__.py").touch() lib.joinpath("__init__.py").touch()
lib.joinpath(f"test_{dirname}.py").write_text( lib.joinpath(f"test_{dirname}.py").write_text(
f"def test_{dirname}(): pass\ndef test_other():pass" f"def test_{dirname}(): pass\ndef test_other():pass",
encoding="utf-8",
) )
# The structure of the test directory is now: # The structure of the test directory is now:
@ -754,10 +760,10 @@ class TestInvocationVariants:
lib.mkdir() lib.mkdir()
lib.joinpath("__init__.py").touch() lib.joinpath("__init__.py").touch()
lib.joinpath("test_bar.py").write_text( lib.joinpath("test_bar.py").write_text(
"def test_bar(): pass\ndef test_other(a_fixture):pass" "def test_bar(): pass\ndef test_other(a_fixture):pass", encoding="utf-8"
) )
lib.joinpath("conftest.py").write_text( lib.joinpath("conftest.py").write_text(
"import pytest\n@pytest.fixture\ndef a_fixture():pass" "import pytest\n@pytest.fixture\ndef a_fixture():pass", encoding="utf-8"
) )
d_local = pytester.mkdir("symlink_root") d_local = pytester.mkdir("symlink_root")
@ -1276,8 +1282,7 @@ def test_tee_stdio_captures_and_live_prints(pytester: Pytester) -> None:
result.stderr.fnmatch_lines(["*@this is stderr@*"]) result.stderr.fnmatch_lines(["*@this is stderr@*"])
# now ensure the output is in the junitxml # now ensure the output is in the junitxml
with open(pytester.path.joinpath("output.xml")) as f: fullXml = pytester.path.joinpath("output.xml").read_text(encoding="utf-8")
fullXml = f.read()
assert "@this is stdout@\n" in fullXml assert "@this is stdout@\n" in fullXml
assert "@this is stderr@\n" in fullXml assert "@this is stderr@\n" in fullXml

View File

@ -374,7 +374,7 @@ def test_excinfo_no_sourcecode():
def test_excinfo_no_python_sourcecode(tmp_path: Path) -> None: def test_excinfo_no_python_sourcecode(tmp_path: Path) -> None:
# XXX: simplified locally testable version # XXX: simplified locally testable version
tmp_path.joinpath("test.txt").write_text("{{ h()}}:") tmp_path.joinpath("test.txt").write_text("{{ h()}}:", encoding="utf-8")
jinja2 = pytest.importorskip("jinja2") jinja2 = pytest.importorskip("jinja2")
loader = jinja2.FileSystemLoader(str(tmp_path)) loader = jinja2.FileSystemLoader(str(tmp_path))
@ -451,7 +451,7 @@ class TestFormattedExcinfo:
source = textwrap.dedent(source) source = textwrap.dedent(source)
modpath = tmp_path.joinpath("mod.py") modpath = tmp_path.joinpath("mod.py")
tmp_path.joinpath("__init__.py").touch() tmp_path.joinpath("__init__.py").touch()
modpath.write_text(source) modpath.write_text(source, encoding="utf-8")
importlib.invalidate_caches() importlib.invalidate_caches()
return import_path(modpath, root=tmp_path) return import_path(modpath, root=tmp_path)
@ -1023,7 +1023,7 @@ raise ValueError()
""" """
) )
excinfo = pytest.raises(ValueError, mod.f) excinfo = pytest.raises(ValueError, mod.f)
tmp_path.joinpath("mod.py").write_text("asdf") tmp_path.joinpath("mod.py").write_text("asdf", encoding="utf-8")
excinfo.traceback = excinfo.traceback.filter(excinfo) excinfo.traceback = excinfo.traceback.filter(excinfo)
repr = excinfo.getrepr() repr = excinfo.getrepr()
repr.toterminal(tw_mock) repr.toterminal(tw_mock)

View File

@ -294,7 +294,7 @@ def test_source_of_class_at_eof_without_newline(_sys_snapshot, tmp_path: Path) -
""" """
) )
path = tmp_path.joinpath("a.py") path = tmp_path.joinpath("a.py")
path.write_text(str(source)) path.write_text(str(source), encoding="utf-8")
mod: Any = import_path(path, root=tmp_path) mod: Any = import_path(path, root=tmp_path)
s2 = Source(mod.A) s2 = Source(mod.A)
assert str(source).strip() == str(s2).strip() assert str(source).strip() == str(s2).strip()

View File

@ -81,7 +81,7 @@ def test_root_logger_affected(pytester: Pytester) -> None:
# not the info one, because the default level of the root logger is # not the info one, because the default level of the root logger is
# WARNING. # WARNING.
assert os.path.isfile(log_file) assert os.path.isfile(log_file)
with open(log_file) as rfh: with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read() contents = rfh.read()
assert "info text going to logger" not in contents assert "info text going to logger" not in contents
assert "warning text going to logger" in contents assert "warning text going to logger" in contents
@ -656,7 +656,7 @@ def test_log_file_cli(pytester: Pytester) -> None:
# make sure that we get a '0' exit code for the testsuite # make sure that we get a '0' exit code for the testsuite
assert result.ret == 0 assert result.ret == 0
assert os.path.isfile(log_file) assert os.path.isfile(log_file)
with open(log_file) as rfh: with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read() contents = rfh.read()
assert "This log message will be shown" in contents assert "This log message will be shown" in contents
assert "This log message won't be shown" not in contents assert "This log message won't be shown" not in contents
@ -687,7 +687,7 @@ def test_log_file_cli_level(pytester: Pytester) -> None:
# make sure that we get a '0' exit code for the testsuite # make sure that we get a '0' exit code for the testsuite
assert result.ret == 0 assert result.ret == 0
assert os.path.isfile(log_file) assert os.path.isfile(log_file)
with open(log_file) as rfh: with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read() contents = rfh.read()
assert "This log message will be shown" in contents assert "This log message will be shown" in contents
assert "This log message won't be shown" not in contents assert "This log message won't be shown" not in contents
@ -738,7 +738,7 @@ def test_log_file_ini(pytester: Pytester) -> None:
# make sure that we get a '0' exit code for the testsuite # make sure that we get a '0' exit code for the testsuite
assert result.ret == 0 assert result.ret == 0
assert os.path.isfile(log_file) assert os.path.isfile(log_file)
with open(log_file) as rfh: with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read() contents = rfh.read()
assert "This log message will be shown" in contents assert "This log message will be shown" in contents
assert "This log message won't be shown" not in contents assert "This log message won't be shown" not in contents
@ -777,7 +777,7 @@ def test_log_file_ini_level(pytester: Pytester) -> None:
# make sure that we get a '0' exit code for the testsuite # make sure that we get a '0' exit code for the testsuite
assert result.ret == 0 assert result.ret == 0
assert os.path.isfile(log_file) assert os.path.isfile(log_file)
with open(log_file) as rfh: with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read() contents = rfh.read()
assert "This log message will be shown" in contents assert "This log message will be shown" in contents
assert "This log message won't be shown" not in contents assert "This log message won't be shown" not in contents
@ -985,7 +985,7 @@ def test_log_in_hooks(pytester: Pytester) -> None:
) )
result = pytester.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["*sessionstart*", "*runtestloop*", "*sessionfinish*"]) result.stdout.fnmatch_lines(["*sessionstart*", "*runtestloop*", "*sessionfinish*"])
with open(log_file) as rfh: with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read() contents = rfh.read()
assert "sessionstart" in contents assert "sessionstart" in contents
assert "runtestloop" in contents assert "runtestloop" in contents
@ -1021,7 +1021,7 @@ def test_log_in_runtest_logreport(pytester: Pytester) -> None:
""" """
) )
pytester.runpytest() pytester.runpytest()
with open(log_file) as rfh: with open(log_file, encoding="utf-8") as rfh:
contents = rfh.read() contents = rfh.read()
assert contents.count("logreport") == 3 assert contents.count("logreport") == 3
@ -1065,11 +1065,11 @@ def test_log_set_path(pytester: Pytester) -> None:
""" """
) )
pytester.runpytest() pytester.runpytest()
with open(os.path.join(report_dir_base, "test_first")) as rfh: with open(os.path.join(report_dir_base, "test_first"), encoding="utf-8") as rfh:
content = rfh.read() content = rfh.read()
assert "message from test 1" in content assert "message from test 1" in content
with open(os.path.join(report_dir_base, "test_second")) as rfh: with open(os.path.join(report_dir_base, "test_second"), encoding="utf-8") as rfh:
content = rfh.read() content = rfh.read()
assert "message from test 2" in content assert "message from test 2" in content

View File

@ -60,7 +60,8 @@ class TestModule:
""".format( """.format(
str(root2) str(root2)
) )
) ),
encoding="utf-8",
) )
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.chdir(root2) mp.chdir(root2)
@ -832,7 +833,8 @@ class TestConftestCustomization:
mod = outcome.get_result() mod = outcome.get_result()
mod.obj.hello = "world" mod.obj.hello = "world"
""" """
) ),
encoding="utf-8",
) )
b.joinpath("test_module.py").write_text( b.joinpath("test_module.py").write_text(
textwrap.dedent( textwrap.dedent(
@ -840,7 +842,8 @@ class TestConftestCustomization:
def test_hello(): def test_hello():
assert hello == "world" assert hello == "world"
""" """
) ),
encoding="utf-8",
) )
reprec = pytester.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
@ -861,7 +864,8 @@ class TestConftestCustomization:
for func in result: for func in result:
func._some123 = "world" func._some123 = "world"
""" """
) ),
encoding="utf-8",
) )
b.joinpath("test_module.py").write_text( b.joinpath("test_module.py").write_text(
textwrap.dedent( textwrap.dedent(
@ -874,7 +878,8 @@ class TestConftestCustomization:
def test_hello(obj): def test_hello(obj):
assert obj == "world" assert obj == "world"
""" """
) ),
encoding="utf-8",
) )
reprec = pytester.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
@ -974,7 +979,8 @@ def test_setup_only_available_in_subdir(pytester: Pytester) -> None:
def pytest_runtest_teardown(item): def pytest_runtest_teardown(item):
assert item.path.stem == "test_in_sub1" assert item.path.stem == "test_in_sub1"
""" """
) ),
encoding="utf-8",
) )
sub2.joinpath("conftest.py").write_text( sub2.joinpath("conftest.py").write_text(
textwrap.dedent( textwrap.dedent(
@ -987,10 +993,11 @@ def test_setup_only_available_in_subdir(pytester: Pytester) -> None:
def pytest_runtest_teardown(item): def pytest_runtest_teardown(item):
assert item.path.stem == "test_in_sub2" assert item.path.stem == "test_in_sub2"
""" """
),
encoding="utf-8",
) )
) sub1.joinpath("test_in_sub1.py").write_text("def test_1(): pass", encoding="utf-8")
sub1.joinpath("test_in_sub1.py").write_text("def test_1(): pass") sub2.joinpath("test_in_sub2.py").write_text("def test_2(): pass", encoding="utf-8")
sub2.joinpath("test_in_sub2.py").write_text("def test_2(): pass")
result = pytester.runpytest("-v", "-s") result = pytester.runpytest("-v", "-s")
result.assert_outcomes(passed=2) result.assert_outcomes(passed=2)
@ -1378,7 +1385,8 @@ def test_skip_duplicates_by_default(pytester: Pytester) -> None:
def test_real(): def test_real():
pass pass
""" """
) ),
encoding="utf-8",
) )
result = pytester.runpytest(str(a), str(a)) result = pytester.runpytest(str(a), str(a))
result.stdout.fnmatch_lines(["*collected 1 item*"]) result.stdout.fnmatch_lines(["*collected 1 item*"])
@ -1398,7 +1406,8 @@ def test_keep_duplicates(pytester: Pytester) -> None:
def test_real(): def test_real():
pass pass
""" """
) ),
encoding="utf-8",
) )
result = pytester.runpytest("--keep-duplicates", str(a), str(a)) result = pytester.runpytest("--keep-duplicates", str(a), str(a))
result.stdout.fnmatch_lines(["*collected 2 item*"]) result.stdout.fnmatch_lines(["*collected 2 item*"])
@ -1443,8 +1452,12 @@ def test_package_with_modules(pytester: Pytester) -> None:
sub2_test = sub2.joinpath("test") sub2_test = sub2.joinpath("test")
sub2_test.mkdir(parents=True) sub2_test.mkdir(parents=True)
sub1_test.joinpath("test_in_sub1.py").write_text("def test_1(): pass") sub1_test.joinpath("test_in_sub1.py").write_text(
sub2_test.joinpath("test_in_sub2.py").write_text("def test_2(): pass") "def test_1(): pass", encoding="utf-8"
)
sub2_test.joinpath("test_in_sub2.py").write_text(
"def test_2(): pass", encoding="utf-8"
)
# Execute from . # Execute from .
result = pytester.runpytest("-v", "-s") result = pytester.runpytest("-v", "-s")
@ -1488,9 +1501,11 @@ def test_package_ordering(pytester: Pytester) -> None:
sub2_test = sub2.joinpath("test") sub2_test = sub2.joinpath("test")
sub2_test.mkdir(parents=True) sub2_test.mkdir(parents=True)
root.joinpath("Test_root.py").write_text("def test_1(): pass") root.joinpath("Test_root.py").write_text("def test_1(): pass", encoding="utf-8")
sub1.joinpath("Test_sub1.py").write_text("def test_2(): pass") sub1.joinpath("Test_sub1.py").write_text("def test_2(): pass", encoding="utf-8")
sub2_test.joinpath("test_sub2.py").write_text("def test_3(): pass") sub2_test.joinpath("test_sub2.py").write_text(
"def test_3(): pass", encoding="utf-8"
)
# Execute from . # Execute from .
result = pytester.runpytest("-v", "-s") result = pytester.runpytest("-v", "-s")

View File

@ -1392,14 +1392,14 @@ def test_sequence_comparison_uses_repr(pytester: Pytester) -> None:
def test_assertrepr_loaded_per_dir(pytester: Pytester) -> None: def test_assertrepr_loaded_per_dir(pytester: Pytester) -> None:
pytester.makepyfile(test_base=["def test_base(): assert 1 == 2"]) pytester.makepyfile(test_base=["def test_base(): assert 1 == 2"])
a = pytester.mkdir("a") a = pytester.mkdir("a")
a.joinpath("test_a.py").write_text("def test_a(): assert 1 == 2") a.joinpath("test_a.py").write_text("def test_a(): assert 1 == 2", encoding="utf-8")
a.joinpath("conftest.py").write_text( a.joinpath("conftest.py").write_text(
'def pytest_assertrepr_compare(): return ["summary a"]' 'def pytest_assertrepr_compare(): return ["summary a"]', encoding="utf-8"
) )
b = pytester.mkdir("b") b = pytester.mkdir("b")
b.joinpath("test_b.py").write_text("def test_b(): assert 1 == 2") b.joinpath("test_b.py").write_text("def test_b(): assert 1 == 2", encoding="utf-8")
b.joinpath("conftest.py").write_text( b.joinpath("conftest.py").write_text(
'def pytest_assertrepr_compare(): return ["summary b"]' 'def pytest_assertrepr_compare(): return ["summary b"]', encoding="utf-8"
) )
result = pytester.runpytest() result = pytester.runpytest()

View File

@ -1161,7 +1161,7 @@ class TestAssertionRewriteHookDetails:
return False return False
def rewrite_self(): def rewrite_self():
with open(__file__, 'w') as self: with open(__file__, 'w', encoding='utf-8') as self:
self.write('def reloaded(): return True') self.write('def reloaded(): return True')
""", """,
test_fun=""" test_fun="""

View File

@ -38,7 +38,9 @@ class TestNewAPI:
@pytest.mark.filterwarnings("ignore:could not create cache path") @pytest.mark.filterwarnings("ignore:could not create cache path")
def test_cache_writefail_cachfile_silent(self, pytester: Pytester) -> None: def test_cache_writefail_cachfile_silent(self, pytester: Pytester) -> None:
pytester.makeini("[pytest]") pytester.makeini("[pytest]")
pytester.path.joinpath(".pytest_cache").write_text("gone wrong") pytester.path.joinpath(".pytest_cache").write_text(
"gone wrong", encoding="utf-8"
)
config = pytester.parseconfigure() config = pytester.parseconfigure()
cache = config.cache cache = config.cache
assert cache is not None assert cache is not None
@ -1134,7 +1136,9 @@ class TestNewFirst:
["*test_2/test_2.py::test_1 PASSED*", "*test_1/test_1.py::test_1 PASSED*"] ["*test_2/test_2.py::test_1 PASSED*", "*test_1/test_1.py::test_1 PASSED*"]
) )
p1.write_text("def test_1(): assert 1\n" "def test_2(): assert 1\n") p1.write_text(
"def test_1(): assert 1\n" "def test_2(): assert 1\n", encoding="utf-8"
)
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9))) os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9)))
result = pytester.runpytest("--nf", "--collect-only", "-q") result = pytester.runpytest("--nf", "--collect-only", "-q")
@ -1207,7 +1211,8 @@ class TestNewFirst:
p1.write_text( p1.write_text(
"import pytest\n" "import pytest\n"
"@pytest.mark.parametrize('num', [1, 2, 3])\n" "@pytest.mark.parametrize('num', [1, 2, 3])\n"
"def test_1(num): assert num\n" "def test_1(num): assert num\n",
encoding="utf-8",
) )
os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9))) os.utime(p1, ns=(p1.stat().st_atime_ns, int(1e9)))
@ -1259,7 +1264,7 @@ def test_gitignore(pytester: Pytester) -> None:
assert gitignore_path.read_text(encoding="UTF-8") == msg assert gitignore_path.read_text(encoding="UTF-8") == msg
# Does not overwrite existing/custom one. # Does not overwrite existing/custom one.
gitignore_path.write_text("custom") gitignore_path.write_text("custom", encoding="utf-8")
cache.set("something", "else") cache.set("something", "else")
assert gitignore_path.read_text(encoding="UTF-8") == "custom" assert gitignore_path.read_text(encoding="UTF-8") == "custom"

View File

@ -750,9 +750,10 @@ def test_setup_failure_does_not_kill_capturing(pytester: Pytester) -> None:
def pytest_runtest_setup(item): def pytest_runtest_setup(item):
raise ValueError(42) raise ValueError(42)
""" """
),
encoding="utf-8",
) )
) sub1.joinpath("test_mod.py").write_text("def test_func1(): pass", encoding="utf-8")
sub1.joinpath("test_mod.py").write_text("def test_func1(): pass")
result = pytester.runpytest(pytester.path, "--traceconfig") result = pytester.runpytest(pytester.path, "--traceconfig")
result.stdout.fnmatch_lines(["*ValueError(42)*", "*1 error*"]) result.stdout.fnmatch_lines(["*ValueError(42)*", "*1 error*"])
@ -1523,9 +1524,9 @@ def test_global_capture_with_live_logging(pytester: Pytester) -> None:
def pytest_runtest_logreport(report): def pytest_runtest_logreport(report):
if "test_global" in report.nodeid: if "test_global" in report.nodeid:
if report.when == "teardown": if report.when == "teardown":
with open("caplog", "w") as f: with open("caplog", "w", encoding="utf-8") as f:
f.write(report.caplog) f.write(report.caplog)
with open("capstdout", "w") as f: with open("capstdout", "w", encoding="utf-8") as f:
f.write(report.capstdout) f.write(report.capstdout)
""" """
) )
@ -1555,14 +1556,14 @@ def test_global_capture_with_live_logging(pytester: Pytester) -> None:
result = pytester.runpytest_subprocess("--log-cli-level=INFO") result = pytester.runpytest_subprocess("--log-cli-level=INFO")
assert result.ret == 0 assert result.ret == 0
with open("caplog") as f: with open("caplog", encoding="utf-8") as f:
caplog = f.read() caplog = f.read()
assert "fix setup" in caplog assert "fix setup" in caplog
assert "something in test" in caplog assert "something in test" in caplog
assert "fix teardown" in caplog assert "fix teardown" in caplog
with open("capstdout") as f: with open("capstdout", encoding="utf-8") as f:
capstdout = f.read() capstdout = f.read()
assert "fix setup" in capstdout assert "fix setup" in capstdout

View File

@ -1273,7 +1273,8 @@ def test_initial_conftests_with_testpaths(pytester: Pytester) -> None:
def pytest_sessionstart(session): def pytest_sessionstart(session):
raise Exception("pytest_sessionstart hook successfully run") raise Exception("pytest_sessionstart hook successfully run")
""" """
) ),
encoding="utf-8",
) )
pytester.makeini( pytester.makeini(
""" """
@ -1369,12 +1370,16 @@ def test_collect_symlink_dir(pytester: Pytester) -> None:
def test_collectignore_via_conftest(pytester: Pytester) -> None: def test_collectignore_via_conftest(pytester: Pytester) -> None:
"""collect_ignore in parent conftest skips importing child (issue #4592).""" """collect_ignore in parent conftest skips importing child (issue #4592)."""
tests = pytester.mkpydir("tests") tests = pytester.mkpydir("tests")
tests.joinpath("conftest.py").write_text("collect_ignore = ['ignore_me']") tests.joinpath("conftest.py").write_text(
"collect_ignore = ['ignore_me']", encoding="utf-8"
)
ignore_me = tests.joinpath("ignore_me") ignore_me = tests.joinpath("ignore_me")
ignore_me.mkdir() ignore_me.mkdir()
ignore_me.joinpath("__init__.py").touch() ignore_me.joinpath("__init__.py").touch()
ignore_me.joinpath("conftest.py").write_text("assert 0, 'should_not_be_called'") ignore_me.joinpath("conftest.py").write_text(
"assert 0, 'should_not_be_called'", encoding="utf-8"
)
result = pytester.runpytest() result = pytester.runpytest()
assert result.ret == ExitCode.NO_TESTS_COLLECTED assert result.ret == ExitCode.NO_TESTS_COLLECTED
@ -1383,9 +1388,9 @@ def test_collectignore_via_conftest(pytester: Pytester) -> None:
def test_collect_pkg_init_and_file_in_args(pytester: Pytester) -> None: def test_collect_pkg_init_and_file_in_args(pytester: Pytester) -> None:
subdir = pytester.mkdir("sub") subdir = pytester.mkdir("sub")
init = subdir.joinpath("__init__.py") init = subdir.joinpath("__init__.py")
init.write_text("def test_init(): pass") init.write_text("def test_init(): pass", encoding="utf-8")
p = subdir.joinpath("test_file.py") p = subdir.joinpath("test_file.py")
p.write_text("def test_file(): pass") p.write_text("def test_file(): pass", encoding="utf-8")
# NOTE: without "-o python_files=*.py" this collects test_file.py twice. # NOTE: without "-o python_files=*.py" this collects test_file.py twice.
# This changed/broke with "Add package scoped fixtures #2283" (2b1410895) # This changed/broke with "Add package scoped fixtures #2283" (2b1410895)
@ -1412,7 +1417,7 @@ def test_collect_pkg_init_and_file_in_args(pytester: Pytester) -> None:
def test_collect_pkg_init_only(pytester: Pytester) -> None: def test_collect_pkg_init_only(pytester: Pytester) -> None:
subdir = pytester.mkdir("sub") subdir = pytester.mkdir("sub")
init = subdir.joinpath("__init__.py") init = subdir.joinpath("__init__.py")
init.write_text("def test_init(): pass") init.write_text("def test_init(): pass", encoding="utf-8")
result = pytester.runpytest(str(init)) result = pytester.runpytest(str(init))
result.stdout.fnmatch_lines(["*no tests ran in*"]) result.stdout.fnmatch_lines(["*no tests ran in*"])
@ -1427,7 +1432,7 @@ def test_collect_sub_with_symlinks(use_pkg: bool, pytester: Pytester) -> None:
sub = pytester.mkdir("sub") sub = pytester.mkdir("sub")
if use_pkg: if use_pkg:
sub.joinpath("__init__.py").touch() sub.joinpath("__init__.py").touch()
sub.joinpath("test_file.py").write_text("def test_file(): pass") sub.joinpath("test_file.py").write_text("def test_file(): pass", encoding="utf-8")
# Create a broken symlink. # Create a broken symlink.
symlink_or_skip("test_doesnotexist.py", sub.joinpath("test_broken.py")) symlink_or_skip("test_doesnotexist.py", sub.joinpath("test_broken.py"))
@ -1465,7 +1470,7 @@ def test_collector_respects_tbstyle(pytester: Pytester) -> None:
def test_does_not_eagerly_collect_packages(pytester: Pytester) -> None: def test_does_not_eagerly_collect_packages(pytester: Pytester) -> None:
pytester.makepyfile("def test(): pass") pytester.makepyfile("def test(): pass")
pydir = pytester.mkpydir("foopkg") pydir = pytester.mkpydir("foopkg")
pydir.joinpath("__init__.py").write_text("assert False") pydir.joinpath("__init__.py").write_text("assert False", encoding="utf-8")
result = pytester.runpytest() result = pytester.runpytest()
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK

View File

@ -638,7 +638,7 @@ def test_search_conftest_up_to_inifile(
root = pytester.path root = pytester.path
src = root.joinpath("src") src = root.joinpath("src")
src.mkdir() src.mkdir()
src.joinpath("pytest.ini").write_text("[pytest]") src.joinpath("pytest.ini").write_text("[pytest]", encoding="utf-8")
src.joinpath("conftest.py").write_text( src.joinpath("conftest.py").write_text(
textwrap.dedent( textwrap.dedent(
"""\ """\

View File

@ -28,7 +28,7 @@ from _pytest.stash import Stash
def schema() -> xmlschema.XMLSchema: def schema() -> xmlschema.XMLSchema:
"""Return an xmlschema.XMLSchema object for the junit-10.xsd file.""" """Return an xmlschema.XMLSchema object for the junit-10.xsd file."""
fn = Path(__file__).parent / "example_scripts/junit-10.xsd" fn = Path(__file__).parent / "example_scripts/junit-10.xsd"
with fn.open() as f: with fn.open(encoding="utf-8") as f:
return xmlschema.XMLSchema(f) return xmlschema.XMLSchema(f)
@ -45,7 +45,7 @@ class RunAndParse:
xml_path = self.pytester.path.joinpath("junit.xml") xml_path = self.pytester.path.joinpath("junit.xml")
result = self.pytester.runpytest("--junitxml=%s" % xml_path, *args) result = self.pytester.runpytest("--junitxml=%s" % xml_path, *args)
if family == "xunit2": if family == "xunit2":
with xml_path.open() as f: with xml_path.open(encoding="utf-8") as f:
self.schema.validate(f) self.schema.validate(f)
xmldoc = minidom.parse(str(xml_path)) xmldoc = minidom.parse(str(xml_path))
return result, DomNode(xmldoc) return result, DomNode(xmldoc)
@ -469,7 +469,7 @@ class TestPython:
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
) -> None: ) -> None:
p = pytester.mkdir("sub").joinpath("test_hello.py") p = pytester.mkdir("sub").joinpath("test_hello.py")
p.write_text("def test_func(): 0/0") p.write_text("def test_func(): 0/0", encoding="utf-8")
result, dom = run_and_parse(family=xunit_family) result, dom = run_and_parse(family=xunit_family)
assert result.ret assert result.ret
node = dom.find_first_by_tag("testsuite") node = dom.find_first_by_tag("testsuite")
@ -987,7 +987,7 @@ class TestNonPython:
return "custom item runtest failed" return "custom item runtest failed"
""" """
) )
pytester.path.joinpath("myfile.xyz").write_text("hello") pytester.path.joinpath("myfile.xyz").write_text("hello", encoding="utf-8")
result, dom = run_and_parse(family=xunit_family) result, dom = run_and_parse(family=xunit_family)
assert result.ret assert result.ret
node = dom.find_first_by_tag("testsuite") node = dom.find_first_by_tag("testsuite")
@ -1013,7 +1013,7 @@ def test_nullbyte(pytester: Pytester, junit_logging: str) -> None:
) )
xmlf = pytester.path.joinpath("junit.xml") xmlf = pytester.path.joinpath("junit.xml")
pytester.runpytest("--junitxml=%s" % xmlf, "-o", "junit_logging=%s" % junit_logging) pytester.runpytest("--junitxml=%s" % xmlf, "-o", "junit_logging=%s" % junit_logging)
text = xmlf.read_text() text = xmlf.read_text(encoding="utf-8")
assert "\x00" not in text assert "\x00" not in text
if junit_logging == "system-out": if junit_logging == "system-out":
assert "#x00" in text assert "#x00" in text
@ -1035,7 +1035,7 @@ def test_nullbyte_replace(pytester: Pytester, junit_logging: str) -> None:
) )
xmlf = pytester.path.joinpath("junit.xml") xmlf = pytester.path.joinpath("junit.xml")
pytester.runpytest("--junitxml=%s" % xmlf, "-o", "junit_logging=%s" % junit_logging) pytester.runpytest("--junitxml=%s" % xmlf, "-o", "junit_logging=%s" % junit_logging)
text = xmlf.read_text() text = xmlf.read_text(encoding="utf-8")
if junit_logging == "system-out": if junit_logging == "system-out":
assert "#x0" in text assert "#x0" in text
if junit_logging == "no": if junit_logging == "no":

View File

@ -59,7 +59,8 @@ def test_link_resolve(pytester: Pytester) -> None:
def test_foo(): def test_foo():
raise AssertionError() raise AssertionError()
""" """
) ),
encoding="utf-8",
) )
subst = subst_path_linux subst = subst_path_linux

View File

@ -461,5 +461,5 @@ def test_syspath_prepend_with_namespace_packages(
# Should invalidate caches via importlib.invalidate_caches. # Should invalidate caches via importlib.invalidate_caches.
modules_tmpdir = pytester.mkdir("modules_tmpdir") modules_tmpdir = pytester.mkdir("modules_tmpdir")
monkeypatch.syspath_prepend(str(modules_tmpdir)) monkeypatch.syspath_prepend(str(modules_tmpdir))
modules_tmpdir.joinpath("main_app.py").write_text("app = True") modules_tmpdir.joinpath("main_app.py").write_text("app = True", encoding="utf-8")
from main_app import app # noqa: F401 from main_app import app # noqa: F401

View File

@ -311,7 +311,7 @@ def test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
script = str(pytester.path.joinpath("test_argcomplete")) script = str(pytester.path.joinpath("test_argcomplete"))
with open(str(script), "w") as fp: with open(str(script), "w", encoding="utf-8") as fp:
# redirect output from argcomplete to stdin and stderr is not trivial # redirect output from argcomplete to stdin and stderr is not trivial
# http://stackoverflow.com/q/12589419/1307905 # http://stackoverflow.com/q/12589419/1307905
# so we use bash # so we use bash

View File

@ -120,9 +120,9 @@ class TestImportPath:
otherdir.joinpath("__init__.py").touch() otherdir.joinpath("__init__.py").touch()
module_a = otherdir / "a.py" module_a = otherdir / "a.py"
module_a.write_text("from .b import stuff as result\n") module_a.write_text("from .b import stuff as result\n", encoding="utf-8")
module_b = otherdir / "b.py" module_b = otherdir / "b.py"
module_b.write_text('stuff="got it"\n') module_b.write_text('stuff="got it"\n', encoding="utf-8")
module_c = otherdir / "c.py" module_c = otherdir / "c.py"
module_c.write_text( module_c.write_text(
dedent( dedent(
@ -131,7 +131,8 @@ class TestImportPath:
import otherdir.a import otherdir.a
value = otherdir.a.result value = otherdir.a.result
""" """
) ),
encoding="utf-8",
) )
module_d = otherdir / "d.py" module_d = otherdir / "d.py"
module_d.write_text( module_d.write_text(
@ -141,7 +142,8 @@ class TestImportPath:
from otherdir import a from otherdir import a
value2 = a.result value2 = a.result
""" """
) ),
encoding="utf-8",
) )
def test_smoke_test(self, path1: Path) -> None: def test_smoke_test(self, path1: Path) -> None:
@ -283,7 +285,7 @@ class TestImportPath:
def simple_module(self, tmp_path: Path) -> Path: def simple_module(self, tmp_path: Path) -> Path:
fn = tmp_path / "_src/tests/mymod.py" fn = tmp_path / "_src/tests/mymod.py"
fn.parent.mkdir(parents=True) fn.parent.mkdir(parents=True)
fn.write_text("def foo(x): return 40 + x") fn.write_text("def foo(x): return 40 + x", encoding="utf-8")
return fn return fn
def test_importmode_importlib(self, simple_module: Path, tmp_path: Path) -> None: def test_importmode_importlib(self, simple_module: Path, tmp_path: Path) -> None:
@ -447,7 +449,7 @@ def test_samefile_false_negatives(tmp_path: Path, monkeypatch: MonkeyPatch) -> N
return False, even when they are clearly equal. return False, even when they are clearly equal.
""" """
module_path = tmp_path.joinpath("my_module.py") module_path = tmp_path.joinpath("my_module.py")
module_path.write_text("def foo(): return 42") module_path.write_text("def foo(): return 42", encoding="utf-8")
monkeypatch.syspath_prepend(tmp_path) monkeypatch.syspath_prepend(tmp_path)
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
@ -473,7 +475,8 @@ class TestImportLibMode:
class Data: class Data:
value: str value: str
""" """
) ),
encoding="utf-8",
) )
module = import_path(fn, mode="importlib", root=tmp_path) module = import_path(fn, mode="importlib", root=tmp_path)
@ -498,7 +501,8 @@ class TestImportLibMode:
s = pickle.dumps(_action) s = pickle.dumps(_action)
return pickle.loads(s) return pickle.loads(s)
""" """
) ),
encoding="utf-8",
) )
module = import_path(fn, mode="importlib", root=tmp_path) module = import_path(fn, mode="importlib", root=tmp_path)
@ -525,7 +529,8 @@ class TestImportLibMode:
class Data: class Data:
x: int = 42 x: int = 42
""" """
) ),
encoding="utf-8",
) )
fn2 = tmp_path.joinpath("_src/m2/tests/test.py") fn2 = tmp_path.joinpath("_src/m2/tests/test.py")
@ -540,7 +545,8 @@ class TestImportLibMode:
class Data: class Data:
x: str = "" x: str = ""
""" """
) ),
encoding="utf-8",
) )
import pickle import pickle

View File

@ -347,7 +347,7 @@ class TestPytestPluginManager:
pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwex.y") pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwex.y")
pytester.syspathinsert() pytester.syspathinsert()
pytester.mkpydir("pkg").joinpath("plug.py").write_text("x=3") pytester.mkpydir("pkg").joinpath("plug.py").write_text("x=3", encoding="utf-8")
pluginname = "pkg.plug" pluginname = "pkg.plug"
pytestpm.import_plugin(pluginname) pytestpm.import_plugin(pluginname)
mod = pytestpm.get_plugin("pkg.plug") mod = pytestpm.get_plugin("pkg.plug")

View File

@ -222,7 +222,7 @@ class TestInlineRunModulesCleanup:
result = pytester.inline_run(str(test_mod)) result = pytester.inline_run(str(test_mod))
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
# rewrite module, now test should fail if module was re-imported # rewrite module, now test should fail if module was re-imported
test_mod.write_text("def test_foo(): assert False") test_mod.write_text("def test_foo(): assert False", encoding="utf-8")
result2 = pytester.inline_run(str(test_mod)) result2 = pytester.inline_run(str(test_mod))
assert result2.ret == ExitCode.TESTS_FAILED assert result2.ret == ExitCode.TESTS_FAILED

View File

@ -410,7 +410,7 @@ class TestReportSerialization:
) -> None: ) -> None:
sub_dir = pytester.path.joinpath("ns") sub_dir = pytester.path.joinpath("ns")
sub_dir.mkdir() sub_dir.mkdir()
sub_dir.joinpath("conftest.py").write_text("import unknown") sub_dir.joinpath("conftest.py").write_text("import unknown", encoding="utf-8")
result = pytester.runpytest_subprocess(".") result = pytester.runpytest_subprocess(".")
result.stdout.fnmatch_lines(["E *Error: No module named 'unknown'"]) result.stdout.fnmatch_lines(["E *Error: No module named 'unknown'"])

View File

@ -265,9 +265,9 @@ def test_plugin_already_exists(pytester: Pytester) -> None:
def test_exclude(pytester: Pytester) -> None: def test_exclude(pytester: Pytester) -> None:
hellodir = pytester.mkdir("hello") hellodir = pytester.mkdir("hello")
hellodir.joinpath("test_hello.py").write_text("x y syntaxerror") hellodir.joinpath("test_hello.py").write_text("x y syntaxerror", encoding="utf-8")
hello2dir = pytester.mkdir("hello2") hello2dir = pytester.mkdir("hello2")
hello2dir.joinpath("test_hello2.py").write_text("x y syntaxerror") hello2dir.joinpath("test_hello2.py").write_text("x y syntaxerror", encoding="utf-8")
pytester.makepyfile(test_ok="def test_pass(): pass") pytester.makepyfile(test_ok="def test_pass(): pass")
result = pytester.runpytest("--ignore=hello", "--ignore=hello2") result = pytester.runpytest("--ignore=hello", "--ignore=hello2")
assert result.ret == 0 assert result.ret == 0
@ -276,13 +276,13 @@ def test_exclude(pytester: Pytester) -> None:
def test_exclude_glob(pytester: Pytester) -> None: def test_exclude_glob(pytester: Pytester) -> None:
hellodir = pytester.mkdir("hello") hellodir = pytester.mkdir("hello")
hellodir.joinpath("test_hello.py").write_text("x y syntaxerror") hellodir.joinpath("test_hello.py").write_text("x y syntaxerror", encoding="utf-8")
hello2dir = pytester.mkdir("hello2") hello2dir = pytester.mkdir("hello2")
hello2dir.joinpath("test_hello2.py").write_text("x y syntaxerror") hello2dir.joinpath("test_hello2.py").write_text("x y syntaxerror", encoding="utf-8")
hello3dir = pytester.mkdir("hallo3") hello3dir = pytester.mkdir("hallo3")
hello3dir.joinpath("test_hello3.py").write_text("x y syntaxerror") hello3dir.joinpath("test_hello3.py").write_text("x y syntaxerror", encoding="utf-8")
subdir = pytester.mkdir("sub") subdir = pytester.mkdir("sub")
subdir.joinpath("test_hello4.py").write_text("x y syntaxerror") subdir.joinpath("test_hello4.py").write_text("x y syntaxerror", encoding="utf-8")
pytester.makepyfile(test_ok="def test_pass(): pass") pytester.makepyfile(test_ok="def test_pass(): pass")
result = pytester.runpytest("--ignore-glob=*h[ea]llo*") result = pytester.runpytest("--ignore-glob=*h[ea]llo*")
assert result.ret == 0 assert result.ret == 0

View File

@ -195,7 +195,8 @@ class TestEvaluation:
def pytest_markeval_namespace(): def pytest_markeval_namespace():
return {"arg": "root"} return {"arg": "root"}
""" """
) ),
encoding="utf-8",
) )
root.joinpath("test_root.py").write_text( root.joinpath("test_root.py").write_text(
textwrap.dedent( textwrap.dedent(
@ -206,7 +207,8 @@ class TestEvaluation:
def test_root(): def test_root():
assert False assert False
""" """
) ),
encoding="utf-8",
) )
foo = root.joinpath("foo") foo = root.joinpath("foo")
foo.mkdir() foo.mkdir()
@ -219,7 +221,8 @@ class TestEvaluation:
def pytest_markeval_namespace(): def pytest_markeval_namespace():
return {"arg": "foo"} return {"arg": "foo"}
""" """
) ),
encoding="utf-8",
) )
foo.joinpath("test_foo.py").write_text( foo.joinpath("test_foo.py").write_text(
textwrap.dedent( textwrap.dedent(
@ -230,7 +233,8 @@ class TestEvaluation:
def test_foo(): def test_foo():
assert False assert False
""" """
) ),
encoding="utf-8",
) )
bar = root.joinpath("bar") bar = root.joinpath("bar")
bar.mkdir() bar.mkdir()
@ -243,7 +247,8 @@ class TestEvaluation:
def pytest_markeval_namespace(): def pytest_markeval_namespace():
return {"arg": "bar"} return {"arg": "bar"}
""" """
) ),
encoding="utf-8",
) )
bar.joinpath("test_bar.py").write_text( bar.joinpath("test_bar.py").write_text(
textwrap.dedent( textwrap.dedent(
@ -254,7 +259,8 @@ class TestEvaluation:
def test_bar(): def test_bar():
assert False assert False
""" """
) ),
encoding="utf-8",
) )
reprec = pytester.inline_run("-vs", "--capture=no") reprec = pytester.inline_run("-vs", "--capture=no")
@ -629,7 +635,8 @@ class TestXFail:
@pytest.mark.xfail(reason='unsupported feature', strict=%s) @pytest.mark.xfail(reason='unsupported feature', strict=%s)
def test_foo(): def test_foo():
with open('foo_executed', 'w'): pass # make sure test executes with open('foo_executed', 'w', encoding='utf-8'):
pass # make sure test executes
""" """
% strict % strict
) )

View File

@ -352,6 +352,6 @@ def test_one():
assert result.ret == 0 assert result.ret == 0
assert Path(stepwise_cache_file).exists() assert Path(stepwise_cache_file).exists()
with stepwise_cache_file.open() as file_handle: with stepwise_cache_file.open(encoding="utf-8") as file_handle:
observed_value = file_handle.readlines() observed_value = file_handle.readlines()
assert [expected_value] == observed_value assert [expected_value] == observed_value

View File

@ -244,7 +244,8 @@ class TestTerminal:
def test_method(self): def test_method(self):
pass pass
""" """
) ),
encoding="utf-8",
) )
result = pytester.runpytest("-vv") result = pytester.runpytest("-vv")
assert result.ret == 0 assert result.ret == 0
@ -1567,7 +1568,8 @@ class TestGenericReporting:
""" """
def pytest_report_header(config, start_path): def pytest_report_header(config, start_path):
return ["line1", str(start_path)] return ["line1", str(start_path)]
""" """,
encoding="utf-8",
) )
result = pytester.runpytest("a") result = pytester.runpytest("a")
result.stdout.fnmatch_lines(["*hello: 42*", "line1", str(pytester.path)]) result.stdout.fnmatch_lines(["*hello: 42*", "line1", str(pytester.path)])
@ -1671,7 +1673,7 @@ def test_fdopen_kept_alive_issue124(pytester: Pytester) -> None:
import os, sys import os, sys
k = [] k = []
def test_open_file_and_keep_alive(capfd): def test_open_file_and_keep_alive(capfd):
stdout = os.fdopen(1, 'w', 1) stdout = os.fdopen(1, 'w', buffering=1, encoding='utf-8')
k.append(stdout) k.append(stdout)
def test_close_kept_alive_file(): def test_close_kept_alive_file():

View File

@ -561,7 +561,7 @@ def test_basetemp_with_read_only_files(pytester: Pytester) -> None:
def test(tmp_path): def test(tmp_path):
fn = tmp_path / 'foo.txt' fn = tmp_path / 'foo.txt'
fn.write_text('hello') fn.write_text('hello', encoding='utf-8')
mode = os.stat(str(fn)).st_mode mode = os.stat(str(fn)).st_mode
os.chmod(str(fn), mode & ~stat.S_IREAD) os.chmod(str(fn), mode & ~stat.S_IREAD)
""" """

View File

@ -810,12 +810,12 @@ def test_resource_warning(pytester: Pytester, monkeypatch: pytest.MonkeyPatch) -
pytester.makepyfile( pytester.makepyfile(
""" """
def open_file(p): def open_file(p):
f = p.open("r") f = p.open("r", encoding="utf-8")
assert p.read_text() == "hello" assert p.read_text() == "hello"
def test_resource_warning(tmp_path): def test_resource_warning(tmp_path):
p = tmp_path.joinpath("foo.txt") p = tmp_path.joinpath("foo.txt")
p.write_text("hello") p.write_text("hello", encoding="utf-8")
open_file(p) open_file(p)
""" """
) )