| 1 |
from contextlib import suppress |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import App, CycloptsError |
| 6 |
from cyclopts.exceptions import UnusedCliTokensError |
| 7 |
|
| 8 |
|
| 9 |
def _create_mock_console(mocker): |
| 10 |
from rich.console import ConsoleDimensions, ConsoleOptions |
| 11 |
|
| 12 |
console = mocker.MagicMock() |
| 13 |
console.width = 80 |
| 14 |
console.height = 24 |
| 15 |
console.legacy_windows = False |
| 16 |
console.is_terminal = True |
| 17 |
console.encoding = "utf-8" |
| 18 |
|
| 19 |
|
| 20 |
console.options = ConsoleOptions( |
| 21 |
size=ConsoleDimensions(console.width, console.height), |
| 22 |
legacy_windows=console.legacy_windows, |
| 23 |
min_width=1, |
| 24 |
max_width=console.width, |
| 25 |
is_terminal=console.is_terminal, |
| 26 |
encoding=console.encoding, |
| 27 |
max_height=console.height, |
| 28 |
) |
| 29 |
|
| 30 |
return console |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture |
| 34 |
def mock_console(mocker): |
| 35 |
"""Create a mock console with required attributes for Rich compatibility.""" |
| 36 |
return _create_mock_console(mocker) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture |
| 40 |
def subapp(app): |
| 41 |
app.command(subapp := App(name="foo")) |
| 42 |
return subapp |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.parametrize("cmd", ["foo --help"]) |
| 46 |
def test_root_console(app, mock_console, cmd): |
| 47 |
app.console = mock_console |
| 48 |
|
| 49 |
with suppress(CycloptsError): |
| 50 |
app(cmd, exit_on_error=False) |
| 51 |
|
| 52 |
app.console.print.assert_called() |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.parametrize("cmd", ["foo --help"]) |
| 56 |
def test_root_console_subapp(app, subapp, mock_console, cmd): |
| 57 |
"""Check if root console is properly resolved (subapp.console not specified).""" |
| 58 |
app.console = mock_console |
| 59 |
|
| 60 |
with suppress(CycloptsError): |
| 61 |
app(cmd, exit_on_error=False) |
| 62 |
|
| 63 |
app.console.print.assert_called() |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.parametrize("cmd", ["foo --help"]) |
| 67 |
def test_root_subapp_console(app, subapp, mock_console, mocker, cmd): |
| 68 |
"""Check if subapp console is properly resolved (NOT app.console).""" |
| 69 |
app.console = mock_console |
| 70 |
subapp.console = _create_mock_console(mocker) |
| 71 |
|
| 72 |
with suppress(CycloptsError): |
| 73 |
app(cmd, exit_on_error=False) |
| 74 |
|
| 75 |
app.console.print.assert_not_called() |
| 76 |
subapp.console.print.assert_called() |
| 77 |
|
| 78 |
|
| 79 |
@pytest.mark.parametrize("cmd", ["foo --help"]) |
| 80 |
def test_root_subapp_arg_console(app, subapp, mock_console, mocker, cmd): |
| 81 |
"""Explicitly provided console should be used.""" |
| 82 |
console = mock_console |
| 83 |
app.console = mocker.MagicMock() |
| 84 |
subapp.console = mocker.MagicMock() |
| 85 |
|
| 86 |
with suppress(CycloptsError): |
| 87 |
app(cmd, console=console, exit_on_error=False) |
| 88 |
|
| 89 |
console.print.assert_called() |
| 90 |
app.console.print.assert_not_called() |
| 91 |
subapp.console.print.assert_not_called() |
| 92 |
|
| 93 |
|
| 94 |
def test_console_populated_issue_103(app): |
| 95 |
"""Ensures console is populated for an UnusedCliTokensError. |
| 96 |
|
| 97 |
https://github.com/BrianPugh/cyclopts/issues/103 |
| 98 |
""" |
| 99 |
|
| 100 |
@app.command |
| 101 |
def foo(): |
| 102 |
pass |
| 103 |
|
| 104 |
with pytest.raises(UnusedCliTokensError): |
| 105 |
app("foo bar", exit_on_error=False) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize("cmd", ["foo invalid-command"]) |
| 109 |
def test_root_error_console(app, mock_console, mocker, cmd): |
| 110 |
"""Test that root error_console is used for errors.""" |
| 111 |
error_console = _create_mock_console(mocker) |
| 112 |
app.error_console = error_console |
| 113 |
|
| 114 |
with suppress(CycloptsError): |
| 115 |
app(cmd, exit_on_error=False) |
| 116 |
|
| 117 |
error_console.print.assert_called() |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.parametrize("cmd", ["foo invalid-command"]) |
| 121 |
def test_root_error_console_subapp(app, subapp, mocker, cmd): |
| 122 |
"""Check if root error_console is properly resolved (subapp.error_console not specified).""" |
| 123 |
error_console = _create_mock_console(mocker) |
| 124 |
app.error_console = error_console |
| 125 |
|
| 126 |
with suppress(CycloptsError): |
| 127 |
app(cmd, exit_on_error=False) |
| 128 |
|
| 129 |
error_console.print.assert_called() |
| 130 |
|
| 131 |
|
| 132 |
@pytest.mark.parametrize("cmd", ["foo invalid-command"]) |
| 133 |
def test_root_subapp_error_console(app, subapp, mocker, cmd): |
| 134 |
"""Check if subapp error_console is properly resolved (NOT app.error_console).""" |
| 135 |
app.error_console = _create_mock_console(mocker) |
| 136 |
subapp.error_console = _create_mock_console(mocker) |
| 137 |
|
| 138 |
with suppress(CycloptsError): |
| 139 |
app(cmd, exit_on_error=False) |
| 140 |
|
| 141 |
app.error_console.print.assert_not_called() |
| 142 |
subapp.error_console.print.assert_called() |
| 143 |
|
| 144 |
|
| 145 |
@pytest.mark.parametrize("cmd", ["foo invalid-command"]) |
| 146 |
def test_root_subapp_arg_error_console(app, subapp, mocker, cmd): |
| 147 |
"""Explicitly provided error_console should be used.""" |
| 148 |
error_console = _create_mock_console(mocker) |
| 149 |
app.error_console = mocker.MagicMock() |
| 150 |
subapp.error_console = mocker.MagicMock() |
| 151 |
|
| 152 |
with suppress(CycloptsError): |
| 153 |
app(cmd, error_console=error_console, exit_on_error=False) |
| 154 |
|
| 155 |
error_console.print.assert_called() |
| 156 |
app.error_console.print.assert_not_called() |
| 157 |
subapp.error_console.print.assert_not_called() |