sopel-irc /
sopel
| 1 | # coding=utf-8 |
||
| 2 | """Test for the ``sopel.plugins`` module.""" |
||
| 3 | from __future__ import unicode_literals, absolute_import, print_function, division |
||
| 4 | |||
| 5 | import sys |
||
| 6 | |||
| 7 | import pkg_resources |
||
| 8 | import pytest |
||
| 9 | |||
| 10 | from sopel import plugins |
||
| 11 | |||
| 12 | |||
| 13 | MOCK_MODULE_CONTENT = """# coding=utf-8 |
||
| 14 | import sopel.module |
||
| 15 | |||
| 16 | |||
| 17 | @sopel.module.commands("first") |
||
| 18 | def first_command(bot, trigger): |
||
| 19 | pass |
||
| 20 | |||
| 21 | |||
| 22 | @sopel.module.commands("second") |
||
| 23 | def second_command(bot, trigger): |
||
| 24 | pass |
||
| 25 | |||
| 26 | |||
| 27 | @sopel.module.interval(5) |
||
| 28 | def interval5s(bot): |
||
| 29 | pass |
||
| 30 | |||
| 31 | |||
| 32 | @sopel.module.interval(10) |
||
| 33 | def interval10s(bot): |
||
| 34 | pass |
||
| 35 | |||
| 36 | |||
| 37 | @sopel.module.url(r'.\\.example\\.com') |
||
| 38 | def example_url(bot): |
||
| 39 | pass |
||
| 40 | |||
| 41 | |||
| 42 | @sopel.module.event('TOPIC') |
||
| 43 | def on_topic_command(bot): |
||
| 44 | pass |
||
| 45 | |||
| 46 | |||
| 47 | def shutdown(): |
||
| 48 | pass |
||
| 49 | |||
| 50 | |||
| 51 | def ignored(): |
||
| 52 | pass |
||
| 53 | |||
| 54 | """ |
||
| 55 | |||
| 56 | |||
| 57 | View Code Duplication | def test_plugin_load_pymod(tmpdir): |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 58 | root = tmpdir.mkdir('loader_mods') |
||
| 59 | mod_file = root.join('file_mod.py') |
||
| 60 | mod_file.write(MOCK_MODULE_CONTENT) |
||
| 61 | |||
| 62 | plugin = plugins.handlers.PyFilePlugin(mod_file.strpath) |
||
| 63 | plugin.load() |
||
| 64 | |||
| 65 | test_mod = plugin._module |
||
| 66 | |||
| 67 | assert hasattr(test_mod, 'first_command') |
||
| 68 | assert hasattr(test_mod, 'second_command') |
||
| 69 | assert hasattr(test_mod, 'interval5s') |
||
| 70 | assert hasattr(test_mod, 'interval10s') |
||
| 71 | assert hasattr(test_mod, 'example_url') |
||
| 72 | assert hasattr(test_mod, 'shutdown') |
||
| 73 | assert hasattr(test_mod, 'ignored') |
||
| 74 | |||
| 75 | |||
| 76 | def test_plugin_load_pymod_bad_file_pyc(tmpdir): |
||
| 77 | root = tmpdir.mkdir('loader_mods') |
||
| 78 | test_file = root.join('file_module.pyc') |
||
| 79 | test_file.write('') |
||
| 80 | |||
| 81 | with pytest.raises(Exception): |
||
| 82 | plugins.handlers.PyFilePlugin(test_file.strpath) |
||
| 83 | |||
| 84 | |||
| 85 | def test_plugin_load_pymod_bad_file_no_ext(tmpdir): |
||
| 86 | root = tmpdir.mkdir('loader_mods') |
||
| 87 | test_file = root.join('file_module') |
||
| 88 | test_file.write('') |
||
| 89 | |||
| 90 | with pytest.raises(Exception): |
||
| 91 | plugins.handlers.PyFilePlugin(test_file.strpath) |
||
| 92 | |||
| 93 | |||
| 94 | View Code Duplication | def test_plugin_load_pypackage(tmpdir): |
|
|
0 ignored issues
–
show
|
|||
| 95 | root = tmpdir.mkdir('loader_mods') |
||
| 96 | package_dir = root.mkdir('dir_mod') |
||
| 97 | mod_file = package_dir.join('__init__.py') |
||
| 98 | mod_file.write(MOCK_MODULE_CONTENT) |
||
| 99 | |||
| 100 | plugin = plugins.handlers.PyFilePlugin(package_dir.strpath) |
||
| 101 | plugin.load() |
||
| 102 | |||
| 103 | test_mod = plugin._module |
||
| 104 | |||
| 105 | assert hasattr(test_mod, 'first_command') |
||
| 106 | assert hasattr(test_mod, 'second_command') |
||
| 107 | assert hasattr(test_mod, 'interval5s') |
||
| 108 | assert hasattr(test_mod, 'interval10s') |
||
| 109 | assert hasattr(test_mod, 'example_url') |
||
| 110 | assert hasattr(test_mod, 'shutdown') |
||
| 111 | assert hasattr(test_mod, 'ignored') |
||
| 112 | |||
| 113 | |||
| 114 | def test_plugin_load_pypackage_bad_dir_empty(tmpdir): |
||
| 115 | root = tmpdir.mkdir('loader_mods') |
||
| 116 | package_dir = root.mkdir('dir_package') |
||
| 117 | |||
| 118 | with pytest.raises(Exception): |
||
| 119 | plugins.handlers.PyFilePlugin(package_dir.strpath) |
||
| 120 | |||
| 121 | |||
| 122 | def test_plugin_load_pypackage_bad_dir_no_init(tmpdir): |
||
| 123 | root = tmpdir.mkdir('loader_mods') |
||
| 124 | package_dir = root.mkdir('dir_package') |
||
| 125 | package_dir.join('no_init.py').write('') |
||
| 126 | |||
| 127 | with pytest.raises(Exception): |
||
| 128 | plugins.handlers.PyFilePlugin(package_dir.strpath) |
||
| 129 | |||
| 130 | |||
| 131 | def test_plugin_load_entry_point(tmpdir): |
||
| 132 | root = tmpdir.mkdir('loader_mods') |
||
| 133 | mod_file = root.join('file_mod.py') |
||
| 134 | mod_file.write(MOCK_MODULE_CONTENT) |
||
| 135 | |||
| 136 | # generate setuptools Distribution object |
||
| 137 | distrib = pkg_resources.Distribution(root.strpath) |
||
| 138 | sys.path.append(root.strpath) |
||
| 139 | |||
| 140 | # load the entry point |
||
| 141 | try: |
||
| 142 | entry_point = pkg_resources.EntryPoint( |
||
| 143 | 'test_plugin', 'file_mod', dist=distrib) |
||
| 144 | plugin = plugins.handlers.EntryPointPlugin(entry_point) |
||
| 145 | plugin.load() |
||
| 146 | finally: |
||
| 147 | sys.path.remove(root.strpath) |
||
| 148 | |||
| 149 | assert plugin.name == 'test_plugin' |
||
|
0 ignored issues
–
show
|
|||
| 150 | |||
| 151 | test_mod = plugin._module |
||
| 152 | |||
| 153 | assert hasattr(test_mod, 'first_command') |
||
| 154 | assert hasattr(test_mod, 'second_command') |
||
| 155 | assert hasattr(test_mod, 'interval5s') |
||
| 156 | assert hasattr(test_mod, 'interval10s') |
||
| 157 | assert hasattr(test_mod, 'example_url') |
||
| 158 | assert hasattr(test_mod, 'shutdown') |
||
| 159 | assert hasattr(test_mod, 'ignored') |
||
| 160 |