|
2 | 2 |
|
3 | 3 | import itertools |
4 | 4 | import shutil |
5 | | -from types import SimpleNamespace |
| 5 | +import sys |
| 6 | +from types import ModuleType, SimpleNamespace |
6 | 7 | from typing import Any, cast |
7 | 8 |
|
8 | 9 | import click |
|
11 | 12 | from prompt_toolkit.formatted_text import ANSI, FormattedText, to_plain_text |
12 | 13 | import pytest |
13 | 14 |
|
| 15 | +from mycli import compat |
14 | 16 | from mycli import output as output_module |
15 | 17 | from mycli.output import OutputMixin |
16 | 18 | from mycli.packages.sqlresult import SQLResult |
| 19 | +from mycli.types import ImageProtocol |
17 | 20 | from test.utils import DummyFormatter, FakeCursorBase, make_bare_mycli # type: ignore[attr-defined] |
18 | 21 |
|
19 | 22 |
|
@@ -112,6 +115,59 @@ def test_output_emits_kitty_image_in_base64_chunks(monkeypatch: pytest.MonkeyPat |
112 | 115 | ] |
113 | 116 |
|
114 | 117 |
|
| 118 | +@pytest.mark.parametrize('image_protocol', ['iterm2', 'kitty']) |
| 119 | +def test_output_suppresses_images_in_windows_console( |
| 120 | + monkeypatch: pytest.MonkeyPatch, |
| 121 | + image_protocol: ImageProtocol, |
| 122 | +) -> None: |
| 123 | + cli = make_bare_mycli() |
| 124 | + cli.prompt_session = cast( |
| 125 | + Any, |
| 126 | + SimpleNamespace(output=SimpleNamespace(get_size=lambda: SimpleNamespace(columns=80, rows=24))), |
| 127 | + ) |
| 128 | + cli.get_output_margin = lambda status=None: 1 # type: ignore[assignment] |
| 129 | + emitted: list[str | None] = [] |
| 130 | + monkeypatch.setattr(output_module, 'is_windows_console', lambda output: True) |
| 131 | + monkeypatch.setattr(click, 'echo', lambda value=None, **_kwargs: emitted.append(value)) |
| 132 | + |
| 133 | + OutputMixin.output(cli, itertools.chain(), SQLResult(image=b'png', image_protocol=image_protocol)) |
| 134 | + |
| 135 | + assert emitted == [] |
| 136 | + |
| 137 | + |
| 138 | +def test_is_windows_console_detects_prompt_toolkit_backends(monkeypatch: pytest.MonkeyPatch) -> None: |
| 139 | + class FakeWin32Output: |
| 140 | + pass |
| 141 | + |
| 142 | + class FakeWindows10Output: |
| 143 | + pass |
| 144 | + |
| 145 | + class FakeConEmuOutput: |
| 146 | + pass |
| 147 | + |
| 148 | + modules = ( |
| 149 | + ('prompt_toolkit.output.win32', 'Win32Output', FakeWin32Output), |
| 150 | + ('prompt_toolkit.output.windows10', 'Windows10_Output', FakeWindows10Output), |
| 151 | + ('prompt_toolkit.output.conemu', 'ConEmuOutput', FakeConEmuOutput), |
| 152 | + ) |
| 153 | + for module_name, class_name, output_class in modules: |
| 154 | + module = ModuleType(module_name) |
| 155 | + setattr(module, class_name, output_class) |
| 156 | + monkeypatch.setitem(sys.modules, module_name, module) |
| 157 | + monkeypatch.setattr(compat.sys, 'platform', 'win32') |
| 158 | + |
| 159 | + assert compat.is_windows_console(FakeWin32Output()) |
| 160 | + assert compat.is_windows_console(FakeWindows10Output()) |
| 161 | + assert compat.is_windows_console(FakeConEmuOutput()) |
| 162 | + assert not compat.is_windows_console(object()) |
| 163 | + |
| 164 | + |
| 165 | +def test_is_windows_console_ignores_non_windows_platform(monkeypatch: pytest.MonkeyPatch) -> None: |
| 166 | + monkeypatch.setattr(compat.sys, 'platform', 'cygwin') |
| 167 | + |
| 168 | + assert not compat.is_windows_console(object()) |
| 169 | + |
| 170 | + |
115 | 171 | def test_get_output_margin_renders_prompt_once_and_counts_status_lines(monkeypatch: pytest.MonkeyPatch) -> None: |
116 | 172 | cli = make_bare_mycli() |
117 | 173 | cli.prompt_lines = 0 |
|
0 commit comments