Skip to content

Commit

Permalink
Add test for shellcomplete of different path options
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-lei committed Jul 22, 2020
1 parent 0190b78 commit 9f332b9
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions tests/test_shellcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9f332b9

Please sign in to comment.