~bzr-pqm/bzr/bzr.dev

2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
1
# Copyright (C) 2005, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
750 by Martin Pool
- stubbed-out tests for python plugins
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
750 by Martin Pool
- stubbed-out tests for python plugins
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
12
#
750 by Martin Pool
- stubbed-out tests for python plugins
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for plugins"""
18
1185.16.83 by mbp at sourcefrog
- notes on testability of plugins
19
# XXX: There are no plugin tests at the moment because the plugin module
20
# affects the global state of the process.  See bzrlib/plugins.py for more
21
# comments.
22
23
import os
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
24
from StringIO import StringIO
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
25
import sys
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
26
import zipfile
750 by Martin Pool
- stubbed-out tests for python plugins
27
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
28
from bzrlib import plugin, tests
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
29
import bzrlib.plugin
30
import bzrlib.plugins
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
31
import bzrlib.commands
32
import bzrlib.help
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
33
from bzrlib.tests import TestCase, TestCaseInTempDir
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
34
from bzrlib.osutils import pathjoin, abspath
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
35
1185.16.83 by mbp at sourcefrog
- notes on testability of plugins
36
1185.16.84 by mbp at sourcefrog
- fix indents
37
PLUGIN_TEXT = """\
38
import bzrlib.commands
39
class cmd_myplug(bzrlib.commands.Command):
40
    '''Just a simple test plugin.'''
41
    aliases = ['mplg']
42
    def run(self):
43
        print 'Hello from my plugin'
44
"""
1492 by Robert Collins
Support decoration of commands.
45
46
# TODO: Write a test for plugin decoration of commands.
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
47
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
48
class TestLoadingPlugins(TestCaseInTempDir):
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
49
50
    activeattributes = {}
51
52
    def test_plugins_with_the_same_name_are_not_loaded(self):
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
53
        # This test tests that having two plugins in different directories does
54
        # not result in both being loaded when they have the same name.  get a
55
        # file name we can use which is also a valid attribute for accessing in
56
        # activeattributes. - we cannot give import parameters.
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
57
        tempattribute = "0"
58
        self.failIf(tempattribute in self.activeattributes)
59
        # set a place for the plugins to record their loading, and at the same
60
        # time validate that the location the plugins should record to is
61
        # valid and correct.
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
62
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
63
            [tempattribute] = []
64
        self.failUnless(tempattribute in self.activeattributes)
65
        # create two plugin directories
66
        os.mkdir('first')
67
        os.mkdir('second')
68
        # write a plugin that will record when its loaded in the 
69
        # tempattribute list.
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
70
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
71
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
72
        print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
73
        print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
74
        try:
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
75
            bzrlib.plugin.load_from_path(['first', 'second'])
76
            self.assertEqual(['first'], self.activeattributes[tempattribute])
77
        finally:
78
            # remove the plugin 'plugin'
79
            del self.activeattributes[tempattribute]
80
            if getattr(bzrlib.plugins, 'plugin', None):
81
                del bzrlib.plugins.plugin
82
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
83
84
    def test_plugins_from_different_dirs_can_demand_load(self):
85
        # This test tests that having two plugins in different
86
        # directories with different names allows them both to be loaded, when
87
        # we do a direct import statement.
88
        # Determine a file name we can use which is also a valid attribute
89
        # for accessing in activeattributes. - we cannot give import parameters.
90
        tempattribute = "different-dirs"
91
        self.failIf(tempattribute in self.activeattributes)
92
        # set a place for the plugins to record their loading, and at the same
93
        # time validate that the location the plugins should record to is
94
        # valid and correct.
95
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
96
            [tempattribute] = []
97
        self.failUnless(tempattribute in self.activeattributes)
98
        # create two plugin directories
99
        os.mkdir('first')
100
        os.mkdir('second')
101
        # write plugins that will record when they are loaded in the 
102
        # tempattribute list.
103
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
104
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
105
        print >> file(os.path.join('first', 'pluginone.py'), 'w'), template % (tempattribute, 'first')
106
        print >> file(os.path.join('second', 'plugintwo.py'), 'w'), template % (tempattribute, 'second')
107
        oldpath = bzrlib.plugins.__path__
108
        try:
109
            bzrlib.plugins.__path__ = ['first', 'second']
110
            exec "import bzrlib.plugins.pluginone"
111
            self.assertEqual(['first'], self.activeattributes[tempattribute])
112
            exec "import bzrlib.plugins.plugintwo"
113
            self.assertEqual(['first', 'second'],
114
                self.activeattributes[tempattribute])
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
115
        finally:
116
            # remove the plugin 'plugin'
117
            del self.activeattributes[tempattribute]
118
            if getattr(bzrlib.plugins, 'plugin', None):
119
                del bzrlib.plugins.plugin
120
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
121
122
123
class TestAllPlugins(TestCaseInTempDir):
124
125
    def test_plugin_appears_in_all_plugins(self):
126
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
127
        # check the plugin is not loaded already
128
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
129
        # write a plugin that _cannot_ fail to load.
130
        print >> file('plugin.py', 'w'), ""
131
        try:
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
132
            bzrlib.plugin.load_from_path(['.'])
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
133
            self.failUnless('plugin' in bzrlib.plugin.all_plugins())
134
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
135
            self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
136
                             bzrlib.plugins.plugin)
137
        finally:
138
            # remove the plugin 'plugin'
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
139
            if 'bzrlib.plugins.plugin' in sys.modules:
140
                del sys.modules['bzrlib.plugins.plugin']
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
141
            if getattr(bzrlib.plugins, 'plugin', None):
142
                del bzrlib.plugins.plugin
143
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
144
145
146
class TestPluginHelp(TestCaseInTempDir):
147
148
    def split_help_commands(self):
149
        help = {}
150
        current = None
2530.3.4 by Martin Pool
Deprecate run_bzr_captured in favour of just run_bzr
151
        for line in self.run_bzr('help commands')[0].splitlines():
2034.1.2 by Aaron Bentley
Fix testcase
152
            if not line.startswith(' '):
153
                current = line.split()[0]
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
154
            help[current] = help.get(current, '') + line
155
156
        return help
157
158
    def test_plugin_help_builtins_unaffected(self):
159
        # Check we don't get false positives
160
        help_commands = self.split_help_commands()
161
        for cmd_name in bzrlib.commands.builtin_command_names():
162
            if cmd_name in bzrlib.commands.plugin_command_names():
163
                continue
164
            try:
2432.1.12 by Robert Collins
Relocate command help onto Command.
165
                help = bzrlib.commands.get_cmd_object(cmd_name).get_help_text()
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
166
            except NotImplementedError:
167
                # some commands have no help
168
                pass
169
            else:
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
170
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
171
2432.1.12 by Robert Collins
Relocate command help onto Command.
172
            if cmd_name in help_commands.keys():
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
173
                # some commands are hidden
174
                help = help_commands[cmd_name]
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
175
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
176
177
    def test_plugin_help_shows_plugin(self):
178
        # Create a test plugin
179
        os.mkdir('plugin_test')
180
        f = open(pathjoin('plugin_test', 'myplug.py'), 'w')
181
        f.write(PLUGIN_TEXT)
182
        f.close()
183
184
        try:
185
            # Check its help
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
186
            bzrlib.plugin.load_from_path(['plugin_test'])
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
187
            bzrlib.commands.register_command( bzrlib.plugins.myplug.cmd_myplug)
2530.3.4 by Martin Pool
Deprecate run_bzr_captured in favour of just run_bzr
188
            help = self.run_bzr('help myplug')[0]
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
189
            self.assertContainsRe(help, 'plugin "myplug"')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
190
            help = self.split_help_commands()['myplug']
2034.1.4 by Aaron Bentley
Change angle brackets to square brackets
191
            self.assertContainsRe(help, '\[myplug\]')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
192
        finally:
2204.3.2 by Alexander Belchenko
cherrypicking: test_plugin_help_shows_plugin: fix cleanup after test
193
            # unregister command
194
            if bzrlib.commands.plugin_cmds.get('myplug', None):
195
                del bzrlib.commands.plugin_cmds['myplug']
196
            # remove the plugin 'myplug'
197
            if getattr(bzrlib.plugins, 'myplug', None):
198
                delattr(bzrlib.plugins, 'myplug')
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
199
200
201
class TestPluginFromZip(TestCaseInTempDir):
202
203
    def make_zipped_plugin(self, zip_name, filename):
204
        z = zipfile.ZipFile(zip_name, 'w')
205
        z.writestr(filename, PLUGIN_TEXT)
206
        z.close()
207
208
    def check_plugin_load(self, zip_name, plugin_name):
209
        self.assertFalse(plugin_name in dir(bzrlib.plugins),
210
                         'Plugin already loaded')
2610.2.1 by Martin Pool
(Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice
211
        old_path = bzrlib.plugins.__path__
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
212
        try:
2610.2.1 by Martin Pool
(Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice
213
            # this is normally done by load_plugins -> set_plugins_path
214
            bzrlib.plugins.__path__ = [zip_name]
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
215
            bzrlib.plugin.load_from_zip(zip_name)
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
216
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
217
                            'Plugin is not loaded')
218
        finally:
219
            # unregister plugin
220
            if getattr(bzrlib.plugins, plugin_name, None):
221
                delattr(bzrlib.plugins, plugin_name)
2610.2.1 by Martin Pool
(Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice
222
                del sys.modules['bzrlib.plugins.' + plugin_name]
223
            bzrlib.plugins.__path__ = old_path
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
224
225
    def test_load_module(self):
226
        self.make_zipped_plugin('./test.zip', 'ziplug.py')
227
        self.check_plugin_load('./test.zip', 'ziplug')
228
229
    def test_load_package(self):
230
        self.make_zipped_plugin('./test.zip', 'ziplug/__init__.py')
231
        self.check_plugin_load('./test.zip', 'ziplug')
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
232
233
234
class TestSetPluginsPath(TestCase):
235
    
236
    def test_set_plugins_path(self):
237
        """set_plugins_path should set the module __path__ correctly."""
238
        old_path = bzrlib.plugins.__path__
239
        try:
240
            bzrlib.plugins.__path__ = []
241
            expected_path = bzrlib.plugin.set_plugins_path()
242
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
243
        finally:
244
            bzrlib.plugins.__path__ = old_path
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
245
246
247
class TestHelpIndex(tests.TestCase):
248
    """Tests for the PluginsHelpIndex class."""
249
250
    def test_default_constructable(self):
251
        index = plugin.PluginsHelpIndex()
252
253
    def test_get_topics_None(self):
254
        """Searching for None returns an empty list."""
255
        index = plugin.PluginsHelpIndex()
256
        self.assertEqual([], index.get_topics(None))
257
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
258
    def test_get_topics_for_plugin(self):
259
        """Searching for plugin name gets its docstring."""
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
260
        index = plugin.PluginsHelpIndex()
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
261
        # make a new plugin here for this test, even if we're run with
262
        # --no-plugins
263
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
264
        demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
265
        sys.modules['bzrlib.plugins.demo_module'] = demo_module
2457.1.1 by Robert Collins
(robertc) Fix bzr --no-plugins selftest which was broken by the help indices patch. (Robert Collins, Martin Pool)
266
        try:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
267
            topics = index.get_topics('demo_module')
2457.1.1 by Robert Collins
(robertc) Fix bzr --no-plugins selftest which was broken by the help indices patch. (Robert Collins, Martin Pool)
268
            self.assertEqual(1, len(topics))
269
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
270
            self.assertEqual(demo_module, topics[0].module)
271
        finally:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
272
            del sys.modules['bzrlib.plugins.demo_module']
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
273
274
    def test_get_topics_no_topic(self):
275
        """Searching for something that is not a plugin returns []."""
276
        # test this by using a name that cannot be a plugin - its not
277
        # a valid python identifier.
278
        index = plugin.PluginsHelpIndex()
279
        self.assertEqual([], index.get_topics('nothing by this name'))
280
281
    def test_prefix(self):
282
        """PluginsHelpIndex has a prefix of 'plugins/'."""
283
        index = plugin.PluginsHelpIndex()
284
        self.assertEqual('plugins/', index.prefix)
285
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
286
    def test_get_plugin_topic_with_prefix(self):
287
        """Searching for plugins/demo_module returns help."""
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
288
        index = plugin.PluginsHelpIndex()
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
289
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
290
        demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
291
        sys.modules['bzrlib.plugins.demo_module'] = demo_module
2457.1.1 by Robert Collins
(robertc) Fix bzr --no-plugins selftest which was broken by the help indices patch. (Robert Collins, Martin Pool)
292
        try:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
293
            topics = index.get_topics('plugins/demo_module')
2457.1.1 by Robert Collins
(robertc) Fix bzr --no-plugins selftest which was broken by the help indices patch. (Robert Collins, Martin Pool)
294
            self.assertEqual(1, len(topics))
295
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
296
            self.assertEqual(demo_module, topics[0].module)
297
        finally:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
298
            del sys.modules['bzrlib.plugins.demo_module']
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
299
300
301
class FakeModule(object):
302
    """A fake module to test with."""
303
304
    def __init__(self, doc, name):
305
        self.__doc__ = doc
306
        self.__name__ = name
307
308
309
class TestModuleHelpTopic(tests.TestCase):
310
    """Tests for the ModuleHelpTopic class."""
311
312
    def test_contruct(self):
313
        """Construction takes the module to document."""
314
        mod = FakeModule('foo', 'foo')
315
        topic = plugin.ModuleHelpTopic(mod)
316
        self.assertEqual(mod, topic.module)
317
318
    def test_get_help_text_None(self):
319
        """A ModuleHelpTopic returns the docstring for get_help_text."""
320
        mod = FakeModule(None, 'demo')
321
        topic = plugin.ModuleHelpTopic(mod)
322
        self.assertEqual("Plugin 'demo' has no docstring.\n",
323
            topic.get_help_text())
324
325
    def test_get_help_text_no_carriage_return(self):
326
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
327
        mod = FakeModule('one line of help', 'demo')
328
        topic = plugin.ModuleHelpTopic(mod)
329
        self.assertEqual("one line of help\n",
330
            topic.get_help_text())
331
332
    def test_get_help_text_carriage_return(self):
333
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
334
        mod = FakeModule('two lines of help\nand more\n', 'demo')
335
        topic = plugin.ModuleHelpTopic(mod)
336
        self.assertEqual("two lines of help\nand more\n",
337
            topic.get_help_text())
338
339
    def test_get_help_text_with_additional_see_also(self):
340
        mod = FakeModule('two lines of help\nand more', 'demo')
341
        topic = plugin.ModuleHelpTopic(mod)
342
        self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
343
            topic.get_help_text(['foo', 'bar']))
2432.1.29 by Robert Collins
Add get_help_topic to ModuleHelpTopic.
344
345
    def test_get_help_topic(self):
346
        """The help topic for a plugin is its module name."""
2432.1.30 by Robert Collins
Fix the ModuleHelpTopic get_help_topic to be tested with closer to real world data and strip the bzrlib.plugins. prefix from the name.
347
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
2432.1.29 by Robert Collins
Add get_help_topic to ModuleHelpTopic.
348
        topic = plugin.ModuleHelpTopic(mod)
349
        self.assertEqual('demo', topic.get_help_topic())
2432.1.30 by Robert Collins
Fix the ModuleHelpTopic get_help_topic to be tested with closer to real world data and strip the bzrlib.plugins. prefix from the name.
350
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
2432.1.29 by Robert Collins
Add get_help_topic to ModuleHelpTopic.
351
        topic = plugin.ModuleHelpTopic(mod)
352
        self.assertEqual('foo_bar', topic.get_help_topic())