Debian Codegraph @

tests/test_command_decorators.py

raw
1 import pytest
2
3 import click
4
5
6 def test_command_no_parens(runner):
7 @click.command
8 def cli():
9 click.echo("hello")
10
11 result = runner.invoke(cli)
12 assert result.exception is None
13 assert result.output == "hello\n"
14
15
16 def test_custom_command_no_parens(runner):
17 class CustomCommand(click.Command):
18 pass
19
20 class CustomGroup(click.Group):
21 command_class = CustomCommand
22
23 @click.group(cls=CustomGroup)
24 def grp():
25 pass
26
27 @grp.command
28 def cli():
29 click.echo("hello custom command class")
30
31 result = runner.invoke(cli)
32 assert result.exception is None
33 assert result.output == "hello custom command class\n"
34
35
36 def test_group_no_parens(runner):
37 @click.group
38 def grp():
39 click.echo("grp1")
40
41 @grp.command
42 def cmd1():
43 click.echo("cmd1")
44
45 @grp.group
46 def grp2():
47 click.echo("grp2")
48
49 @grp2.command
50 def cmd2():
51 click.echo("cmd2")
52
53 result = runner.invoke(grp, ["cmd1"])
54 assert result.exception is None
55 assert result.output == "grp1\ncmd1\n"
56
57 result = runner.invoke(grp, ["grp2", "cmd2"])
58 assert result.exception is None
59 assert result.output == "grp1\ngrp2\ncmd2\n"
60
61
62 def test_params_argument(runner):
63 opt = click.Argument(["a"])
64
65 @click.command(params=[opt])
66 @click.argument("b")
67 def cli(a, b):
68 click.echo(f"{a} {b}")
69
70 assert cli.params[0].name == "a"
71 assert cli.params[1].name == "b"
72 result = runner.invoke(cli, ["1", "2"])
73 assert result.output == "1 2\n"
74
75
76 @pytest.mark.parametrize(
77 "name",
78 [
79 "init_data",
80 "init_data_command",
81 "init_data_cmd",
82 "init_data_group",
83 "init_data_grp",
84 ],
85 )
86 def test_generate_name(name: str) -> None:
87 def f():
88 pass
89
90 f.__name__ = name
91 f = click.command(f)
92 assert f.name == "init-data"