Source code for reactpy.testing.logs
from __future__ import annotations
import logging
import re
from collections.abc import Iterator
from contextlib import contextmanager
from traceback import format_exception
from typing import Any, NoReturn
from reactpy.logging import ROOT_LOGGER
[docs]class LogAssertionError(AssertionError):
"""An assertion error raised in relation to log messages."""
[docs]@contextmanager
def assert_reactpy_did_log(
match_message: str = "",
error_type: type[Exception] | None = None,
match_error: str = "",
) -> Iterator[None]:
"""Assert that ReactPy produced a log matching the described message or error.
Args:
match_message: Must match a logged message.
error_type: Checks the type of logged exceptions.
match_error: Must match an error message.
"""
message_pattern = re.compile(match_message)
error_pattern = re.compile(match_error)
with capture_reactpy_logs() as log_records:
try:
yield None
except Exception:
raise
else:
for record in list(log_records):
if (
# record message matches
message_pattern.findall(record.getMessage())
# error type matches
and (
error_type is None
or (
record.exc_info is not None
and record.exc_info[0] is not None
and issubclass(record.exc_info[0], error_type)
)
)
# error message pattern matches
and (
not match_error
or (
record.exc_info is not None