Completed
Push — master ( 39f074...c752b1 )
by dgw
15s queued 12s
created

test_clean_callable_events_basestring()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.95
c 0
b 0
f 0
cc 1
nop 2
1
# coding=utf-8
2
"""Tests for the ``sopel.loader`` module."""
3
from __future__ import unicode_literals, absolute_import, print_function, division
4
5
import inspect
6
7
import pytest
8
9
from sopel import loader, config, module, plugins
10
11
12
MOCK_MODULE_CONTENT = """# coding=utf-8
13
import sopel.module
14
15
16
@sopel.module.commands("first")
17
def first_command(bot, trigger):
18
    pass
19
20
21
@sopel.module.commands("second")
22
def second_command(bot, trigger):
23
    pass
24
25
26
@sopel.module.interval(5)
27
def interval5s(bot):
28
    pass
29
30
31
@sopel.module.interval(10)
32
def interval10s(bot):
33
    pass
34
35
36
@sopel.module.url(r'.\\.example\\.com')
37
def example_url(bot):
38
    pass
39
40
41
@sopel.module.event('TOPIC')
42
def on_topic_command(bot):
43
    pass
44
45
46
def shutdown():
47
    pass
48
49
50
def ignored():
51
    pass
52
53
"""
54
55
56
@pytest.fixture
57
def func():
58
    """Pytest fixture to get a function that will return True all the time"""
59
    def bot_command():
60
        """Test callable defined as a pytest fixture."""
61
        return True
62
    return bot_command
63
64
65
@pytest.fixture
66
def tmpconfig(tmpdir):
67
    conf_file = tmpdir.join('conf.ini')
68
    conf_file.write("\n".join([
69
        "[core]",
70
        "owner=testnick",
71
        "nick = TestBot",
72
        ""
73
    ]))
74
    return config.Config(conf_file.strpath)
75
76
77
def test_clean_module_commands(tmpdir, tmpconfig):
78
    root = tmpdir.mkdir('loader_mods')
79
    mod_file = root.join('file_mod.py')
80
    mod_file.write(MOCK_MODULE_CONTENT)
81
82
    plugin = plugins.handlers.PyFilePlugin(mod_file.strpath)
83
    plugin.load()
84
    test_mod = plugin._module
85
86
    callables, jobs, shutdowns, urls = loader.clean_module(
87
        test_mod, tmpconfig)
88
89
    assert len(callables) == 3
90
    assert test_mod.first_command in callables
91
    assert test_mod.second_command in callables
92
    assert test_mod.on_topic_command in callables
93
    assert len(jobs) == 2
94
    assert test_mod.interval5s in jobs
95
    assert test_mod.interval10s in jobs
96
    assert len(shutdowns)
97
    assert test_mod.shutdown in shutdowns
98
    assert len(urls) == 1
99
    assert test_mod.example_url in urls
100
101
    # ignored function is ignored
102
    assert test_mod.ignored not in callables
103
    assert test_mod.ignored not in jobs
104
    assert test_mod.ignored not in shutdowns
105
    assert test_mod.ignored not in urls
106
107
108
def test_clean_callable_default(tmpconfig, func):
109
    loader.clean_callable(func, tmpconfig)
110
111
    # Default values
112
    assert hasattr(func, 'unblockable')
113
    assert func.unblockable is False
114
    assert hasattr(func, 'priority')
115
    assert func.priority == 'medium'
116
    assert hasattr(func, 'thread')
117
    assert func.thread is True
118
    assert hasattr(func, 'rate')
119
    assert func.rate == 0
120
    assert hasattr(func, 'channel_rate')
121
    assert func.rate == 0
122
    assert hasattr(func, 'global_rate')
123
    assert func.global_rate == 0
124
    assert hasattr(func, 'event')
125
    assert func.event == ['PRIVMSG']
126
127
    # Not added by default
128
    assert not hasattr(func, 'rule')
129
    assert not hasattr(func, 'commands')
130
    assert not hasattr(func, 'intents')
131
132
133
def test_clean_callable_event(tmpconfig, func):
134
    setattr(func, 'event', ['low', 'UP', 'MiXeD'])
135
    loader.clean_callable(func, tmpconfig)
136
137
    assert hasattr(func, 'event')
138
    assert func.event == ['LOW', 'UP', 'MIXED']
139
140
    # idempotency
141
    loader.clean_callable(func, tmpconfig)
142
    assert func.event == ['LOW', 'UP', 'MIXED']
143
144
145
def test_clean_callable_event_string(tmpconfig, func):
146
    setattr(func, 'event', 'some')
147
    loader.clean_callable(func, tmpconfig)
148
149
    assert hasattr(func, 'event')
150
    assert func.event == ['SOME']
151
152
    # idempotency
153
    loader.clean_callable(func, tmpconfig)
154
    assert func.event == ['SOME']
155
156
157 View Code Duplication
def test_clean_callable_rule(tmpconfig, func):
158
    setattr(func, 'rule', [r'abc'])
159
    loader.clean_callable(func, tmpconfig)
160
161
    assert hasattr(func, 'rule')
162
    assert len(func.rule) == 1
163
164
    # Test the regex is compiled properly
165
    regex = func.rule[0]
166
    assert regex.match('abc')
167
    assert regex.match('abcd')
168
    assert not regex.match('efg')
169
170
    # idempotency
171
    loader.clean_callable(func, tmpconfig)
172
    assert len(func.rule) == 1
173
    assert regex in func.rule
174
175
176 View Code Duplication
def test_clean_callable_rule_string(tmpconfig, func):
177
    setattr(func, 'rule', r'abc')
178
    loader.clean_callable(func, tmpconfig)
179
180
    assert hasattr(func, 'rule')
181
    assert len(func.rule) == 1
182
183
    # Test the regex is compiled properly
184
    regex = func.rule[0]
185
    assert regex.match('abc')
186
    assert regex.match('abcd')
187
    assert not regex.match('efg')
188
189
    # idempotency
190
    loader.clean_callable(func, tmpconfig)
191
    assert len(func.rule) == 1
192
    assert regex in func.rule
193
194
195 View Code Duplication
def test_clean_callable_rule_nick(tmpconfig, func):
196
    """Assert ``$nick`` in a rule will match ``TestBot: `` or ``TestBot, ``."""
197
    setattr(func, 'rule', [r'$nickhello'])
198
    loader.clean_callable(func, tmpconfig)
199
200
    assert hasattr(func, 'rule')
201
    assert len(func.rule) == 1
202
203
    # Test the regex is compiled properly
204
    regex = func.rule[0]
205
    assert regex.match('TestBot: hello')
206
    assert regex.match('TestBot, hello')
207
    assert not regex.match('TestBot not hello')
208
209
    # idempotency
210
    loader.clean_callable(func, tmpconfig)
211
    assert len(func.rule) == 1
212
    assert regex in func.rule
213
214
215 View Code Duplication
def test_clean_callable_rule_nickname(tmpconfig, func):
216
    """Assert ``$nick`` in a rule will match ``TestBot``."""
217
    setattr(func, 'rule', [r'$nickname\s+hello'])
218
    loader.clean_callable(func, tmpconfig)
219
220
    assert hasattr(func, 'rule')
221
    assert len(func.rule) == 1
222
223
    # Test the regex is compiled properly
224
    regex = func.rule[0]
225
    assert regex.match('TestBot hello')
226
    assert not regex.match('TestBot not hello')
227
228
    # idempotency
229
    loader.clean_callable(func, tmpconfig)
230
    assert len(func.rule) == 1
231
    assert regex in func.rule
232
233
234
def test_clean_callable_nickname_command(tmpconfig, func):
235
    setattr(func, 'nickname_commands', ['hello!'])
236
    loader.clean_callable(func, tmpconfig)
237
238
    assert hasattr(func, 'nickname_commands')
239
    assert len(func.nickname_commands) == 1
240
    assert func.nickname_commands == ['hello!']
241
    assert hasattr(func, 'rule')
242
    assert len(func.rule) == 1
243
244
    regex = func.rule[0]
245
    assert regex.match('TestBot hello!')
246
    assert regex.match('TestBot, hello!')
247
    assert regex.match('TestBot: hello!')
248
    assert not regex.match('TestBot not hello')
249
250
    # idempotency
251
    loader.clean_callable(func, tmpconfig)
252
    assert len(func.rule) == 1
253
    assert regex in func.rule
254
255
256
def test_clean_callable_events(tmpconfig, func):
257
    setattr(func, 'event', ['TOPIC'])
258
    loader.clean_callable(func, tmpconfig)
259
260
    assert hasattr(func, 'event')
261
    assert func.event == ['TOPIC']
262
263
    setattr(func, 'event', ['TOPIC', 'JOIN'])
264
    loader.clean_callable(func, tmpconfig)
265
266
    assert hasattr(func, 'event')
267
    assert func.event == ['TOPIC', 'JOIN']
268
269
    setattr(func, 'event', ['TOPIC', 'join', 'Nick'])
270
    loader.clean_callable(func, tmpconfig)
271
272
    assert hasattr(func, 'event')
273
    assert func.event == ['TOPIC', 'JOIN', 'NICK']
274
275
276
def test_clean_callable_events_basestring(tmpconfig, func):
277
    setattr(func, 'event', 'topic')
278
    loader.clean_callable(func, tmpconfig)
279
280
    assert hasattr(func, 'event')
281
    assert func.event == ['TOPIC']
282
283
    setattr(func, 'event', 'JOIN')
284
    loader.clean_callable(func, tmpconfig)
285
286
    assert hasattr(func, 'event')
287
    assert func.event == ['JOIN']
288
289
290 View Code Duplication
def test_clean_callable_example(tmpconfig, func):
291
    module.commands('test')(func)
292
    module.example('.test hello')(func)
293
294
    loader.clean_callable(func, tmpconfig)
295
296
    assert hasattr(func, '_docs')
297
    assert len(func._docs) == 1
298
    assert 'test' in func._docs
299
300
    docs = func._docs['test']
301
    assert len(docs) == 2
302
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
303
    assert docs[1] == ['.test hello']
304
305
306 View Code Duplication
def test_clean_callable_example_not_set(tmpconfig, func):
307
    module.commands('test')(func)
308
309
    loader.clean_callable(func, tmpconfig)
310
311
    assert hasattr(func, '_docs')
312
    assert len(func._docs) == 1
313
    assert 'test' in func._docs
314
315
    docs = func._docs['test']
316
    assert len(docs) == 2
317
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
318
    assert docs[1] == []
319
320
321 View Code Duplication
def test_clean_callable_example_multi_commands(tmpconfig, func):
322
    module.commands('test')(func)
323
    module.commands('unit')(func)
324
    module.example('.test hello')(func)
325
326
    loader.clean_callable(func, tmpconfig)
327
328
    assert hasattr(func, '_docs')
329
    assert len(func._docs) == 2
330
    assert 'test' in func._docs
331
    assert 'unit' in func._docs
332
333
    test_docs = func._docs['test']
334
    unit_docs = func._docs['unit']
335
    assert len(test_docs) == 2
336
    assert test_docs == unit_docs
337
338
    assert test_docs[0] == inspect.cleandoc(func.__doc__).splitlines()
339
    assert test_docs[1] == ['.test hello']
340
341
342 View Code Duplication
def test_clean_callable_example_first_only(tmpconfig, func):
343
    module.commands('test')(func)
344
    module.example('.test hello')(func)
345
    module.example('.test bonjour')(func)
346
347
    loader.clean_callable(func, tmpconfig)
348
349
    assert len(func._docs) == 1
350
    assert 'test' in func._docs
351
352
    docs = func._docs['test']
353
    assert len(docs) == 2
354
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
355
    assert docs[1] == ['.test hello']
356
357
358 View Code Duplication
def test_clean_callable_example_first_only_multi_commands(tmpconfig, func):
359
    module.commands('test')(func)
360
    module.commands('unit')(func)
361
    module.example('.test hello')(func)
362
    module.example('.test bonjour')(func)
363
364
    loader.clean_callable(func, tmpconfig)
365
366
    assert hasattr(func, '_docs')
367
    assert len(func._docs) == 2
368
    assert 'test' in func._docs
369
    assert 'unit' in func._docs
370
371
    test_docs = func._docs['test']
372
    unit_docs = func._docs['unit']
373
    assert len(test_docs) == 2
374
    assert test_docs == unit_docs
375
376
    assert test_docs[0] == inspect.cleandoc(func.__doc__).splitlines()
377
    assert test_docs[1] == ['.test hello']
378
379
380 View Code Duplication
def test_clean_callable_example_user_help(tmpconfig, func):
381
    module.commands('test')(func)
382
    module.example('.test hello', user_help=True)(func)
383
384
    loader.clean_callable(func, tmpconfig)
385
386
    assert len(func._docs) == 1
387
    assert 'test' in func._docs
388
389
    docs = func._docs['test']
390
    assert len(docs) == 2
391
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
392
    assert docs[1] == ['.test hello']
393
394
395 View Code Duplication
def test_clean_callable_example_user_help_multi(tmpconfig, func):
396
    module.commands('test')(func)
397
    module.example('.test hello', user_help=True)(func)
398
    module.example('.test bonjour', user_help=True)(func)
399
400
    loader.clean_callable(func, tmpconfig)
401
402
    assert len(func._docs) == 1
403
    assert 'test' in func._docs
404
405
    docs = func._docs['test']
406
    assert len(docs) == 2
407
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
408
    assert docs[1] == ['.test hello', '.test bonjour']
409
410
411 View Code Duplication
def test_clean_callable_example_user_help_mixed(tmpconfig, func):
412
    module.commands('test')(func)
413
    module.example('.test hello')(func)
414
    module.example('.test bonjour', user_help=True)(func)
415
416
    loader.clean_callable(func, tmpconfig)
417
418
    assert len(func._docs) == 1
419
    assert 'test' in func._docs
420
421
    docs = func._docs['test']
422
    assert len(docs) == 2
423
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
424
    assert docs[1] == ['.test bonjour']
425
426
427 View Code Duplication
def test_clean_callable_example_default_prefix(tmpconfig, func):
428
    module.commands('test')(func)
429
    module.example('.test hello')(func)
430
431
    tmpconfig.core.help_prefix = '!'
432
    loader.clean_callable(func, tmpconfig)
433
434
    assert len(func._docs) == 1
435
    assert 'test' in func._docs
436
437
    docs = func._docs['test']
438
    assert len(docs) == 2
439
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
440
    assert docs[1] == ['!test hello']
441
442
443 View Code Duplication
def test_clean_callable_example_nickname(tmpconfig, func):
444
    module.commands('test')(func)
445
    module.example('$nickname: hello')(func)
446
447
    loader.clean_callable(func, tmpconfig)
448
449
    assert len(func._docs) == 1
450
    assert 'test' in func._docs
451
452
    docs = func._docs['test']
453
    assert len(docs) == 2
454
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
455
    assert docs[1] == ['TestBot: hello']
456
457
458 View Code Duplication
def test_clean_callable_example_nickname_custom_prefix(tmpconfig, func):
459
    module.commands('test')(func)
460
    module.example('$nickname: hello')(func)
461
462
    tmpconfig.core.help_prefix = '!'
463
    loader.clean_callable(func, tmpconfig)
464
465
    assert len(func._docs) == 1
466
    assert 'test' in func._docs
467
468
    docs = func._docs['test']
469
    assert len(docs) == 2
470
    assert docs[0] == inspect.cleandoc(func.__doc__).splitlines()
471
    assert docs[1] == ['TestBot: hello']
472
473
474
def test_clean_callable_intents(tmpconfig, func):
475
    setattr(func, 'intents', [r'abc'])
476
    loader.clean_callable(func, tmpconfig)
477
478
    assert hasattr(func, 'intents')
479
    assert len(func.intents) == 1
480
481
    # Test the regex is compiled properly
482
    regex = func.intents[0]
483
    assert regex.match('abc')
484
    assert regex.match('abcd')
485
    assert regex.match('ABC')
486
    assert regex.match('AbCdE')
487
    assert not regex.match('efg')
488
489
    # idempotency
490
    loader.clean_callable(func, tmpconfig)
491
    assert len(func.intents) == 1
492
    assert regex in func.intents
493