diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
commit | f8363b456f1ab31ee56abad579b215af195093d5 (patch) | |
tree | b1500c675c2e0a55fb75721a854e1510acf7c862 /tests/test_logging.py | |
parent | Initial commit. (diff) | |
download | rich-f8363b456f1ab31ee56abad579b215af195093d5.tar.xz rich-f8363b456f1ab31ee56abad579b215af195093d5.zip |
Adding upstream version 9.11.0.upstream/9.11.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_logging.py')
-rw-r--r-- | tests/test_logging.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/test_logging.py b/tests/test_logging.py new file mode 100644 index 0000000..566c685 --- /dev/null +++ b/tests/test_logging.py @@ -0,0 +1,80 @@ +import io +import sys +import os +import logging +import pytest + +from rich.console import Console +from rich.logging import RichHandler + +handler = RichHandler( + console=Console( + file=io.StringIO(), force_terminal=True, width=80, color_system="truecolor" + ), + enable_link_path=False, +) +logging.basicConfig( + level="NOTSET", format="%(message)s", datefmt="[DATE]", handlers=[handler] +) +log = logging.getLogger("rich") + + +skip_win = pytest.mark.skipif( + os.name == "nt", + reason="rendered differently on windows", +) + + +@skip_win +def test_exception(): + console = Console( + file=io.StringIO(), force_terminal=True, width=140, color_system="truecolor" + ) + handler_with_tracebacks = RichHandler( + console=console, enable_link_path=False, rich_tracebacks=True + ) + log.addHandler(handler_with_tracebacks) + + try: + 1 / 0 + except ZeroDivisionError: + log.exception("message") + + render = handler_with_tracebacks.console.file.getvalue() + print(render) + + assert "ZeroDivisionError" in render + assert "message" in render + assert "division by zero" in render + + +def test_exception_with_extra_lines(): + console = Console( + file=io.StringIO(), force_terminal=True, width=140, color_system="truecolor" + ) + handler_extra_lines = RichHandler( + console=console, + enable_link_path=False, + markup=True, + rich_tracebacks=True, + tracebacks_extra_lines=5, + ) + log.addHandler(handler_extra_lines) + + try: + 1 / 0 + except ZeroDivisionError: + log.exception("message") + + render = handler_extra_lines.console.file.getvalue() + print(render) + + assert "ZeroDivisionError" in render + assert "message" in render + assert "division by zero" in render + + +if __name__ == "__main__": + render = make_log() + print(render) + print(repr(render)) |