~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Martin Pool
  • Date: 2007-04-24 05:02:04 UTC
  • mfrom: (2449 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2450.
  • Revision ID: mbp@sourcefrog.net-20070424050204-bfkc1qiq0axt5f14
Merge trunk & fix NEWS conflict

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import sys
26
26
import zipfile
27
27
 
 
28
from bzrlib import plugin, tests
28
29
import bzrlib.plugin
29
30
import bzrlib.plugins
30
31
import bzrlib.commands
160
161
        for cmd_name in bzrlib.commands.builtin_command_names():
161
162
            if cmd_name in bzrlib.commands.plugin_command_names():
162
163
                continue
163
 
            help = StringIO()
164
164
            try:
165
 
                bzrlib.help.help_on_command(cmd_name, help)
 
165
                help = bzrlib.commands.get_cmd_object(cmd_name).get_help_text()
166
166
            except NotImplementedError:
167
167
                # some commands have no help
168
168
                pass
169
169
            else:
170
 
                help.seek(0)
171
 
                self.assertNotContainsRe(help.read(), 'From plugin "[^"]*"')
 
170
                self.assertNotContainsRe(help, 'From plugin "[^"]*"')
172
171
 
173
 
            if help in help_commands.keys():
 
172
            if cmd_name in help_commands.keys():
174
173
                # some commands are hidden
175
174
                help = help_commands[cmd_name]
176
175
                self.assertNotContainsRe(help, 'From plugin "[^"]*"')
238
237
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
239
238
        finally:
240
239
            bzrlib.plugins.__path__ = old_path
 
240
 
 
241
 
 
242
class TestHelpIndex(tests.TestCase):
 
243
    """Tests for the PluginsHelpIndex class."""
 
244
 
 
245
    def test_default_constructable(self):
 
246
        index = plugin.PluginsHelpIndex()
 
247
 
 
248
    def test_get_topics_None(self):
 
249
        """Searching for None returns an empty list."""
 
250
        index = plugin.PluginsHelpIndex()
 
251
        self.assertEqual([], index.get_topics(None))
 
252
 
 
253
    def test_get_topics_launchpad(self):
 
254
        """Searching for 'launchpad' returns the launchpad plugin docstring."""
 
255
        index = plugin.PluginsHelpIndex()
 
256
        # if bzr was run with '--no-plugins' we need to manually load the
 
257
        # reference plugin. Its shipped with bzr, and loading at this point
 
258
        # won't add additional tests to run.
 
259
        import bzrlib.plugins.launchpad
 
260
        topics = index.get_topics('launchpad')
 
261
        self.assertEqual(1, len(topics))
 
262
        self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
 
263
        self.assertEqual(bzrlib.plugins.launchpad, topics[0].module)
 
264
 
 
265
    def test_get_topics_no_topic(self):
 
266
        """Searching for something that is not a plugin returns []."""
 
267
        # test this by using a name that cannot be a plugin - its not
 
268
        # a valid python identifier.
 
269
        index = plugin.PluginsHelpIndex()
 
270
        self.assertEqual([], index.get_topics('nothing by this name'))
 
271
 
 
272
    def test_prefix(self):
 
273
        """PluginsHelpIndex has a prefix of 'plugins/'."""
 
274
        index = plugin.PluginsHelpIndex()
 
275
        self.assertEqual('plugins/', index.prefix)
 
276
 
 
277
    def test_get_topic_with_prefix(self):
 
278
        """Searching for plugins/launchpad returns launchpad module help."""
 
279
        index = plugin.PluginsHelpIndex()
 
280
        # if bzr was run with '--no-plugins' we need to manually load the
 
281
        # reference plugin. Its shipped with bzr, and loading at this point
 
282
        # won't add additional tests to run.
 
283
        import bzrlib.plugins.launchpad
 
284
        topics = index.get_topics('plugins/launchpad')
 
285
        self.assertEqual(1, len(topics))
 
286
        self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
 
287
        self.assertEqual(bzrlib.plugins.launchpad, topics[0].module)
 
288
 
 
289
 
 
290
class FakeModule(object):
 
291
    """A fake module to test with."""
 
292
 
 
293
    def __init__(self, doc, name):
 
294
        self.__doc__ = doc
 
295
        self.__name__ = name
 
296
 
 
297
 
 
298
class TestModuleHelpTopic(tests.TestCase):
 
299
    """Tests for the ModuleHelpTopic class."""
 
300
 
 
301
    def test_contruct(self):
 
302
        """Construction takes the module to document."""
 
303
        mod = FakeModule('foo', 'foo')
 
304
        topic = plugin.ModuleHelpTopic(mod)
 
305
        self.assertEqual(mod, topic.module)
 
306
 
 
307
    def test_get_help_text_None(self):
 
308
        """A ModuleHelpTopic returns the docstring for get_help_text."""
 
309
        mod = FakeModule(None, 'demo')
 
310
        topic = plugin.ModuleHelpTopic(mod)
 
311
        self.assertEqual("Plugin 'demo' has no docstring.\n",
 
312
            topic.get_help_text())
 
313
 
 
314
    def test_get_help_text_no_carriage_return(self):
 
315
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
 
316
        mod = FakeModule('one line of help', 'demo')
 
317
        topic = plugin.ModuleHelpTopic(mod)
 
318
        self.assertEqual("one line of help\n",
 
319
            topic.get_help_text())
 
320
 
 
321
    def test_get_help_text_carriage_return(self):
 
322
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
 
323
        mod = FakeModule('two lines of help\nand more\n', 'demo')
 
324
        topic = plugin.ModuleHelpTopic(mod)
 
325
        self.assertEqual("two lines of help\nand more\n",
 
326
            topic.get_help_text())
 
327
 
 
328
    def test_get_help_text_with_additional_see_also(self):
 
329
        mod = FakeModule('two lines of help\nand more', 'demo')
 
330
        topic = plugin.ModuleHelpTopic(mod)
 
331
        self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
 
332
            topic.get_help_text(['foo', 'bar']))
 
333
 
 
334
    def test_get_help_topic(self):
 
335
        """The help topic for a plugin is its module name."""
 
336
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
 
337
        topic = plugin.ModuleHelpTopic(mod)
 
338
        self.assertEqual('demo', topic.get_help_topic())
 
339
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
 
340
        topic = plugin.ModuleHelpTopic(mod)
 
341
        self.assertEqual('foo_bar', topic.get_help_topic())