1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
# -*- coding: utf-8 -*-
import click
from click.testing import CliRunner
import pytest
from click_default_group import DefaultGroup
@click.group(cls=DefaultGroup, default='foo', invoke_without_command=True)
@click.option('--group-only', is_flag=True)
def cli(group_only):
# Called if invoke_without_command=True.
if group_only:
click.echo('--group-only passed.')
@cli.command()
@click.option('--foo', default='foo')
def foo(foo):
click.echo(foo)
@cli.command()
def bar():
click.echo('bar')
r = CliRunner()
def test_default_command_with_arguments():
assert r.invoke(cli, ['--foo', 'foooo']).output == 'foooo\n'
assert 'no such option' in r.invoke(cli, ['-x']).output.lower()
def test_group_arguments():
assert r.invoke(cli, ['--group-only']).output == '--group-only passed.\n'
def test_explicit_command():
assert r.invoke(cli, ['foo']).output == 'foo\n'
assert r.invoke(cli, ['bar']).output == 'bar\n'
def test_set_ignore_unknown_options_to_false():
with pytest.raises(ValueError):
DefaultGroup(ignore_unknown_options=False)
def test_default_if_no_args():
cli = DefaultGroup()
@cli.command()
@click.argument('foo', required=False)
@click.option('--bar')
def foobar(foo, bar):
click.echo(foo)
click.echo(bar)
cli.set_default_command(foobar)
assert r.invoke(cli, []).output.startswith('Usage:')
assert r.invoke(cli, ['foo']).output == 'foo\n\n'
assert r.invoke(cli, ['foo', '--bar', 'bar']).output == 'foo\nbar\n'
cli.default_if_no_args = True
assert r.invoke(cli, []).output == '\n\n'
def test_format_commands():
help = r.invoke(cli, ['--help']).output
assert 'foo*' in help
assert 'bar*' not in help
assert 'bar' in help
def test_deprecation():
# @cli.command(default=True) has been deprecated since 1.2.
cli = DefaultGroup()
pytest.deprecated_call(cli.command, default=True)
if __name__ == '__main__':
cli()
|