From 7454bef46853f5a39d0cb8ed369a99acb5fff280 Mon Sep 17 00:00:00 2001 From: Ondrej Pirko Date: Thu, 9 Nov 2023 17:21:53 +0100 Subject: [PATCH 1/4] Unhandled exception during autocomplete fix --- click_repl/_completer.py | 5 ++- .../test_common_tests/test_cmd_completion.py | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/test_completion/test_common_tests/test_cmd_completion.py diff --git a/click_repl/_completer.py b/click_repl/_completer.py index 2c592d6..47cbf6a 100644 --- a/click_repl/_completer.py +++ b/click_repl/_completer.py @@ -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): diff --git a/tests/test_completion/test_common_tests/test_cmd_completion.py b/tests/test_completion/test_common_tests/test_cmd_completion.py new file mode 100644 index 0000000..679900d --- /dev/null +++ b/tests/test_completion/test_common_tests/test_cmd_completion.py @@ -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: + completions = list(self.c.get_completions(Document("not cmd"))) + except Exception as e: + self.fail(f"Autocompletion raised exception: {e}") + self.assertListEqual(completions, []) From 6c7eb53362b5da0ac639ac8dd790cba10c019fea Mon Sep 17 00:00:00 2001 From: Ondrej Pirko Date: Fri, 10 Nov 2023 10:23:27 +0100 Subject: [PATCH 2/4] fix for click 7, var rename --- pyproject.toml | 1 - .../test_common_tests/test_cmd_completion.py | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5b5d05d..22fe509 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,6 @@ build-backend = "setuptools.build_meta" [tool.pytest.ini_options] addopts = [ - "--cov=click_repl" ] testpaths = [ diff --git a/tests/test_completion/test_common_tests/test_cmd_completion.py b/tests/test_completion/test_common_tests/test_cmd_completion.py index 679900d..6238668 100644 --- a/tests/test_completion/test_common_tests/test_cmd_completion.py +++ b/tests/test_completion/test_common_tests/test_cmd_completion.py @@ -9,12 +9,12 @@ def cli(): pass -@cli.group +@cli.group() def cmd(): pass -@cmd.command +@cmd.command() def subcmd(): pass @@ -29,7 +29,7 @@ def test_valid_subcmd(self): def test_not_valid_subcmd(self): try: - completions = list(self.c.get_completions(Document("not cmd"))) + res = list(self.c.get_completions(Document("not cmd"))) except Exception as e: self.fail(f"Autocompletion raised exception: {e}") - self.assertListEqual(completions, []) + self.assertListEqual(res, []) From 13b1100b9b0206b4f3d4402dfd5ed7ce553337ca Mon Sep 17 00:00:00 2001 From: Ondrej Pirko Date: Fri, 10 Nov 2023 10:24:38 +0100 Subject: [PATCH 3/4] returned coverage for pytest --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 22fe509..5b5d05d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,7 @@ build-backend = "setuptools.build_meta" [tool.pytest.ini_options] addopts = [ + "--cov=click_repl" ] testpaths = [ From b0aa281e09654f6b320d71a042416bd18295a83e Mon Sep 17 00:00:00 2001 From: Ondrej Pirko Date: Fri, 10 Nov 2023 12:10:45 +0100 Subject: [PATCH 4/4] added one more space before comment --- click_repl/_completer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/click_repl/_completer.py b/click_repl/_completer.py index 47cbf6a..67b8bda 100644 --- a/click_repl/_completer.py +++ b/click_repl/_completer.py @@ -277,7 +277,7 @@ def get_completions(self, document, complete_event=None): try: self.parsed_ctx = _resolve_context(args, self.ctx) except Exception: - return [] # autocompletion for nonexistent cmd can throw here + return [] # autocompletion for nonexistent cmd can throw here self.ctx_command = self.parsed_ctx.command if getattr(self.ctx_command, "hidden", False):