~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Alexander Belchenko
  • Date: 2007-01-30 23:05:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2259.
  • Revision ID: bialix@ukr.net-20070130230535-kx1rd478rtigyc3v
standalone installer: win98 support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007 Canonical Ltd
 
1
# Copyright (C) 2005 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
# affects the global state of the process.  See bzrlib/plugins.py for more
21
21
# comments.
22
22
 
23
 
import logging
24
23
import os
25
24
from StringIO import StringIO
26
 
import sys
27
25
import zipfile
28
26
 
29
 
from bzrlib import plugin, tests
30
27
import bzrlib.plugin
31
28
import bzrlib.plugins
32
29
import bzrlib.commands
33
30
import bzrlib.help
34
 
from bzrlib.symbol_versioning import one_three
35
 
from bzrlib.tests import TestCase, TestCaseInTempDir
36
 
from bzrlib.osutils import pathjoin, abspath, normpath
37
 
 
 
31
from bzrlib.tests import TestCaseInTempDir
 
32
from bzrlib.osutils import pathjoin, abspath
 
33
 
 
34
class PluginTest(TestCaseInTempDir):
 
35
    """Create an external plugin and test loading."""
 
36
#    def test_plugin_loading(self):
 
37
#        orig_help = self.run_bzr_captured('bzr help commands')[0]
 
38
#        os.mkdir('plugin_test')
 
39
#        f = open(pathjoin('plugin_test', 'myplug.py'), 'wt')
 
40
#        f.write(PLUGIN_TEXT)
 
41
#        f.close()
 
42
#        newhelp = self.run_bzr_captured('bzr help commands')[0]
 
43
#        assert newhelp.startswith('You have been overridden\n')
 
44
#        # We added a line, but the rest should work
 
45
#        assert newhelp[25:] == help
 
46
#
 
47
#        assert backtick('bzr commit -m test') == "I'm sorry dave, you can't do that\n"
 
48
#
 
49
#        shutil.rmtree('plugin_test')
 
50
#
 
51
 
 
52
#         os.environ['BZRPLUGINPATH'] = abspath('plugin_test')
 
53
#         help = backtick('bzr help commands')
 
54
#         assert help.find('myplug') != -1
 
55
#         assert help.find('Just a simple test plugin.') != -1
 
56
 
 
57
 
 
58
#         assert backtick('bzr myplug') == 'Hello from my plugin\n'
 
59
#         assert backtick('bzr mplg') == 'Hello from my plugin\n'
 
60
 
 
61
#         f = open(pathjoin('plugin_test', 'override.py'), 'wb')
 
62
#         f.write("""import bzrlib, bzrlib.commands
 
63
#     class cmd_commit(bzrlib.commands.cmd_commit):
 
64
#         '''Commit changes into a new revision.'''
 
65
#         def run(self, *args, **kwargs):
 
66
#             print "I'm sorry dave, you can't do that"
 
67
 
 
68
#     class cmd_help(bzrlib.commands.cmd_help):
 
69
#         '''Show help on a command or other topic.'''
 
70
#         def run(self, *args, **kwargs):
 
71
#             print "You have been overridden"
 
72
#             bzrlib.commands.cmd_help.run(self, *args, **kwargs)
 
73
 
 
74
#         """
38
75
 
39
76
PLUGIN_TEXT = """\
40
77
import bzrlib.commands
47
84
 
48
85
# TODO: Write a test for plugin decoration of commands.
49
86
 
50
 
class TestLoadingPlugins(TestCaseInTempDir):
 
87
class TestOneNamedPluginOnly(TestCaseInTempDir):
51
88
 
52
89
    activeattributes = {}
53
90
 
54
91
    def test_plugins_with_the_same_name_are_not_loaded(self):
55
 
        # This test tests that having two plugins in different directories does
56
 
        # not result in both being loaded when they have the same name.  get a
57
 
        # file name we can use which is also a valid attribute for accessing in
58
 
        # activeattributes. - we cannot give import parameters.
59
 
        tempattribute = "0"
60
 
        self.failIf(tempattribute in self.activeattributes)
61
 
        # set a place for the plugins to record their loading, and at the same
62
 
        # time validate that the location the plugins should record to is
63
 
        # valid and correct.
64
 
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
65
 
            [tempattribute] = []
66
 
        self.failUnless(tempattribute in self.activeattributes)
67
 
        # create two plugin directories
68
 
        os.mkdir('first')
69
 
        os.mkdir('second')
70
 
        # write a plugin that will record when its loaded in the 
71
 
        # tempattribute list.
72
 
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
73
 
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
74
 
 
75
 
        outfile = open(os.path.join('first', 'plugin.py'), 'w')
76
 
        try:
77
 
            outfile.write(template % (tempattribute, 'first'))
78
 
            outfile.write('\n')
79
 
        finally:
80
 
            outfile.close()
81
 
 
82
 
        outfile = open(os.path.join('second', 'plugin.py'), 'w')
83
 
        try:
84
 
            outfile.write(template % (tempattribute, 'second'))
85
 
            outfile.write('\n')
86
 
        finally:
87
 
            outfile.close()
88
 
 
89
 
        try:
90
 
            bzrlib.plugin.load_from_path(['first', 'second'])
91
 
            self.assertEqual(['first'], self.activeattributes[tempattribute])
92
 
        finally:
93
 
            # remove the plugin 'plugin'
94
 
            del self.activeattributes[tempattribute]
95
 
            if 'bzrlib.plugins.plugin' in sys.modules:
96
 
                del sys.modules['bzrlib.plugins.plugin']
97
 
            if getattr(bzrlib.plugins, 'plugin', None):
98
 
                del bzrlib.plugins.plugin
99
 
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
100
 
 
101
 
    def test_plugins_from_different_dirs_can_demand_load(self):
102
92
        # This test tests that having two plugins in different
103
 
        # directories with different names allows them both to be loaded, when
104
 
        # we do a direct import statement.
105
 
        # Determine a file name we can use which is also a valid attribute
 
93
        # directories does not result in both being loaded.
 
94
        # get a file name we can use which is also a valid attribute
106
95
        # for accessing in activeattributes. - we cannot give import parameters.
107
 
        tempattribute = "different-dirs"
 
96
        tempattribute = "0"
108
97
        self.failIf(tempattribute in self.activeattributes)
109
98
        # set a place for the plugins to record their loading, and at the same
110
99
        # time validate that the location the plugins should record to is
111
100
        # valid and correct.
112
 
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
 
101
        bzrlib.tests.test_plugins.TestOneNamedPluginOnly.activeattributes \
113
102
            [tempattribute] = []
114
103
        self.failUnless(tempattribute in self.activeattributes)
115
104
        # create two plugin directories
116
105
        os.mkdir('first')
117
106
        os.mkdir('second')
118
 
        # write plugins that will record when they are loaded in the 
 
107
        # write a plugin that will record when its loaded in the 
119
108
        # tempattribute list.
120
 
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
121
 
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
122
 
 
123
 
        outfile = open(os.path.join('first', 'pluginone.py'), 'w')
124
 
        try:
125
 
            outfile.write(template % (tempattribute, 'first'))
126
 
            outfile.write('\n')
127
 
        finally:
128
 
            outfile.close()
129
 
 
130
 
        outfile = open(os.path.join('second', 'plugintwo.py'), 'w')
131
 
        try:
132
 
            outfile.write(template % (tempattribute, 'second'))
133
 
            outfile.write('\n')
134
 
        finally:
135
 
            outfile.close()
136
 
 
137
 
        oldpath = bzrlib.plugins.__path__
138
 
        try:
139
 
            bzrlib.plugins.__path__ = ['first', 'second']
140
 
            exec "import bzrlib.plugins.pluginone"
 
109
        template = ("from bzrlib.tests.test_plugins import TestOneNamedPluginOnly\n"
 
110
                    "TestOneNamedPluginOnly.activeattributes[%r].append('%s')\n")
 
111
        print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
 
112
        print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
 
113
        try:
 
114
            bzrlib.plugin.load_from_dirs(['first', 'second'])
141
115
            self.assertEqual(['first'], self.activeattributes[tempattribute])
142
 
            exec "import bzrlib.plugins.plugintwo"
143
 
            self.assertEqual(['first', 'second'],
144
 
                self.activeattributes[tempattribute])
145
 
        finally:
146
 
            # remove the plugin 'plugin'
147
 
            del self.activeattributes[tempattribute]
148
 
            if getattr(bzrlib.plugins, 'pluginone', None):
149
 
                del bzrlib.plugins.pluginone
150
 
            if getattr(bzrlib.plugins, 'plugintwo', None):
151
 
                del bzrlib.plugins.plugintwo
152
 
        self.failIf(getattr(bzrlib.plugins, 'pluginone', None))
153
 
        self.failIf(getattr(bzrlib.plugins, 'plugintwo', None))
154
 
 
155
 
    def test_plugins_can_load_from_directory_with_trailing_slash(self):
156
 
        # This test tests that a plugin can load from a directory when the
157
 
        # directory in the path has a trailing slash.
158
 
        # check the plugin is not loaded already
159
 
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
160
 
        tempattribute = "trailing-slash"
161
 
        self.failIf(tempattribute in self.activeattributes)
162
 
        # set a place for the plugin to record its loading, and at the same
163
 
        # time validate that the location the plugin should record to is
164
 
        # valid and correct.
165
 
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
166
 
            [tempattribute] = []
167
 
        self.failUnless(tempattribute in self.activeattributes)
168
 
        # create a directory for the plugin
169
 
        os.mkdir('plugin_test')
170
 
        # write a plugin that will record when its loaded in the 
171
 
        # tempattribute list.
172
 
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
173
 
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
174
 
 
175
 
        outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
176
 
        try:
177
 
            outfile.write(template % (tempattribute, 'plugin'))
178
 
            outfile.write('\n')
179
 
        finally:
180
 
            outfile.close()
181
 
 
182
 
        try:
183
 
            bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
184
 
            self.assertEqual(['plugin'], self.activeattributes[tempattribute])
185
 
        finally:
186
 
            # remove the plugin 'plugin'
187
 
            del self.activeattributes[tempattribute]
188
 
            if getattr(bzrlib.plugins, 'ts_plugin', None):
189
 
                del bzrlib.plugins.ts_plugin
190
 
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
191
 
 
192
 
    def test_plugin_with_bad_name_does_not_load(self):
193
 
        # Create badly-named plugin
194
 
        file('bad plugin-name..py', 'w').close()
195
 
 
196
 
        # Capture output
197
 
        stream = StringIO()
198
 
        handler = logging.StreamHandler(stream)
199
 
        log = logging.getLogger('bzr')
200
 
        log.addHandler(handler)
201
 
 
202
 
        bzrlib.plugin.load_from_dir('.')
203
 
 
204
 
        # Stop capturing output
205
 
        handler.flush()
206
 
        handler.close()
207
 
        log.removeHandler(handler)
208
 
 
209
 
        self.assertContainsRe(stream.getvalue(),
210
 
            r"Unable to load 'bad plugin-name\.' in '\.' as a plugin because"
211
 
            " file path isn't a valid module name; try renaming it to"
212
 
            " 'bad_plugin_name_'\.")
213
 
 
214
 
        stream.close()
215
 
 
216
 
 
217
 
class TestPlugins(TestCaseInTempDir):
218
 
 
219
 
    def setup_plugin(self, source=""):
220
 
        # This test tests a new plugin appears in bzrlib.plugin.plugins().
 
116
        finally:
 
117
            # remove the plugin 'plugin'
 
118
            del self.activeattributes[tempattribute]
 
119
            if getattr(bzrlib.plugins, 'plugin', None):
 
120
                del bzrlib.plugins.plugin
 
121
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
122
 
 
123
 
 
124
class TestAllPlugins(TestCaseInTempDir):
 
125
 
 
126
    def test_plugin_appears_in_all_plugins(self):
 
127
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
221
128
        # check the plugin is not loaded already
222
129
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
223
130
        # write a plugin that _cannot_ fail to load.
224
 
        file('plugin.py', 'w').write(source + '\n')
225
 
        self.addCleanup(self.teardown_plugin)
226
 
        bzrlib.plugin.load_from_path(['.'])
227
 
    
228
 
    def teardown_plugin(self):
229
 
        # remove the plugin 'plugin'
230
 
        if 'bzrlib.plugins.plugin' in sys.modules:
231
 
            del sys.modules['bzrlib.plugins.plugin']
232
 
        if getattr(bzrlib.plugins, 'plugin', None):
233
 
            del bzrlib.plugins.plugin
 
131
        print >> file('plugin.py', 'w'), ""
 
132
        try:
 
133
            bzrlib.plugin.load_from_dirs(['.'])
 
134
            self.failUnless('plugin' in bzrlib.plugin.all_plugins())
 
135
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
 
136
            self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
 
137
                             bzrlib.plugins.plugin)
 
138
        finally:
 
139
            # remove the plugin 'plugin'
 
140
            if getattr(bzrlib.plugins, 'plugin', None):
 
141
                del bzrlib.plugins.plugin
234
142
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
235
143
 
236
 
    def test_plugin_appears_in_plugins(self):
237
 
        self.setup_plugin()
238
 
        self.failUnless('plugin' in bzrlib.plugin.plugins())
239
 
        self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
240
 
        plugins = bzrlib.plugin.plugins()
241
 
        plugin = plugins['plugin']
242
 
        self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
243
 
        self.assertEqual(bzrlib.plugins.plugin, plugin.module)
244
 
 
245
 
    def test_trivial_plugin_get_path(self):
246
 
        self.setup_plugin()
247
 
        plugins = bzrlib.plugin.plugins()
248
 
        plugin = plugins['plugin']
249
 
        plugin_path = self.test_dir + '/plugin.py'
250
 
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
251
 
 
252
 
    def test_plugin_get_path_py_not_pyc(self):
253
 
        self.setup_plugin()         # after first import there will be plugin.pyc
254
 
        self.teardown_plugin()
255
 
        bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
256
 
        plugins = bzrlib.plugin.plugins()
257
 
        plugin = plugins['plugin']
258
 
        plugin_path = self.test_dir + '/plugin.py'
259
 
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
260
 
 
261
 
    def test_plugin_get_path_pyc_only(self):
262
 
        self.setup_plugin()         # after first import there will be plugin.pyc
263
 
        self.teardown_plugin()
264
 
        os.unlink(self.test_dir + '/plugin.py')
265
 
        bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
266
 
        plugins = bzrlib.plugin.plugins()
267
 
        plugin = plugins['plugin']
268
 
        if __debug__:
269
 
            plugin_path = self.test_dir + '/plugin.pyc'
270
 
        else:
271
 
            plugin_path = self.test_dir + '/plugin.pyo'
272
 
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
273
 
 
274
 
    def test_no_test_suite_gives_None_for_test_suite(self):
275
 
        self.setup_plugin()
276
 
        plugin = bzrlib.plugin.plugins()['plugin']
277
 
        self.assertEqual(None, plugin.test_suite())
278
 
 
279
 
    def test_test_suite_gives_test_suite_result(self):
280
 
        source = """def test_suite(): return 'foo'"""
281
 
        self.setup_plugin(source)
282
 
        plugin = bzrlib.plugin.plugins()['plugin']
283
 
        self.assertEqual('foo', plugin.test_suite())
284
 
 
285
 
    def test_no_version_info(self):
286
 
        self.setup_plugin()
287
 
        plugin = bzrlib.plugin.plugins()['plugin']
288
 
        self.assertEqual(None, plugin.version_info())
289
 
 
290
 
    def test_with_version_info(self):
291
 
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
292
 
        plugin = bzrlib.plugin.plugins()['plugin']
293
 
        self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
294
 
 
295
 
    def test_short_version_info_gets_padded(self):
296
 
        # the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
297
 
        # so we adapt it
298
 
        self.setup_plugin("version_info = (1, 2, 3)")
299
 
        plugin = bzrlib.plugin.plugins()['plugin']
300
 
        self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
301
 
 
302
 
    def test_no_version_info___version__(self):
303
 
        self.setup_plugin()
304
 
        plugin = bzrlib.plugin.plugins()['plugin']
305
 
        self.assertEqual("unknown", plugin.__version__)
306
 
 
307
 
    def test___version__with_version_info(self):
308
 
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
309
 
        plugin = bzrlib.plugin.plugins()['plugin']
310
 
        self.assertEqual("1.2.3dev4", plugin.__version__)
311
 
 
312
 
    def test_final__version__with_version_info(self):
313
 
        self.setup_plugin("version_info = (1, 2, 3, 'final', 4)")
314
 
        plugin = bzrlib.plugin.plugins()['plugin']
315
 
        self.assertEqual("1.2.3", plugin.__version__)
316
 
 
317
144
 
318
145
class TestPluginHelp(TestCaseInTempDir):
319
146
 
320
147
    def split_help_commands(self):
321
148
        help = {}
322
149
        current = None
323
 
        for line in self.run_bzr('help commands')[0].splitlines():
 
150
        for line in self.capture('help commands').splitlines():
324
151
            if not line.startswith(' '):
325
152
                current = line.split()[0]
326
153
            help[current] = help.get(current, '') + line
333
160
        for cmd_name in bzrlib.commands.builtin_command_names():
334
161
            if cmd_name in bzrlib.commands.plugin_command_names():
335
162
                continue
 
163
            help = StringIO()
336
164
            try:
337
 
                help = bzrlib.commands.get_cmd_object(cmd_name).get_help_text()
 
165
                bzrlib.help.help_on_command(cmd_name, help)
338
166
            except NotImplementedError:
339
167
                # some commands have no help
340
168
                pass
341
169
            else:
342
 
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
 
170
                help.seek(0)
 
171
                self.assertNotContainsRe(help.read(), 'From plugin "[^"]*"')
343
172
 
344
 
            if cmd_name in help_commands.keys():
 
173
            if help in help_commands.keys():
345
174
                # some commands are hidden
346
175
                help = help_commands[cmd_name]
347
 
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
 
176
                self.assertNotContainsRe(help, 'From plugin "[^"]*"')
348
177
 
349
178
    def test_plugin_help_shows_plugin(self):
350
179
        # Create a test plugin
355
184
 
356
185
        try:
357
186
            # Check its help
358
 
            bzrlib.plugin.load_from_path(['plugin_test'])
 
187
            bzrlib.plugin.load_from_dirs(['plugin_test'])
359
188
            bzrlib.commands.register_command( bzrlib.plugins.myplug.cmd_myplug)
360
 
            help = self.run_bzr('help myplug')[0]
361
 
            self.assertContainsRe(help, 'plugin "myplug"')
 
189
            help = self.capture('help myplug')
 
190
            self.assertContainsRe(help, 'From plugin "myplug"')
362
191
            help = self.split_help_commands()['myplug']
363
192
            self.assertContainsRe(help, '\[myplug\]')
364
193
        finally:
380
209
    def check_plugin_load(self, zip_name, plugin_name):
381
210
        self.assertFalse(plugin_name in dir(bzrlib.plugins),
382
211
                         'Plugin already loaded')
383
 
        old_path = bzrlib.plugins.__path__
384
212
        try:
385
 
            # this is normally done by load_plugins -> set_plugins_path
386
 
            bzrlib.plugins.__path__ = [zip_name]
387
 
            self.applyDeprecated(one_three,
388
 
                bzrlib.plugin.load_from_zip, zip_name)
 
213
            bzrlib.plugin.load_from_zips([zip_name])
389
214
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
390
215
                            'Plugin is not loaded')
391
216
        finally:
392
217
            # unregister plugin
393
218
            if getattr(bzrlib.plugins, plugin_name, None):
394
219
                delattr(bzrlib.plugins, plugin_name)
395
 
                del sys.modules['bzrlib.plugins.' + plugin_name]
396
 
            bzrlib.plugins.__path__ = old_path
397
220
 
398
221
    def test_load_module(self):
399
222
        self.make_zipped_plugin('./test.zip', 'ziplug.py')
402
225
    def test_load_package(self):
403
226
        self.make_zipped_plugin('./test.zip', 'ziplug/__init__.py')
404
227
        self.check_plugin_load('./test.zip', 'ziplug')
405
 
 
406
 
 
407
 
class TestSetPluginsPath(TestCase):
408
 
    
409
 
    def test_set_plugins_path(self):
410
 
        """set_plugins_path should set the module __path__ correctly."""
411
 
        old_path = bzrlib.plugins.__path__
412
 
        try:
413
 
            bzrlib.plugins.__path__ = []
414
 
            expected_path = bzrlib.plugin.set_plugins_path()
415
 
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
416
 
        finally:
417
 
            bzrlib.plugins.__path__ = old_path
418
 
 
419
 
    def test_set_plugins_path_with_trailing_slashes(self):
420
 
        """set_plugins_path should set the module __path__ based on
421
 
        BZR_PLUGIN_PATH."""
422
 
        old_path = bzrlib.plugins.__path__
423
 
        old_env = os.environ.get('BZR_PLUGIN_PATH')
424
 
        try:
425
 
            bzrlib.plugins.__path__ = []
426
 
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
427
 
                "second/\\/\\/"
428
 
            bzrlib.plugin.set_plugins_path()
429
 
            expected_path = ['first', 'second',
430
 
                os.path.dirname(bzrlib.plugins.__file__)]
431
 
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
432
 
        finally:
433
 
            bzrlib.plugins.__path__ = old_path
434
 
            if old_env != None:
435
 
                os.environ['BZR_PLUGIN_PATH'] = old_env
436
 
            else:
437
 
                del os.environ['BZR_PLUGIN_PATH']
438
 
 
439
 
class TestHelpIndex(tests.TestCase):
440
 
    """Tests for the PluginsHelpIndex class."""
441
 
 
442
 
    def test_default_constructable(self):
443
 
        index = plugin.PluginsHelpIndex()
444
 
 
445
 
    def test_get_topics_None(self):
446
 
        """Searching for None returns an empty list."""
447
 
        index = plugin.PluginsHelpIndex()
448
 
        self.assertEqual([], index.get_topics(None))
449
 
 
450
 
    def test_get_topics_for_plugin(self):
451
 
        """Searching for plugin name gets its docstring."""
452
 
        index = plugin.PluginsHelpIndex()
453
 
        # make a new plugin here for this test, even if we're run with
454
 
        # --no-plugins
455
 
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
456
 
        demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
457
 
        sys.modules['bzrlib.plugins.demo_module'] = demo_module
458
 
        try:
459
 
            topics = index.get_topics('demo_module')
460
 
            self.assertEqual(1, len(topics))
461
 
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
462
 
            self.assertEqual(demo_module, topics[0].module)
463
 
        finally:
464
 
            del sys.modules['bzrlib.plugins.demo_module']
465
 
 
466
 
    def test_get_topics_no_topic(self):
467
 
        """Searching for something that is not a plugin returns []."""
468
 
        # test this by using a name that cannot be a plugin - its not
469
 
        # a valid python identifier.
470
 
        index = plugin.PluginsHelpIndex()
471
 
        self.assertEqual([], index.get_topics('nothing by this name'))
472
 
 
473
 
    def test_prefix(self):
474
 
        """PluginsHelpIndex has a prefix of 'plugins/'."""
475
 
        index = plugin.PluginsHelpIndex()
476
 
        self.assertEqual('plugins/', index.prefix)
477
 
 
478
 
    def test_get_plugin_topic_with_prefix(self):
479
 
        """Searching for plugins/demo_module returns help."""
480
 
        index = plugin.PluginsHelpIndex()
481
 
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
482
 
        demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
483
 
        sys.modules['bzrlib.plugins.demo_module'] = demo_module
484
 
        try:
485
 
            topics = index.get_topics('plugins/demo_module')
486
 
            self.assertEqual(1, len(topics))
487
 
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
488
 
            self.assertEqual(demo_module, topics[0].module)
489
 
        finally:
490
 
            del sys.modules['bzrlib.plugins.demo_module']
491
 
 
492
 
 
493
 
class FakeModule(object):
494
 
    """A fake module to test with."""
495
 
 
496
 
    def __init__(self, doc, name):
497
 
        self.__doc__ = doc
498
 
        self.__name__ = name
499
 
 
500
 
 
501
 
class TestModuleHelpTopic(tests.TestCase):
502
 
    """Tests for the ModuleHelpTopic class."""
503
 
 
504
 
    def test_contruct(self):
505
 
        """Construction takes the module to document."""
506
 
        mod = FakeModule('foo', 'foo')
507
 
        topic = plugin.ModuleHelpTopic(mod)
508
 
        self.assertEqual(mod, topic.module)
509
 
 
510
 
    def test_get_help_text_None(self):
511
 
        """A ModuleHelpTopic returns the docstring for get_help_text."""
512
 
        mod = FakeModule(None, 'demo')
513
 
        topic = plugin.ModuleHelpTopic(mod)
514
 
        self.assertEqual("Plugin 'demo' has no docstring.\n",
515
 
            topic.get_help_text())
516
 
 
517
 
    def test_get_help_text_no_carriage_return(self):
518
 
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
519
 
        mod = FakeModule('one line of help', 'demo')
520
 
        topic = plugin.ModuleHelpTopic(mod)
521
 
        self.assertEqual("one line of help\n",
522
 
            topic.get_help_text())
523
 
 
524
 
    def test_get_help_text_carriage_return(self):
525
 
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
526
 
        mod = FakeModule('two lines of help\nand more\n', 'demo')
527
 
        topic = plugin.ModuleHelpTopic(mod)
528
 
        self.assertEqual("two lines of help\nand more\n",
529
 
            topic.get_help_text())
530
 
 
531
 
    def test_get_help_text_with_additional_see_also(self):
532
 
        mod = FakeModule('two lines of help\nand more', 'demo')
533
 
        topic = plugin.ModuleHelpTopic(mod)
534
 
        self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
535
 
            topic.get_help_text(['foo', 'bar']))
536
 
 
537
 
    def test_get_help_topic(self):
538
 
        """The help topic for a plugin is its module name."""
539
 
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
540
 
        topic = plugin.ModuleHelpTopic(mod)
541
 
        self.assertEqual('demo', topic.get_help_topic())
542
 
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
543
 
        topic = plugin.ModuleHelpTopic(mod)
544
 
        self.assertEqual('foo_bar', topic.get_help_topic())