Add tests for completion

This commit is contained in:
Sebastián Ramírez 2019-12-31 11:13:38 +01:00
parent e9c5cd6061
commit 7b369b79f2
2 changed files with 50 additions and 3 deletions

47
tests/test_completion.py Normal file
View file

@ -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)

View file

@ -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