"""Test error handlers.""" import logging from typing import Any import pytest from pytest import LogCaptureFixture from typer import Exit from m2mconfig.error_handlers import handle_uncaught_errors def test_handle_uncaught_errors__with_error(caplog: LogCaptureFixture) -> None: """Test the uncaught exception handler when an error occurs.""" caplog.set_level(logging.ERROR) @handle_uncaught_errors def myfunc(*args: Any, **kwargs: Any) -> None: raise RuntimeError( "A modern-day warrior. Mean, mean stride, Today's Tom Sawyer, Mean, mean pride" ) with pytest.raises(Exit) as excinfo: myfunc(1) assert excinfo.value.exit_code == 1 # Verify that the function name is logged. assert "myfunc" in caplog.text # Verify that the handle_uncaught_errors() logger message is logged. assert "Unhandled error in" in caplog.text # Verify that the exception text is logged. assert "Tom Sawyer" in caplog.text def test_handle_uncaught_errors__no_error(caplog: LogCaptureFixture) -> None: """Test the uncaught exception handler when an error does not occur.""" caplog.set_level(logging.ERROR) @handle_uncaught_errors def myfunc(*args: Any, **kwargs: Any) -> int: return 1 assert myfunc(1)