Convenience functions for callable hooks
[mediagoblin.git] / mediagoblin / tests / test_pluginapi.py
CommitLineData
29b6f917
WKG
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import sys
18from configobj import ConfigObj
19from mediagoblin import mg_globals
20from mediagoblin.init.plugins import setup_plugins
21from mediagoblin.tools import pluginapi
29b6f917
WKG
22
23
24def with_cleanup(*modules_to_delete):
25 def _with_cleanup(fun):
26 """Wrapper that saves and restores mg_globals"""
27 def _with_cleanup_inner(*args, **kwargs):
28 old_app_config = mg_globals.app_config
29 old_global_config = mg_globals.global_config
30 # Need to delete icky modules before and after so as to make
31 # sure things work correctly.
32 for module in modules_to_delete:
33 try:
34 del sys.modules[module]
35 except KeyError:
36 pass
37 # The plugin cache gets populated as a side-effect of
38 # importing, so it's best to clear it before and after a test.
05e007c1
WKG
39 pman = pluginapi.PluginManager()
40 pman.clear()
29b6f917
WKG
41 try:
42 return fun(*args, **kwargs)
43 finally:
44 mg_globals.app_config = old_app_config
45 mg_globals.global_config = old_global_config
46 # Need to delete icky modules before and after so as to make
47 # sure things work correctly.
48 for module in modules_to_delete:
49 try:
50 del sys.modules[module]
51 except KeyError:
52 pass
05e007c1 53 pman.clear()
29b6f917
WKG
54
55 _with_cleanup_inner.__name__ = fun.__name__
56 return _with_cleanup_inner
57 return _with_cleanup
58
59
60def build_config(sections):
61 """Builds a ConfigObj object with specified data
62
63 :arg sections: list of ``(section_name, section_data,
64 subsection_list)`` tuples where section_data is a dict and
65 subsection_list is a list of ``(section_name, section_data,
66 subsection_list)``, ...
67
68 For example:
69
70 >>> build_config([
71 ... ('mediagoblin', {'key1': 'val1'}, []),
72 ... ('section2', {}, [
73 ... ('subsection1', {}, [])
74 ... ])
75 ... ])
76 """
77 cfg = ConfigObj()
78 cfg.filename = 'foo'
79 def _iter_section(cfg, section_list):
80 for section_name, data, subsection_list in section_list:
81 cfg[section_name] = data
82 _iter_section(cfg[section_name], subsection_list)
83
84 _iter_section(cfg, sections)
85 return cfg
86
87
88@with_cleanup()
89def test_no_plugins():
90 """Run setup_plugins with no plugins in config"""
91 cfg = build_config([('mediagoblin', {}, [])])
92 mg_globals.app_config = cfg['mediagoblin']
93 mg_globals.global_config = cfg
94
05e007c1 95 pman = pluginapi.PluginManager()
29b6f917
WKG
96 setup_plugins()
97
98 # Make sure we didn't load anything.
7d503a89 99 assert len(pman.plugins) == 0
29b6f917
WKG
100
101
05e007c1 102@with_cleanup('mediagoblin.plugins.sampleplugin')
29b6f917
WKG
103def test_one_plugin():
104 """Run setup_plugins with a single working plugin"""
105 cfg = build_config([
106 ('mediagoblin', {}, []),
107 ('plugins', {}, [
108 ('mediagoblin.plugins.sampleplugin', {}, [])
109 ])
110 ])
111
112 mg_globals.app_config = cfg['mediagoblin']
113 mg_globals.global_config = cfg
114
05e007c1 115 pman = pluginapi.PluginManager()
29b6f917
WKG
116 setup_plugins()
117
05e007c1 118 # Make sure we only found one plugin
7d503a89 119 assert len(pman.plugins) == 1
05e007c1 120 # Make sure the plugin is the one we think it is.
7d503a89 121 assert pman.plugins[0] == 'mediagoblin.plugins.sampleplugin'
05e007c1 122 # Make sure there was one hook registered
7d503a89 123 assert len(pman.hooks) == 1
05e007c1
WKG
124 # Make sure _setup_plugin_called was called once
125 import mediagoblin.plugins.sampleplugin
7d503a89 126 assert mediagoblin.plugins.sampleplugin._setup_plugin_called == 1
29b6f917 127
29b6f917 128
05e007c1 129@with_cleanup('mediagoblin.plugins.sampleplugin')
29b6f917
WKG
130def test_same_plugin_twice():
131 """Run setup_plugins with a single working plugin twice"""
132 cfg = build_config([
133 ('mediagoblin', {}, []),
134 ('plugins', {}, [
135 ('mediagoblin.plugins.sampleplugin', {}, []),
136 ('mediagoblin.plugins.sampleplugin', {}, []),
137 ])
138 ])
139
140 mg_globals.app_config = cfg['mediagoblin']
141 mg_globals.global_config = cfg
142
05e007c1 143 pman = pluginapi.PluginManager()
29b6f917
WKG
144 setup_plugins()
145
05e007c1 146 # Make sure we only found one plugin
7d503a89 147 assert len(pman.plugins) == 1
05e007c1 148 # Make sure the plugin is the one we think it is.
7d503a89 149 assert pman.plugins[0] == 'mediagoblin.plugins.sampleplugin'
05e007c1 150 # Make sure there was one hook registered
7d503a89 151 assert len(pman.hooks) == 1
05e007c1
WKG
152 # Make sure _setup_plugin_called was called once
153 import mediagoblin.plugins.sampleplugin
7d503a89 154 assert mediagoblin.plugins.sampleplugin._setup_plugin_called == 1
05d8f314
WKG
155
156
157@with_cleanup()
158def test_disabled_plugin():
159 """Run setup_plugins with a single working plugin twice"""
160 cfg = build_config([
161 ('mediagoblin', {}, []),
162 ('plugins', {}, [
163 ('-mediagoblin.plugins.sampleplugin', {}, []),
164 ])
165 ])
166
167 mg_globals.app_config = cfg['mediagoblin']
168 mg_globals.global_config = cfg
169
170 pman = pluginapi.PluginManager()
171 setup_plugins()
172
173 # Make sure we didn't load the plugin
7d503a89 174 assert len(pman.plugins) == 0