1
# Copyright (C) 2008-2012 Canonical, Ltd.
3
# This program is free software: you can redistribute it and/or modify it
4
# under the terms of the GNU General Public License as published by the Free
5
# Software Foundation, version 3 of the License.
7
# This program is distributed in the hope that it will be useful, but WITHOUT
8
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
# You should have received a copy of the GNU General Public License along with
13
# this program. If not, see <http://www.gnu.org/licenses/>.
15
from __future__ import absolute_import, print_function, unicode_literals
27
from janitor.plugincore.manager import PluginManager
28
from janitor.plugincore.plugin import Plugin
29
from janitor.plugincore.testing.helpers import setup_plugins, Application
32
class ManagerTests(unittest.TestCase):
33
"""Test of the plugin manager."""
36
self._app = Application()
37
self._sys_path = sys.path[:]
40
# The tests which actually load plugins pollutes sys.path, so save and
41
# restore it around tests.
42
sys.path = self._sys_path
44
def test_missing_plugindir_is_ignored(self):
45
plugin_dir, cleanup = setup_plugins()
46
self.addCleanup(cleanup)
47
missing_dir = os.path.join(plugin_dir, 'does', 'not', 'exist')
48
manager = PluginManager(self._app, [missing_dir])
49
# Even though the manager is pointing to a missing plugins dir,
50
# getting all the plugin files will not crash, it will just return an
52
self.assertEqual(list(manager.plugin_files), [])
54
def test_finds_no_plugins_in_empty_directory(self):
55
plugin_dir, cleanup = setup_plugins()
56
self.addCleanup(cleanup)
57
manager = PluginManager(self._app, [plugin_dir])
58
self.assertEqual(len(manager.get_plugins()), 0)
60
def test_finds_one_plugin_file(self):
61
plugin_dir, cleanup = setup_plugins('alpha_plugin.py')
62
self.addCleanup(cleanup)
63
manager = PluginManager(self._app, [plugin_dir])
64
self.assertEqual(list(manager.plugin_files),
65
[os.path.join(plugin_dir, 'alpha_plugin.py')])
67
def test_finds_one_plugin(self):
68
plugin_dir, cleanup = setup_plugins('alpha_plugin.py')
69
self.addCleanup(cleanup)
70
manager = PluginManager(self._app, [plugin_dir])
71
plugins = list(manager.get_plugins())
72
self.assertEqual(len(plugins), 1)
73
self.assertTrue(isinstance(plugins[0], Plugin))
75
def test_plugin_loading_sets_application(self):
76
plugin_dir, cleanup = setup_plugins('alpha_plugin.py')
77
self.addCleanup(cleanup)
78
manager = PluginManager(self._app, [plugin_dir])
79
plugins = list(manager.get_plugins())
80
self.assertEqual(plugins[0].app, self._app)
82
def test_plugin_loading_callback(self):
84
def callback(filename, i, total):
85
callback_calls.append((os.path.basename(filename), i, total))
86
plugin_dir, cleanup = setup_plugins('alpha_plugin.py')
87
manager = PluginManager(self._app, [plugin_dir])
88
manager.get_plugins(callback=callback)
89
self.assertEqual(callback_calls, [('alpha_plugin.py', 0, 1)])
91
def test_plugin_loading_callback_with_multiple_plugins(self):
93
def callback(filename, i, total):
94
callback_calls.append((os.path.basename(filename), i, total))
95
plugin_dir, cleanup = setup_plugins(
96
'alpha_plugin.py', 'bravo_plugin.py')
97
manager = PluginManager(self._app, [plugin_dir])
98
manager.get_plugins(callback=callback)
99
self.assertEqual(callback_calls, [
100
('alpha_plugin.py', 0, 2),
101
('bravo_plugin.py', 1, 2),
104
def test_condition_equality(self):
105
# The first part of the conditions test looks for exactly equality
106
# between the condition argument and the plugin's condition
108
plugin_dir, cleanup = setup_plugins(
109
'alpha_plugin.py', 'bravo_plugin.py')
110
manager = PluginManager(self._app, [plugin_dir])
111
# Start by getting all the plugins.
112
all_plugins = manager.get_plugins()
113
# Set some conditions on the plugins.
114
all_plugins[0].condition = 'alpha'
115
all_plugins[1].condition = 'bravo'
116
self.assertEqual(manager.get_plugins(condition='zero'), [])
117
self.assertEqual(manager.get_plugins(condition='alpha'),
119
self.assertEqual(manager.get_plugins(condition='bravo'),
122
def test_condition_in(self):
123
# The second part of the conditions test checks for the given
124
# condition being in the sequence of conditions in the plugin. This
125
# is kind of crappy because let's say a plugin's condition is
126
# 'happy_days' and you pass in condition='happy', you'll get a match.
127
# Oh well, it's been this way forever.
128
plugin_dir, cleanup = setup_plugins(
129
'alpha_plugin.py', 'bravo_plugin.py')
130
manager = PluginManager(self._app, [plugin_dir])
131
# Start by getting all the plugins.
132
all_plugins = manager.get_plugins()
133
# Set some conditions on the plugins.
134
all_plugins[0].condition = ['alpha', 'happy']
135
all_plugins[1].condition = ['bravo', 'happy', 'sad']
136
self.assertEqual(manager.get_plugins(condition='zero'), [])
137
self.assertEqual(manager.get_plugins(condition='alpha'),
139
self.assertEqual(manager.get_plugins(condition='bravo'),
141
self.assertEqual(manager.get_plugins(condition='happy'), all_plugins)
142
self.assertEqual(manager.get_plugins(condition='sad'),
145
def test_condition_wildcard(self):
146
# The third conditions test matches everything.
147
plugin_dir, cleanup = setup_plugins(
148
'alpha_plugin.py', 'bravo_plugin.py', 'charlie_plugin.py')
149
manager = PluginManager(self._app, [plugin_dir])
150
# Start by getting all the plugins.
151
all_plugins = manager.get_plugins()
152
self.assertEqual(len(all_plugins), 3)
153
# Set some conditions on the plugins.
154
all_plugins[0].condition = ['alpha', 'happy']
155
all_plugins[1].condition = ['bravo', 'happy', 'sad']
156
# Do not give the third plugin an explicit condition.
157
self.assertEqual(manager.get_plugins(condition='*'), all_plugins)
159
def test_condition_default_matches_conditionless(self):
160
# By default, only conditionless plugins match the manager default.
161
plugin_dir, cleanup = setup_plugins(
162
'alpha_plugin.py', 'bravo_plugin.py', 'charlie_plugin.py')
163
manager = PluginManager(self._app, [plugin_dir])
164
# Start by getting all the plugins.
165
all_plugins = manager.get_plugins()
166
self.assertEqual(len(all_plugins), 3)
167
# Set some conditions on the plugins.
168
all_plugins[0].condition = ['alpha', 'happy']
169
all_plugins[1].condition = ['bravo', 'happy', 'sad']
170
# Do not give the third plugin an explicit condition.
171
self.assertEqual(manager.get_plugins(), [all_plugins[2]])