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 pman
= pluginapi
.PluginManager()
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 pman
= pluginapi
.PluginManager()
99 # Make sure we didn't load anything.
100 eq_(len(pman
.plugins
), 0)
103 @with_cleanup('mediagoblin.plugins.sampleplugin')
104 def test_one_plugin():
105 """Run setup_plugins with a single working plugin"""
107 ('mediagoblin', {}, []),
109 ('mediagoblin.plugins.sampleplugin', {}, [])
113 mg_globals
.app_config
= cfg
['mediagoblin']
114 mg_globals
.global_config
= cfg
116 pman
= pluginapi
.PluginManager()
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)
130 @with_cleanup('mediagoblin.plugins.sampleplugin')
131 def test_same_plugin_twice():
132 """Run setup_plugins with a single working plugin twice"""
134 ('mediagoblin', {}, []),
136 ('mediagoblin.plugins.sampleplugin', {}, []),
137 ('mediagoblin.plugins.sampleplugin', {}, []),
141 mg_globals
.app_config
= cfg
['mediagoblin']
142 mg_globals
.global_config
= cfg
144 pman
= pluginapi
.PluginManager()
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)
159 def test_disabled_plugin():
160 """Run setup_plugins with a single working plugin twice"""
162 ('mediagoblin', {}, []),
164 ('-mediagoblin.plugins.sampleplugin', {}, []),
168 mg_globals
.app_config
= cfg
['mediagoblin']
169 mg_globals
.global_config
= cfg
171 pman
= pluginapi
.PluginManager()
174 # Make sure we didn't load the plugin
175 eq_(len(pman
.plugins
), 0)