Add tests for completion

This commit is contained in:
Sebastián Ramírez 2019-12-31 11:23:56 +01:00 committed by GitHub
commit dcbe3fc53b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 3 deletions

49
tests/test_completion.py Normal file
View file

@ -0,0 +1,49 @@
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 = ""
if bash_completion_path.is_file():
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