-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathtest_log.py
More file actions
105 lines (85 loc) · 3.01 KB
/
test_log.py
File metadata and controls
105 lines (85 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""Tests for tmuxp.log module."""
from __future__ import annotations
import logging
import sys
import pytest
from tmuxp.log import (
LEVEL_COLORS,
DebugLogFormatter,
LogFormatter,
tmuxp_echo,
)
def test_level_colors_no_colorama() -> None:
"""LEVEL_COLORS must be raw ANSI escape strings, not colorama objects."""
for level, code in LEVEL_COLORS.items():
assert code.startswith("\033["), (
f"LEVEL_COLORS[{level!r}] should start with ANSI ESC, got {code!r}"
)
def test_log_formatter_format_plain_text() -> None:
"""LogFormatter.format() produces plain text without ANSI when unstylized."""
formatter = LogFormatter()
record = logging.LogRecord(
name="tmuxp",
level=logging.INFO,
pathname="",
lineno=0,
msg="test message",
args=(),
exc_info=None,
)
output = formatter.format(record)
assert "test message" in output
assert "\033[" not in output
def test_debug_log_formatter_format_smoke() -> None:
"""DebugLogFormatter.format() runs without error."""
formatter = DebugLogFormatter()
record = logging.LogRecord(
name="tmuxp",
level=logging.DEBUG,
pathname="",
lineno=42,
msg="debug message",
args=(),
exc_info=None,
)
output = formatter.format(record)
assert "debug message" in output
def test_timestamp_format_has_minutes() -> None:
"""Timestamp format must use %M (minutes), not %m (month)."""
formatter = LogFormatter()
record = logging.LogRecord(
name="tmuxp",
level=logging.INFO,
pathname="",
lineno=0,
msg="ts check",
args=(),
exc_info=None,
)
formatter.format(record)
# asctime is set during format(); if %m were used, seconds portion would
# show month (01-12) instead of minutes (00-59) — we can't easily
# distinguish that directly, so just verify the format string constant.
# Inspect the source: date_format in LogFormatter.format is "%H:%M:%S"
import inspect
import tmuxp.log as log_module
src = inspect.getsource(log_module.LogFormatter.format)
assert '"%H:%M:%S"' in src, "Timestamp format must be %H:%M:%S (M = minutes)"
def test_tmuxp_echo_default_stdout(capsys: pytest.CaptureFixture[str]) -> None:
"""tmuxp_echo writes to stdout by default."""
tmuxp_echo("hello stdout")
captured = capsys.readouterr()
assert captured.out == "hello stdout\n"
assert captured.err == ""
def test_tmuxp_echo_to_stderr(capsys: pytest.CaptureFixture[str]) -> None:
"""tmuxp_echo writes to stderr when file=sys.stderr."""
tmuxp_echo("hello stderr", file=sys.stderr)
captured = capsys.readouterr()
assert captured.err == "hello stderr\n"
assert captured.out == ""
def test_tmuxp_echo_none_is_no_op(capsys: pytest.CaptureFixture[str]) -> None:
"""tmuxp_echo(None) produces no output."""
tmuxp_echo(None)
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""