Skip to content

Commit

Permalink
Specify encoding in more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jun 20, 2023
1 parent 4e743be commit fa9c498
Show file tree
Hide file tree
Showing 31 changed files with 218 additions and 149 deletions.
2 changes: 1 addition & 1 deletion doc/en/example/simple.rst
Expand Up @@ -817,7 +817,7 @@ case we just write some information out to a ``failures`` file:
# we only look at actual failing test calls, not setup/teardown
if rep.when == "call" and rep.failed:
mode = "a" if os.path.exists("failures") else "w"
with open("failures", mode) as f:
with open("failures", mode, encoding="utf-8") as f:
# let's also access a fixture for the fun of it
if "tmp_path" in item.fixturenames:
extra = " ({})".format(item.funcargs["tmp_path"])
Expand Down
2 changes: 1 addition & 1 deletion doc/en/how-to/fixtures.rst
Expand Up @@ -1698,7 +1698,7 @@ and declare its use in a test module via a ``usefixtures`` marker:
class TestDirectoryInit:
def test_cwd_starts_empty(self):
assert os.listdir(os.getcwd()) == []
with open("myfile", "w") as f:
with open("myfile", "w", encoding="utf-8") as f:
f.write("hello")
def test_cwd_again_starts_empty(self):
Expand Down
4 changes: 2 additions & 2 deletions doc/en/how-to/unittest.rst
Expand Up @@ -207,10 +207,10 @@ creation of a per-test temporary directory:
@pytest.fixture(autouse=True)
def initdir(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path) # change to pytest-provided temporary directory
tmp_path.joinpath("samplefile.ini").write_text("# testdata")
tmp_path.joinpath("samplefile.ini").write_text("# testdata", encoding="utf-8")
def test_method(self):
with open("samplefile.ini") as f:
with open("samplefile.ini", encoding="utf-8") as f:
s = f.read()
assert "testdata" in s
Expand Down
4 changes: 3 additions & 1 deletion scripts/towncrier-draft-to-file.py
Expand Up @@ -7,7 +7,9 @@ def main():
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.
"""
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)


Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/helpconfig.py
Expand Up @@ -105,7 +105,7 @@ def pytest_cmdline_parse():
if config.option.debug:
# --debug | --debug <file.log> was provided.
path = config.option.debug
debugfile = open(path, "w")
debugfile = open(path, "w", encoding="utf-8")
debugfile.write(
"versions pytest-%s, "
"python-%s\ncwd=%s\nargs=%s\n\n"
Expand Down
98 changes: 59 additions & 39 deletions testing/_py/test_local.py
@@ -1,14 +1,24 @@
import contextlib
import multiprocessing
import os
import sys
import time
import warnings
from unittest import mock

import pytest
from py import error
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:
def test_constructor_equality(self, path1):
p = path1.__class__(path1)
Expand Down Expand Up @@ -223,7 +233,8 @@ def test_cmp(self, path1):
assert not (path1 < path1)

def test_simple_read(self, path1):
x = path1.join("samplefile").read("r")
with ignore_encoding_warning():
x = path1.join("samplefile").read("r")
assert x == "samplefile\n"

def test_join_div_operator(self, path1):
Expand Down Expand Up @@ -265,12 +276,14 @@ def test_newext(self, path1):

def test_readlines(self, path1):
fn = path1.join("samplefile")
contents = fn.readlines()
with ignore_encoding_warning():
contents = fn.readlines()
assert contents == ["samplefile\n"]

def test_readlines_nocr(self, path1):
fn = path1.join("samplefile")
contents = fn.readlines(cr=0)
with ignore_encoding_warning():
contents = fn.readlines(cr=0)
assert contents == ["samplefile", ""]

def test_file(self, path1):
Expand Down Expand Up @@ -362,8 +375,8 @@ def test_copy_file(self, path1):
initpy.copy(copied)
try:
assert copied.check()
s1 = initpy.read()
s2 = copied.read()
s1 = initpy.read_text(encoding="utf-8")
s2 = copied.read_text(encoding="utf-8")
assert s1 == s2
finally:
if copied.check():
Expand All @@ -376,8 +389,8 @@ def test_copy_dir(self, path1):
otherdir.copy(copied)
assert copied.check(dir=1)
assert copied.join("__init__.py").check(file=1)
s1 = otherdir.join("__init__.py").read()
s2 = copied.join("__init__.py").read()
s1 = otherdir.join("__init__.py").read_text(encoding="utf-8")
s2 = copied.join("__init__.py").read_text(encoding="utf-8")
assert s1 == s2
finally:
if copied.check(dir=1):
Expand Down Expand Up @@ -463,13 +476,13 @@ def setuptestfs(path):
return
# print "setting up test fs for", repr(path)
samplefile = path.ensure("samplefile")
samplefile.write("samplefile\n")
samplefile.write_text("samplefile\n", encoding="utf-8")

execfile = path.ensure("execfile")
execfile.write("x=42")
execfile.write_text("x=42", encoding="utf-8")

execfilepy = path.ensure("execfile.py")
execfilepy.write("x=42")
execfilepy.write_text("x=42", encoding="utf-8")

d = {1: 2, "hello": "world", "answer": 42}
path.ensure("samplepickle").dump(d)
Expand All @@ -481,22 +494,24 @@ def setuptestfs(path):
otherdir.ensure("__init__.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.write('stuff="got it"\n')
module_b.write_text('stuff="got it"\n', encoding="utf-8")
module_c = otherdir.ensure("c.py")
module_c.write(
module_c.write_text(
"""import py;
import otherdir.a
value = otherdir.a.result
"""
""",
encoding="utf-8",
)
module_d = otherdir.ensure("d.py")
module_d.write(
module_d.write_text(
"""import py;
from otherdir import a
value2 = a.result
"""
""",
encoding="utf-8",
)


Expand Down Expand Up @@ -534,9 +549,11 @@ def batch_make_numbered_dirs(rootdir, repeats):
for i in range(repeats):
dir_ = local.make_numbered_dir(prefix="repro-", rootdir=rootdir)
file_ = dir_.join("foo")
file_.write("%s" % i)
actual = int(file_.read())
assert actual == i, f"int(file_.read()) is {actual} instead of {i}"
file_.write_text("%s" % i, encoding="utf-8")
actual = int(file_.read_text(encoding="utf-8"))
assert (

Check warning on line 554 in testing/_py/test_local.py

View check run for this annotation

Codecov / codecov/patch

testing/_py/test_local.py#L552-L554

Added lines #L552 - L554 were not covered by tests
actual == i
), f"int(file_.read_text(encoding='utf-8')) is {actual} instead of {i}"
dir_.join(".lock").remove(ignore_errors=True)
return True

Expand Down Expand Up @@ -692,14 +709,14 @@ def test_gt_with_strings(self, path1):

def test_open_and_ensure(self, path1):
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")
assert p.read() == "hello"
assert p.read_text(encoding="utf-8") == "hello"

def test_write_and_ensure(self, path1):
p = path1.join("sub1", "sub2", "file")
p.write("hello", ensure=1)
assert p.read() == "hello"
p.write_text("hello", ensure=1, encoding="utf-8")
assert p.read_text(encoding="utf-8") == "hello"

@pytest.mark.parametrize("bin", (False, True))
def test_dump(self, tmpdir, bin):
Expand Down Expand Up @@ -770,9 +787,9 @@ def test_ensure_filepath_withdir(self, tmpdir):
newfile = tmpdir.join("test1", "test")
newfile.ensure()
assert newfile.check(file=1)
newfile.write("42")
newfile.write_text("42", encoding="utf-8")
newfile.ensure()
s = newfile.read()
s = newfile.read_text(encoding="utf-8")
assert s == "42"

def test_ensure_filepath_withoutdir(self, tmpdir):
Expand Down Expand Up @@ -806,9 +823,9 @@ def test_long_filenames(self, tmpdir):
newfilename = "/test" * 60 # type:ignore[unreachable]
l1 = tmpdir.join(newfilename)
l1.ensure(file=True)
l1.write("foo")
l1.write_text("foo", encoding="utf-8")

Check warning on line 826 in testing/_py/test_local.py

View check run for this annotation

Codecov / codecov/patch

testing/_py/test_local.py#L826

Added line #L826 was not covered by tests
l2 = tmpdir.join(newfilename)
assert l2.read() == "foo"
assert l2.read_text(encoding="utf-8") == "foo"

Check warning on line 828 in testing/_py/test_local.py

View check run for this annotation

Codecov / codecov/patch

testing/_py/test_local.py#L828

Added line #L828 was not covered by tests

def test_visit_depth_first(self, tmpdir):
tmpdir.ensure("a", "1")
Expand Down Expand Up @@ -1278,22 +1295,22 @@ class TestPOSIXLocalPath:
def test_hardlink(self, tmpdir):
linkpath = tmpdir.join("test")
filepath = tmpdir.join("file")
filepath.write("Hello")
filepath.write_text("Hello", encoding="utf-8")
nlink = filepath.stat().nlink
linkpath.mklinkto(filepath)
assert filepath.stat().nlink == nlink + 1

def test_symlink_are_identical(self, tmpdir):
filepath = tmpdir.join("file")
filepath.write("Hello")
filepath.write_text("Hello", encoding="utf-8")
linkpath = tmpdir.join("test")
linkpath.mksymlinkto(filepath)
assert linkpath.readlink() == str(filepath)

def test_symlink_isfile(self, tmpdir):
linkpath = tmpdir.join("test")
filepath = tmpdir.join("file")
filepath.write("")
filepath.write_text("", encoding="utf-8")
linkpath.mksymlinkto(filepath)
assert linkpath.check(file=1)
assert not linkpath.check(link=0, file=1)
Expand All @@ -1302,10 +1319,12 @@ def test_symlink_isfile(self, tmpdir):
def test_symlink_relative(self, tmpdir):
linkpath = tmpdir.join("test")
filepath = tmpdir.join("file")
filepath.write("Hello")
filepath.write_text("Hello", encoding="utf-8")
linkpath.mksymlinkto(filepath, absolute=False)
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):
linkpath = tmpdir.join("testnotexisting")
Expand Down Expand Up @@ -1338,7 +1357,7 @@ def test_symlink_remove(self, tmpdir):
def test_realpath_file(self, tmpdir):
linkpath = tmpdir.join("test")
filepath = tmpdir.join("file")
filepath.write("")
filepath.write_text("", encoding="utf-8")
linkpath.mksymlinkto(filepath)
realpath = linkpath.realpath()
assert realpath.basename == "file"
Expand Down Expand Up @@ -1383,7 +1402,7 @@ def test_atime(self, tmpdir):
atime1 = path.atime()
# we could wait here but timer resolution is very
# system dependent
path.read()
path.read_binary()
time.sleep(ATIME_RESOLUTION)
atime2 = path.atime()
time.sleep(ATIME_RESOLUTION)
Expand Down Expand Up @@ -1467,7 +1486,7 @@ def test_copy_stat_dir(self, tmpdir):
test_files = ["a", "b", "c"]
src = tmpdir.join("src")
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")
# a small delay before the copy
time.sleep(ATIME_RESOLUTION)
Expand Down Expand Up @@ -1521,10 +1540,11 @@ def test_listdir(self, tmpdir):
def test_read_write(self, tmpdir):
x = tmpdir.join("hello")
part = "hällo"
x.write(part)
assert x.read() == part
x.write(part.encode(sys.getdefaultencoding()))
assert x.read() == part.encode(sys.getdefaultencoding())
with ignore_encoding_warning():
x.write(part)
assert x.read() == part
x.write(part.encode(sys.getdefaultencoding()))
assert x.read() == part.encode(sys.getdefaultencoding())


class TestBinaryAndTextMethods:
Expand Down
23 changes: 14 additions & 9 deletions testing/acceptance_test.py
Expand Up @@ -613,7 +613,7 @@ def test_pyargs_only_imported_once(self, pytester: Pytester) -> None:

def test_pyargs_filename_looks_like_module(self, pytester: Pytester) -> None:
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")
assert result.ret == ExitCode.OK

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

monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
path = pytester.mkpydir("tpkg")
path.joinpath("test_hello.py").write_text("def test_hello(): pass")
path.joinpath("test_world.py").write_text("def test_world(): pass")
path.joinpath("test_hello.py").write_text(
"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")
assert result.ret == 0
result.stdout.fnmatch_lines(["*2 passed*"])
Expand Down Expand Up @@ -662,13 +666,15 @@ def test_cmdline_python_namespace_package(
ns = d.joinpath("ns_pkg")
ns.mkdir()
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.mkdir()
lib.joinpath("__init__.py").touch()
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:
Expand Down Expand Up @@ -754,10 +760,10 @@ def test_cmdline_python_package_symlink(
lib.mkdir()
lib.joinpath("__init__.py").touch()
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(
"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")
Expand Down Expand Up @@ -1276,8 +1282,7 @@ def test_simple():
result.stderr.fnmatch_lines(["*@this is stderr@*"])

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

Expand Down

0 comments on commit fa9c498

Please sign in to comment.