| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for anta.logger.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import logging |
| 9 |
from pathlib import Path |
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from anta.logger import Log, LogLevel, _get_file_handler, _get_rich_handler, anta_log_exception, exc_to_str, setup_logging, tb_to_str |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize( |
| 18 |
("level", "path", "debug_value"), |
| 19 |
[ |
| 20 |
pytest.param(Log.INFO, None, False, id="INFO no file"), |
| 21 |
pytest.param(Log.DEBUG, None, False, id="DEBUG no file"), |
| 22 |
pytest.param(Log.INFO, Path("/tmp/file.log"), False, id="INFO file"), |
| 23 |
pytest.param(Log.DEBUG, Path("/tmp/file.log"), False, id="DEBUG file"), |
| 24 |
pytest.param(Log.INFO, None, True, id="INFO no file __DEBUG__ set"), |
| 25 |
pytest.param(Log.DEBUG, None, True, id="INFO no file __DEBUG__ set"), |
| 26 |
], |
| 27 |
) |
| 28 |
def test_setup_logging(level: LogLevel, path: Path | None, debug_value: bool) -> None: |
| 29 |
"""Test setup_logging.""" |
| 30 |
|
| 31 |
root = logging.getLogger() |
| 32 |
if root.hasHandlers(): |
| 33 |
root.handlers = [] |
| 34 |
|
| 35 |
with patch("anta.logger.__DEBUG__", new=debug_value): |
| 36 |
setup_logging(level, path) |
| 37 |
|
| 38 |
rich_handler = _get_rich_handler(root) |
| 39 |
assert rich_handler is not None |
| 40 |
|
| 41 |
|
| 42 |
if debug_value: |
| 43 |
assert root.level == logging.DEBUG |
| 44 |
if path is not None: |
| 45 |
assert rich_handler.level == logging.INFO |
| 46 |
|
| 47 |
if path is not None: |
| 48 |
assert _get_file_handler(root, path) is not None |
| 49 |
expected_handlers = 2 |
| 50 |
else: |
| 51 |
expected_handlers = 1 |
| 52 |
assert len(root.handlers) == expected_handlers |
| 53 |
|
| 54 |
|
| 55 |
setup_logging(level, path) |
| 56 |
assert len(root.handlers) == expected_handlers |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize( |
| 60 |
("exception", "message", "calling_logger", "debug_value", "expected_message"), |
| 61 |
[ |
| 62 |
pytest.param( |
| 63 |
ValueError("exception message"), |
| 64 |
None, |
| 65 |
None, |
| 66 |
False, |
| 67 |
"ValueError: exception message", |
| 68 |
id="exception only", |
| 69 |
), |
| 70 |
pytest.param( |
| 71 |
ValueError("exception message"), |
| 72 |
"custom message", |
| 73 |
None, |
| 74 |
False, |
| 75 |
"custom message\nValueError: exception message", |
| 76 |
id="custom message", |
| 77 |
), |
| 78 |
pytest.param( |
| 79 |
ValueError("exception message"), |
| 80 |
"custom logger", |
| 81 |
logging.getLogger("custom"), |
| 82 |
False, |
| 83 |
"custom logger\nValueError: exception message", |
| 84 |
id="custom logger", |
| 85 |
), |
| 86 |
pytest.param( |
| 87 |
ValueError("exception message"), |
| 88 |
"Use with custom message", |
| 89 |
None, |
| 90 |
True, |
| 91 |
"Use with custom message\nValueError: exception message", |
| 92 |
id="__DEBUG__ on", |
| 93 |
), |
| 94 |
], |
| 95 |
) |
| 96 |
def test_anta_log_exception( |
| 97 |
caplog: pytest.LogCaptureFixture, |
| 98 |
exception: Exception, |
| 99 |
message: str | None, |
| 100 |
calling_logger: logging.Logger | None, |
| 101 |
debug_value: bool, |
| 102 |
expected_message: str, |
| 103 |
) -> None: |
| 104 |
"""Test anta_log_exception.""" |
| 105 |
if calling_logger is not None: |
| 106 |
|
| 107 |
calling_logger.propagate = True |
| 108 |
caplog.set_level(logging.ERROR, logger=calling_logger.name) |
| 109 |
else: |
| 110 |
caplog.set_level(logging.ERROR) |
| 111 |
|
| 112 |
try: |
| 113 |
raise exception |
| 114 |
except ValueError as exc: |
| 115 |
with patch("anta.logger.__DEBUG__", new=debug_value): |
| 116 |
anta_log_exception(exc, message=message, calling_logger=calling_logger) |
| 117 |
|
| 118 |
|
| 119 |
if debug_value: |
| 120 |
assert len(caplog.record_tuples) == 2 |
| 121 |
else: |
| 122 |
assert len(caplog.record_tuples) == 1 |
| 123 |
logger, level, message = caplog.record_tuples[0] |
| 124 |
|
| 125 |
if calling_logger is not None: |
| 126 |
assert calling_logger.name == logger |
| 127 |
else: |
| 128 |
assert logger == "anta.logger" |
| 129 |
|
| 130 |
assert level == logging.CRITICAL |
| 131 |
assert message == expected_message |
| 132 |
|
| 133 |
if debug_value: |
| 134 |
assert "Traceback" in caplog.text |
| 135 |
|
| 136 |
|
| 137 |
def my_raising_function(exception: Exception) -> None: |
| 138 |
"""Raise Exception.""" |
| 139 |
raise exception |
| 140 |
|
| 141 |
|
| 142 |
@pytest.mark.parametrize( |
| 143 |
("exception", "expected_output"), |
| 144 |
[(ValueError("test"), "ValueError: test"), (ValueError(), "ValueError")], |
| 145 |
) |
| 146 |
def test_exc_to_str(exception: Exception, expected_output: str) -> None: |
| 147 |
"""Test exc_to_str.""" |
| 148 |
assert exc_to_str(exception) == expected_output |
| 149 |
|
| 150 |
|
| 151 |
def test_tb_to_str() -> None: |
| 152 |
"""Test tb_to_str.""" |
| 153 |
try: |
| 154 |
my_raising_function(ValueError("test")) |
| 155 |
except ValueError as exc: |
| 156 |
output = tb_to_str(exc) |
| 157 |
assert "Traceback" in output |
| 158 |
assert 'my_raising_function(ValueError("test"))' in output |