Debian Codegraph @

envisage/tests/test_plugin_manager.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ Tests for the plugin manager. """
11
12
13 # Standard library imports.
14 import unittest
15
16 from traits.api import Bool
17
18 # Enthought library imports.
19 from envisage.api import Plugin, PluginManager
20
21
22 class SimplePlugin(Plugin):
23 """A simple plugin."""
24
25 #### 'SimplePlugin' interface #############################################
26
27 started = Bool(False)
28 stopped = Bool(False)
29
30 ###########################################################################
31 # 'IPlugin' interface.
32 ###########################################################################
33
34 def start(self):
35 """Start the plugin."""
36
37 self.started = True
38 self.stopped = False
39
40 def stop(self):
41 """Stop the plugin."""
42
43 self.started = False
44 self.stopped = True
45
46
47 class BadPlugin(Plugin):
48 """A plugin that just causes trouble ;^)."""
49
50 ###########################################################################
51 # 'IPlugin' interface.
52 ###########################################################################
53
54 def start(self):
55 """Start the plugin."""
56
57 raise 1 / 0
58
59 def stop(self):
60 """Stop the plugin."""
61
62 raise 1 / 0
63
64
65 class PluginManagerTestCase(unittest.TestCase):
66 """Tests for the plugin manager."""
67
68 def test_get_plugin(self):
69 """get plugin"""
70
71 simple_plugin = SimplePlugin()
72 plugin_manager = PluginManager(plugins=[simple_plugin])
73
74 # Get the plugin.
75 plugin = plugin_manager.get_plugin(simple_plugin.id)
76 self.assertEqual(plugin, simple_plugin)
77
78 # Try to get a non-existent plugin.
79 self.assertEqual(None, plugin_manager.get_plugin("bogus"))
80
81 def test_iteration_over_plugins(self):
82 """iteration over plugins"""
83
84 simple_plugin = SimplePlugin()
85 bad_plugin = BadPlugin()
86
87 plugin_manager = PluginManager(plugins=[simple_plugin, bad_plugin])
88
89 # Iterate over the plugin manager's plugins.
90 plugins = []
91 for plugin in plugin_manager:
92 plugins.append(plugin)
93
94 self.assertEqual([simple_plugin, bad_plugin], plugins)
95
96 def test_start_and_stop(self):
97 """start and stop"""
98
99 simple_plugin = SimplePlugin()
100 plugin_manager = PluginManager(plugins=[simple_plugin])
101
102 # Start the plugin manager. This starts all of the plugin manager's
103 # plugins.
104 plugin_manager.start()
105
106 # Make sure the plugin was started.
107 self.assertEqual(True, simple_plugin.started)
108
109 # Stop the plugin manager. This stops all of the plugin manager's
110 # plugins.
111 plugin_manager.stop()
112
113 # Make sure the plugin was stopped.
114 self.assertEqual(True, simple_plugin.stopped)
115
116 def test_start_and_stop_errors(self):
117 """start and stop errors"""
118
119 simple_plugin = SimplePlugin()
120 bad_plugin = BadPlugin()
121 plugin_manager = PluginManager(plugins=[simple_plugin, bad_plugin])
122
123 # Start the plugin manager. This starts all of the plugin manager's
124 # plugins.
125 with self.assertRaises(ZeroDivisionError):
126 plugin_manager.start()
127
128 # Stop the plugin manager. This stops all of the plugin manager's
129 # plugins.
130 with self.assertRaises(ZeroDivisionError):
131 plugin_manager.stop()
132
133 # Try to start a non-existent plugin.
134 with self.assertRaises(ValueError):
135 plugin_manager.start_plugin(plugin_id="bogus")
136
137 # Try to stop a non-existent plugin.
138 with self.assertRaises(ValueError):
139 plugin_manager.stop_plugin(plugin_id="bogus")
140
141 def test_only_include_plugins_whose_ids_are_in_the_include_list(self):
142 # Note that the items in the list use the 'fnmatch' syntax for matching
143 # plugins Ids.
144 include = ["foo", "bar"]
145
146 with self.assertWarns(DeprecationWarning):
147 plugin_manager = PluginManager(
148 include=include,
149 plugins=[
150 SimplePlugin(id="foo"),
151 SimplePlugin(id="bar"),
152 SimplePlugin(id="baz"),
153 ],
154 )
155
156 # The Ids of the plugins that we expect the plugin manager to find.
157 expected = ["foo", "bar"]
158
159 # Make sure the plugin manager found only the required plugins and that
160 # it starts and stops them correctly..
161 self._test_start_and_stop(plugin_manager, expected)
162
163 def test_only_include_plugins_matching_a_wildcard_in_the_include_list(
164 self,
165 ):
166 # Note that the items in the list use the 'fnmatch' syntax for matching
167 # plugins Ids.
168 include = ["b*"]
169
170 with self.assertWarns(DeprecationWarning):
171 plugin_manager = PluginManager(
172 include=include,
173 plugins=[
174 SimplePlugin(id="foo"),
175 SimplePlugin(id="bar"),
176 SimplePlugin(id="baz"),
177 ],
178 )
179
180 # The Ids of the plugins that we expect the plugin manager to find.
181 expected = ["bar", "baz"]
182
183 # Make sure the plugin manager found only the required plugins and that
184 # it starts and stops them correctly..
185 self._test_start_and_stop(plugin_manager, expected)
186
187 def test_ignore_plugins_whose_ids_are_in_the_exclude_list(self):
188 # Note that the items in the list use the 'fnmatch' syntax for matching
189 # plugins Ids.
190 exclude = ["foo", "baz"]
191
192 with self.assertWarns(DeprecationWarning):
193 plugin_manager = PluginManager(
194 exclude=exclude,
195 plugins=[
196 SimplePlugin(id="foo"),
197 SimplePlugin(id="bar"),
198 SimplePlugin(id="baz"),
199 ],
200 )
201
202 # The Ids of the plugins that we expect the plugin manager to find.
203 expected = ["bar"]
204
205 # Make sure the plugin manager found only the required plugins and that
206 # it starts and stops them correctly..
207 self._test_start_and_stop(plugin_manager, expected)
208
209 def test_ignore_plugins_matching_a_wildcard_in_the_exclude_list(self):
210 # Note that the items in the list use the 'fnmatch' syntax for matching
211 # plugins Ids.
212 exclude = ["b*"]
213
214 with self.assertWarns(DeprecationWarning):
215 plugin_manager = PluginManager(
216 exclude=exclude,
217 plugins=[
218 SimplePlugin(id="foo"),
219 SimplePlugin(id="bar"),
220 SimplePlugin(id="baz"),
221 ],
222 )
223
224 # The Ids of the plugins that we expect the plugin manager to find.
225 expected = ["foo"]
226
227 # Make sure the plugin manager found only the required plugins and that
228 # it starts and stops them correctly..
229 self._test_start_and_stop(plugin_manager, expected)
230
231 #### Private protocol #####################################################
232
233 def _test_start_and_stop(self, plugin_manager, expected):
234 """
235 Make sure the plugin manager starts and stops the expected plugins.
236 """
237
238 # Make sure the plugin manager found only the required plugins.
239 self.assertEqual(expected, [plugin.id for plugin in plugin_manager])
240
241 # Start the plugin manager. This starts all of the plugin manager's
242 # plugins.
243 plugin_manager.start()
244
245 # Make sure all of the the plugins were started.
246 for id in expected:
247 plugin = plugin_manager.get_plugin(id)
248 self.assertNotEqual(None, plugin)
249 self.assertEqual(True, plugin.started)
250
251 # Stop the plugin manager. This stops all of the plugin manager's
252 # plugins.
253 plugin_manager.stop()
254
255 # Make sure all of the the plugins were stopped.
256 for id in expected:
257 plugin = plugin_manager.get_plugin(id)
258 self.assertNotEqual(None, plugin)
259 self.assertEqual(True, plugin.stopped)