1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
18 from configobj
import ConfigObj
19 from mediagoblin
import mg_globals
20 from mediagoblin
.init
.plugins
import setup_plugins
21 from mediagoblin
.tools
import pluginapi
24 def 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
:
34 del sys
.modules
[module
]
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.
39 pman
= pluginapi
.PluginManager()
42 return fun(*args
, **kwargs
)
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
:
50 del sys
.modules
[module
]
55 _with_cleanup_inner
.__name
__ = fun
.__name
__
56 return _with_cleanup_inner
60 def build_config(sections
):
61 """Builds a ConfigObj object with specified data
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)``, ...
71 ... ('mediagoblin', {'key1': 'val1'}, []),
72 ... ('section2', {}, [
73 ... ('subsection1', {}, [])
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
)
84 _iter_section(cfg
, sections
)
89 def 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
95 pman
= pluginapi
.PluginManager()
98 # Make sure we didn't load anything.
99 assert len(pman
.plugins
) == 0
102 @with_cleanup('mediagoblin.plugins.sampleplugin')
103 def test_one_plugin():
104 """Run setup_plugins with a single working plugin"""
106 ('mediagoblin', {}, []),
108 ('mediagoblin.plugins.sampleplugin', {}, [])
112 mg_globals
.app_config
= cfg
['mediagoblin']
113 mg_globals
.global_config
= cfg
115 pman
= pluginapi
.PluginManager()
118 # Make sure we only found one plugin
119 assert len(pman
.plugins
) == 1
120 # Make sure the plugin is the one we think it is.
121 assert pman
.plugins
[0] == 'mediagoblin.plugins.sampleplugin'
122 # Make sure there was one hook registered
123 assert len(pman
.hooks
) == 1
124 # Make sure _setup_plugin_called was called once
125 import mediagoblin
.plugins
.sampleplugin
126 assert mediagoblin
.plugins
.sampleplugin
._setup
_plugin
_called
== 1
129 @with_cleanup('mediagoblin.plugins.sampleplugin')
130 def test_same_plugin_twice():
131 """Run setup_plugins with a single working plugin twice"""
133 ('mediagoblin', {}, []),
135 ('mediagoblin.plugins.sampleplugin', {}, []),
136 ('mediagoblin.plugins.sampleplugin', {}, []),
140 mg_globals
.app_config
= cfg
['mediagoblin']
141 mg_globals
.global_config
= cfg
143 pman
= pluginapi
.PluginManager()
146 # Make sure we only found one plugin
147 assert len(pman
.plugins
) == 1
148 # Make sure the plugin is the one we think it is.
149 assert pman
.plugins
[0] == 'mediagoblin.plugins.sampleplugin'
150 # Make sure there was one hook registered
151 assert len(pman
.hooks
) == 1
152 # Make sure _setup_plugin_called was called once
153 import mediagoblin
.plugins
.sampleplugin
154 assert mediagoblin
.plugins
.sampleplugin
._setup
_plugin
_called
== 1
158 def test_disabled_plugin():
159 """Run setup_plugins with a single working plugin twice"""
161 ('mediagoblin', {}, []),
163 ('-mediagoblin.plugins.sampleplugin', {}, []),
167 mg_globals
.app_config
= cfg
['mediagoblin']
168 mg_globals
.global_config
= cfg
170 pman
= pluginapi
.PluginManager()
173 # Make sure we didn't load the plugin
174 assert len(pman
.plugins
) == 0