Fix errors in collection views
[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
22from nose.tools import eq_
23
24
25def with_cleanup(*modules_to_delete):
26 def _with_cleanup(fun):
27 """Wrapper that saves and restores mg_globals"""
28 def _with_cleanup_inner(*args, **kwargs):
29 old_app_config = mg_globals.app_config
30 old_global_config = mg_globals.global_config
31 # Need to delete icky modules before and after so as to make
32 # sure things work correctly.
33 for module in modules_to_delete:
34 try:
35 del sys.modules[module]
36 except KeyError:
37 pass
38 # The plugin cache gets populated as a side-effect of
39 # importing, so it's best to clear it before and after a test.
05e007c1
WKG
40 pman = pluginapi.PluginManager()
41 pman.clear()
29b6f917
WKG
42 try:
43 return fun(*args, **kwargs)
44 finally:
45 mg_globals.app_config = old_app_config
46 mg_globals.global_config = old_global_config
47 # Need to delete icky modules before and after so as to make
48 # sure things work correctly.
49 for module in modules_to_delete:
50 try:
51 del sys.modules[module]
52 except KeyError:
53 pass
05e007c1 54 pman.clear()
29b6f917
WKG
55
56 _with_cleanup_inner.__name__ = fun.__name__
57 return _with_cleanup_inner
58 return _with_cleanup
59
60
61def build_config(sections):
62 """Builds a ConfigObj object with specified data
63
64 :arg sections: list of ``(section_name, section_data,
65 subsection_list)`` tuples where section_data is a dict and
66 subsection_list is a list of ``(section_name, section_data,
67 subsection_list)``, ...
68
69 For example:
70
71 >>> build_config([
72 ... ('mediagoblin', {'key1': 'val1'}, []),
73 ... ('section2', {}, [
74 ... ('subsection1', {}, [])
75 ... ])
76 ... ])
77 """
78 cfg = ConfigObj()
79 cfg.filename = 'foo'
80 def _iter_section(cfg, section_list):
81 for section_name, data, subsection_list in section_list:
82 cfg[section_name] = data
83 _iter_section(cfg[section_name], subsection_list)
84
85 _iter_section(cfg, sections)
86 return cfg
87
88
89@with_cleanup()
90def test_no_plugins():
91 """Run setup_plugins with no plugins in config"""
92 cfg = build_config([('mediagoblin', {}, [])])
93 mg_globals.app_config = cfg['mediagoblin']
94 mg_globals.global_config = cfg
95
05e007c1 96 pman = pluginapi.PluginManager()
29b6f917
WKG
97 setup_plugins()
98
99 # Make sure we didn't load anything.
05e007c1 100 eq_(len(pman.plugins), 0)
29b6f917
WKG
101
102
05e007c1 103@with_cleanup('mediagoblin.plugins.sampleplugin')
29b6f917
WKG
104def test_one_plugin():
105 """Run setup_plugins with a single working plugin"""
106 cfg = build_config([
107 ('mediagoblin', {}, []),
108 ('plugins', {}, [
109 ('mediagoblin.plugins.sampleplugin', {}, [])
110 ])
111 ])
112
113 mg_globals.app_config = cfg['mediagoblin']
114 mg_globals.global_config = cfg
115
05e007c1 116 pman = pluginapi.PluginManager()
29b6f917
WKG
117 setup_plugins()
118
05e007c1
WKG
119 # Make sure we only found one plugin
120 eq_(len(pman.plugins), 1)
121 # Make sure the plugin is the one we think it is.
122 eq_(pman.plugins[0], 'mediagoblin.plugins.sampleplugin')
123 # Make sure there was one hook registered
124 eq_(len(pman.hooks), 1)
125 # Make sure _setup_plugin_called was called once
126 import mediagoblin.plugins.sampleplugin
127 eq_(mediagoblin.plugins.sampleplugin._setup_plugin_called, 1)
29b6f917 128
29b6f917 129
05e007c1 130@with_cleanup('mediagoblin.plugins.sampleplugin')
29b6f917
WKG
131def test_same_plugin_twice():
132 """Run setup_plugins with a single working plugin twice"""
133 cfg = build_config([
134 ('mediagoblin', {}, []),
135 ('plugins', {}, [
136 ('mediagoblin.plugins.sampleplugin', {}, []),
137 ('mediagoblin.plugins.sampleplugin', {}, []),
138 ])
139 ])
140
141 mg_globals.app_config = cfg['mediagoblin']
142 mg_globals.global_config = cfg
143
05e007c1 144 pman = pluginapi.PluginManager()
29b6f917
WKG
145 setup_plugins()
146
05e007c1
WKG
147 # Make sure we only found one plugin
148 eq_(len(pman.plugins), 1)
149 # Make sure the plugin is the one we think it is.
150 eq_(pman.plugins[0], 'mediagoblin.plugins.sampleplugin')
151 # Make sure there was one hook registered
152 eq_(len(pman.hooks), 1)
153 # Make sure _setup_plugin_called was called once
154 import mediagoblin.plugins.sampleplugin
155 eq_(mediagoblin.plugins.sampleplugin._setup_plugin_called, 1)
05d8f314
WKG
156
157
158@with_cleanup()
159def test_disabled_plugin():
160 """Run setup_plugins with a single working plugin twice"""
161 cfg = build_config([
162 ('mediagoblin', {}, []),
163 ('plugins', {}, [
164 ('-mediagoblin.plugins.sampleplugin', {}, []),
165 ])
166 ])
167
168 mg_globals.app_config = cfg['mediagoblin']
169 mg_globals.global_config = cfg
170
171 pman = pluginapi.PluginManager()
172 setup_plugins()
173
174 # Make sure we didn't load the plugin
175 eq_(len(pman.plugins), 0)