1
|
|
|
""" |
2
|
|
|
For ideas how to test commands based on clicks, see http://click.pocoo.org/5/testing/#basic-testing |
3
|
|
|
""" |
4
|
|
|
import click |
|
|
|
|
5
|
|
|
import pytest |
|
|
|
|
6
|
|
|
from click import Argument, Option |
|
|
|
|
7
|
|
|
from click.testing import CliRunner |
|
|
|
|
8
|
|
|
|
9
|
|
|
from groundwork.patterns.gw_commands_pattern import CommandExistException |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def test_command_plugin_activation(basicApp): |
|
|
|
|
13
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
14
|
|
|
assert plugin is not None |
15
|
|
|
assert plugin.active is True |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
# def test_command_cli_activation(basicApp): |
19
|
|
|
# test = basicApp.commands.start_cli(standalone_mode=True, args=[]) |
20
|
|
|
# print(test) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def test_command_plugin_execution(basicApp): |
|
|
|
|
24
|
|
|
runner = CliRunner() |
25
|
|
|
result = runner.invoke(basicApp.commands._commands["test"].click_command, ["--invalid"]) |
|
|
|
|
26
|
|
|
assert "no such option" in result.output |
27
|
|
|
assert result.exit_code == 2 |
28
|
|
|
|
29
|
|
|
result = runner.invoke(basicApp.commands._commands["test"].click_command, ["--arg", "12345"]) |
|
|
|
|
30
|
|
|
assert "12345" in result.output |
31
|
|
|
assert result.exit_code == 0 |
32
|
|
|
|
33
|
|
|
command = basicApp.commands.get("test") |
34
|
|
|
assert command is not None |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def test_command_multi_registration(basicApp): |
|
|
|
|
38
|
|
|
def _test_command(arg): |
39
|
|
|
print(arg) |
40
|
|
|
|
41
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
42
|
|
|
with pytest.raises(CommandExistException): |
43
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Option(("--arg", "-a"))]) |
|
|
|
|
44
|
|
|
|
45
|
|
|
plugin.commands.unregister("test") |
46
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Option(("--arg", "-a"))]) |
|
|
|
|
47
|
|
|
assert len(basicApp.commands.get()) == 1 |
48
|
|
|
|
49
|
|
|
plugin.commands.register("test2", "my test2 command", _test_command, params=[Option(("--arg", "-a"))]) |
|
|
|
|
50
|
|
|
assert len(basicApp.commands.get()) == 2 |
51
|
|
|
|
52
|
|
|
basicApp.plugins.deactivate(["CommandPlugin"]) |
53
|
|
|
print(basicApp.commands.get().keys()) |
54
|
|
|
assert len(basicApp.commands.get().keys()) == 0 |
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_command_multi_plugin_registration(basicApp, EmptyCommandPlugin): |
|
|
|
|
58
|
|
|
def _test_command(arg): |
59
|
|
|
print(arg) |
60
|
|
|
|
61
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
62
|
|
|
plugin2 = EmptyCommandPlugin(app=basicApp, name="CommandPlugin2") |
63
|
|
|
plugin2.activate() |
64
|
|
|
plugin2.commands.register("test2", "my test2 command", _test_command, params=[Option(("--arg", "-a"))]) |
|
|
|
|
65
|
|
|
assert len(basicApp.commands.get()) == 2 |
66
|
|
|
assert len(plugin.commands.get()) == 1 |
67
|
|
|
assert len(plugin2.commands.get()) == 1 |
68
|
|
|
|
69
|
|
|
basicApp.plugins.deactivate(["CommandPlugin2"]) |
70
|
|
|
assert len(basicApp.commands.get()) == 1 |
71
|
|
|
assert len(plugin.commands.get()) == 1 |
72
|
|
|
assert len(plugin2.commands.get()) == 0 |
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
View Code Duplication |
def test_command_mandatory_argument(basicApp): |
|
|
|
|
76
|
|
|
def _test_command(*args, **kwargs): |
77
|
|
|
print(args) |
78
|
|
|
print(kwargs) |
79
|
|
|
|
80
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
81
|
|
|
plugin.commands.unregister("test") |
82
|
|
|
# register a command with a mandatory argument |
83
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Argument(("man_arg",), required=True)]) |
|
|
|
|
84
|
|
|
# call command with argument -> expect ok |
85
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["arg"]) |
86
|
|
|
assert result.exit_code == 0 |
87
|
|
|
|
88
|
|
|
# call command with no argument -> expect error |
89
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, []) |
90
|
|
|
assert result.exit_code == 2 |
91
|
|
|
|
92
|
|
|
|
93
|
|
View Code Duplication |
def test_command_optional_argument(basicApp): |
|
|
|
|
94
|
|
|
def _test_command(*args, **kwargs): |
95
|
|
|
print(args) |
96
|
|
|
print(kwargs) |
97
|
|
|
|
98
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
99
|
|
|
plugin.commands.unregister("test") |
100
|
|
|
# register a command with an optional argument |
101
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Argument(("opt_arg",), required=False)]) |
|
|
|
|
102
|
|
|
# call command with argument -> expect ok |
103
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["arg"]) |
104
|
|
|
assert result.exit_code == 0 |
105
|
|
|
|
106
|
|
|
# call command with no argument -> expect ok |
107
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, []) |
108
|
|
|
assert result.exit_code == 0 |
109
|
|
|
|
110
|
|
|
|
111
|
|
View Code Duplication |
def test_command_mandatory_option(basicApp): |
|
|
|
|
112
|
|
|
def _test_command(*args, **kwargs): |
113
|
|
|
print(args) |
114
|
|
|
print(kwargs) |
115
|
|
|
|
116
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
117
|
|
|
plugin.commands.unregister("test") |
118
|
|
|
# register a command with a mandatory option |
119
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Option(["--man-opt"], required=True)]) |
|
|
|
|
120
|
|
|
# call command with option --man-opt -> expect ok |
121
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["--man-opt", 123]) |
122
|
|
|
assert result.exit_code == 0 |
123
|
|
|
|
124
|
|
|
# call command with no option -> expect error |
125
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, []) |
126
|
|
|
assert result.exit_code == 2 |
127
|
|
|
|
128
|
|
|
|
129
|
|
View Code Duplication |
def test_command_optional_option(basicApp): |
|
|
|
|
130
|
|
|
def _test_command(*args, **kwargs): |
131
|
|
|
print(args) |
132
|
|
|
print(kwargs) |
133
|
|
|
|
134
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
135
|
|
|
plugin.commands.unregister("test") |
136
|
|
|
# register a command with a mandatory option |
137
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Option(["--opt-opt"], required=False)]) |
|
|
|
|
138
|
|
|
# call command with option --opt-opt -> expect ok |
139
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ["--opt-opt", 123]) |
140
|
|
|
assert result.exit_code == 0 |
141
|
|
|
|
142
|
|
|
# call command with no option -> expect ok |
143
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, []) |
144
|
|
|
assert result.exit_code == 0 |
145
|
|
|
|
146
|
|
|
|
147
|
|
View Code Duplication |
def test_command_path_argument(basicApp): |
|
|
|
|
148
|
|
|
def _test_command(*args, **kwargs): |
149
|
|
|
print(args) |
150
|
|
|
print(kwargs) |
151
|
|
|
|
152
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
153
|
|
|
plugin.commands.unregister("test") |
154
|
|
|
# register a command with a path argument, that must exist |
155
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Argument(("man_arg",), |
|
|
|
|
156
|
|
|
type=click.Path(exists=True))]) |
|
|
|
|
157
|
|
|
# call command with existing path as argument -> expect ok |
158
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, [__file__]) |
159
|
|
|
assert result.exit_code == 0 |
160
|
|
|
|
161
|
|
|
# call command with non-existing path as argument -> expect error |
162
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['no such path']) |
163
|
|
|
assert result.exit_code == 2 |
164
|
|
|
|
165
|
|
|
|
166
|
|
View Code Duplication |
def test_command_path_option(basicApp): |
|
|
|
|
167
|
|
|
def _test_command(*args, **kwargs): |
168
|
|
|
print(args) |
169
|
|
|
print(kwargs) |
170
|
|
|
|
171
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
172
|
|
|
plugin.commands.unregister("test") |
173
|
|
|
# register a command with a path option, that must exist |
174
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Option(["--path"], |
175
|
|
|
type=click.Path(exists=True))]) |
|
|
|
|
176
|
|
|
# call command with existing path as option -> expect ok |
177
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--path', __file__]) |
178
|
|
|
assert result.exit_code == 0 |
179
|
|
|
|
180
|
|
|
# call command with non-existing path as option -> expect error |
181
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--path', 'no such path']) |
|
|
|
|
182
|
|
|
assert result.exit_code == 2 |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
def test_command_flag_on_off(basicApp): |
|
|
|
|
186
|
|
|
def _test_command(*args, **kwargs): |
187
|
|
|
print(args) |
188
|
|
|
print(kwargs) |
189
|
|
|
if not kwargs['flag_on']: |
190
|
|
|
raise Exception |
191
|
|
|
|
192
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
193
|
|
|
plugin.commands.unregister("test") |
194
|
|
|
# register a command with an on/off flag; command throws exception if flag is off |
195
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Option(["--flag-on/--flag-off"])]) |
|
|
|
|
196
|
|
|
|
197
|
|
|
# call command with --flag-on (True) -> expect ok |
198
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--flag-on']) |
199
|
|
|
assert result.exit_code == 0 |
200
|
|
|
|
201
|
|
|
# call command with --flag-off (False) -> expect error |
202
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['--flag-off']) |
203
|
|
|
assert result.exit_code == -1 |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
def test_command_flag_count(basicApp): |
|
|
|
|
207
|
|
|
def _test_command(*args, **kwargs): |
208
|
|
|
print(args) |
209
|
|
|
print(kwargs) |
210
|
|
|
assert kwargs['flag'] == 3 |
211
|
|
|
|
212
|
|
|
plugin = basicApp.plugins.get("CommandPlugin") |
213
|
|
|
plugin.commands.unregister("test") |
214
|
|
|
# register a command with a countable flag |
215
|
|
|
plugin.commands.register("test", "my test command", _test_command, params=[Option(["-f", "--flag"], count=True)]) |
|
|
|
|
216
|
|
|
|
217
|
|
|
# call command with --fff -> count == 3 is asserted in the command handler |
218
|
|
|
result = CliRunner().invoke(basicApp.commands.get("test").click_command, ['-fff']) |
219
|
|
|
assert result.exit_code == 0 |
220
|
|
|
|