File: test_formatter.py

package info (click to toggle)
tmuxp 1.64.0-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 3,500 kB
  • sloc: python: 17,788; sh: 22; makefile: 6
file content (264 lines) | stat: -rw-r--r-- 8,405 bytes parent folder | download | duplicates (3)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""Tests for TmuxpHelpFormatter and themed formatter factory."""

from __future__ import annotations

import argparse

import pytest

from tests.cli.conftest import ANSI_RESET
from tmuxp.cli._colors import ColorMode, Colors
from tmuxp.cli._formatter import (
    HelpTheme,
    TmuxpHelpFormatter,
    create_themed_formatter,
)


def test_create_themed_formatter_returns_subclass() -> None:
    """Factory returns a TmuxpHelpFormatter subclass."""
    formatter_cls = create_themed_formatter()
    assert issubclass(formatter_cls, TmuxpHelpFormatter)


def test_create_themed_formatter_with_colors_enabled(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Formatter has theme when colors enabled."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    colors = Colors(ColorMode.ALWAYS)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("test")

    assert formatter._theme is not None
    assert formatter._theme.prog != ""  # Has color codes


def test_create_themed_formatter_with_colors_disabled() -> None:
    """Formatter has no theme when colors disabled."""
    colors = Colors(ColorMode.NEVER)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("test")

    assert formatter._theme is None


def test_create_themed_formatter_auto_mode_respects_no_color(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Auto mode respects NO_COLOR environment variable."""
    monkeypatch.setenv("NO_COLOR", "1")
    formatter_cls = create_themed_formatter()
    formatter = formatter_cls("test")

    assert formatter._theme is None


def test_create_themed_formatter_auto_mode_respects_force_color(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Auto mode respects FORCE_COLOR environment variable."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    monkeypatch.setenv("FORCE_COLOR", "1")
    formatter_cls = create_themed_formatter()
    formatter = formatter_cls("test")

    assert formatter._theme is not None


def test_fill_text_with_theme_colorizes_examples(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Examples section is colorized when theme is set."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    colors = Colors(ColorMode.ALWAYS)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("tmuxp")

    text = "Examples:\n  tmuxp load myproject"
    result = formatter._fill_text(text, 80, "")

    # Should contain ANSI escape codes
    assert "\033[" in result
    assert "tmuxp" in result
    assert "load" in result


def test_fill_text_without_theme_plain_text() -> None:
    """Examples section is plain text when no theme."""
    colors = Colors(ColorMode.NEVER)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("tmuxp")

    text = "Examples:\n  tmuxp load myproject"
    result = formatter._fill_text(text, 80, "")

    # Should NOT contain ANSI escape codes
    assert "\033[" not in result
    assert "tmuxp load myproject" in result


def test_fill_text_category_headings_colorized(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Category headings within examples block are colorized."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    colors = Colors(ColorMode.ALWAYS)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("tmuxp")

    # Test category heading without "examples:" suffix
    text = "examples:\n  tmuxp ls\n\nMachine-readable output:\n  tmuxp ls --json"
    result = formatter._fill_text(text, 80, "")

    # Both headings should be colorized
    assert "\033[" in result
    assert "examples:" in result
    assert "Machine-readable output:" in result
    # Commands should also be colorized
    assert "tmuxp" in result
    assert "--json" in result


def test_fill_text_category_heading_only_in_examples_block(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Category headings are only recognized within examples block."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    colors = Colors(ColorMode.ALWAYS)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("tmuxp")

    # Text before examples block should not be colorized as heading
    text = "Some heading:\n  not a command\n\nexamples:\n  tmuxp load"
    result = formatter._fill_text(text, 80, "")

    # "Some heading:" should NOT be colorized (it's before examples block)
    # "examples:" and the command should be colorized
    lines = result.split("\n")
    # First line should be plain (no ANSI in "Some heading:")
    assert "Some heading:" in lines[0]


def test_parser_help_respects_no_color(
    monkeypatch: pytest.MonkeyPatch,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Parser --help output is plain when NO_COLOR set."""
    monkeypatch.setenv("NO_COLOR", "1")
    monkeypatch.setenv("COLUMNS", "100")

    formatter_cls = create_themed_formatter()
    parser = argparse.ArgumentParser(
        prog="test",
        description="Examples:\n  test command",
        formatter_class=formatter_cls,
    )

    with pytest.raises(SystemExit):
        parser.parse_args(["--help"])

    captured = capsys.readouterr()
    assert "\033[" not in captured.out


def test_parser_help_colorized_with_force_color(
    monkeypatch: pytest.MonkeyPatch,
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Parser --help output is colorized when FORCE_COLOR set."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    monkeypatch.setenv("FORCE_COLOR", "1")
    monkeypatch.setenv("COLUMNS", "100")

    formatter_cls = create_themed_formatter()
    parser = argparse.ArgumentParser(
        prog="test",
        description="Examples:\n  test command",
        formatter_class=formatter_cls,
    )

    with pytest.raises(SystemExit):
        parser.parse_args(["--help"])

    captured = capsys.readouterr()
    assert "\033[" in captured.out


def test_help_theme_from_colors_with_none_returns_empty() -> None:
    """HelpTheme.from_colors(None) returns empty theme."""
    theme = HelpTheme.from_colors(None)

    assert theme.prog == ""
    assert theme.action == ""
    assert theme.reset == ""


def test_help_theme_from_colors_disabled_returns_empty() -> None:
    """HelpTheme.from_colors with disabled colors returns empty theme."""
    colors = Colors(ColorMode.NEVER)
    theme = HelpTheme.from_colors(colors)

    assert theme.prog == ""
    assert theme.action == ""
    assert theme.reset == ""


def test_help_theme_from_colors_enabled_returns_colored(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """HelpTheme.from_colors with enabled colors returns colored theme."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    colors = Colors(ColorMode.ALWAYS)
    theme = HelpTheme.from_colors(colors)

    # Should have ANSI codes
    assert "\033[" in theme.prog
    assert "\033[" in theme.action
    assert theme.reset == ANSI_RESET


def test_fill_text_section_heading_with_examples_suffix(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Section headings ending with 'examples:' are colorized without initial block."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    colors = Colors(ColorMode.ALWAYS)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("tmuxp")

    # This is what build_description produces - no initial "examples:" block
    text = (
        "load examples:\n  tmuxp load myproject\n\n"
        "freeze examples:\n  tmuxp freeze mysession"
    )
    result = formatter._fill_text(text, 80, "")

    # Both headings should be colorized (contain ANSI escape codes)
    assert "\033[" in result
    # Commands should be colorized
    assert "tmuxp" in result


def test_fill_text_multiple_example_sections_all_colorized(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """Multiple 'X examples:' sections should all be colorized."""
    monkeypatch.delenv("NO_COLOR", raising=False)
    colors = Colors(ColorMode.ALWAYS)
    formatter_cls = create_themed_formatter(colors)
    formatter = formatter_cls("tmuxp")

    text = """load examples:
  tmuxp load myproject

freeze examples:
  tmuxp freeze mysession

ls examples:
  tmuxp ls"""
    result = formatter._fill_text(text, 80, "")

    # All sections should have ANSI codes
    # Count ANSI escape sequences - should have at least heading + command per section
    assert result.count("\033[") >= 6