Skip to content

Commit

Permalink
Add 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 7e51076 commit 661b938
Show file tree
Hide file tree
Showing 27 changed files with 213 additions and 144 deletions.
4 changes: 3 additions & 1 deletion scripts/towncrier-draft-to-file.py
Original file line number Diff line number Diff line change
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
98 changes: 59 additions & 39 deletions testing/_py/test_local.py
Original file line number Diff line number Diff line change
@@ -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 (
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")
l2 = tmpdir.join(newfilename)
assert l2.read() == "foo"
assert l2.read_text(encoding="utf-8") == "foo"

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
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def test_excinfo_no_sourcecode():

def test_excinfo_no_python_sourcecode(tmp_path: Path) -> None:
# 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")
loader = jinja2.FileSystemLoader(str(tmp_path))
Expand Down Expand Up @@ -451,7 +451,7 @@ def importasmod(source):
source = textwrap.dedent(source)
modpath = tmp_path.joinpath("mod.py")
tmp_path.joinpath("__init__.py").touch()
modpath.write_text(source)
modpath.write_text(source, encoding="utf-8")
importlib.invalidate_caches()
return import_path(modpath, root=tmp_path)

Expand Down Expand Up @@ -1023,7 +1023,7 @@ def 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)
repr = excinfo.getrepr()
repr.toterminal(tw_mock)
Expand Down
2 changes: 1 addition & 1 deletion testing/code/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def method(self):
"""
)
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)
s2 = Source(mod.A)
assert str(source).strip() == str(s2).strip()
Expand Down
Loading

0 comments on commit 661b938

Please sign in to comment.