|
| 1 | +import re |
| 2 | +from typing import Annotated |
| 3 | + |
| 4 | +import pytest |
| 5 | +import typer |
| 6 | +from typer._click.exceptions import NoSuchOption |
| 7 | +from typer.testing import CliRunner |
| 8 | + |
| 9 | +runner = CliRunner() |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(params=["rich", None], ids=["rich", "plain"]) |
| 13 | +def app(request: pytest.FixtureRequest) -> typer.Typer: |
| 14 | + app = typer.Typer(add_completion=False, rich_markup_mode=request.param) |
| 15 | + |
| 16 | + @app.command() |
| 17 | + def no_args() -> None: |
| 18 | + pass # pragma: no cover |
| 19 | + |
| 20 | + @app.command() |
| 21 | + def read_file(value: typer.FileText) -> None: |
| 22 | + pass # pragma: no cover |
| 23 | + |
| 24 | + @app.command() |
| 25 | + def parse( |
| 26 | + value: Annotated[int, typer.Argument(parser=lambda x: int(x, 0))], |
| 27 | + ) -> None: |
| 28 | + pass # pragma: no cover |
| 29 | + |
| 30 | + return app |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("color", [False, True]) |
| 34 | +@pytest.mark.parametrize( |
| 35 | + ("command", "prefix", "message"), |
| 36 | + [ |
| 37 | + ("no-args", "", "Got unexpected extra argument(s)"), |
| 38 | + ("no-args", "--", "No such option"), |
| 39 | + ("read-file", "", "Invalid value"), |
| 40 | + ("parse", "", "Invalid value"), |
| 41 | + ], |
| 42 | + ids=["extra-argument", "unknown-option", "file-open", "custom-parser"], |
| 43 | +) |
| 44 | +@pytest.mark.parametrize( |
| 45 | + ("value", "escaped"), |
| 46 | + [ |
| 47 | + ("\x1b[2J\x1b[H", r"\x1b[2J\x1b[H"), |
| 48 | + ("\x1b]52;c;YWJj\x07", r"\x1b]52;c;YWJj\x07"), |
| 49 | + ("\x1b]52;c;YWJj\x1b\\", "\\x1b]52;c;YWJj\\x1b\\"), |
| 50 | + ("\x9b2J\x9d52;c;YWJj\x9c", r"\x9b2J\x9d52;c;YWJj\x9c"), |
| 51 | + ("\r\n\t\b\x7f", r"\x0d\x0a\x09\x08\x7f"), |
| 52 | + ( |
| 53 | + "text-\u00e9-\u65e5\u672c-\U0001f469\u200d\U0001f4bb-'quoted'-\\text", |
| 54 | + "text-\u00e9-\u65e5\u672c-\U0001f469\u200d\U0001f4bb-'quoted'-\\text", |
| 55 | + ), |
| 56 | + ], |
| 57 | + ids=["screen", "osc-bel", "osc-st", "c1", "whitespace-controls", "unicode"], |
| 58 | +) |
| 59 | +def test_error_values( |
| 60 | + app: typer.Typer, |
| 61 | + command: str, |
| 62 | + prefix: str, |
| 63 | + message: str, |
| 64 | + value: str, |
| 65 | + escaped: str, |
| 66 | + color: bool, |
| 67 | + monkeypatch: pytest.MonkeyPatch, |
| 68 | +) -> None: |
| 69 | + from typer import rich_utils |
| 70 | + |
| 71 | + monkeypatch.delenv("NO_COLOR", raising=False) |
| 72 | + monkeypatch.setenv("TERM", "xterm-256color") |
| 73 | + monkeypatch.setattr(rich_utils, "FORCE_TERMINAL", color) |
| 74 | + monkeypatch.setattr(rich_utils, "MAX_WIDTH", 3000) |
| 75 | + result = runner.invoke(app, [command, prefix + "input-" + value], color=color) |
| 76 | + |
| 77 | + assert result.exit_code == 2 |
| 78 | + # Remove only generated color styles so other terminal controls cannot hide. |
| 79 | + output = re.sub(r"\x1b\[[0-9;]*m", "", result.stderr) |
| 80 | + assert message in output |
| 81 | + assert prefix + "input-" + escaped in output |
| 82 | + assert re.search(r"[\x00-\x09\x0b-\x1f\x7f-\x9f]", output) is None |
| 83 | + |
| 84 | + |
| 85 | +def test_unknown_option_preserves_original_value() -> None: |
| 86 | + value = "--input-\x00\x1f\x7f\x80\x9f" |
| 87 | + error = NoSuchOption(value) |
| 88 | + |
| 89 | + assert error.option_name == value |
| 90 | + assert error.format_message() == r"No such option: --input-\x00\x1f\x7f\x80\x9f" |
| 91 | + |
| 92 | + |
| 93 | +def test_argument_preserves_original_value() -> None: |
| 94 | + app = typer.Typer(add_completion=False) |
| 95 | + value = "input-\x1b[2J\x1b]52;c;YWJj\x07" |
| 96 | + |
| 97 | + @app.command() |
| 98 | + def main(value: str) -> None: |
| 99 | + typer.echo(repr(value)) |
| 100 | + |
| 101 | + result = runner.invoke(app, [value]) |
| 102 | + |
| 103 | + assert result.exit_code == 0 |
| 104 | + assert result.stdout == repr(value) + "\n" |
| 105 | + |
| 106 | + |
| 107 | +def test_echo_preserves_terminal_sequences() -> None: |
| 108 | + app = typer.Typer(add_completion=False) |
| 109 | + value = "\x1b[2J\x1b]52;c;YWJj\x07" |
| 110 | + |
| 111 | + @app.command() |
| 112 | + def main() -> None: |
| 113 | + typer.echo(value, nl=False) |
| 114 | + |
| 115 | + result = runner.invoke(app, color=True) |
| 116 | + |
| 117 | + assert result.exit_code == 0 |
| 118 | + assert result.stdout_bytes == value.encode() |
0 commit comments