| 1 |
"""Tests for error_console functionality.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
from io import StringIO |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from cyclopts import App |
| 9 |
from cyclopts.exceptions import UnknownOptionError |
| 10 |
|
| 11 |
|
| 12 |
def test_error_goes_to_stderr(capfd): |
| 13 |
"""Test that error messages go to stderr.""" |
| 14 |
app = App() |
| 15 |
|
| 16 |
@app.default |
| 17 |
def main(name: str = "World"): |
| 18 |
print(f"Hello, {name}!") |
| 19 |
|
| 20 |
with pytest.raises(SystemExit): |
| 21 |
app("--invalid-option", exit_on_error=True, print_error=True) |
| 22 |
|
| 23 |
captured = capfd.readouterr() |
| 24 |
assert captured.err != "" |
| 25 |
assert "Unknown option" in captured.err or "invalid-option" in captured.err |
| 26 |
assert captured.out == "" |
| 27 |
|
| 28 |
|
| 29 |
def test_help_goes_to_stdout(capfd): |
| 30 |
"""Test that help messages go to stdout.""" |
| 31 |
app = App() |
| 32 |
|
| 33 |
@app.default |
| 34 |
def main(name: str = "World"): |
| 35 |
print(f"Hello, {name}!") |
| 36 |
|
| 37 |
with pytest.raises(SystemExit): |
| 38 |
app("--help") |
| 39 |
|
| 40 |
captured = capfd.readouterr() |
| 41 |
assert captured.out != "" |
| 42 |
assert captured.err == "" |
| 43 |
|
| 44 |
|
| 45 |
def test_version_goes_to_stdout(capfd): |
| 46 |
"""Test that version messages go to stdout.""" |
| 47 |
app = App(version="1.2.3") |
| 48 |
|
| 49 |
@app.default |
| 50 |
def main(name: str = "World"): |
| 51 |
print(f"Hello, {name}!") |
| 52 |
|
| 53 |
with pytest.raises(SystemExit): |
| 54 |
app("--version") |
| 55 |
|
| 56 |
captured = capfd.readouterr() |
| 57 |
assert "1.2.3" in captured.out |
| 58 |
assert captured.err == "" |
| 59 |
|
| 60 |
|
| 61 |
def test_custom_error_console(): |
| 62 |
"""Test that custom error_console can be provided.""" |
| 63 |
from rich.console import Console |
| 64 |
|
| 65 |
error_output = StringIO() |
| 66 |
error_console = Console(file=error_output, force_terminal=False) |
| 67 |
|
| 68 |
app = App(error_console=error_console) |
| 69 |
|
| 70 |
@app.default |
| 71 |
def main(name: str): |
| 72 |
pass |
| 73 |
|
| 74 |
with pytest.raises(UnknownOptionError): |
| 75 |
app("--invalid", exit_on_error=False, print_error=True) |
| 76 |
|
| 77 |
error_text = error_output.getvalue() |
| 78 |
assert "Unknown option" in error_text or "invalid" in error_text |
| 79 |
|
| 80 |
|
| 81 |
def test_error_console_resolution_through_app_stack(mocker): |
| 82 |
"""Test that error_console is properly resolved through app_stack.""" |
| 83 |
from rich.console import Console |
| 84 |
|
| 85 |
|
| 86 |
normal_console = mocker.MagicMock(spec=Console) |
| 87 |
error_console_mock = mocker.MagicMock(spec=Console) |
| 88 |
|
| 89 |
app = App(console=normal_console, error_console=error_console_mock) |
| 90 |
|
| 91 |
@app.default |
| 92 |
def main(name: str): |
| 93 |
pass |
| 94 |
|
| 95 |
try: |
| 96 |
app("--invalid", exit_on_error=False, print_error=True) |
| 97 |
except UnknownOptionError: |
| 98 |
pass |
| 99 |
|
| 100 |
|
| 101 |
error_console_mock.print.assert_called() |
| 102 |
|
| 103 |
normal_console.print.assert_not_called() |
| 104 |
|
| 105 |
|
| 106 |
def test_subapp_error_console_inheritance(mocker): |
| 107 |
"""Test that subapp inherits error_console from parent.""" |
| 108 |
from rich.console import Console |
| 109 |
|
| 110 |
error_console_mock = mocker.MagicMock(spec=Console) |
| 111 |
|
| 112 |
app = App(error_console=error_console_mock) |
| 113 |
subapp = App(name="sub") |
| 114 |
app.command(subapp) |
| 115 |
|
| 116 |
@subapp.default |
| 117 |
def sub_cmd(value: int): |
| 118 |
pass |
| 119 |
|
| 120 |
try: |
| 121 |
app("sub --invalid", exit_on_error=False, print_error=True) |
| 122 |
except UnknownOptionError: |
| 123 |
pass |
| 124 |
|
| 125 |
|
| 126 |
error_console_mock.print.assert_called() |
| 127 |
|
| 128 |
|
| 129 |
def test_explicit_error_console_parameter_overrides(mocker): |
| 130 |
"""Test that explicit error_console parameter overrides default.""" |
| 131 |
from rich.console import Console |
| 132 |
|
| 133 |
|
| 134 |
default_error_console = mocker.MagicMock(spec=Console) |
| 135 |
|
| 136 |
|
| 137 |
override_error_console = mocker.MagicMock(spec=Console) |
| 138 |
|
| 139 |
app = App(error_console=default_error_console) |
| 140 |
|
| 141 |
@app.default |
| 142 |
def main(name: str): |
| 143 |
pass |
| 144 |
|
| 145 |
try: |
| 146 |
app("--invalid", error_console=override_error_console, exit_on_error=False, print_error=True) |
| 147 |
except UnknownOptionError: |
| 148 |
pass |
| 149 |
|
| 150 |
|
| 151 |
override_error_console.print.assert_called() |
| 152 |
|
| 153 |
default_error_console.print.assert_not_called() |
| 154 |
|
| 155 |
|
| 156 |
def test_error_console_with_parse_args(mocker): |
| 157 |
"""Test error_console works with parse_args method.""" |
| 158 |
from rich.console import Console |
| 159 |
|
| 160 |
error_console_mock = mocker.MagicMock(spec=Console) |
| 161 |
|
| 162 |
app = App(error_console=error_console_mock) |
| 163 |
|
| 164 |
@app.default |
| 165 |
def main(name: str): |
| 166 |
pass |
| 167 |
|
| 168 |
with pytest.raises(UnknownOptionError): |
| 169 |
app.parse_args("--invalid", exit_on_error=False, print_error=True) |
| 170 |
|
| 171 |
|
| 172 |
error_console_mock.print.assert_called() |
| 173 |
|
| 174 |
|
| 175 |
def test_unused_tokens_error_to_stderr(capfd): |
| 176 |
"""Test that UnusedCliTokensError goes to stderr.""" |
| 177 |
app = App() |
| 178 |
|
| 179 |
@app.command |
| 180 |
def foo(): |
| 181 |
pass |
| 182 |
|
| 183 |
with pytest.raises(SystemExit): |
| 184 |
app("foo bar", exit_on_error=True, print_error=True) |
| 185 |
|
| 186 |
captured = capfd.readouterr() |
| 187 |
assert captured.err != "" |
| 188 |
assert "Unused" in captured.err or "bar" in captured.err |
| 189 |
assert captured.out == "" |
| 190 |
|
| 191 |
|
| 192 |
def test_normal_output_to_stdout(capfd): |
| 193 |
"""Test that normal command output goes to stdout.""" |
| 194 |
app = App() |
| 195 |
|
| 196 |
@app.default |
| 197 |
def main(name: str = "World"): |
| 198 |
return f"Hello, {name}!" |
| 199 |
|
| 200 |
app([], result_action="return_none") |
| 201 |
|
| 202 |
captured = capfd.readouterr() |
| 203 |
|
| 204 |
assert captured.err == "" |
| 205 |
|
| 206 |
|
| 207 |
def test_error_console_default_is_stderr(): |
| 208 |
"""Test that default error_console writes to stderr.""" |
| 209 |
app = App() |
| 210 |
|
| 211 |
|
| 212 |
error_console = app.error_console |
| 213 |
|
| 214 |
|
| 215 |
assert error_console.file == sys.stderr |
| 216 |
|
| 217 |
|
| 218 |
def test_console_default_is_stdout(): |
| 219 |
"""Test that default console writes to stdout.""" |
| 220 |
app = App() |
| 221 |
|
| 222 |
|
| 223 |
console = app.console |
| 224 |
|
| 225 |
|
| 226 |
assert console.file == sys.stdout |