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
22 from nose
.tools
import eq_
25 def 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
:
35 del sys
.modules
[module
]
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.
40 pcache
= pluginapi
.PluginCache()
43 return fun(*args
, **kwargs
)
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
:
51 del sys
.modules
[module
]
56 _with_cleanup_inner
.__name
__ = fun
.__name
__
57 return _with_cleanup_inner
61 def build_config(sections
):
62 """Builds a ConfigObj object with specified data
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)``, ...
72 ... ('mediagoblin', {'key1': 'val1'}, []),
73 ... ('section2', {}, [
74 ... ('subsection1', {}, [])
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
)
85 _iter_section(cfg
, sections
)
90 def 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
96 pcache
= pluginapi
.PluginCache()
99 # Make sure we didn't load anything.
100 eq_(len(pcache
.plugin_classes
), 0)
101 eq_(len(pcache
.plugin_objects
), 0)
104 @with_cleanup('mediagoblin.plugins.sampleplugin',
105 'mediagoblin.plugins.sampleplugin.main')
106 def test_one_plugin():
107 """Run setup_plugins with a single working plugin"""
109 ('mediagoblin', {}, []),
111 ('mediagoblin.plugins.sampleplugin', {}, [])
115 mg_globals
.app_config
= cfg
['mediagoblin']
116 mg_globals
.global_config
= cfg
118 pcache
= pluginapi
.PluginCache()
121 # Make sure we only found one plugin class
122 eq_(len(pcache
.plugin_classes
), 1)
123 # Make sure the class is the one we think it is.
124 eq_(pcache
.plugin_classes
[0].__name
__, 'SamplePlugin')
126 # Make sure there was one plugin created
127 eq_(len(pcache
.plugin_objects
), 1)
128 # Make sure we called setup_plugin on SamplePlugin
129 eq_(pcache
.plugin_objects
[0]._setup
_plugin
_called
, 1)
132 @with_cleanup('mediagoblin.plugins.sampleplugin',
133 'mediagoblin.plugins.sampleplugin.main')
134 def test_same_plugin_twice():
135 """Run setup_plugins with a single working plugin twice"""
137 ('mediagoblin', {}, []),
139 ('mediagoblin.plugins.sampleplugin', {}, []),
140 ('mediagoblin.plugins.sampleplugin', {}, []),
144 mg_globals
.app_config
= cfg
['mediagoblin']
145 mg_globals
.global_config
= cfg
147 pcache
= pluginapi
.PluginCache()
150 # Make sure we only found one plugin class
151 eq_(len(pcache
.plugin_classes
), 1)
152 # Make sure the class is the one we think it is.
153 eq_(pcache
.plugin_classes
[0].__name
__, 'SamplePlugin')
155 # Make sure there was one plugin created
156 eq_(len(pcache
.plugin_objects
), 1)
157 # Make sure we called setup_plugin on SamplePlugin
158 eq_(pcache
.plugin_objects
[0]._setup
_plugin
_called
, 1)