Public Access
The v0.49.x tag line diverged from origin/master, leaving many features only accessible via tags but not on the master branch. New modules: - ci/cancel_superseded_runs.py — cancel superseded CI runs - ci/check_workflow_artifact_deps.py — validate artifact deps - ci/check_workflow_tofu_init.py — validate tofu init steps - tools/check_alert_rules.py — validate Prometheus alert rules - tools/check_ansible_set_fact_to_json.py — lint set_fact usage - tools/check_docker_init.py — validate Docker init scripts - utils/jinja.py — Jinja2 template utilities - utils/ui.py — UI/console utilities Modified modules: - distribute_molecule.py: add --include-roles/--exclude-roles - utils/api.py: add container.credentials for private registry auth - install_tools.py: retry ansible-galaxy on transient timeouts - setup_image.py: skip dep resolution with --no-deps - cli.py: register new commands - i18n.py: add new translation keys Also removes accidentally committed .vale/styles/Google/ files. Test results: 2195 passed, 100% coverage. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
102 lines
3.7 KiB
Python
102 lines
3.7 KiB
Python
"""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"
|