|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
"""Tests for command handling""" |
|
3
|
|
|
from __future__ import unicode_literals, absolute_import, print_function, division |
|
4
|
|
|
|
|
5
|
|
|
import argparse |
|
6
|
|
|
from contextlib import contextmanager |
|
7
|
|
|
import os |
|
8
|
|
|
|
|
9
|
|
|
import pytest |
|
10
|
|
|
|
|
11
|
|
|
from sopel import config |
|
12
|
|
|
from sopel.cli.run import ( |
|
13
|
|
|
build_parser, |
|
14
|
|
|
get_configuration, |
|
15
|
|
|
get_pid_filename, |
|
16
|
|
|
get_running_pid |
|
17
|
|
|
) |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
@contextmanager |
|
21
|
|
|
def cd(newdir): |
|
22
|
|
|
prevdir = os.getcwd() |
|
23
|
|
|
os.chdir(os.path.expanduser(newdir)) |
|
24
|
|
|
try: |
|
25
|
|
|
yield |
|
26
|
|
|
finally: |
|
27
|
|
|
os.chdir(prevdir) |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
@pytest.fixture |
|
31
|
|
|
def config_dir(tmpdir): |
|
32
|
|
|
"""Pytest fixture used to generate a temporary configuration directory""" |
|
33
|
|
|
test_dir = tmpdir.mkdir("config") |
|
34
|
|
|
test_dir.join('config.cfg').write('') |
|
35
|
|
|
test_dir.join('extra.ini').write('') |
|
36
|
|
|
test_dir.join('module.cfg').write('') |
|
37
|
|
|
test_dir.join('README').write('') |
|
38
|
|
|
|
|
39
|
|
|
return test_dir |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_build_parser_legacy(): |
|
43
|
|
|
"""Assert parser's namespace exposes legacy's options (default values)""" |
|
44
|
|
|
parser = build_parser() |
|
45
|
|
|
options = parser.parse_args(['legacy']) |
|
46
|
|
|
|
|
47
|
|
|
assert isinstance(options, argparse.Namespace) |
|
48
|
|
|
assert hasattr(options, 'config') |
|
49
|
|
|
assert hasattr(options, 'configdir') |
|
50
|
|
|
assert hasattr(options, 'daemonize') |
|
51
|
|
|
assert hasattr(options, 'quiet') |
|
52
|
|
|
assert hasattr(options, 'quit') |
|
53
|
|
|
assert hasattr(options, 'kill') |
|
54
|
|
|
assert hasattr(options, 'restart') |
|
55
|
|
|
assert hasattr(options, 'version') |
|
56
|
|
|
assert hasattr(options, 'version_legacy') |
|
57
|
|
|
assert hasattr(options, 'wizard') |
|
58
|
|
|
assert hasattr(options, 'mod_wizard') |
|
59
|
|
|
assert hasattr(options, 'list_configs') |
|
60
|
|
|
|
|
61
|
|
|
assert options.config == 'default' |
|
62
|
|
|
assert options.configdir == config.DEFAULT_HOMEDIR |
|
63
|
|
|
assert options.daemonize is False |
|
64
|
|
|
assert options.quiet is False |
|
65
|
|
|
assert options.quit is False |
|
66
|
|
|
assert options.kill is False |
|
67
|
|
|
assert options.restart is False |
|
68
|
|
|
assert options.version is False |
|
69
|
|
|
assert options.version_legacy is False |
|
70
|
|
|
assert options.wizard is False |
|
71
|
|
|
assert options.mod_wizard is False |
|
72
|
|
|
assert options.list_configs is False |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
def test_build_parser_legacy_config(): |
|
76
|
|
|
parser = build_parser() |
|
77
|
|
|
options = parser.parse_args(['legacy', '-c', 'custom']) |
|
78
|
|
|
assert options.config == 'custom' |
|
79
|
|
|
|
|
80
|
|
|
options = parser.parse_args(['legacy', '--config', 'custom']) |
|
81
|
|
|
assert options.config == 'custom' |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def test_build_parser_legacy_configdir(): |
|
85
|
|
|
parser = build_parser() |
|
86
|
|
|
|
|
87
|
|
|
options = parser.parse_args(['legacy', '--config-dir', 'custom']) |
|
88
|
|
|
assert options.configdir == 'custom' |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def test_build_parser_legacy_daemonize(): |
|
92
|
|
|
parser = build_parser() |
|
93
|
|
|
options = parser.parse_args(['legacy', '-d']) |
|
94
|
|
|
assert options.daemonize is True |
|
95
|
|
|
|
|
96
|
|
|
options = parser.parse_args(['legacy', '--fork']) |
|
97
|
|
|
assert options.daemonize is True |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
def test_build_parser_legacy_quiet(): |
|
101
|
|
|
parser = build_parser() |
|
102
|
|
|
options = parser.parse_args(['legacy', '--quiet']) |
|
103
|
|
|
assert options.quiet is True |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
def test_build_parser_legacy_quit(): |
|
107
|
|
|
parser = build_parser() |
|
108
|
|
|
options = parser.parse_args(['legacy', '-q']) |
|
109
|
|
|
assert options.quit is True |
|
110
|
|
|
|
|
111
|
|
|
options = parser.parse_args(['legacy', '--quit']) |
|
112
|
|
|
assert options.quit is True |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
def test_build_parser_legacy_kill(): |
|
116
|
|
|
parser = build_parser() |
|
117
|
|
|
options = parser.parse_args(['legacy', '-k']) |
|
118
|
|
|
assert options.kill is True |
|
119
|
|
|
|
|
120
|
|
|
options = parser.parse_args(['legacy', '--kill']) |
|
121
|
|
|
assert options.kill is True |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
def test_build_parser_legacy_restart(): |
|
125
|
|
|
parser = build_parser() |
|
126
|
|
|
options = parser.parse_args(['legacy', '-r']) |
|
127
|
|
|
assert options.restart is True |
|
128
|
|
|
|
|
129
|
|
|
options = parser.parse_args(['legacy', '--restart']) |
|
130
|
|
|
assert options.restart is True |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
def test_build_parser_legacy_version(): |
|
134
|
|
|
parser = build_parser() |
|
135
|
|
|
options = parser.parse_args(['legacy', '-v']) |
|
136
|
|
|
assert options.version is False |
|
137
|
|
|
assert options.version_legacy is True |
|
138
|
|
|
|
|
139
|
|
|
options = parser.parse_args(['legacy', '-V']) |
|
140
|
|
|
assert options.version is True |
|
141
|
|
|
assert options.version_legacy is False |
|
142
|
|
|
|
|
143
|
|
|
options = parser.parse_args(['legacy', '--version']) |
|
144
|
|
|
assert options.version is True |
|
145
|
|
|
assert options.version_legacy is False |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
def test_build_parser_legacy_wizard(): |
|
149
|
|
|
parser = build_parser() |
|
150
|
|
|
options = parser.parse_args(['legacy', '-w']) |
|
151
|
|
|
assert options.wizard is True |
|
152
|
|
|
assert options.mod_wizard is False |
|
153
|
|
|
|
|
154
|
|
|
options = parser.parse_args(['legacy', '--configure-all']) |
|
155
|
|
|
assert options.wizard is True |
|
156
|
|
|
assert options.mod_wizard is False |
|
157
|
|
|
|
|
158
|
|
|
options = parser.parse_args(['legacy', '--configure-modules']) |
|
159
|
|
|
assert options.wizard is False |
|
160
|
|
|
assert options.mod_wizard is True |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
def test_build_parser_legacy_list_config(): |
|
164
|
|
|
parser = build_parser() |
|
165
|
|
|
options = parser.parse_args(['legacy', '-l']) |
|
166
|
|
|
assert options.list_configs is True |
|
167
|
|
|
|
|
168
|
|
|
options = parser.parse_args(['legacy', '--list']) |
|
169
|
|
|
assert options.list_configs is True |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
View Code Duplication |
def test_build_parser_start(): |
|
|
|
|
|
|
173
|
|
|
"""Assert parser's namespace exposes start's options (default values)""" |
|
174
|
|
|
parser = build_parser() |
|
175
|
|
|
options = parser.parse_args(['start']) |
|
176
|
|
|
|
|
177
|
|
|
assert isinstance(options, argparse.Namespace) |
|
178
|
|
|
assert hasattr(options, 'config') |
|
179
|
|
|
assert hasattr(options, 'configdir') |
|
180
|
|
|
assert hasattr(options, 'daemonize') |
|
181
|
|
|
assert hasattr(options, 'quiet') |
|
182
|
|
|
|
|
183
|
|
|
assert options.config == 'default' |
|
184
|
|
|
assert options.configdir == config.DEFAULT_HOMEDIR |
|
185
|
|
|
assert options.daemonize is False |
|
186
|
|
|
assert options.quiet is False |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
def test_build_parser_start_config(): |
|
190
|
|
|
parser = build_parser() |
|
191
|
|
|
|
|
192
|
|
|
options = parser.parse_args(['start', '-c', 'custom']) |
|
193
|
|
|
assert options.config == 'custom' |
|
194
|
|
|
|
|
195
|
|
|
options = parser.parse_args(['start', '--config', 'custom']) |
|
196
|
|
|
assert options.config == 'custom' |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
def test_build_parser_start_configdir(): |
|
200
|
|
|
parser = build_parser() |
|
201
|
|
|
|
|
202
|
|
|
options = parser.parse_args(['start', '--config-dir', 'custom']) |
|
203
|
|
|
assert options.configdir == 'custom' |
|
204
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
def test_build_parser_start_daemonize(): |
|
207
|
|
|
parser = build_parser() |
|
208
|
|
|
|
|
209
|
|
|
options = parser.parse_args(['start', '-d']) |
|
210
|
|
|
assert options.daemonize is True |
|
211
|
|
|
|
|
212
|
|
|
options = parser.parse_args(['start', '--fork']) |
|
213
|
|
|
assert options.daemonize is True |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
def test_build_parser_start_quiet(): |
|
217
|
|
|
parser = build_parser() |
|
218
|
|
|
|
|
219
|
|
|
options = parser.parse_args(['start', '--quiet']) |
|
220
|
|
|
assert options.quiet is True |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
View Code Duplication |
def test_build_parser_stop(): |
|
|
|
|
|
|
224
|
|
|
"""Assert parser's namespace exposes stop's options (default values)""" |
|
225
|
|
|
parser = build_parser() |
|
226
|
|
|
options = parser.parse_args(['stop']) |
|
227
|
|
|
|
|
228
|
|
|
assert isinstance(options, argparse.Namespace) |
|
229
|
|
|
assert hasattr(options, 'config') |
|
230
|
|
|
assert hasattr(options, 'configdir') |
|
231
|
|
|
assert hasattr(options, 'kill') |
|
232
|
|
|
assert hasattr(options, 'quiet') |
|
233
|
|
|
|
|
234
|
|
|
assert options.config == 'default' |
|
235
|
|
|
assert options.configdir == config.DEFAULT_HOMEDIR |
|
236
|
|
|
assert options.kill is False |
|
237
|
|
|
assert options.quiet is False |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
def test_build_parser_stop_config(): |
|
241
|
|
|
parser = build_parser() |
|
242
|
|
|
|
|
243
|
|
|
options = parser.parse_args(['stop', '-c', 'custom']) |
|
244
|
|
|
assert options.config == 'custom' |
|
245
|
|
|
|
|
246
|
|
|
options = parser.parse_args(['stop', '--config', 'custom']) |
|
247
|
|
|
assert options.config == 'custom' |
|
248
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
def test_build_parser_stop_configdir(): |
|
251
|
|
|
parser = build_parser() |
|
252
|
|
|
|
|
253
|
|
|
options = parser.parse_args(['stop', '--config-dir', 'custom']) |
|
254
|
|
|
assert options.configdir == 'custom' |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
def test_build_parser_stop_kill(): |
|
258
|
|
|
parser = build_parser() |
|
259
|
|
|
|
|
260
|
|
|
options = parser.parse_args(['stop', '-k']) |
|
261
|
|
|
assert options.kill is True |
|
262
|
|
|
|
|
263
|
|
|
options = parser.parse_args(['stop', '--kill']) |
|
264
|
|
|
assert options.kill is True |
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
def test_build_parser_stop_quiet(): |
|
268
|
|
|
parser = build_parser() |
|
269
|
|
|
|
|
270
|
|
|
options = parser.parse_args(['stop', '--quiet']) |
|
271
|
|
|
assert options.quiet is True |
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
def test_build_parser_restart(): |
|
275
|
|
|
"""Assert parser's namespace exposes restart's options (default values)""" |
|
276
|
|
|
parser = build_parser() |
|
277
|
|
|
options = parser.parse_args(['restart']) |
|
278
|
|
|
|
|
279
|
|
|
assert isinstance(options, argparse.Namespace) |
|
280
|
|
|
assert hasattr(options, 'config') |
|
281
|
|
|
assert hasattr(options, 'configdir') |
|
282
|
|
|
assert hasattr(options, 'quiet') |
|
283
|
|
|
|
|
284
|
|
|
assert options.config == 'default' |
|
285
|
|
|
assert options.configdir == config.DEFAULT_HOMEDIR |
|
286
|
|
|
assert options.quiet is False |
|
287
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
def test_build_parser_restart_config(): |
|
290
|
|
|
parser = build_parser() |
|
291
|
|
|
|
|
292
|
|
|
options = parser.parse_args(['restart', '-c', 'custom']) |
|
293
|
|
|
assert options.config == 'custom' |
|
294
|
|
|
|
|
295
|
|
|
options = parser.parse_args(['restart', '--config', 'custom']) |
|
296
|
|
|
assert options.config == 'custom' |
|
297
|
|
|
|
|
298
|
|
|
|
|
299
|
|
|
def test_build_parser_restart_configdir(): |
|
300
|
|
|
parser = build_parser() |
|
301
|
|
|
|
|
302
|
|
|
options = parser.parse_args(['restart', '--config-dir', 'custom']) |
|
303
|
|
|
assert options.configdir == 'custom' |
|
304
|
|
|
|
|
305
|
|
|
|
|
306
|
|
|
def test_build_parser_restart_quiet(): |
|
307
|
|
|
parser = build_parser() |
|
308
|
|
|
|
|
309
|
|
|
options = parser.parse_args(['restart', '--quiet']) |
|
310
|
|
|
assert options.quiet is True |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
def test_build_parser_configure(): |
|
314
|
|
|
"""Assert parser's namespace exposes configure's options (default values)""" |
|
315
|
|
|
parser = build_parser() |
|
316
|
|
|
options = parser.parse_args(['configure']) |
|
317
|
|
|
|
|
318
|
|
|
assert isinstance(options, argparse.Namespace) |
|
319
|
|
|
assert hasattr(options, 'config') |
|
320
|
|
|
assert hasattr(options, 'configdir') |
|
321
|
|
|
assert hasattr(options, 'modules') |
|
322
|
|
|
|
|
323
|
|
|
assert options.config == 'default' |
|
324
|
|
|
assert options.configdir == config.DEFAULT_HOMEDIR |
|
325
|
|
|
assert options.modules is False |
|
326
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
def test_build_parser_configure_config(): |
|
329
|
|
|
parser = build_parser() |
|
330
|
|
|
|
|
331
|
|
|
options = parser.parse_args(['configure', '-c', 'custom']) |
|
332
|
|
|
assert options.config == 'custom' |
|
333
|
|
|
|
|
334
|
|
|
options = parser.parse_args(['configure', '--config', 'custom']) |
|
335
|
|
|
assert options.config == 'custom' |
|
336
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
def test_build_parser_configure_configdir(): |
|
339
|
|
|
parser = build_parser() |
|
340
|
|
|
|
|
341
|
|
|
options = parser.parse_args(['configure', '--config-dir', 'custom']) |
|
342
|
|
|
assert options.configdir == 'custom' |
|
343
|
|
|
|
|
344
|
|
|
|
|
345
|
|
|
def test_build_parser_configure_modules(): |
|
346
|
|
|
parser = build_parser() |
|
347
|
|
|
|
|
348
|
|
|
options = parser.parse_args(['configure', '--modules']) |
|
349
|
|
|
assert options.modules is True |
|
350
|
|
|
|
|
351
|
|
|
|
|
352
|
|
|
def test_get_configuration(tmpdir): |
|
353
|
|
|
"""Assert function returns a Sopel ``Config`` object""" |
|
354
|
|
|
working_dir = tmpdir.mkdir("working") |
|
355
|
|
|
working_dir.join('default.cfg').write('\n'.join([ |
|
356
|
|
|
'[core]', |
|
357
|
|
|
'owner = TestName' |
|
358
|
|
|
])) |
|
359
|
|
|
|
|
360
|
|
|
parser = build_parser() |
|
361
|
|
|
options = parser.parse_args(['legacy', '-c', 'default.cfg']) |
|
362
|
|
|
|
|
363
|
|
|
with cd(working_dir.strpath): |
|
364
|
|
|
result = get_configuration(options) |
|
365
|
|
|
assert isinstance(result, config.Config) |
|
366
|
|
|
assert result.core.owner == 'TestName' |
|
367
|
|
|
|
|
368
|
|
|
|
|
369
|
|
|
def test_get_pid_filename_default(): |
|
370
|
|
|
"""Assert function returns the default filename from given ``pid_dir``""" |
|
371
|
|
|
pid_dir = '/pid' |
|
372
|
|
|
parser = build_parser() |
|
373
|
|
|
options = parser.parse_args(['legacy']) |
|
374
|
|
|
|
|
375
|
|
|
result = get_pid_filename(options, pid_dir) |
|
376
|
|
|
assert result == pid_dir + '/sopel.pid' |
|
377
|
|
|
|
|
378
|
|
|
|
|
379
|
|
|
def test_get_pid_filename_named(): |
|
380
|
|
|
"""Assert function returns a specific filename when config (with extension) is set""" |
|
381
|
|
|
pid_dir = '/pid' |
|
382
|
|
|
parser = build_parser() |
|
383
|
|
|
|
|
384
|
|
|
# With extension |
|
385
|
|
|
options = parser.parse_args(['legacy', '-c', 'test.cfg']) |
|
386
|
|
|
|
|
387
|
|
|
result = get_pid_filename(options, pid_dir) |
|
388
|
|
|
assert result == pid_dir + '/sopel-test.pid' |
|
389
|
|
|
|
|
390
|
|
|
# Without extension |
|
391
|
|
|
options = parser.parse_args(['legacy', '-c', 'test']) |
|
392
|
|
|
|
|
393
|
|
|
result = get_pid_filename(options, pid_dir) |
|
394
|
|
|
assert result == pid_dir + '/sopel-test.pid' |
|
395
|
|
|
|
|
396
|
|
|
|
|
397
|
|
|
def test_get_pid_filename_ext_not_cfg(): |
|
398
|
|
|
"""Assert function keeps the config file extension when it is not cfg""" |
|
399
|
|
|
pid_dir = '/pid' |
|
400
|
|
|
parser = build_parser() |
|
401
|
|
|
options = parser.parse_args(['legacy', '-c', 'test.ini']) |
|
402
|
|
|
|
|
403
|
|
|
result = get_pid_filename(options, pid_dir) |
|
404
|
|
|
assert result == pid_dir + '/sopel-test.ini.pid' |
|
405
|
|
|
|
|
406
|
|
|
|
|
407
|
|
|
def test_get_running_pid(tmpdir): |
|
408
|
|
|
"""Assert function retrieves an integer from a given filename""" |
|
409
|
|
|
pid_file = tmpdir.join('sopel.pid') |
|
410
|
|
|
pid_file.write('7814') |
|
411
|
|
|
|
|
412
|
|
|
result = get_running_pid(pid_file.strpath) |
|
413
|
|
|
assert result == 7814 |
|
414
|
|
|
|
|
415
|
|
|
|
|
416
|
|
|
def test_get_running_pid_not_integer(tmpdir): |
|
417
|
|
|
"""Assert function returns None when the content is not an Integer""" |
|
418
|
|
|
pid_file = tmpdir.join('sopel.pid') |
|
419
|
|
|
pid_file.write('') |
|
420
|
|
|
|
|
421
|
|
|
result = get_running_pid(pid_file.strpath) |
|
422
|
|
|
assert result is None |
|
423
|
|
|
|
|
424
|
|
|
pid_file.write('abcdefg') |
|
425
|
|
|
|
|
426
|
|
|
result = get_running_pid(pid_file.strpath) |
|
427
|
|
|
assert result is None |
|
428
|
|
|
|
|
429
|
|
|
|
|
430
|
|
|
def test_get_running_pid_no_file(tmpdir): |
|
431
|
|
|
"""Assert function returns None when there is no such file""" |
|
432
|
|
|
pid_file = tmpdir.join('sopel.pid') |
|
433
|
|
|
|
|
434
|
|
|
result = get_running_pid(pid_file.strpath) |
|
435
|
|
|
assert result is None |
|
436
|
|
|
|