~dylanmccall/update-manager/dialogs-refactor

« back to all changes in this revision

Viewing changes to janitor/plugincore/tests/test_manager.py

  • Committer: Barry Warsaw
  • Date: 2012-06-11 15:08:35 UTC
  • mto: (2446.1.6 py3)
  • mto: This revision was merged to the branch mainline in revision 2454.
  • Revision ID: barry@python.org-20120611150835-lbx9cjwzfjy3y7bw
Re-integrate the Python 3 Computer Janitor refactoring branch for now.  The
code itself lives in ~computer-janitor-hackers/computer-janitor/py3refactor
and will eventually be ripped out of here, but for now, this unblocks other
folks.

Still getting test failures in `bzr bd -S` though.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2012  Canonical, Ltd.
 
2
#
 
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.
 
6
#
 
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
 
10
# more details.
 
11
#
 
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/>.
 
14
 
 
15
from __future__ import absolute_import, print_function, unicode_literals
 
16
 
 
17
__metaclass__ = type
 
18
__all__ = [
 
19
    'ManagerTests',
 
20
    ]
 
21
 
 
22
 
 
23
import os
 
24
import sys
 
25
import unittest
 
26
 
 
27
from janitor.plugincore.manager import PluginManager
 
28
from janitor.plugincore.plugin import Plugin
 
29
from janitor.plugincore.testing.helpers import setup_plugins, Application
 
30
 
 
31
 
 
32
class ManagerTests(unittest.TestCase):
 
33
    """Test of the plugin manager."""
 
34
 
 
35
    def setUp(self):
 
36
        self._app = Application()
 
37
        self._sys_path = sys.path[:]
 
38
 
 
39
    def tearDown(self):
 
40
        # The tests which actually load plugins pollutes sys.path, so save and
 
41
        # restore it around tests.
 
42
        sys.path = self._sys_path
 
43
 
 
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
 
51
        # empty sequence.
 
52
        self.assertEqual(list(manager.plugin_files), [])
 
53
 
 
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)
 
59
 
 
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')])
 
66
 
 
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))
 
74
 
 
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)
 
81
 
 
82
    def test_plugin_loading_callback(self):
 
83
        callback_calls = []
 
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)])
 
90
 
 
91
    def test_plugin_loading_callback_with_multiple_plugins(self):
 
92
        callback_calls = []
 
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),
 
102
            ])
 
103
 
 
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
 
107
        # attribute.
 
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'),
 
118
                         [all_plugins[0]])
 
119
        self.assertEqual(manager.get_plugins(condition='bravo'),
 
120
                         [all_plugins[1]])
 
121
 
 
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'),
 
138
                         [all_plugins[0]])
 
139
        self.assertEqual(manager.get_plugins(condition='bravo'),
 
140
                         [all_plugins[1]])
 
141
        self.assertEqual(manager.get_plugins(condition='happy'), all_plugins)
 
142
        self.assertEqual(manager.get_plugins(condition='sad'),
 
143
                         [all_plugins[1]])
 
144
 
 
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)
 
158
 
 
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]])