diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 08:36:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 08:36:50 +0000 |
commit | 7763cc454d686d51bf2e0ccc1f2ccf7d62a0d625 (patch) | |
tree | f36d2006dd01bd01a069956741d831d9d5633377 /tests/units/test_logger.py | |
parent | Adding debian version 0.13.0-1. (diff) | |
download | anta-7763cc454d686d51bf2e0ccc1f2ccf7d62a0d625.tar.xz anta-7763cc454d686d51bf2e0ccc1f2ccf7d62a0d625.zip |
Merging upstream version 0.14.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | tests/units/test_logger.py | 84 |
1 files changed, 60 insertions, 24 deletions
diff --git a/tests/units/test_logger.py b/tests/units/test_logger.py index 6e1e5b4..d9b7c76 100644 --- a/tests/units/test_logger.py +++ b/tests/units/test_logger.py @@ -1,53 +1,65 @@ # Copyright (c) 2023-2024 Arista Networks, Inc. # Use of this source code is governed by the Apache License 2.0 # that can be found in the LICENSE file. -""" -Tests for anta.logger -""" +"""Tests for anta.logger.""" + from __future__ import annotations import logging -from typing import TYPE_CHECKING from unittest.mock import patch import pytest -from anta.logger import anta_log_exception - -if TYPE_CHECKING: - from pytest import LogCaptureFixture +from anta.logger import anta_log_exception, exc_to_str, tb_to_str @pytest.mark.parametrize( - "exception, message, calling_logger, __DEBUG__value, expected_message", + ("exception", "message", "calling_logger", "debug_value", "expected_message"), [ - pytest.param(ValueError("exception message"), None, None, False, "ValueError (exception message)", id="exception only"), - pytest.param(ValueError("exception message"), "custom message", None, False, "custom message\nValueError (exception message)", id="custom message"), + pytest.param( + ValueError("exception message"), + None, + None, + False, + "ValueError: exception message", + id="exception only", + ), + pytest.param( + ValueError("exception message"), + "custom message", + None, + False, + "custom message\nValueError: exception message", + id="custom message", + ), pytest.param( ValueError("exception message"), "custom logger", logging.getLogger("custom"), False, - "custom logger\nValueError (exception message)", + "custom logger\nValueError: exception message", id="custom logger", ), pytest.param( - ValueError("exception message"), "Use with custom message", None, True, "Use with custom message\nValueError (exception message)", id="__DEBUG__ on" + ValueError("exception message"), + "Use with custom message", + None, + True, + "Use with custom message\nValueError: exception message", + id="__DEBUG__ on", ), ], ) def test_anta_log_exception( - caplog: LogCaptureFixture, + caplog: pytest.LogCaptureFixture, exception: Exception, message: str | None, calling_logger: logging.Logger | None, - __DEBUG__value: bool, + debug_value: bool, expected_message: str, ) -> None: - """ - Test anta_log_exception - """ - + # pylint: disable=too-many-arguments + """Test anta_log_exception.""" if calling_logger is not None: # https://github.com/pytest-dev/pytest/issues/3697 calling_logger.propagate = True @@ -57,12 +69,12 @@ def test_anta_log_exception( # Need to raise to trigger nice stacktrace for __DEBUG__ == True try: raise exception - except ValueError as e: - with patch("anta.logger.__DEBUG__", __DEBUG__value): - anta_log_exception(e, message=message, calling_logger=calling_logger) + except ValueError as exc: + with patch("anta.logger.__DEBUG__", new=debug_value): + anta_log_exception(exc, message=message, calling_logger=calling_logger) # Two log captured - if __DEBUG__value: + if debug_value: assert len(caplog.record_tuples) == 2 else: assert len(caplog.record_tuples) == 1 @@ -76,5 +88,29 @@ def test_anta_log_exception( assert level == logging.CRITICAL assert message == expected_message # the only place where we can see the stracktrace is in the capture.text - if __DEBUG__value is True: + if debug_value: assert "Traceback" in caplog.text + + +def my_raising_function(exception: Exception) -> None: + """Raise Exception.""" + raise exception + + +@pytest.mark.parametrize( + ("exception", "expected_output"), + [(ValueError("test"), "ValueError: test"), (ValueError(), "ValueError")], +) +def test_exc_to_str(exception: Exception, expected_output: str) -> None: + """Test exc_to_str.""" + assert exc_to_str(exception) == expected_output + + +def test_tb_to_str() -> None: + """Test tb_to_str.""" + try: + my_raising_function(ValueError("test")) + except ValueError as exc: + output = tb_to_str(exc) + assert "Traceback" in output + assert 'my_raising_function(ValueError("test"))' in output |