Skip to content

Commit 9d2f006

Browse files
authored
Add tests for output (#233)
1 parent 0ba5248 commit 9d2f006

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

.config/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
argnames
2+
argvalues
13
bindep
24
bindir
35
bthornto

tests/unit/test_output.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""Test the output module."""
2+
3+
from __future__ import annotations
4+
5+
from types import SimpleNamespace
6+
from typing import TYPE_CHECKING
7+
8+
import pytest
9+
10+
from ansible_dev_environment.output import Color, Level, Msg, Output, console_width
11+
from ansible_dev_environment.utils import TermFeatures
12+
13+
14+
if TYPE_CHECKING:
15+
from pathlib import Path
16+
17+
18+
@pytest.mark.parametrize(argnames="width, expected", argvalues=((79, 79), (131, 81), (133, 132)))
19+
def test_console_width(width: int, expected: int, monkeypatch: pytest.MonkeyPatch) -> None:
20+
"""Test the console width function."""
21+
22+
def mock_get_terminal_size() -> SimpleNamespace:
23+
return SimpleNamespace(columns=width, lines=24)
24+
25+
monkeypatch.setattr("shutil.get_terminal_size", mock_get_terminal_size)
26+
27+
monkeypatch.delenv("COLUMNS", raising=False)
28+
29+
assert console_width() == expected
30+
31+
32+
@pytest.mark.parametrize(
33+
"params",
34+
(
35+
(Level.CRITICAL, Color.BRIGHT_RED),
36+
(Level.DEBUG, Color.GREY),
37+
(Level.ERROR, Color.RED),
38+
(Level.HINT, Color.CYAN),
39+
(Level.INFO, Color.MAGENTA),
40+
(Level.NOTE, Color.GREEN),
41+
(Level.WARNING, Color.YELLOW),
42+
),
43+
ids=("critical", "debug", "error", "hint", "info", "note", "warning"),
44+
)
45+
def test_color_mapping(params: tuple[Level, Color]) -> None:
46+
"""Test the color mapping for Msg in the output module.
47+
48+
Args:
49+
params: Tuple of Level and Color.
50+
"""
51+
assert Msg(message="", prefix=params[0]).color == str(params[1])
52+
53+
54+
@pytest.mark.parametrize("level", ("info", "warning", "error", "debug", "critical", "hint", "note"))
55+
def test_console_output(level: str, capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None:
56+
"""Test the console output function.
57+
58+
Args:
59+
level: Log level.
60+
capsys: Pytest fixture.
61+
tmp_path: Pytest fixture
62+
"""
63+
output = Output(
64+
log_file=str(tmp_path / "test.log"),
65+
log_level="debug",
66+
log_append="false",
67+
term_features=TermFeatures(color=True, links=True),
68+
verbosity=3,
69+
)
70+
message = f"{level} message"
71+
msg = Msg(message=message, prefix=getattr(Level, level.upper()))
72+
if level == "critical":
73+
with pytest.raises(SystemExit):
74+
getattr(output, level)(message)
75+
else:
76+
getattr(output, level)(message)
77+
captured = capsys.readouterr()
78+
standard_x = captured.err if level in ("critical", "error") else captured.out
79+
assert standard_x.startswith(msg.color)
80+
assert standard_x.endswith(Color.END + "\n")
81+
assert level.capitalize() in standard_x
82+
assert message in standard_x
83+
84+
85+
def test_output_log_exists(tmp_path: Path) -> None:
86+
"""Test the log file is reinitialized if append is false.
87+
88+
Args:
89+
tmp_path: Pytest fixture.
90+
"""
91+
log_file = tmp_path / "test.log"
92+
log_file.write_text("test")
93+
pre_stat = log_file.stat()
94+
Output(
95+
log_file=str(log_file),
96+
log_level="debug",
97+
log_append="false",
98+
term_features=TermFeatures(color=True, links=True),
99+
verbosity=3,
100+
)
101+
post_stat = log_file.stat()
102+
assert pre_stat.st_size > 0
103+
assert post_stat.st_size != pre_stat.st_size
104+
assert post_stat.st_size == 0

0 commit comments

Comments
 (0)