Kanta colors log header parts: extra is a plain string, logheader callbacks receive pre-colored parts
This commit is contained in:
+26
-4
@@ -2,7 +2,29 @@ import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from kanta.logging import configure_logging, log_change
|
||||
from kanta.logging import (
|
||||
_ACTION,
|
||||
_RESET,
|
||||
_TARGET,
|
||||
_USER,
|
||||
colorize_header_parts,
|
||||
configure_logging,
|
||||
log_change,
|
||||
)
|
||||
|
||||
|
||||
def test_colorize_header_parts():
|
||||
action, user, extra = colorize_header_parts("update", "alice", "tgt")
|
||||
assert action == f"{_ACTION}update{_RESET}"
|
||||
assert user == f"{_USER}alice{_RESET}"
|
||||
assert extra == f"{_TARGET}tgt{_RESET}"
|
||||
|
||||
|
||||
def test_colorize_header_parts_missing_user_and_extra():
|
||||
action, user, extra = colorize_header_parts("update")
|
||||
assert action == f"{_ACTION}update{_RESET}"
|
||||
assert user == ""
|
||||
assert extra == ""
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -55,7 +77,7 @@ def test_log_change_appends_extra_string(capsys):
|
||||
log_change("export", {}, extra="mydb.db")
|
||||
captured = capsys.readouterr()
|
||||
assert "export" in captured.err
|
||||
assert "mydb.db" in captured.err
|
||||
assert f"{_TARGET}mydb.db{_RESET}" in captured.err
|
||||
|
||||
|
||||
def test_log_change_headerfmt_replaces_header(capsys):
|
||||
@@ -65,8 +87,8 @@ def test_log_change_headerfmt_replaces_header(capsys):
|
||||
log_change(
|
||||
"update",
|
||||
{},
|
||||
headerfmt=lambda action, user, extra: f"CUSTOM {action} {extra['id']}",
|
||||
extra={"id": 7},
|
||||
headerfmt=lambda action, user, extra: f"CUSTOM {action} {extra}",
|
||||
extra="7",
|
||||
)
|
||||
captured = capsys.readouterr()
|
||||
assert "CUSTOM update 7" in captured.err
|
||||
|
||||
Reference in New Issue
Block a user