diff --git a/tests/test_completion/test_sanitization.py b/tests/test_completion/test_sanitization.py index 9150b94cd5..c2c1a4e0e7 100644 --- a/tests/test_completion/test_sanitization.py +++ b/tests/test_completion/test_sanitization.py @@ -2,7 +2,8 @@ from unittest.mock import patch import pytest -from typer._completion_classes import _sanitize_help_text +from typer._click.shell_completion import CompletionItem +from typer._completion_classes import ZshComplete, _sanitize_help_text @pytest.mark.parametrize( @@ -36,3 +37,19 @@ def test_sanitize_help_text( with patch("importlib.util.find_spec", return_value=find_spec) as mock_find_spec: assert _sanitize_help_text(help_text) == expected mock_find_spec.assert_called_once_with("rich") + + +@pytest.mark.parametrize( + "help_text, expected", + [ + (":warning: Spin up [bold]now[/bold]", '"up":"⚠ Spin up now"'), + ("Note: careful", '"up":"Note\\\\: careful"'), + ("Cost $5 and `x`", '"up":"Cost \\$5 and \\`x\\`"'), + ], +) +def test_zsh_format_completion_renders_help_before_escaping( + help_text: str, expected: str +): + zsh_complete = ZshComplete(None, {}, "prog", "_PROG_COMPLETE") # type: ignore[arg-type] + item = CompletionItem("up", help=help_text) + assert zsh_complete.format_completion(item) == expected diff --git a/typer/_completion_classes.py b/typer/_completion_classes.py index cfae02c9bf..01fb66785a 100644 --- a/typer/_completion_classes.py +++ b/typer/_completion_classes.py @@ -131,7 +131,7 @@ def escape(s: str) -> str: # the difference with and without escape # return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}" if item.help: - return f'"{escape(item.value)}":"{_sanitize_help_text(escape(item.help))}"' + return f'"{escape(item.value)}":"{escape(_sanitize_help_text(item.help))}"' else: return f'"{escape(item.value)}"'