-
-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add reusable completer function tutorial, add tutorial test for coverage
- Loading branch information
Showing
7 changed files
with
313 additions
and
2 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
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,38 @@ | ||
from typing import List | ||
|
||
import click | ||
import typer | ||
from click.shell_completion import CompletionItem | ||
|
||
valid_completion_items = [ | ||
("Camila", "The reader of books."), | ||
("Carlos", "The writer of scripts."), | ||
("Sebastian", "The type hints guy."), | ||
] | ||
|
||
|
||
def complete_name(ctx: typer.Context, param: click.Parameter, incomplete: str): | ||
names = (ctx.params.get(param.name) if param.name else []) or [] | ||
for name, help_text in valid_completion_items: | ||
if name.startswith(incomplete) and name not in names: | ||
yield CompletionItem(name, help=help_text) | ||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def main( | ||
name: List[str] = typer.Option( | ||
["World"], help="The name to say hi to.", autocompletion=complete_name | ||
), | ||
greeter: List[str] = typer.Option( | ||
None, help="Who are the greeters?.", autocompletion=complete_name | ||
), | ||
): | ||
for n in name: | ||
print(f"Hello {n}, from {' and '.join(greeter or [])}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
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,41 @@ | ||
from typing import List | ||
|
||
import click | ||
import typer | ||
from click.shell_completion import CompletionItem | ||
from typing_extensions import Annotated | ||
|
||
valid_completion_items = [ | ||
("Camila", "The reader of books."), | ||
("Carlos", "The writer of scripts."), | ||
("Sebastian", "The type hints guy."), | ||
] | ||
|
||
|
||
def complete_name(ctx: typer.Context, param: click.Parameter, incomplete: str): | ||
names = (ctx.params.get(param.name) if param.name else []) or [] | ||
for name, help_text in valid_completion_items: | ||
if name.startswith(incomplete) and name not in names: | ||
yield CompletionItem(name, help=help_text) | ||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def main( | ||
name: Annotated[ | ||
List[str], | ||
typer.Option(help="The name to say hi to.", autocompletion=complete_name), | ||
] = ["World"], | ||
greeter: Annotated[ | ||
List[str], | ||
typer.Option(help="Who are the greeters?.", autocompletion=complete_name), | ||
] = [], | ||
): | ||
for n in name: | ||
print(f"Hello {n}, from {' and '.join(greeter)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
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
90 changes: 90 additions & 0 deletions
90
tests/test_tutorial/test_options_autocompletion/test_tutorial010.py
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,90 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.options_autocompletion import tutorial010 as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_completion(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010.py --name Sebastian --name ", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' in result.stdout | ||
assert '"Sebastian":"The type hints guy."' not in result.stdout | ||
|
||
|
||
def test_completion_greeter1(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010.py --name Sebastian --greeter Ca", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' in result.stdout | ||
assert '"Sebastian":"The type hints guy."' not in result.stdout | ||
|
||
|
||
def test_completion_greeter2(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010.py --name Sebastian --greeter Carlos --greeter ", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' not in result.stdout | ||
assert '"Sebastian":"The type hints guy."' in result.stdout | ||
|
||
|
||
def test_1(): | ||
result = runner.invoke(mod.app, ["--name", "Camila", "--name", "Sebastian"]) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila" in result.output | ||
assert "Hello Sebastian" in result.output | ||
|
||
|
||
def test_2(): | ||
result = runner.invoke( | ||
mod.app, ["--name", "Camila", "--name", "Sebastian", "--greeter", "Carlos"] | ||
) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila, from Carlos" in result.output | ||
assert "Hello Sebastian, from Carlos" in result.output | ||
|
||
|
||
def test_3(): | ||
result = runner.invoke( | ||
mod.app, ["--name", "Camila", "--greeter", "Carlos", "--greeter", "Sebastian"] | ||
) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila, from Carlos and Sebastian" in result.output | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
capture_output=True, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
90 changes: 90 additions & 0 deletions
90
tests/test_tutorial/test_options_autocompletion/test_tutorial010_an.py
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,90 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.options_autocompletion import tutorial010_an as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_completion(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010_AN.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010_an.py --name Sebastian --name ", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' in result.stdout | ||
assert '"Sebastian":"The type hints guy."' not in result.stdout | ||
|
||
|
||
def test_completion_greeter1(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010_AN.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010_an.py --name Sebastian --greeter Ca", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' in result.stdout | ||
assert '"Sebastian":"The type hints guy."' not in result.stdout | ||
|
||
|
||
def test_completion_greeter2(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, " "], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={ | ||
**os.environ, | ||
"_TUTORIAL010_AN.PY_COMPLETE": "complete_zsh", | ||
"_TYPER_COMPLETE_ARGS": "tutorial010_an.py --name Sebastian --greeter Carlos --greeter ", | ||
}, | ||
) | ||
assert '"Camila":"The reader of books."' in result.stdout | ||
assert '"Carlos":"The writer of scripts."' not in result.stdout | ||
assert '"Sebastian":"The type hints guy."' in result.stdout | ||
|
||
|
||
def test_1(): | ||
result = runner.invoke(mod.app, ["--name", "Camila", "--name", "Sebastian"]) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila" in result.output | ||
assert "Hello Sebastian" in result.output | ||
|
||
|
||
def test_2(): | ||
result = runner.invoke( | ||
mod.app, ["--name", "Camila", "--name", "Sebastian", "--greeter", "Carlos"] | ||
) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila, from Carlos" in result.output | ||
assert "Hello Sebastian, from Carlos" in result.output | ||
|
||
|
||
def test_3(): | ||
result = runner.invoke( | ||
mod.app, ["--name", "Camila", "--greeter", "Carlos", "--greeter", "Sebastian"] | ||
) | ||
assert result.exit_code == 0 | ||
assert "Hello Camila, from Carlos and Sebastian" in result.output | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
capture_output=True, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
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