/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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")
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
72
73
        outfile = open(os.path.join('first', 'plugin.py'), 'w')
74
        try:
75
            print >> outfile, template % (tempattribute, 'first')
76
        finally:
77
            outfile.close()
78
79
        outfile = open(os.path.join('second', 'plugin.py'), 'w')
80
        try:
81
            print >> outfile, template % (tempattribute, 'second')
82
        finally:
83
            outfile.close()
84
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
85
        try:
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
86
            bzrlib.plugin.load_from_path(['first', 'second'])
87
            self.assertEqual(['first'], self.activeattributes[tempattribute])
88
        finally:
89
            # remove the plugin 'plugin'
90
            del self.activeattributes[tempattribute]
91
            if getattr(bzrlib.plugins, 'plugin', None):
92
                del bzrlib.plugins.plugin
93
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
94
95
    def test_plugins_from_different_dirs_can_demand_load(self):
96
        # This test tests that having two plugins in different
97
        # directories with different names allows them both to be loaded, when
98
        # we do a direct import statement.
99
        # Determine a file name we can use which is also a valid attribute
100
        # for accessing in activeattributes. - we cannot give import parameters.
101
        tempattribute = "different-dirs"
102
        self.failIf(tempattribute in self.activeattributes)
103
        # set a place for the plugins to record their loading, and at the same
104
        # time validate that the location the plugins should record to is
105
        # valid and correct.
106
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
107
            [tempattribute] = []
108
        self.failUnless(tempattribute in self.activeattributes)
109
        # create two plugin directories
110
        os.mkdir('first')
111
        os.mkdir('second')
112
        # write plugins that will record when they are loaded in the 
113
        # tempattribute list.
114
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
115
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
116
117
        outfile = open(os.path.join('first', 'pluginone.py'), 'w')
118
        try:
119
            print >> outfile, template % (tempattribute, 'first')
120
        finally:
121
            outfile.close()
122
123
        outfile = open(os.path.join('second', 'plugintwo.py'), 'w')
124
        try:
125
            print >> outfile, template % (tempattribute, 'second')
126
        finally:
127
            outfile.close()
128
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
129
        oldpath = bzrlib.plugins.__path__
130
        try:
131
            bzrlib.plugins.__path__ = ['first', 'second']
132
            exec "import bzrlib.plugins.pluginone"
133
            self.assertEqual(['first'], self.activeattributes[tempattribute])
134
            exec "import bzrlib.plugins.plugintwo"
135
            self.assertEqual(['first', 'second'],
136
                self.activeattributes[tempattribute])
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
137
        finally:
138
            # remove the plugin 'plugin'
139
            del self.activeattributes[tempattribute]
140
            if getattr(bzrlib.plugins, 'plugin', None):
141
                del bzrlib.plugins.plugin
142
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
143
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
144
    def test_plugins_can_load_from_directory_with_trailing_slash(self):
145
        # This test tests that a plugin can load from a directory when the
146
        # directory in the path has a trailing slash.
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
147
        # check the plugin is not loaded already
148
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
149
        tempattribute = "trailing-slash"
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
150
        self.failIf(tempattribute in self.activeattributes)
2652.2.3 by Blake Winton
Understand the code and comments of the test, instead of just cargo-culting them.
151
        # set a place for the plugin to record its loading, and at the same
152
        # time validate that the location the plugin should record to is
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
153
        # valid and correct.
154
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
155
            [tempattribute] = []
156
        self.failUnless(tempattribute in self.activeattributes)
2652.2.3 by Blake Winton
Understand the code and comments of the test, instead of just cargo-culting them.
157
        # create a directory for the plugin
158
        os.mkdir('plugin_test')
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
159
        # write a plugin that will record when its loaded in the 
160
        # tempattribute list.
161
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
162
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
163
164
        outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
165
        try:
166
            print >> outfile, template % (tempattribute, 'plugin')
167
        finally:
168
            outfile.close()
169
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
170
        try:
2652.2.3 by Blake Winton
Understand the code and comments of the test, instead of just cargo-culting them.
171
            bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
172
            self.assertEqual(['plugin'], self.activeattributes[tempattribute])
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
173
        finally:
174
            # remove the plugin 'plugin'
175
            del self.activeattributes[tempattribute]
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
176
            if getattr(bzrlib.plugins, 'ts_plugin', None):
177
                del bzrlib.plugins.ts_plugin
178
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
179
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
180
181
class TestAllPlugins(TestCaseInTempDir):
182
183
    def test_plugin_appears_in_all_plugins(self):
184
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
185
        # check the plugin is not loaded already
186
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
187
        # write a plugin that _cannot_ fail to load.
188
        print >> file('plugin.py', 'w'), ""
189
        try:
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
190
            bzrlib.plugin.load_from_path(['.'])
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
191
            self.failUnless('plugin' in bzrlib.plugin.all_plugins())
192
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
193
            self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
194
                             bzrlib.plugins.plugin)
195
        finally:
196
            # remove the plugin 'plugin'
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
197
            if 'bzrlib.plugins.plugin' in sys.modules:
198
                del sys.modules['bzrlib.plugins.plugin']
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
199
            if getattr(bzrlib.plugins, 'plugin', None):
200
                del bzrlib.plugins.plugin
201
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
202
203
204
class TestPluginHelp(TestCaseInTempDir):
205
206
    def split_help_commands(self):
207
        help = {}
208
        current = None
2530.3.4 by Martin Pool
Deprecate run_bzr_captured in favour of just run_bzr
209
        for line in self.run_bzr('help commands')[0].splitlines():
2034.1.2 by Aaron Bentley
Fix testcase
210
            if not line.startswith(' '):
211
                current = line.split()[0]
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
212
            help[current] = help.get(current, '') + line
213
214
        return help
215
216
    def test_plugin_help_builtins_unaffected(self):
217
        # Check we don't get false positives
218
        help_commands = self.split_help_commands()
219
        for cmd_name in bzrlib.commands.builtin_command_names():
220
            if cmd_name in bzrlib.commands.plugin_command_names():
221
                continue
222
            try:
2432.1.12 by Robert Collins
Relocate command help onto Command.
223
                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.
224
            except NotImplementedError:
225
                # some commands have no help
226
                pass
227
            else:
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
228
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
229
2432.1.12 by Robert Collins
Relocate command help onto Command.
230
            if cmd_name in help_commands.keys():
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
231
                # some commands are hidden
232
                help = help_commands[cmd_name]
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
233
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
234
235
    def test_plugin_help_shows_plugin(self):
236
        # Create a test plugin
237
        os.mkdir('plugin_test')
238
        f = open(pathjoin('plugin_test', 'myplug.py'), 'w')
239
        f.write(PLUGIN_TEXT)
240
        f.close()
241
242
        try:
243
            # Check its help
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
244
            bzrlib.plugin.load_from_path(['plugin_test'])
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
245
            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
246
            help = self.run_bzr('help myplug')[0]
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
247
            self.assertContainsRe(help, 'plugin "myplug"')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
248
            help = self.split_help_commands()['myplug']
2034.1.4 by Aaron Bentley
Change angle brackets to square brackets
249
            self.assertContainsRe(help, '\[myplug\]')
1733.2.5 by Michael Ellerman
Show which plugin (if any) provides a command.
250
        finally:
2204.3.2 by Alexander Belchenko
cherrypicking: test_plugin_help_shows_plugin: fix cleanup after test
251
            # unregister command
252
            if bzrlib.commands.plugin_cmds.get('myplug', None):
253
                del bzrlib.commands.plugin_cmds['myplug']
254
            # remove the plugin 'myplug'
255
            if getattr(bzrlib.plugins, 'myplug', None):
256
                delattr(bzrlib.plugins, 'myplug')
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
257
258
259
class TestPluginFromZip(TestCaseInTempDir):
260
261
    def make_zipped_plugin(self, zip_name, filename):
262
        z = zipfile.ZipFile(zip_name, 'w')
263
        z.writestr(filename, PLUGIN_TEXT)
264
        z.close()
265
266
    def check_plugin_load(self, zip_name, plugin_name):
267
        self.assertFalse(plugin_name in dir(bzrlib.plugins),
268
                         'Plugin already loaded')
2610.2.1 by Martin Pool
(Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice
269
        old_path = bzrlib.plugins.__path__
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
270
        try:
2610.2.1 by Martin Pool
(Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice
271
            # this is normally done by load_plugins -> set_plugins_path
272
            bzrlib.plugins.__path__ = [zip_name]
2256.2.2 by Robert Collins
Allow 'import bzrlib.plugins.NAME' to work when the plugin NAME has not
273
            bzrlib.plugin.load_from_zip(zip_name)
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
274
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
275
                            'Plugin is not loaded')
276
        finally:
277
            # unregister plugin
278
            if getattr(bzrlib.plugins, plugin_name, None):
279
                delattr(bzrlib.plugins, plugin_name)
2610.2.1 by Martin Pool
(Lukas Lalinsky) don't create a duplicate zipimporter, avoiding loading plugins twice
280
                del sys.modules['bzrlib.plugins.' + plugin_name]
281
            bzrlib.plugins.__path__ = old_path
2215.4.1 by Alexander Belchenko
Bugfix #68124: Allow plugins import from zip archives.
282
283
    def test_load_module(self):
284
        self.make_zipped_plugin('./test.zip', 'ziplug.py')
285
        self.check_plugin_load('./test.zip', 'ziplug')
286
287
    def test_load_package(self):
288
        self.make_zipped_plugin('./test.zip', 'ziplug/__init__.py')
289
        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
290
291
292
class TestSetPluginsPath(TestCase):
293
    
294
    def test_set_plugins_path(self):
295
        """set_plugins_path should set the module __path__ correctly."""
296
        old_path = bzrlib.plugins.__path__
297
        try:
298
            bzrlib.plugins.__path__ = []
299
            expected_path = bzrlib.plugin.set_plugins_path()
300
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
301
        finally:
302
            bzrlib.plugins.__path__ = old_path
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
303
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
304
    def test_set_plugins_path_with_trailing_slashes(self):
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
305
        """set_plugins_path should set the module __path__ based on
306
        BZR_PLUGIN_PATH."""
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
307
        old_path = bzrlib.plugins.__path__
308
        old_env = os.environ.get('BZR_PLUGIN_PATH')
309
        try:
310
            bzrlib.plugins.__path__ = []
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
311
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
312
                "second/\\/\\/"
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
313
            bzrlib.plugin.set_plugins_path()
2652.2.7 by Blake Winton
fix lines which were wider than 79 chars. Also handle files a little more safely.
314
            expected_path = ['first', 'second',
315
                os.path.dirname(bzrlib.plugins.__file__)]
2652.2.1 by Blake Winton
Add a test for BZR_PLUGIN_PATH, and code and another test to allow BZR_PLUGIN_PATH to contain trailing slashes.
316
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
317
        finally:
318
            bzrlib.plugins.__path__ = old_path
319
            if old_env != None:
320
                os.environ['BZR_PLUGIN_PATH'] = old_env
321
            else:
322
                del os.environ['BZR_PLUGIN_PATH']
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
323
324
class TestHelpIndex(tests.TestCase):
325
    """Tests for the PluginsHelpIndex class."""
326
327
    def test_default_constructable(self):
328
        index = plugin.PluginsHelpIndex()
329
330
    def test_get_topics_None(self):
331
        """Searching for None returns an empty list."""
332
        index = plugin.PluginsHelpIndex()
333
        self.assertEqual([], index.get_topics(None))
334
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
335
    def test_get_topics_for_plugin(self):
336
        """Searching for plugin name gets its docstring."""
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
337
        index = plugin.PluginsHelpIndex()
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
338
        # make a new plugin here for this test, even if we're run with
339
        # --no-plugins
340
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
341
        demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
342
        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)
343
        try:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
344
            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)
345
            self.assertEqual(1, len(topics))
346
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
347
            self.assertEqual(demo_module, topics[0].module)
348
        finally:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
349
            del sys.modules['bzrlib.plugins.demo_module']
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
350
351
    def test_get_topics_no_topic(self):
352
        """Searching for something that is not a plugin returns []."""
353
        # test this by using a name that cannot be a plugin - its not
354
        # a valid python identifier.
355
        index = plugin.PluginsHelpIndex()
356
        self.assertEqual([], index.get_topics('nothing by this name'))
357
358
    def test_prefix(self):
359
        """PluginsHelpIndex has a prefix of 'plugins/'."""
360
        index = plugin.PluginsHelpIndex()
361
        self.assertEqual('plugins/', index.prefix)
362
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
363
    def test_get_plugin_topic_with_prefix(self):
364
        """Searching for plugins/demo_module returns help."""
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
365
        index = plugin.PluginsHelpIndex()
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
366
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.demo_module'))
367
        demo_module = FakeModule('', 'bzrlib.plugins.demo_module')
368
        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)
369
        try:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
370
            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)
371
            self.assertEqual(1, len(topics))
372
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
373
            self.assertEqual(demo_module, topics[0].module)
374
        finally:
2475.1.1 by Martin Pool
Rename test_plugin tests and the example module used there.
375
            del sys.modules['bzrlib.plugins.demo_module']
2432.1.25 by Robert Collins
Return plugin module docstrings for 'bzr help plugin'.
376
377
378
class FakeModule(object):
379
    """A fake module to test with."""
380
381
    def __init__(self, doc, name):
382
        self.__doc__ = doc
383
        self.__name__ = name
384
385
386
class TestModuleHelpTopic(tests.TestCase):
387
    """Tests for the ModuleHelpTopic class."""
388
389
    def test_contruct(self):
390
        """Construction takes the module to document."""
391
        mod = FakeModule('foo', 'foo')
392
        topic = plugin.ModuleHelpTopic(mod)
393
        self.assertEqual(mod, topic.module)
394
395
    def test_get_help_text_None(self):
396
        """A ModuleHelpTopic returns the docstring for get_help_text."""
397
        mod = FakeModule(None, 'demo')
398
        topic = plugin.ModuleHelpTopic(mod)
399
        self.assertEqual("Plugin 'demo' has no docstring.\n",
400
            topic.get_help_text())
401
402
    def test_get_help_text_no_carriage_return(self):
403
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
404
        mod = FakeModule('one line of help', 'demo')
405
        topic = plugin.ModuleHelpTopic(mod)
406
        self.assertEqual("one line of help\n",
407
            topic.get_help_text())
408
409
    def test_get_help_text_carriage_return(self):
410
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
411
        mod = FakeModule('two lines of help\nand more\n', 'demo')
412
        topic = plugin.ModuleHelpTopic(mod)
413
        self.assertEqual("two lines of help\nand more\n",
414
            topic.get_help_text())
415
416
    def test_get_help_text_with_additional_see_also(self):
417
        mod = FakeModule('two lines of help\nand more', 'demo')
418
        topic = plugin.ModuleHelpTopic(mod)
419
        self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
420
            topic.get_help_text(['foo', 'bar']))
2432.1.29 by Robert Collins
Add get_help_topic to ModuleHelpTopic.
421
422
    def test_get_help_topic(self):
423
        """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.
424
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
2432.1.29 by Robert Collins
Add get_help_topic to ModuleHelpTopic.
425
        topic = plugin.ModuleHelpTopic(mod)
426
        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.
427
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
2432.1.29 by Robert Collins
Add get_help_topic to ModuleHelpTopic.
428
        topic = plugin.ModuleHelpTopic(mod)
429
        self.assertEqual('foo_bar', topic.get_help_topic())