From 9f332b9a912ce3565779022f567d6b13b30d1ff3 Mon Sep 17 00:00:00 2001 From: Amy Date: Wed, 22 Jul 2020 12:39:20 -0400 Subject: [PATCH] Add test for shellcomplete of different path options --- tests/test_shellcomplete.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/test_shellcomplete.py b/tests/test_shellcomplete.py index 4753b898d..314517ed9 100644 --- a/tests/test_shellcomplete.py +++ b/tests/test_shellcomplete.py @@ -16,14 +16,19 @@ def get_completions(cli, args, incomplete): return [c[1] for c in do_complete(cli, cli.name, args, incomplete)] -def get_partial_value(cli, args, incomplete): +def get_partial_value_and_ctx(cli, args, incomplete): prog_name = cli.name all_args = copy.deepcopy(args) ctx = resolve_ctx(cli, prog_name, args) if ctx is None: return [] - return resolve_partial_value(ctx, all_args, incomplete)[0] + return resolve_partial_value(ctx, all_args, incomplete)[0], ctx + + +def get_partial_value(cli, args, incomplete): + partial_value, ctx = get_partial_value_and_ctx(cli, args, incomplete) + return partial_value def test_partial_value_command(): @@ -84,6 +89,30 @@ def complete(self): add_completion_class("myshell", MyComplete) +def test_shell_complete_path(): + @click.command() + @click.option("--default", type=click.Path()) + @click.option("--dir", type=click.Path(file_okay=False)) + @click.option("--no-dir", type=click.Path(dir_okay=False)) + def cli(default, dir, no_dir): + pass + + args = ["--default"] + incomplete = "../" + opt, ctx = get_partial_value_and_ctx(cli, args, incomplete) + assert opt.shell_complete(ctx, args, incomplete) == [("file", incomplete, None)] + + args = ["--dir"] + incomplete = "../" + opt, ctx = get_partial_value_and_ctx(cli, args, incomplete) + assert opt.shell_complete(ctx, args, incomplete) == [("dir", incomplete, None)] + + args = ["--no-dir"] + incomplete = "../" + opt, ctx = get_partial_value_and_ctx(cli, args, incomplete) + assert opt.shell_complete(ctx, args, incomplete) == [("file", incomplete, None)] + + def test_single_command(): @click.command() @click.option("--opt")