File: test_debug_info_colors.py

package info (click to toggle)
tmuxp 1.64.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: python: 17,788; sh: 22; makefile: 6
file content (154 lines) | stat: -rw-r--r-- 5,376 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
"""Tests for debug-info command color output and privacy masking."""

from __future__ import annotations

import pathlib

import pytest

from tests.cli.conftest import ANSI_BLUE, ANSI_BOLD, ANSI_CYAN, ANSI_MAGENTA
from tmuxp._internal.private_path import PrivatePath, collapse_home_in_string
from tmuxp.cli._colors import ColorMode, Colors

# Privacy masking in debug-info context


def test_debug_info_masks_home_in_paths(mock_home: pathlib.Path) -> None:
    """debug-info should mask home directory in paths."""
    # Simulate what debug-info does with tmuxp_path
    tmuxp_path = mock_home / "work/python/tmuxp/src/tmuxp"
    private_path = str(PrivatePath(tmuxp_path))

    assert private_path == "~/work/python/tmuxp/src/tmuxp"
    assert "/home/testuser" not in private_path


def test_debug_info_masks_home_in_system_path(mock_home: pathlib.Path) -> None:
    """debug-info should mask home directory in system PATH."""
    path_env = "/home/testuser/.local/bin:/usr/bin:/home/testuser/.cargo/bin"
    masked = collapse_home_in_string(path_env)

    assert masked == "~/.local/bin:/usr/bin:~/.cargo/bin"
    assert "/home/testuser" not in masked


def test_debug_info_preserves_system_paths(mock_home: pathlib.Path) -> None:
    """debug-info should preserve paths outside home directory."""
    tmux_path = "/usr/bin/tmux"
    private_path = str(PrivatePath(tmux_path))

    assert private_path == "/usr/bin/tmux"


# Formatting helpers in debug-info context


def test_debug_info_format_kv_labels(colors_always: Colors) -> None:
    """debug-info should highlight labels in key-value pairs."""
    result = colors_always.format_kv("tmux version", "3.2a")
    assert ANSI_MAGENTA in result  # magenta for label
    assert ANSI_BOLD in result  # bold for label
    assert "tmux version" in result
    assert "3.2a" in result


def test_debug_info_format_version(colors_always: Colors) -> None:
    """debug-info should highlight version strings."""
    result = colors_always.format_kv(
        "tmux version", colors_always.format_version("3.2a")
    )
    assert ANSI_CYAN in result  # cyan for version
    assert "3.2a" in result


def test_debug_info_format_path(colors_always: Colors) -> None:
    """debug-info should highlight paths."""
    result = colors_always.format_kv(
        "tmux path", colors_always.format_path("/usr/bin/tmux")
    )
    assert ANSI_CYAN in result  # cyan for path
    assert "/usr/bin/tmux" in result


def test_debug_info_format_separator(colors_always: Colors) -> None:
    """debug-info should use muted separators."""
    result = colors_always.format_separator()
    assert ANSI_BLUE in result  # blue for muted
    assert "-" * 25 in result


# tmux option formatting


def test_debug_info_format_tmux_option_space_sep(colors_always: Colors) -> None:
    """debug-info should format space-separated tmux options."""
    result = colors_always.format_tmux_option("status on")
    assert ANSI_MAGENTA in result  # magenta for key
    assert ANSI_CYAN in result  # cyan for value
    assert "status" in result
    assert "on" in result


def test_debug_info_format_tmux_option_equals_sep(colors_always: Colors) -> None:
    """debug-info should format equals-separated tmux options."""
    result = colors_always.format_tmux_option("base-index=0")
    assert ANSI_MAGENTA in result  # magenta for key
    assert ANSI_CYAN in result  # cyan for value
    assert "base-index" in result
    assert "0" in result


# Color mode behavior


def test_debug_info_respects_never_mode(colors_never: Colors) -> None:
    """debug-info should return plain text in NEVER mode."""
    result = colors_never.format_kv("tmux version", colors_never.format_version("3.2a"))
    assert "\033[" not in result
    assert result == "tmux version: 3.2a"


def test_debug_info_respects_no_color_env(monkeypatch: pytest.MonkeyPatch) -> None:
    """debug-info should respect NO_COLOR environment variable."""
    monkeypatch.setenv("NO_COLOR", "1")
    colors = Colors(ColorMode.ALWAYS)

    result = colors.format_kv("tmux path", "/usr/bin/tmux")
    assert "\033[" not in result
    assert result == "tmux path: /usr/bin/tmux"


# Combined formatting


def test_debug_info_combined_path_with_privacy(
    colors_always: Colors,
    mock_home: pathlib.Path,
) -> None:
    """debug-info should combine privacy masking with color formatting."""
    # Simulate what debug-info does
    raw_path = mock_home / "work/tmuxp/src/tmuxp"
    private_path = str(PrivatePath(raw_path))
    formatted = colors_always.format_kv(
        "tmuxp path", colors_always.format_path(private_path)
    )

    assert "~/work/tmuxp/src/tmuxp" in formatted
    assert "/home/testuser" not in formatted
    assert ANSI_CYAN in formatted  # cyan for path
    assert ANSI_MAGENTA in formatted  # magenta for label


def test_debug_info_environment_section_format(colors_always: Colors) -> None:
    """debug-info environment section should have proper format."""
    # Simulate environment section format
    env_items = [
        f"\t{colors_always.format_kv('dist', 'Linux-6.6.87')}",
        f"\t{colors_always.format_kv('arch', 'x86_64')}",
    ]
    section = f"{colors_always.format_label('environment')}:\n" + "\n".join(env_items)

    assert "environment" in section
    assert "\t" in section  # indented items
    assert "dist" in section
    assert "arch" in section