From 7b369b79f20231d077819469bd381e526fe861b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Tue, 31 Dec 2019 11:13:38 +0100 Subject: [PATCH] :white_check_mark: Add tests for completion --- tests/test_completion.py | 47 ++++++++++++++++++++++++++++++++++++++++ typer/completion.py | 6 ++--- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 tests/test_completion.py 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