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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
import re
import click
import pytest
import cloup
from cloup._util import reindent
from tests.util import new_dummy_func
def test_command_handling_of_unknown_argument():
with pytest.raises(TypeError, match='Hint: you set `cls='):
cloup.command(cls=click.Command, align_option_groups=True)(new_dummy_func())
with pytest.raises(TypeError, match='nonexisting') as info:
cloup.command(nonexisting=True)(new_dummy_func())
assert re.search(str(info.value), 'Hint') is None
def test_group_raises_if_cls_is_not_subclass_of_click_Group():
cloup.group()
cloup.group(cls=click.Group)
cloup.group(cls=cloup.Group)
with pytest.raises(TypeError):
cloup.group(cls=click.Command)
def test_group_handling_of_unknown_argument():
with pytest.raises(TypeError, match='Hint'):
cloup.group(cls=click.Group, align_sections=True)(new_dummy_func())
with pytest.raises(TypeError) as info:
cloup.group(unexisting_arg=True)(new_dummy_func())
assert re.search(str(info.value), 'Hint') is None
def test_command_works_with_no_parameters(runner):
cmd = cloup.Command(name='cmd', callback=new_dummy_func())
res = runner.invoke(cmd, '--help')
assert res.output == reindent("""
Usage: cmd [OPTIONS]
Options:
--help Show this message and exit.
""")
def test_group_works_with_no_params_and_subcommands(runner):
cmd = cloup.Group(name='cmd')
res = runner.invoke(cmd, '--help')
assert res.output == reindent("""
Usage: cmd [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
""")
class TestDidYouMean:
@pytest.fixture(scope="class")
def cmd(self):
cmd = cloup.Group(name="cmd")
subcommands = [
('install', ['ins']),
('remove', ['rm']),
('clear', [])
]
for name, aliases in subcommands:
cmd.add_command(
cloup.Command(name=name, aliases=aliases, callback=new_dummy_func()))
return cmd
def test_with_no_matches(self, runner, cmd):
res = runner.invoke(cmd, 'asdfdsgdfgdf')
assert res.output == reindent("""
Usage: cmd [OPTIONS] COMMAND [ARGS]...
Try 'cmd --help' for help.
Error: No such command 'asdfdsgdfgdf'.
""")
def test_with_one_match(self, runner, cmd):
res = runner.invoke(cmd, 'clearr')
assert res.output == reindent("""
Usage: cmd [OPTIONS] COMMAND [ARGS]...
Try 'cmd --help' for help.
Error: No such command 'clearr'. Did you mean 'clear'?
""")
def test_with_multiple_matches(self, runner, cmd):
res = runner.invoke(cmd, 'inst')
assert res.output == reindent("""
Usage: cmd [OPTIONS] COMMAND [ARGS]...
Try 'cmd --help' for help.
Error: No such command 'inst'. Did you mean one of these?
ins
install
""")
@pytest.mark.parametrize("decorator", [cloup.command, cloup.group])
def test_error_is_raised_when_command_decorators_are_used_without_parenthesis(decorator):
with pytest.raises(Exception, match="parenthesis"):
@decorator
def cmd():
pass
def test_error_is_raised_when_group_subcommand_decorators_are_used_without_parenthesis():
@cloup.group()
def root():
pass
with pytest.raises(Exception, match="parenthesis"):
@root.group
def subgroup():
pass
with pytest.raises(Exception, match="parenthesis"):
@root.command
def subcommand():
pass
def test_group_command_class_is_used_to_create_subcommands(runner):
class CustomCommand(cloup.Command):
def __init__(self, *args, **kwargs):
kwargs.setdefault("context_settings", {"help_option_names": ("--help", "-h")})
super().__init__(*args, **kwargs)
class CustomGroup(cloup.Group):
command_class = CustomCommand
@cloup.group("cli", cls=CustomGroup)
def my_cli():
pass
@my_cli.command()
def subcommand():
pass
assert isinstance(subcommand, CustomCommand)
res = runner.invoke(my_cli, ["subcommand", "--help"])
assert res.output == reindent("""
Usage: cli subcommand [OPTIONS]
Options:
-h, --help Show this message and exit.
""")
def test_group_class_is_used_to_create_subgroups(runner):
class CustomGroup(cloup.Group):
group_class = type
class OtherCustomGroup(cloup.Group):
group_class = cloup.Group
@cloup.group("cli", cls=CustomGroup)
def my_cli():
pass
@my_cli.group()
def sub_group():
pass
@my_cli.group(cls=OtherCustomGroup)
def other_group():
pass
@other_group.group()
def other_sub_group():
pass
assert isinstance(sub_group, CustomGroup)
assert isinstance(other_group, OtherCustomGroup)
assert isinstance(other_sub_group, cloup.Group)
|