Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_completion/test_completion_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,29 @@ def test_completion_complete_subcommand_noshell(mod: ModuleType):
},
)
assert ("") in result.stdout


def test_completion_complete_output_has_no_carriage_returns(mod: ModuleType):
"""Bash on MSYS2 / Git Bash splits completion output on ``\\n`` only, so
any stray ``\\r`` (introduced by Windows text-mode I/O translating
in-string ``\\n`` to ``\\r\\n``) leaks into the option names and shows
up as ``^M`` between completions.

Regression test for https://github.com/fastapi/typer/issues/202.
"""
file_name = Path(mod.__file__).name
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
env={
**os.environ,
f"_{file_name.upper()}_COMPLETE": "complete_bash",
"COMP_WORDS": f"{file_name} del",
"COMP_CWORD": "1",
},
)
# Read raw bytes — encoding="utf-8" on subprocess.run would hide a
# newline-translation regression by silently decoding it away. The
# in-string separator must remain a bare ``\\n``.
assert b"delete\ndelete-all" in result.stdout
assert b"delete\r\ndelete-all" not in result.stdout
7 changes: 6 additions & 1 deletion typer/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ def shell_complete(

# Typer override to print the completion help msg with Rich
if instruction == "complete":
click.echo(comp.complete())
# Write the completion output as raw bytes so Python's text-mode I/O
# on Windows doesn't translate the in-string ``\n`` separators into
# ``\r\n``. Bash splits on ``\n`` only, and any stray ``\r`` leaks
# into the option names (showing up as ``^M`` between completions in
# MSYS2 / Git Bash). See https://github.com/fastapi/typer/issues/202.
click.echo(comp.complete().encode("utf-8"))
return 0
# Typer override end

Expand Down
Loading