Skip to content

Commit

Permalink
Fix encoding warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nondescryptid authored and Zac-HD committed Jun 20, 2023
1 parent 8528052 commit 593d1b1
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 112 deletions.
10 changes: 5 additions & 5 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def test_conftest_printing_shows_if_error(self, pytester: Pytester) -> None:
def test_issue109_sibling_conftests_not_loaded(self, pytester: Pytester) -> None:
sub1 = pytester.mkdir("sub1")
sub2 = pytester.mkdir("sub2")
sub1.joinpath("conftest.py").write_text("assert 0")
sub1.joinpath("conftest.py").write_text("assert 0", encoding="utf-8")
result = pytester.runpytest(sub2)
assert result.ret == ExitCode.NO_TESTS_COLLECTED
sub2.joinpath("__init__.py").touch()
Expand Down Expand Up @@ -467,7 +467,7 @@ def test_plugins_given_as_strings(
assert "invalid" in str(excinfo.value)

p = pytester.path.joinpath("test_test_plugins_given_as_strings.py")
p.write_text("def test_foo(): pass")
p.write_text("def test_foo(): pass", encoding="utf-8")
mod = types.ModuleType("myplugin")
monkeypatch.setitem(sys.modules, "myplugin", mod)
assert pytest.main(args=[str(pytester.path)], plugins=["myplugin"]) == 0
Expand Down Expand Up @@ -587,7 +587,7 @@ def pytest_addoption(self, parser):
def test_pyargs_importerror(self, pytester: Pytester, monkeypatch) -> None:
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
path = pytester.mkpydir("tpkg")
path.joinpath("test_hello.py").write_text("raise ImportError")
path.joinpath("test_hello.py").write_text("raise ImportError", encoding="utf-8")

result = pytester.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True)
assert result.ret != 0
Expand All @@ -597,10 +597,10 @@ def test_pyargs_importerror(self, pytester: Pytester, monkeypatch) -> None:
def test_pyargs_only_imported_once(self, pytester: Pytester) -> None:
pkg = pytester.mkpydir("foo")
pkg.joinpath("test_foo.py").write_text(
"print('hello from test_foo')\ndef test(): pass"
"print('hello from test_foo')\ndef test(): pass", encoding="utf-8"
)
pkg.joinpath("conftest.py").write_text(
"def pytest_configure(config): print('configuring')"
"def pytest_configure(config): print('configuring')", encoding="utf-8"
)

result = pytester.runpytest(
Expand Down
94 changes: 62 additions & 32 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ def spam(request):
def spam():
return 'spam'
"""
)
),
encoding="utf-8",
)
testfile = subdir.joinpath("test_spam.py")
testfile.write_text(
Expand All @@ -296,7 +297,8 @@ def spam():
def test_spam(spam):
assert spam == "spam"
"""
)
),
encoding="utf-8",
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
Expand Down Expand Up @@ -359,7 +361,8 @@ def spam():
def spam(request):
return request.param
"""
)
),
encoding="utf-8",
)
testfile = subdir.joinpath("test_spam.py")
testfile.write_text(
Expand All @@ -371,7 +374,8 @@ def test_spam(spam):
assert spam == params['spam']
params['spam'] += 1
"""
)
),
encoding="utf-8",
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*3 passed*"])
Expand Down Expand Up @@ -403,7 +407,8 @@ def spam():
def spam(request):
return request.param
"""
)
),
encoding="utf-8",
)
testfile = subdir.joinpath("test_spam.py")
testfile.write_text(
Expand All @@ -415,7 +420,8 @@ def test_spam(spam):
assert spam == params['spam']
params['spam'] += 1
"""
)
),
encoding="utf-8",
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*3 passed*"])
Expand Down Expand Up @@ -1037,10 +1043,11 @@ def test_fixtures_sub_subdir_normalize_sep(self, pytester: Pytester) -> None:
def arg1():
pass
"""
)
),
encoding="utf-8",
)
p = b.joinpath("test_module.py")
p.write_text("def test_func(arg1): pass")
p.write_text("def test_func(arg1): pass", encoding="utf-8")
result = pytester.runpytest(p, "--fixtures")
assert result.ret == 0
result.stdout.fnmatch_lines(
Expand Down Expand Up @@ -1617,15 +1624,17 @@ def test_parsefactories_relative_node_ids(
def one():
return 1
"""
)
),
encoding="utf-8",
)
package.joinpath("test_x.py").write_text(
textwrap.dedent(
"""\
def test_x(one):
assert one == 1
"""
)
),
encoding="utf-8",
)
sub = package.joinpath("sub")
sub.mkdir()
Expand All @@ -1638,15 +1647,17 @@ def test_x(one):
def one():
return 2
"""
)
),
encoding="utf-8",
)
sub.joinpath("test_y.py").write_text(
textwrap.dedent(
"""\
def test_x(one):
assert one == 2
"""
)
),
encoding="utf-8",
)
reprec = pytester.inline_run()
reprec.assertoutcome(passed=2)
Expand All @@ -1671,7 +1682,8 @@ def setup_module():
def teardown_module():
values[:] = []
"""
)
),
encoding="utf-8",
)
package.joinpath("test_x.py").write_text(
textwrap.dedent(
Expand All @@ -1680,7 +1692,8 @@ def teardown_module():
def test_x():
assert values == ["package"]
"""
)
),
encoding="utf-8",
)
package = pytester.mkdir("package2")
package.joinpath("__init__.py").write_text(
Expand All @@ -1692,7 +1705,8 @@ def setup_module():
def teardown_module():
values[:] = []
"""
)
),
encoding="utf-8",
)
package.joinpath("test_x.py").write_text(
textwrap.dedent(
Expand All @@ -1701,7 +1715,8 @@ def teardown_module():
def test_x():
assert values == ["package2"]
"""
)
),
encoding="utf-8",
)
reprec = pytester.inline_run()
reprec.assertoutcome(passed=2)
Expand All @@ -1714,7 +1729,7 @@ def test_package_fixture_complex(self, pytester: Pytester) -> None:
)
pytester.syspathinsert(pytester.path.name)
package = pytester.mkdir("package")
package.joinpath("__init__.py").write_text("")
package.joinpath("__init__.py").write_text("", encoding="utf-8")
package.joinpath("conftest.py").write_text(
textwrap.dedent(
"""\
Expand All @@ -1731,7 +1746,8 @@ def two():
yield values
values.pop()
"""
)
),
encoding="utf-8",
)
package.joinpath("test_x.py").write_text(
textwrap.dedent(
Expand All @@ -1742,7 +1758,8 @@ def test_package_autouse():
def test_package(one):
assert values == ["package-auto", "package"]
"""
)
),
encoding="utf-8",
)
reprec = pytester.inline_run()
reprec.assertoutcome(passed=2)
Expand Down Expand Up @@ -1892,8 +1909,12 @@ def hello():
"""
)
conftest.rename(a.joinpath(conftest.name))
a.joinpath("test_something.py").write_text("def test_func(): pass")
b.joinpath("test_otherthing.py").write_text("def test_func(): pass")
a.joinpath("test_something.py").write_text(
"def test_func(): pass", encoding="utf-8"
)
b.joinpath("test_otherthing.py").write_text(
"def test_func(): pass", encoding="utf-8"
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
"""
Expand Down Expand Up @@ -1939,7 +1960,8 @@ def app():
import sys
sys._myapp = "hello"
"""
)
),
encoding="utf-8",
)
sub = pkgdir.joinpath("tests")
sub.mkdir()
Expand All @@ -1952,7 +1974,8 @@ def app():
def test_app():
assert sys._myapp == "hello"
"""
)
),
encoding="utf-8",
)
reprec = pytester.inline_run("-s")
reprec.assertoutcome(passed=1)
Expand Down Expand Up @@ -2882,7 +2905,7 @@ def test_fixture_finalizer(self, pytester: Pytester) -> None:
def browser(request):
def finalize():
sys.stdout.write_text('Finalized')
sys.stdout.write_text('Finalized', encoding='utf-8')
request.addfinalizer(finalize)
return {}
"""
Expand All @@ -2900,7 +2923,8 @@ def browser(browser):
def test_browser(browser):
assert browser['visited'] is True
"""
)
),
encoding="utf-8",
)
reprec = pytester.runpytest("-s")
for test in ["test_browser"]:
Expand Down Expand Up @@ -3855,7 +3879,8 @@ def test_non_relative_path(self, pytester: Pytester) -> None:
def fix_with_param(request):
return request.param
"""
)
),
encoding="utf-8",
)

testfile = tests_dir.joinpath("test_foos.py")
Expand All @@ -3867,7 +3892,8 @@ def fix_with_param(request):
def test_foo(request):
request.getfixturevalue('fix_with_param')
"""
)
),
encoding="utf-8",
)

os.chdir(tests_dir)
Expand Down Expand Up @@ -4196,7 +4222,7 @@ def test_multiple_packages(self, pytester: Pytester) -> None:
└── test_2.py
"""
root = pytester.mkdir("root")
root.joinpath("__init__.py").write_text("values = []")
root.joinpath("__init__.py").write_text("values = []", encoding="utf-8")
sub1 = root.joinpath("sub1")
sub1.mkdir()
sub1.joinpath("__init__.py").touch()
Expand All @@ -4211,7 +4237,8 @@ def fix():
yield values
assert values.pop() == "pre-sub1"
"""
)
),
encoding="utf-8",
)
sub1.joinpath("test_1.py").write_text(
textwrap.dedent(
Expand All @@ -4220,7 +4247,8 @@ def fix():
def test_1(fix):
assert values == ["pre-sub1"]
"""
)
),
encoding="utf-8",
)
sub2 = root.joinpath("sub2")
sub2.mkdir()
Expand All @@ -4236,7 +4264,8 @@ def fix():
yield values
assert values.pop() == "pre-sub2"
"""
)
),
encoding="utf-8",
)
sub2.joinpath("test_2.py").write_text(
textwrap.dedent(
Expand All @@ -4245,7 +4274,8 @@ def fix():
def test_2(fix):
assert values == ["pre-sub2"]
"""
)
),
encoding="utf-8",
)
reprec = pytester.inline_run()
reprec.assertoutcome(passed=2)
Expand Down
14 changes: 10 additions & 4 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,18 +1443,24 @@ def test_generate_tests_only_done_in_subdir(self, pytester: Pytester) -> None:
def pytest_generate_tests(metafunc):
assert metafunc.function.__name__ == "test_1"
"""
)
),
encoding="utf-8",
)
sub2.joinpath("conftest.py").write_text(
textwrap.dedent(
"""\
def pytest_generate_tests(metafunc):
assert metafunc.function.__name__ == "test_2"
"""
)
),
encoding="utf-8",
)
sub1.joinpath("test_in_sub1.py").write_text(
"def test_1(): pass", encoding="utf-8"
)
sub2.joinpath("test_in_sub2.py").write_text(
"def test_2(): 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")
result = pytester.runpytest("--keep-duplicates", "-v", "-s", sub1, sub2, sub1)
result.assert_outcomes(passed=3)

Expand Down

0 comments on commit 593d1b1

Please sign in to comment.