Public Access
DEVX-153: feat: sync missing features from v0.49.x line to master
Co-authored-by: emil User <emil.simeonov@tutanota.com>
This commit was merged in pull request #267.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
"""Unit tests for devx.utils.ui."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
import devx.utils.ui as ui_mod
|
||||
from devx.utils.ui import _console_level, configure_ui, say
|
||||
|
||||
|
||||
class TestConsoleLevel:
|
||||
def test_default_is_info(self) -> None:
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
assert _console_level() == logging.INFO
|
||||
|
||||
def test_env_override(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("DEVX_LOG_LEVEL", "DEBUG")
|
||||
assert _console_level() == logging.DEBUG
|
||||
|
||||
def test_invalid_fallback(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("DEVX_LOG_LEVEL", "VERBOSE")
|
||||
assert _console_level() == logging.INFO
|
||||
|
||||
def test_custom_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
configure_ui(log_level_env_var="GRM_LOG_LEVEL")
|
||||
try:
|
||||
monkeypatch.setenv("GRM_LOG_LEVEL", "DEBUG")
|
||||
monkeypatch.delenv("DEVX_LOG_LEVEL", raising=False)
|
||||
assert _console_level() == logging.DEBUG
|
||||
finally:
|
||||
configure_ui()
|
||||
|
||||
|
||||
class TestSay:
|
||||
def test_echoes_to_console(self) -> None:
|
||||
with patch("devx.utils.ui.click.echo") as mock_echo:
|
||||
say("hello")
|
||||
mock_echo.assert_called_once_with("hello", err=False)
|
||||
|
||||
def test_logs_at_info_level(self) -> None:
|
||||
with (
|
||||
patch("devx.utils.ui.click.echo"),
|
||||
patch("devx.utils.ui.logging.getLogger") as mock_get_logger,
|
||||
):
|
||||
mock_logger = mock_get_logger.return_value
|
||||
say("hello")
|
||||
mock_logger.log.assert_called_once_with(logging.INFO, "hello")
|
||||
|
||||
def test_passes_level_and_err(self) -> None:
|
||||
with (
|
||||
patch("devx.utils.ui.click.echo") as mock_echo,
|
||||
patch("devx.utils.ui.logging.getLogger") as mock_get_logger,
|
||||
):
|
||||
mock_logger = mock_get_logger.return_value
|
||||
say("error msg", level=logging.ERROR, err=True)
|
||||
mock_echo.assert_called_once_with("error msg", err=True)
|
||||
mock_logger.log.assert_called_once_with(logging.ERROR, "error msg")
|
||||
|
||||
def test_suppresses_console_below_level(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("DEVX_LOG_LEVEL", "WARNING")
|
||||
with (
|
||||
patch("devx.utils.ui.click.echo") as mock_echo,
|
||||
patch("devx.utils.ui.logging.getLogger") as mock_get_logger,
|
||||
):
|
||||
mock_logger = mock_get_logger.return_value
|
||||
say("debug msg", level=logging.DEBUG)
|
||||
mock_echo.assert_not_called()
|
||||
mock_logger.log.assert_called_once_with(logging.DEBUG, "debug msg")
|
||||
|
||||
def test_color_applied(self) -> None:
|
||||
with (
|
||||
patch("devx.utils.ui.click.echo") as mock_echo,
|
||||
patch("devx.utils.ui.click.style") as mock_style,
|
||||
):
|
||||
mock_style.return_value = "styled-output"
|
||||
say("success", color="green")
|
||||
mock_style.assert_called_once_with("success", fg="green")
|
||||
mock_echo.assert_called_once_with("styled-output", err=False)
|
||||
|
||||
def test_custom_logger_name(self) -> None:
|
||||
configure_ui(logger_name="grm")
|
||||
try:
|
||||
with (
|
||||
patch("devx.utils.ui.click.echo"),
|
||||
patch("devx.utils.ui.logging.getLogger") as mock_get_logger,
|
||||
):
|
||||
say("hello")
|
||||
mock_get_logger.assert_called_with("grm")
|
||||
finally:
|
||||
configure_ui()
|
||||
|
||||
|
||||
class TestConfigureUi:
|
||||
def test_reset_to_defaults(self) -> None:
|
||||
configure_ui(log_level_env_var="GRM_LOG_LEVEL", logger_name="grm")
|
||||
configure_ui()
|
||||
assert ui_mod._LOG_LEVEL_ENV_VAR == "DEVX_LOG_LEVEL"
|
||||
assert ui_mod._LOGGER_NAME == "devx"
|
||||
Reference in New Issue
Block a user