1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
import re
import sys
import pytest
import logbook
from .utils import capturing_stderr_context
__file_without_pyc__ = __file__
if __file_without_pyc__.endswith(".pyc"):
__file_without_pyc__ = __file_without_pyc__[:-1]
def test_handler_exception(activation_strategy, logger):
class ErroringHandler(logbook.TestHandler):
def emit(self, record):
raise RuntimeError("something bad happened")
with capturing_stderr_context() as stderr:
with activation_strategy(ErroringHandler()):
logger.warn("I warn you.")
assert "something bad happened" in stderr.getvalue()
assert "I warn you" not in stderr.getvalue()
def test_formatting_exception():
def make_record():
return logbook.LogRecord(
"Test Logger",
logbook.WARNING,
"Hello {foo:invalid}",
kwargs={"foo": 42},
frame=sys._getframe(),
)
record = make_record()
with pytest.raises(TypeError) as caught:
record.message
errormsg = str(caught.value)
assert re.search(
"Could not format message with provided arguments: Invalid "
"(?:format specifier)|(?:conversion specification)|(?:format spec)",
errormsg,
re.M | re.S,
)
assert "msg='Hello {foo:invalid}'" in errormsg
assert "args=()" in errormsg
assert "kwargs={'foo': 42}" in errormsg
assert re.search(
r"Happened in file .*%s, line \d+" % re.escape(__file_without_pyc__),
errormsg,
re.M | re.S,
)
|