Skip to content

Commit 43c7c50

Browse files
opirkoopirko-ui
andauthored
Unhandled exception during autocomplete fix (#109)
* 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 <[email protected]>
1 parent 0c32dce commit 43c7c50

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

click_repl/_completer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ def get_completions(self, document, complete_event=None):
274274

275275
if self.parsed_args != args:
276276
self.parsed_args = args
277-
self.parsed_ctx = _resolve_context(args, self.ctx)
277+
try:
278+
self.parsed_ctx = _resolve_context(args, self.ctx)
279+
except Exception:
280+
return [] # autocompletion for nonexistent cmd can throw here
278281
self.ctx_command = self.parsed_ctx.command
279282

280283
if getattr(self.ctx_command, "hidden", False):
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import click
2+
from unittest import TestCase
3+
from prompt_toolkit.document import Document
4+
from click_repl import ClickCompleter
5+
6+
7+
@click.group()
8+
def cli():
9+
pass
10+
11+
12+
@cli.group()
13+
def cmd():
14+
pass
15+
16+
17+
@cmd.command()
18+
def subcmd():
19+
pass
20+
21+
22+
class Test_Command_Autocompletion(TestCase):
23+
def setUp(self):
24+
self.c = ClickCompleter(cli, click.Context(cli))
25+
26+
def test_valid_subcmd(self):
27+
res = list(self.c.get_completions(Document("cmd s")))
28+
self.assertListEqual([i.text for i in res], ["subcmd"])
29+
30+
def test_not_valid_subcmd(self):
31+
try:
32+
res = list(self.c.get_completions(Document("not cmd")))
33+
except Exception as e:
34+
self.fail(f"Autocompletion raised exception: {e}")
35+
self.assertListEqual(res, [])

0 commit comments

Comments
 (0)