Files
pytest2/testing/test_unraisableexception.py
Pierre Sassoulas 4588653b24 Migrate from autoflake, black, isort, pyupgrade, flake8 and pydocstyle, to ruff
ruff is faster and handle everything we had prior.

isort configuration done based on the indication from
https://github.com/astral-sh/ruff/issues/4670, previousely based on
reorder-python-import (#11896)

flake8-docstrings was a wrapper around pydocstyle (now archived) that
explicitly asks to use ruff in https://github.com/PyCQA/pydocstyle/pull/658.

flake8-typing-import is useful mainly for project that support python 3.7
and the one useful check will be implemented in https://github.com/astral-sh/ruff/issues/2302

We need to keep blacken-doc because ruff does not handle detection
of python code inside .md and .rst. The direct link to the repo is
now used to avoid a redirection.

Manual fixes:
- Lines that became too long
- % formatting that was not done automatically
- type: ignore that were moved around
- noqa of hard to fix issues (UP031 generally)
- fmt: off and fmt: on that is not really identical
  between black and ruff
- autofix re-order in pre-commit from faster to slower

Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-02-02 09:27:00 +01:00

137 lines
4.0 KiB
Python

import sys
from _pytest.pytester import Pytester
import pytest
PYPY = hasattr(sys, "pypy_version_info")
@pytest.mark.skipif(PYPY, reason="garbage-collection differences make this flaky")
@pytest.mark.filterwarnings("default::pytest.PytestUnraisableExceptionWarning")
def test_unraisable(pytester: Pytester) -> None:
pytester.makepyfile(
test_it="""
class BrokenDel:
def __del__(self):
raise ValueError("del is broken")
def test_it():
obj = BrokenDel()
del obj
def test_2(): pass
"""
)
result = pytester.runpytest()
assert result.ret == 0
assert result.parseoutcomes() == {"passed": 2, "warnings": 1}
result.stdout.fnmatch_lines(
[
"*= warnings summary =*",
"test_it.py::test_it",
" * PytestUnraisableExceptionWarning: Exception ignored in: <function BrokenDel.__del__ at *>",
" ",
" Traceback (most recent call last):",
" ValueError: del is broken",
" ",
" warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))",
]
)
@pytest.mark.skipif(PYPY, reason="garbage-collection differences make this flaky")
@pytest.mark.filterwarnings("default::pytest.PytestUnraisableExceptionWarning")
def test_unraisable_in_setup(pytester: Pytester) -> None:
pytester.makepyfile(
test_it="""
import pytest
class BrokenDel:
def __del__(self):
raise ValueError("del is broken")
@pytest.fixture
def broken_del():
obj = BrokenDel()
del obj
def test_it(broken_del): pass
def test_2(): pass
"""
)
result = pytester.runpytest()
assert result.ret == 0
assert result.parseoutcomes() == {"passed": 2, "warnings": 1}
result.stdout.fnmatch_lines(
[
"*= warnings summary =*",
"test_it.py::test_it",
" * PytestUnraisableExceptionWarning: Exception ignored in: <function BrokenDel.__del__ at *>",
" ",
" Traceback (most recent call last):",
" ValueError: del is broken",
" ",
" warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))",
]
)
@pytest.mark.skipif(PYPY, reason="garbage-collection differences make this flaky")
@pytest.mark.filterwarnings("default::pytest.PytestUnraisableExceptionWarning")
def test_unraisable_in_teardown(pytester: Pytester) -> None:
pytester.makepyfile(
test_it="""
import pytest
class BrokenDel:
def __del__(self):
raise ValueError("del is broken")
@pytest.fixture
def broken_del():
yield
obj = BrokenDel()
del obj
def test_it(broken_del): pass
def test_2(): pass
"""
)
result = pytester.runpytest()
assert result.ret == 0
assert result.parseoutcomes() == {"passed": 2, "warnings": 1}
result.stdout.fnmatch_lines(
[
"*= warnings summary =*",
"test_it.py::test_it",
" * PytestUnraisableExceptionWarning: Exception ignored in: <function BrokenDel.__del__ at *>",
" ",
" Traceback (most recent call last):",
" ValueError: del is broken",
" ",
" warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))",
]
)
@pytest.mark.filterwarnings("error::pytest.PytestUnraisableExceptionWarning")
def test_unraisable_warning_error(pytester: Pytester) -> None:
pytester.makepyfile(
test_it=f"""
class BrokenDel:
def __del__(self) -> None:
raise ValueError("del is broken")
def test_it() -> None:
obj = BrokenDel()
del obj
{"import gc; gc.collect()" * PYPY}
def test_2(): pass
"""
)
result = pytester.runpytest()
assert result.ret == pytest.ExitCode.TESTS_FAILED
assert result.parseoutcomes() == {"passed": 1, "failed": 1}