| 1 |
from textwrap import dedent |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from cyclopts import CycloptsError |
| 6 |
from cyclopts.exceptions import MissingArgumentError, UnknownCommandError |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture |
| 10 |
def mock_get_function_info(mocker): |
| 11 |
mocker.patch("cyclopts.exceptions._get_function_info", return_value=("FILENAME", 100)) |
| 12 |
|
| 13 |
|
| 14 |
def test_runtime_exception_not_enough_tokens(app, console, mock_get_function_info): |
| 15 |
@app.default |
| 16 |
def foo(a: tuple[int, int, int]): |
| 17 |
pass |
| 18 |
|
| 19 |
with console.capture() as capture, pytest.raises(CycloptsError): |
| 20 |
app(["1", "2"], exit_on_error=False, error_console=console) |
| 21 |
|
| 22 |
actual = capture.get() |
| 23 |
assert actual == ( |
| 24 |
"╭─ Error ────────────────────────────────────────────────────────────╮\n" |
| 25 |
"│ Parameter --a requires 3 positional arguments. Only got 2. │\n" |
| 26 |
"╰────────────────────────────────────────────────────────────────────╯\n" |
| 27 |
) |
| 28 |
|
| 29 |
with console.capture() as capture, pytest.raises(CycloptsError): |
| 30 |
app(["1", "2"], exit_on_error=False, error_console=console, verbose=True) |
| 31 |
|
| 32 |
actual = capture.get() |
| 33 |
assert actual == dedent( |
| 34 |
"""\ |
| 35 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 36 |
│ MissingArgumentError │ |
| 37 |
│ Function defined in file "FILENAME", line 100: │ |
| 38 |
│ foo(a: tuple[int, int, int]) │ |
| 39 |
│ Root Input Tokens: ['1', '2'] │ |
| 40 |
│ Parameter --a requires 3 positional arguments. Only got 2. │ |
| 41 |
│ Parsed: ['1', '2']. │ |
| 42 |
╰────────────────────────────────────────────────────────────────────╯ |
| 43 |
""" |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def test_runtime_exception_missing_parameter(app, console): |
| 48 |
@app.default |
| 49 |
def foo(a, b, c): |
| 50 |
pass |
| 51 |
|
| 52 |
with console.capture() as capture, pytest.raises(CycloptsError): |
| 53 |
app(["1", "2"], exit_on_error=False, error_console=console) |
| 54 |
|
| 55 |
actual = capture.get() |
| 56 |
assert actual == ( |
| 57 |
"╭─ Error ────────────────────────────────────────────────────────────╮\n" |
| 58 |
"│ Parameter --c requires an argument. │\n" |
| 59 |
"╰────────────────────────────────────────────────────────────────────╯\n" |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
def test_runtime_exception_bad_command(app, console): |
| 64 |
with console.capture() as capture, pytest.raises(UnknownCommandError): |
| 65 |
app(["bad-command", "123"], exit_on_error=False, error_console=console) |
| 66 |
|
| 67 |
actual = capture.get() |
| 68 |
assert actual == ( |
| 69 |
"╭─ Error ────────────────────────────────────────────────────────────╮\n" |
| 70 |
'│ Unknown command "bad-command". │\n' |
| 71 |
"╰────────────────────────────────────────────────────────────────────╯\n" |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
def test_runtime_exception_bad_command_recommend(app, console): |
| 76 |
@app.command |
| 77 |
def mad_command(): |
| 78 |
pass |
| 79 |
|
| 80 |
with console.capture() as capture, pytest.raises(UnknownCommandError): |
| 81 |
app(["bad-command", "123"], exit_on_error=False, error_console=console) |
| 82 |
|
| 83 |
actual = capture.get() |
| 84 |
assert actual == dedent( |
| 85 |
"""\ |
| 86 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 87 |
│ Unknown command "bad-command". Did you mean "mad-command"? │ |
| 88 |
│ Available commands: mad-command. │ |
| 89 |
╰────────────────────────────────────────────────────────────────────╯ |
| 90 |
""" |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
def test_runtime_exception_bad_command_recommend_no_show(app, console): |
| 95 |
"""If a command is hidden, do not show recommendations for it.""" |
| 96 |
|
| 97 |
@app.command(show=False) |
| 98 |
def mad_command(): |
| 99 |
pass |
| 100 |
|
| 101 |
@app.command |
| 102 |
def other_command(): |
| 103 |
pass |
| 104 |
|
| 105 |
with console.capture() as capture, pytest.raises(UnknownCommandError): |
| 106 |
app(["bad-command", "123"], exit_on_error=False, error_console=console) |
| 107 |
|
| 108 |
actual = capture.get() |
| 109 |
assert actual == dedent( |
| 110 |
"""\ |
| 111 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 112 |
│ Unknown command "bad-command". Did you mean "other-command"? │ |
| 113 |
│ Available commands: other-command. │ |
| 114 |
╰────────────────────────────────────────────────────────────────────╯ |
| 115 |
""" |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
def test_runtime_exception_bad_command_list_ellipsis(app, console): |
| 120 |
def cmd(): |
| 121 |
pass |
| 122 |
|
| 123 |
app.command(name="cmd1")(cmd) |
| 124 |
app.command(name="cmd2")(cmd) |
| 125 |
app.command(name="cmd3")(cmd) |
| 126 |
app.command(name="cmd4")(cmd) |
| 127 |
app.command(name="cmd5")(cmd) |
| 128 |
app.command(name="cmd6")(cmd) |
| 129 |
app.command(name="cmd7")(cmd) |
| 130 |
app.command(name="cmd8")(cmd) |
| 131 |
app.command(name="cmd9")(cmd) |
| 132 |
|
| 133 |
with console.capture() as capture, pytest.raises(UnknownCommandError): |
| 134 |
app(["cmd", "123"], exit_on_error=False, error_console=console) |
| 135 |
|
| 136 |
actual = capture.get() |
| 137 |
assert actual == dedent( |
| 138 |
"""\ |
| 139 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 140 |
│ Unknown command "cmd". Did you mean "cmd9"? Available commands: │ |
| 141 |
│ cmd1, cmd2, cmd3, cmd4, cmd5, cmd6, cmd7, cmd8, ... │ |
| 142 |
╰────────────────────────────────────────────────────────────────────╯ |
| 143 |
""" |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
def test_runtime_exception_bad_parameter_recommend(app, console): |
| 148 |
@app.command |
| 149 |
def some_command(*, foo: int): |
| 150 |
pass |
| 151 |
|
| 152 |
with console.capture() as capture, pytest.raises(MissingArgumentError): |
| 153 |
app(["some-command", "--boo", "123"], exit_on_error=False, error_console=console) |
| 154 |
|
| 155 |
actual = capture.get() |
| 156 |
assert actual == dedent( |
| 157 |
"""\ |
| 158 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 159 |
│ Command "some-command" parameter --foo requires an argument. Did │ |
| 160 |
│ you mean --foo instead of --boo? │ |
| 161 |
╰────────────────────────────────────────────────────────────────────╯ |
| 162 |
""" |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
def test_runtime_exception_repeat_arguments(app, console): |
| 167 |
@app.default |
| 168 |
def foo(a): |
| 169 |
pass |
| 170 |
|
| 171 |
with console.capture() as capture, pytest.raises(CycloptsError): |
| 172 |
app(["--a=1", "--a=2"], exit_on_error=False, error_console=console) |
| 173 |
|
| 174 |
actual = capture.get() |
| 175 |
assert actual == ( |
| 176 |
"╭─ Error ────────────────────────────────────────────────────────────╮\n" |
| 177 |
"│ Parameter --a specified multiple times. │\n" |
| 178 |
"╰────────────────────────────────────────────────────────────────────╯\n" |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
def test_runtime_exception_missing_tuple(app, console): |
| 183 |
""" |
| 184 |
A bug was found where the "Did you mean" would be inappropriately displayed |
| 185 |
when insufficient tokens were supplied to a tuple type. |
| 186 |
|
| 187 |
https://github.com/BrianPugh/cyclopts/issues/443 |
| 188 |
""" |
| 189 |
|
| 190 |
@app.default |
| 191 |
def main( |
| 192 |
*, |
| 193 |
network_delay: tuple[int, int] | None = None, |
| 194 |
): |
| 195 |
pass |
| 196 |
|
| 197 |
with console.capture() as capture, pytest.raises(MissingArgumentError): |
| 198 |
app(["--network-delay", "1"], exit_on_error=False, error_console=console) |
| 199 |
|
| 200 |
actual = capture.get() |
| 201 |
assert actual == dedent( |
| 202 |
"""\ |
| 203 |
╭─ Error ────────────────────────────────────────────────────────────╮ |
| 204 |
│ Parameter --network-delay requires 2 positional arguments. Only │ |
| 205 |
│ got 1. │ |
| 206 |
╰────────────────────────────────────────────────────────────────────╯ |
| 207 |
""" |
| 208 |
) |