summaryrefslogtreecommitdiff
path: root/tests/test_execfile.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_execfile.py')
-rw-r--r--tests/test_execfile.py56
1 files changed, 29 insertions, 27 deletions
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index e1db7bb5..229d8d95 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -12,6 +12,8 @@ import py_compile
import re
import sys
+from typing import Any, Generator
+
import pytest
from coverage.exceptions import NoCode, NoSource, _ExceptionDuringRun
@@ -27,12 +29,12 @@ class RunFileTest(CoverageTest):
"""Test cases for `run_python_file`."""
@pytest.fixture(autouse=True)
- def clean_up(self):
+ def clean_up(self) -> Generator[None, None, None]:
"""These tests all run in-process. Clean up global changes."""
yield
sys.excepthook = sys.__excepthook__
- def test_run_python_file(self):
+ def test_run_python_file(self) -> None:
run_python_file([TRY_EXECFILE, "arg1", "arg2"])
mod_globs = json.loads(self.stdout())
@@ -58,7 +60,7 @@ class RunFileTest(CoverageTest):
# __builtins__ should have the right values, like open().
assert mod_globs['__builtins__.has_open'] is True
- def test_no_extra_file(self):
+ def test_no_extra_file(self) -> None:
# Make sure that running a file doesn't create an extra compiled file.
self.make_file("xxx", """\
desc = "a non-.py file!"
@@ -68,7 +70,7 @@ class RunFileTest(CoverageTest):
run_python_file(["xxx"])
assert os.listdir(".") == ["xxx"]
- def test_universal_newlines(self):
+ def test_universal_newlines(self) -> None:
# Make sure we can read any sort of line ending.
pylines = """# try newlines|print('Hello, world!')|""".split('|')
for nl in ('\n', '\r\n', '\r'):
@@ -77,7 +79,7 @@ class RunFileTest(CoverageTest):
run_python_file(['nl.py'])
assert self.stdout() == "Hello, world!\n"*3
- def test_missing_final_newline(self):
+ def test_missing_final_newline(self) -> None:
# Make sure we can deal with a Python file with no final newline.
self.make_file("abrupt.py", """\
if 1:
@@ -90,25 +92,25 @@ class RunFileTest(CoverageTest):
run_python_file(["abrupt.py"])
assert self.stdout() == "a is 1\n"
- def test_no_such_file(self):
+ def test_no_such_file(self) -> None:
path = python_reported_file('xyzzy.py')
msg = re.escape(f"No file to run: '{path}'")
with pytest.raises(NoSource, match=msg):
run_python_file(["xyzzy.py"])
- def test_directory_with_main(self):
+ def test_directory_with_main(self) -> None:
self.make_file("with_main/__main__.py", """\
print("I am __main__")
""")
run_python_file(["with_main"])
assert self.stdout() == "I am __main__\n"
- def test_directory_without_main(self):
+ def test_directory_without_main(self) -> None:
self.make_file("without_main/__init__.py", "")
with pytest.raises(NoSource, match="Can't find '__main__' module in 'without_main'"):
run_python_file(["without_main"])
- def test_code_throws(self):
+ def test_code_throws(self) -> None:
self.make_file("throw.py", """\
class MyException(Exception):
pass
@@ -129,7 +131,7 @@ class RunFileTest(CoverageTest):
assert self.stdout() == "about to raise..\n"
assert self.stderr() == ""
- def test_code_exits(self):
+ def test_code_exits(self) -> None:
self.make_file("exit.py", """\
import sys
def f1():
@@ -148,7 +150,7 @@ class RunFileTest(CoverageTest):
assert self.stdout() == "about to exit..\n"
assert self.stderr() == ""
- def test_excepthook_exit(self):
+ def test_excepthook_exit(self) -> None:
self.make_file("excepthook_exit.py", """\
import sys
@@ -165,7 +167,7 @@ class RunFileTest(CoverageTest):
cov_out = self.stdout()
assert cov_out == "in excepthook\n"
- def test_excepthook_throw(self):
+ def test_excepthook_throw(self) -> None:
self.make_file("excepthook_throw.py", """\
import sys
@@ -193,7 +195,7 @@ class RunFileTest(CoverageTest):
class RunPycFileTest(CoverageTest):
"""Test cases for `run_python_file`."""
- def make_pyc(self, **kwargs):
+ def make_pyc(self, **kwargs: Any) -> str:
"""Create a .pyc file, and return the path to it."""
self.make_file("compiled.py", """\
def doit():
@@ -207,12 +209,12 @@ class RunPycFileTest(CoverageTest):
# Find the .pyc file!
return str(next(pathlib.Path(".").rglob("compiled*.pyc")))
- def test_running_pyc(self):
+ def test_running_pyc(self) -> None:
pycfile = self.make_pyc()
run_python_file([pycfile])
assert self.stdout() == "I am here!\n"
- def test_running_pyo(self):
+ def test_running_pyo(self) -> None:
pycfile = self.make_pyc()
pyofile = re.sub(r"[.]pyc$", ".pyo", pycfile)
assert pycfile != pyofile
@@ -220,7 +222,7 @@ class RunPycFileTest(CoverageTest):
run_python_file([pyofile])
assert self.stdout() == "I am here!\n"
- def test_running_pyc_from_wrong_python(self):
+ def test_running_pyc_from_wrong_python(self) -> None:
pycfile = self.make_pyc()
# Jam Python 2.1 magic number into the .pyc file.
@@ -234,18 +236,18 @@ class RunPycFileTest(CoverageTest):
# In some environments, the pycfile persists and pollutes another test.
os.remove(pycfile)
- def test_running_hashed_pyc(self):
+ def test_running_hashed_pyc(self) -> None:
pycfile = self.make_pyc(invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)
run_python_file([pycfile])
assert self.stdout() == "I am here!\n"
- def test_no_such_pyc_file(self):
+ def test_no_such_pyc_file(self) -> None:
path = python_reported_file('xyzzy.pyc')
msg = re.escape(f"No file to run: '{path}'")
with pytest.raises(NoCode, match=msg):
run_python_file(["xyzzy.pyc"])
- def test_running_py_from_binary(self):
+ def test_running_py_from_binary(self) -> None:
# Use make_file to get the bookkeeping. Ideally, it would
# be able to write binary files.
bf = self.make_file("binary")
@@ -266,43 +268,43 @@ class RunModuleTest(UsingModulesMixin, CoverageTest):
run_in_temp_dir = False
- def test_runmod1(self):
+ def test_runmod1(self) -> None:
run_python_module(["runmod1", "hello"])
out, err = self.stdouterr()
assert out == "runmod1: passed hello\n"
assert err == ""
- def test_runmod2(self):
+ def test_runmod2(self) -> None:
run_python_module(["pkg1.runmod2", "hello"])
out, err = self.stdouterr()
assert out == "pkg1.__init__: pkg1\nrunmod2: passed hello\n"
assert err == ""
- def test_runmod3(self):
+ def test_runmod3(self) -> None:
run_python_module(["pkg1.sub.runmod3", "hello"])
out, err = self.stdouterr()
assert out == "pkg1.__init__: pkg1\nrunmod3: passed hello\n"
assert err == ""
- def test_pkg1_main(self):
+ def test_pkg1_main(self) -> None:
run_python_module(["pkg1", "hello"])
out, err = self.stdouterr()
assert out == "pkg1.__init__: pkg1\npkg1.__main__: passed hello\n"
assert err == ""
- def test_pkg1_sub_main(self):
+ def test_pkg1_sub_main(self) -> None:
run_python_module(["pkg1.sub", "hello"])
out, err = self.stdouterr()
assert out == "pkg1.__init__: pkg1\npkg1.sub.__main__: passed hello\n"
assert err == ""
- def test_pkg1_init(self):
+ def test_pkg1_init(self) -> None:
run_python_module(["pkg1.__init__", "wut?"])
out, err = self.stdouterr()
assert out == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n"
assert err == ""
- def test_no_such_module(self):
+ def test_no_such_module(self) -> None:
with pytest.raises(NoSource, match="No module named '?i_dont_exist'?"):
run_python_module(["i_dont_exist"])
with pytest.raises(NoSource, match="No module named '?i'?"):
@@ -310,6 +312,6 @@ class RunModuleTest(UsingModulesMixin, CoverageTest):
with pytest.raises(NoSource, match="No module named '?i'?"):
run_python_module(["i.dont.exist"])
- def test_no_main(self):
+ def test_no_main(self) -> None:
with pytest.raises(NoSource):
run_python_module(["pkg2", "hi"])