File: test_plugin_manager.py

package info (click to toggle)
python-envisage 7.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,880 kB
  • sloc: python: 8,696; makefile: 76; sh: 5
file content (259 lines) | stat: -rw-r--r-- 8,479 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# (C) Copyright 2007-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Tests for the plugin manager. """


# Standard library imports.
import unittest

from traits.api import Bool

# Enthought library imports.
from envisage.api import Plugin, PluginManager


class SimplePlugin(Plugin):
    """A simple plugin."""

    #### 'SimplePlugin' interface #############################################

    started = Bool(False)
    stopped = Bool(False)

    ###########################################################################
    # 'IPlugin' interface.
    ###########################################################################

    def start(self):
        """Start the plugin."""

        self.started = True
        self.stopped = False

    def stop(self):
        """Stop the plugin."""

        self.started = False
        self.stopped = True


class BadPlugin(Plugin):
    """A plugin that just causes trouble ;^)."""

    ###########################################################################
    # 'IPlugin' interface.
    ###########################################################################

    def start(self):
        """Start the plugin."""

        raise 1 / 0

    def stop(self):
        """Stop the plugin."""

        raise 1 / 0


class PluginManagerTestCase(unittest.TestCase):
    """Tests for the plugin manager."""

    def test_get_plugin(self):
        """get plugin"""

        simple_plugin = SimplePlugin()
        plugin_manager = PluginManager(plugins=[simple_plugin])

        # Get the plugin.
        plugin = plugin_manager.get_plugin(simple_plugin.id)
        self.assertEqual(plugin, simple_plugin)

        # Try to get a non-existent plugin.
        self.assertEqual(None, plugin_manager.get_plugin("bogus"))

    def test_iteration_over_plugins(self):
        """iteration over plugins"""

        simple_plugin = SimplePlugin()
        bad_plugin = BadPlugin()

        plugin_manager = PluginManager(plugins=[simple_plugin, bad_plugin])

        # Iterate over the plugin manager's plugins.
        plugins = []
        for plugin in plugin_manager:
            plugins.append(plugin)

        self.assertEqual([simple_plugin, bad_plugin], plugins)

    def test_start_and_stop(self):
        """start and stop"""

        simple_plugin = SimplePlugin()
        plugin_manager = PluginManager(plugins=[simple_plugin])

        # Start the plugin manager. This starts all of the plugin manager's
        # plugins.
        plugin_manager.start()

        # Make sure the plugin was started.
        self.assertEqual(True, simple_plugin.started)

        # Stop the plugin manager. This stops all of the plugin manager's
        # plugins.
        plugin_manager.stop()

        # Make sure the plugin was stopped.
        self.assertEqual(True, simple_plugin.stopped)

    def test_start_and_stop_errors(self):
        """start and stop errors"""

        simple_plugin = SimplePlugin()
        bad_plugin = BadPlugin()
        plugin_manager = PluginManager(plugins=[simple_plugin, bad_plugin])

        # Start the plugin manager. This starts all of the plugin manager's
        # plugins.
        with self.assertRaises(ZeroDivisionError):
            plugin_manager.start()

        # Stop the plugin manager. This stops all of the plugin manager's
        # plugins.
        with self.assertRaises(ZeroDivisionError):
            plugin_manager.stop()

        # Try to start a non-existent plugin.
        with self.assertRaises(ValueError):
            plugin_manager.start_plugin(plugin_id="bogus")

        # Try to stop a non-existent plugin.
        with self.assertRaises(ValueError):
            plugin_manager.stop_plugin(plugin_id="bogus")

    def test_only_include_plugins_whose_ids_are_in_the_include_list(self):
        # Note that the items in the list use the 'fnmatch' syntax for matching
        # plugins Ids.
        include = ["foo", "bar"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PluginManager(
                include=include,
                plugins=[
                    SimplePlugin(id="foo"),
                    SimplePlugin(id="bar"),
                    SimplePlugin(id="baz"),
                ],
            )

        # The Ids of the plugins that we expect the plugin manager to find.
        expected = ["foo", "bar"]

        # Make sure the plugin manager found only the required plugins and that
        # it starts and stops them correctly..
        self._test_start_and_stop(plugin_manager, expected)

    def test_only_include_plugins_matching_a_wildcard_in_the_include_list(
        self,
    ):
        # Note that the items in the list use the 'fnmatch' syntax for matching
        # plugins Ids.
        include = ["b*"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PluginManager(
                include=include,
                plugins=[
                    SimplePlugin(id="foo"),
                    SimplePlugin(id="bar"),
                    SimplePlugin(id="baz"),
                ],
            )

        # The Ids of the plugins that we expect the plugin manager to find.
        expected = ["bar", "baz"]

        # Make sure the plugin manager found only the required plugins and that
        # it starts and stops them correctly..
        self._test_start_and_stop(plugin_manager, expected)

    def test_ignore_plugins_whose_ids_are_in_the_exclude_list(self):
        # Note that the items in the list use the 'fnmatch' syntax for matching
        # plugins Ids.
        exclude = ["foo", "baz"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PluginManager(
                exclude=exclude,
                plugins=[
                    SimplePlugin(id="foo"),
                    SimplePlugin(id="bar"),
                    SimplePlugin(id="baz"),
                ],
            )

        # The Ids of the plugins that we expect the plugin manager to find.
        expected = ["bar"]

        # Make sure the plugin manager found only the required plugins and that
        # it starts and stops them correctly..
        self._test_start_and_stop(plugin_manager, expected)

    def test_ignore_plugins_matching_a_wildcard_in_the_exclude_list(self):
        # Note that the items in the list use the 'fnmatch' syntax for matching
        # plugins Ids.
        exclude = ["b*"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PluginManager(
                exclude=exclude,
                plugins=[
                    SimplePlugin(id="foo"),
                    SimplePlugin(id="bar"),
                    SimplePlugin(id="baz"),
                ],
            )

        # The Ids of the plugins that we expect the plugin manager to find.
        expected = ["foo"]

        # Make sure the plugin manager found only the required plugins and that
        # it starts and stops them correctly..
        self._test_start_and_stop(plugin_manager, expected)

    #### Private protocol #####################################################

    def _test_start_and_stop(self, plugin_manager, expected):
        """
        Make sure the plugin manager starts and stops the expected plugins.
        """

        # Make sure the plugin manager found only the required plugins.
        self.assertEqual(expected, [plugin.id for plugin in plugin_manager])

        # Start the plugin manager. This starts all of the plugin manager's
        # plugins.
        plugin_manager.start()

        # Make sure all of the the plugins were started.
        for id in expected:
            plugin = plugin_manager.get_plugin(id)
            self.assertNotEqual(None, plugin)
            self.assertEqual(True, plugin.started)

        # Stop the plugin manager. This stops all of the plugin manager's
        # plugins.
        plugin_manager.stop()

        # Make sure all of the the plugins were stopped.
        for id in expected:
            plugin = plugin_manager.get_plugin(id)
            self.assertNotEqual(None, plugin)
            self.assertEqual(True, plugin.stopped)