File: test_package_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 (162 lines) | stat: -rw-r--r-- 6,132 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
# (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 'Package' plugin manager. """


import contextlib
import unittest
from os.path import dirname, join

from envisage.package_plugin_manager import PackagePluginManager
from envisage.tests.support import restore_sys_path


class PackagePluginManagerTestCase(unittest.TestCase):
    """Tests for the 'Package' plugin manager."""

    def setUp(self):
        """Prepares the test fixture before each test method is called."""

        cleanup_stack = contextlib.ExitStack()
        self.addCleanup(cleanup_stack.close)
        cleanup_stack.enter_context(restore_sys_path())

        # The location of the 'plugins' test data directory.
        self.plugins_dir = join(dirname(__file__), "plugins")

    def test_find_plugins_in_packages_on_the_plugin_path(self):
        with self.assertWarns(DeprecationWarning):
            plugin_manager = PackagePluginManager(
                plugin_path=[self.plugins_dir]
            )
        ids = [plugin.id for plugin in plugin_manager]

        self.assertEqual(len(ids), 3)
        self.assertIn("banana", ids)
        self.assertIn("orange", ids)
        self.assertIn("pear", ids)

    def test_only_find_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 = ["orange", "pear"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PackagePluginManager(
                plugin_path=[self.plugins_dir], include=include
            )

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

        # 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_find_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 = ["*r*"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PackagePluginManager(
                plugin_path=[self.plugins_dir], include=include
            )

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

        # 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 = ["orange", "pear"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PackagePluginManager(
                plugin_path=[self.plugins_dir], exclude=exclude
            )

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

        # 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 = ["*r*"]

        with self.assertWarns(DeprecationWarning):
            plugin_manager = PackagePluginManager(
                plugin_path=[self.plugins_dir], exclude=exclude
            )

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

        # 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_reflect_changes_to_the_plugin_path(self):
        with self.assertWarns(DeprecationWarning):
            plugin_manager = PackagePluginManager()
        ids = [plugin.id for plugin in plugin_manager]
        self.assertEqual(len(ids), 0)

        plugin_manager.plugin_path.append(self.plugins_dir)
        ids = [plugin.id for plugin in plugin_manager]
        self.assertEqual(len(ids), 3)
        self.assertIn("banana", ids)
        self.assertIn("orange", ids)
        self.assertIn("pear", ids)

        del plugin_manager.plugin_path[0]
        ids = [plugin.id for plugin in plugin_manager]
        self.assertEqual(len(ids), 0)

    #### 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(
            list(sorted(expected)),
            list(sorted(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)