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

Unhandled exception during autocomplete fix #109

Merged
merged 4 commits into from
Nov 10, 2023
Merged
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
5 changes: 4 additions & 1 deletion click_repl/_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ def get_completions(self, document, complete_event=None):

if self.parsed_args != args:
self.parsed_args = args
self.parsed_ctx = _resolve_context(args, self.ctx)
try:
self.parsed_ctx = _resolve_context(args, self.ctx)
except Exception:
return [] # autocompletion for nonexistent cmd can throw here
self.ctx_command = self.parsed_ctx.command

if getattr(self.ctx_command, "hidden", False):
Expand Down
35 changes: 35 additions & 0 deletions tests/test_completion/test_common_tests/test_cmd_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import click
from unittest import TestCase
from prompt_toolkit.document import Document
from click_repl import ClickCompleter


@click.group()
def cli():
pass


@cli.group()
def cmd():
pass


@cmd.command()
def subcmd():
pass


class Test_Command_Autocompletion(TestCase):
def setUp(self):
self.c = ClickCompleter(cli, click.Context(cli))

def test_valid_subcmd(self):
res = list(self.c.get_completions(Document("cmd s")))
self.assertListEqual([i.text for i in res], ["subcmd"])

def test_not_valid_subcmd(self):
try:
res = list(self.c.get_completions(Document("not cmd")))
except Exception as e:
self.fail(f"Autocompletion raised exception: {e}")
self.assertListEqual(res, [])