diff --git a/tests/test_completion.py b/tests/test_completion.py new file mode 100644 index 0000000..0f64c1a --- /dev/null +++ b/tests/test_completion.py @@ -0,0 +1,47 @@ +import os +import subprocess +import sys +from pathlib import Path + +import typer +from typer.testing import CliRunner + +from first_steps import tutorial001 as mod + +runner = CliRunner() +app = typer.Typer() +app.command()(mod.main) + + +def test_show_completion(): + result = subprocess.run( + [ + "bash", + "-c", + f"{sys.executable} -m coverage run {mod.__file__} --show-completion", + ], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + env={**os.environ, "SHELL": "/bin/bash"}, + ) + assert "_TUTORIAL001.PY_COMPLETE=complete-bash" in result.stdout + + +def test_install_completion(): + bash_completion_path: Path = Path.home() / ".bash_completion" + text = bash_completion_path.read_text() + result = subprocess.run( + [ + "bash", + "-c", + f"{sys.executable} -m coverage run {mod.__file__} --install-completion", + ], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + env={**os.environ, "SHELL": "/bin/bash"}, + ) + new_text = bash_completion_path.read_text() + assert "_TUTORIAL001.PY_COMPLETE=complete-bash" in new_text + bash_completion_path.write_text(text) diff --git a/typer/completion.py b/typer/completion.py index 586cc79..4ef063b 100644 --- a/typer/completion.py +++ b/typer/completion.py @@ -12,7 +12,7 @@ click_completion.init() def install_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any: if not value or ctx.resilient_parsing: - return value + return value # pragma no cover shell, path = click_completion.core.install() click.secho(f"{shell} completion installed in {path}.", fg="green") click.echo("Completion will take effect once you restart the terminal.") @@ -21,7 +21,7 @@ def install_callback(ctx: click.Context, param: click.Parameter, value: Any) -> def show_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any: if not value or ctx.resilient_parsing: - return value + return value # pragma no cover click.echo(click_completion.core.get_code()) sys.exit(0) @@ -45,4 +45,4 @@ def _install_completion_placeholder_function( help="Show completion for the current shell, to copy it or customize the installation.", ), ) -> Any: - pass + pass # pragma no cover