Skip to content

Commit

Permalink
✅ Add tests for compatibility with Click 8 and 7 and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo committed Aug 29, 2021
1 parent 49739e5 commit 525b6dd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/assets/compat_click7_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import List

import click
import typer

app = typer.Typer()


def shell_complete(
ctx: click.Context, param: click.Parameter, incomplete: str
) -> List[str]:
return ["Jonny"]


@app.command(context_settings={"auto_envvar_prefix": "TEST"})
def main(
name: str = typer.Option("John", hidden=True),
lastname: str = typer.Option("Doe", "/lastname", show_default="Mr. Doe"),
age: int = typer.Option(lambda: 42, show_default=True),
nickname: str = typer.Option("", shell_complete=shell_complete),
):
"""
Say hello.
"""
typer.echo(f"Hello {name} {lastname}, it seems you have {age}, {nickname}")


if __name__ == "__main__":
app()
Empty file added tests/test_compat/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions tests/test_compat/test_option_get_help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import subprocess

from typer.testing import CliRunner

from tests.assets import compat_click7_8 as mod

runner = CliRunner()


def test_hidden_option():
result = runner.invoke(mod.app, ["--help"])
assert result.exit_code == 0
assert "Say hello" in result.output
assert "--name" not in result.output
assert "/lastname" in result.output
assert "TEST_LASTNAME" in result.output
assert "(dynamic)" in result.output


def test_coverage_call():
result = runner.invoke(mod.app)
assert result.exit_code == 0
assert "Hello John Doe, it seems you have 42" in result.output


def test_completion():
result = subprocess.run(
["coverage", "run", mod.__file__, " "],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_COMPAT_CLICK7_8.PY_COMPLETE": "complete_zsh",
"_TYPER_COMPLETE_ARGS": "compat_click7_8.py --nickname ",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert "Jonny" in result.stdout

0 comments on commit 525b6dd

Please sign in to comment.