Skip to content
19 changes: 18 additions & 1 deletion tests/test_completion/test_sanitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion typer/_completion_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"'

Expand Down
Loading