Post-merge / detect-type (push) Successful in 53s
Post-merge / release (push) Successful in 1m14s
Post-merge / validate-commit-msg (push) Successful in 1m25s
Post-merge / vikunja (push) Successful in 1m21s
Post-merge / badges (push) Successful in 1m45s
Post-merge / configure-repo (push) Successful in 1m15s
Post-merge / sync-wiki (push) Successful in 3m5s
Post-merge / publish (push) Successful in 1m1s
59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
"""Unit tests for logging configuration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from collections.abc import Generator
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from grm.logging_config import get_logger
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _cleanup_loggers() -> Generator[None, None, None]:
|
|
"""Remove test loggers after each test to prevent state leakage."""
|
|
yield
|
|
for name in list(logging.Logger.manager.loggerDict.keys()):
|
|
if name.startswith("test_"):
|
|
logger = logging.getLogger(name)
|
|
logger.handlers.clear()
|
|
del logging.Logger.manager.loggerDict[name]
|
|
|
|
|
|
class TestGetLogger:
|
|
def test_returns_logger(self, tmp_path: Path) -> None:
|
|
with patch("grm.logging_config.Path.home", return_value=tmp_path):
|
|
logger = get_logger("test_returns_logger")
|
|
|
|
assert isinstance(logger, logging.Logger)
|
|
assert logger.name == "test_returns_logger"
|
|
assert len(logger.handlers) == 1 # file only
|
|
|
|
def test_caches_same_instance(self, tmp_path: Path) -> None:
|
|
with patch("grm.logging_config.Path.home", return_value=tmp_path):
|
|
logger1 = get_logger("test_caches_same")
|
|
logger2 = get_logger("test_caches_same")
|
|
|
|
assert logger1 is logger2
|
|
|
|
def test_file_handler_writes(self, tmp_path: Path) -> None:
|
|
with patch("grm.logging_config.Path.home", return_value=tmp_path):
|
|
logger = get_logger("test_file_writes")
|
|
logger.info("hello from test")
|
|
|
|
log_file = tmp_path / ".local" / "state" / "grm" / "logs" / "grm.log"
|
|
assert log_file.exists()
|
|
content = log_file.read_text()
|
|
assert "hello from test" in content
|
|
assert "INFO" in content
|
|
|
|
def test_only_file_handler(self, tmp_path: Path) -> None:
|
|
with patch("grm.logging_config.Path.home", return_value=tmp_path):
|
|
logger = get_logger("test_only_file")
|
|
|
|
assert len(logger.handlers) == 1
|
|
assert isinstance(logger.handlers[0], logging.FileHandler)
|