Skip to content

Commit

Permalink
Merged PR click-contrib#62: click_repl/__init__.py
Browse files Browse the repository at this point in the history
	Added some test cases:   tests/test_command_collection.py
  • Loading branch information
GhostOps77 committed Feb 4, 2023
1 parent 7f5cd1a commit aa968f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 6 additions & 1 deletion click_repl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,12 @@ def get_command():
continue

try:
with group.make_context(None, args, parent=group_ctx) as ctx:
# default_map passes the top-level params to the new group to
# support top-level required params that would reject the
# invocation if missing.
with group.make_context(
None, args, parent=group_ctx, default_map=old_ctx.params
) as ctx:
group.invoke(ctx)
ctx.exit()
except click.ClickException as e:
Expand Down
27 changes: 25 additions & 2 deletions tests/test_command_collection.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import click
from click_repl import ClickCompleter
from click_repl import ClickCompleter, repl
from prompt_toolkit.document import Document


def test_completion():
def test_command_collection():
@click.group()
def foo_group():
pass
Expand All @@ -24,3 +24,26 @@ def foobar_cmd():
completions = list(c.get_completions(Document("foo")))

assert set(x.text for x in completions) == set(["foo-cmd", "foobar-cmd"])


def test_subcommand_invocation():
@click.group(invoke_without_command=True)
@click.option("--user", required=True)
@click.pass_context
def cli(ctx, user):
if ctx.invoked_subcommand is None:
click.echo("Top-level user: {}".format(user))
repl(ctx)

@cli.command()
@click.option("--user")
def c1(user):
click.echo("Executed C1 with {}!".format(user))

c = ClickCompleter(cli)

completions = list(c.get_completions(Document(" ")))
assert set(x.text for x in completions) == set(["--user", "c1"])

completions = list(c.get_completions(Document("c1 ")))
assert set(x.text for x in completions) == set(["--user"])

0 comments on commit aa968f0

Please sign in to comment.