-
-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add tests for compatibility with Click 8 and 7 and test coverage
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |