"""Test error handlers.""" import logging from typing import Any import pytest from pytest import LogCaptureFixture from typer import Exit from backfill.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( "We are the priests of the Temples of Syrinx. Our great computers fill the hollowed halls" # noqa: E501 ) 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 "Temples of Syrinx" 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)