Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧐 Investigate issue with multi hyphens #1095

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/test_completion/dashes_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import typer

image_desc = [
("alpine-latest-666", "latest alpine image"),
("alpine-hello-666", "fake image: for testing"),
("something-else-666", "yet another image"),
]


def _complete(incomplete: str) -> str:
for image, desc in image_desc:
if image.startswith(incomplete):
yield image, desc


app = typer.Typer()


@app.command()
def image(name: str = typer.Option(autocompletion=_complete)):
typer.echo(name)


if __name__ == "__main__":
app()
219 changes: 219 additions & 0 deletions tests/test_completion/test_completion_option_dashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import os
import subprocess
import sys

from . import dashes_example as mod


def test_script():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "--name", "DeadPool"],
capture_output=True,
encoding="utf-8",
)
assert result.returncode == 0
assert "DeadPool" in result.stdout


def test_completion_dashes_bash_all():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_bash",
"COMP_WORDS": "dashes_example.py --name ",
"COMP_CWORD": "2",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" in result.stdout


def test_completion_dashes_bash_partial():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_bash",
"COMP_WORDS": "dashes_example.py --name alpine ",
"COMP_CWORD": "2",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" not in result.stdout


def test_completion_dashes_bash_single():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_bash",
"COMP_WORDS": "dashes_example.py --name alpine-latest ",
"COMP_CWORD": "2",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" not in result.stdout
assert "something-else-666" not in result.stdout


def test_completion_dashes_zsh_all():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_zsh",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name ",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" in result.stdout


def test_completion_dashes_zsh_partial():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_zsh",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name alpine",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" not in result.stdout


def test_completion_dashes_zsh_single():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_zsh",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name alpine-latest",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" not in result.stdout
assert "something-else-666" not in result.stdout


def test_completion_dashes_powershell_all():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_powershell",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name ",
"_TYPER_COMPLETE_WORD_TO_COMPLETE": "",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" in result.stdout


def test_completion_dashes_powershell_partial():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_powershell",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name alpine",
"_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" not in result.stdout


def test_completion_dashes_powershell_single():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_powershell",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name alpine-latest",
"_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine-latest",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" not in result.stdout
assert "something-else-666" not in result.stdout


def test_completion_dashes_pwsh_all():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_pwsh",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name",
},
)

assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" in result.stdout


def test_completion_dashes_pwsh_partial():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_pwsh",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name alpine",
"_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" in result.stdout
assert "something-else-666" not in result.stdout


def test_completion_dashes_pwsh_single():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, " "],
capture_output=True,
encoding="utf-8",
env={
**os.environ,
"_DASHES_EXAMPLE.PY_COMPLETE": "complete_pwsh",
"_TYPER_COMPLETE_ARGS": "dashes_example.py --name alpine-latest",
"_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine-latest",
},
)
assert "alpine-latest-666" in result.stdout
assert "alpine-hello-666" not in result.stdout
assert "something-else-666" not in result.stdout


# TODO: tests for complete_fish
Loading