| 1 | import textwrap |
| 2 | from io import StringIO |
| 3 | |
| 4 | import pytest |
| 5 | from rich.console import Console |
| 6 | |
| 7 | import cyclopts |
| 8 | from cyclopts import CoercionError |
| 9 | |
| 10 | |
| 11 | def test_error_formatter_default_none(): |
| 12 | app = cyclopts.App() |
| 13 | assert app.error_formatter is None |
| 14 | |
| 15 | |
| 16 | def test_error_formatter_custom(): |
| 17 | """A custom error_formatter replaces CycloptsPanel output.""" |
| 18 | buf = StringIO() |
| 19 | error_console = Console( |
| 20 | file=buf, width=70, force_terminal=True, highlight=False, color_system=None, legacy_windows=False |
| 21 | ) |
| 22 | |
| 23 | def my_formatter(e): |
| 24 | return f"error: {e}" |
| 25 | |
| 26 | app = cyclopts.App(error_formatter=my_formatter, result_action="return_value") |
| 27 | |
| 28 | @app.default |
| 29 | def main(value: int): |
| 30 | pass |
| 31 | |
| 32 | with pytest.raises(CoercionError): |
| 33 | app.parse_args("abc", exit_on_error=False, error_console=error_console) |
| 34 | |
| 35 | assert buf.getvalue() == 'error: Invalid value for VALUE: unable to convert "abc" into int.\n' |
| 36 | |
| 37 | |
| 38 | def test_error_formatter_none_uses_cyclopts_panel(): |
| 39 | """When error_formatter is None, the default CycloptsPanel is used.""" |
| 40 | buf = StringIO() |
| 41 | error_console = Console( |
| 42 | file=buf, width=70, force_terminal=True, highlight=False, color_system=None, legacy_windows=False |
| 43 | ) |
| 44 | |
| 45 | app = cyclopts.App(result_action="return_value") |
| 46 | |
| 47 | @app.default |
| 48 | def main(value: int): |
| 49 | pass |
| 50 | |
| 51 | with pytest.raises(CoercionError): |
| 52 | app.parse_args("abc", exit_on_error=False, error_console=error_console) |
| 53 | |
| 54 | expected = textwrap.dedent( |
| 55 | """\ |
| 56 | ╭─ Error ────────────────────────────────────────────────────────────╮ |
| 57 | │ Invalid value for VALUE: unable to convert "abc" into int. │ |
| 58 | ╰────────────────────────────────────────────────────────────────────╯ |
| 59 | """ |
| 60 | ) |
| 61 | assert buf.getvalue() == expected |
| 62 | |
| 63 | |
| 64 | def test_error_formatter_runtime_override(): |
| 65 | """error_formatter can be passed as a runtime argument to parse_args.""" |
| 66 | buf = StringIO() |
| 67 | error_console = Console( |
| 68 | file=buf, width=70, force_terminal=True, highlight=False, color_system=None, legacy_windows=False |
| 69 | ) |
| 70 | |
| 71 | def my_formatter(e): |
| 72 | return f"custom: {e}" |
| 73 | |
| 74 | app = cyclopts.App(result_action="return_value") |
| 75 | |
| 76 | @app.default |
| 77 | def main(value: int): |
| 78 | pass |
| 79 | |
| 80 | with pytest.raises(CoercionError): |
| 81 | app.parse_args("abc", exit_on_error=False, error_console=error_console, error_formatter=my_formatter) |
| 82 | |
| 83 | assert buf.getvalue() == 'custom: Invalid value for VALUE: unable to convert "abc" into int.\n' |
| 84 | |
| 85 | |
| 86 | def test_error_formatter_inherited_by_subcommand(): |
| 87 | """Subcommands inherit error_formatter from the parent app.""" |
| 88 | buf = StringIO() |
| 89 | error_console = Console( |
| 90 | file=buf, width=120, force_terminal=True, highlight=False, color_system=None, legacy_windows=False |
| 91 | ) |
| 92 | |
| 93 | def my_formatter(e): |
| 94 | return f"inherited: {e}" |
| 95 | |
| 96 | app = cyclopts.App(error_formatter=my_formatter, result_action="return_value") |
| 97 | |
| 98 | @app.command |
| 99 | def sub(value: int): |
| 100 | pass |
| 101 | |
| 102 | with pytest.raises(CoercionError): |
| 103 | app.parse_args("sub abc", exit_on_error=False, error_console=error_console) |
| 104 | |
| 105 | assert buf.getvalue() == 'inherited: Invalid value for VALUE: unable to convert "abc" into int.\n' |
| 106 | |
| 107 | |
| 108 | def test_error_formatter_call(): |
| 109 | """error_formatter works when using __call__ instead of parse_args.""" |
| 110 | buf = StringIO() |
| 111 | error_console = Console( |
| 112 | file=buf, width=70, force_terminal=True, highlight=False, color_system=None, legacy_windows=False |
| 113 | ) |
| 114 | |
| 115 | def my_formatter(e): |
| 116 | return f"call: {e}" |
| 117 | |
| 118 | app = cyclopts.App(error_formatter=my_formatter, result_action="return_value") |
| 119 | |
| 120 | @app.default |
| 121 | def main(value: int): |
| 122 | pass |
| 123 | |
| 124 | with pytest.raises(SystemExit): |
| 125 | app("abc", error_console=error_console) |
| 126 | |
| 127 | assert buf.getvalue() == 'call: Invalid value for VALUE: unable to convert "abc" into int.\n' |