Skip to content

Commit

Permalink
Unhandled exception during autocomplete fix (#109)
Browse files Browse the repository at this point in the history
* Unhandled exception during autocomplete fix

* fix for click 7, var rename

* returned coverage for pytest

* added one more space before comment

---------

Co-authored-by: Ondrej Pirko <ondrej.pirko@ui.com>
  • Loading branch information
opirko and opirko-ui authored Nov 10, 2023
1 parent 0c32dce commit 43c7c50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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, [])

0 comments on commit 43c7c50

Please sign in to comment.