~ubuntu-branches/ubuntu/gutsy/bzr/gutsy

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Bazaar Package Importer
  • Author(s): Etienne Goyer
  • Date: 2007-04-27 17:53:49 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20070427175349-rvowqx994rfuikuu
Tags: 0.16~rc1-0ubuntu1
New upstream development release 

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
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.get_topics'))
 
257
        demo_module = FakeModule('', 'bzrlib.plugins.get_topics')
 
258
        sys.modules['bzrlib.plugins.get_topics'] = demo_module
 
259
        try:
 
260
            topics = index.get_topics('get_topics')
 
261
            self.assertEqual(1, len(topics))
 
262
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
 
263
            self.assertEqual(demo_module, topics[0].module)
 
264
        finally:
 
265
            del sys.modules['bzrlib.plugins.get_topics']
 
266
 
 
267
    def test_get_topics_no_topic(self):
 
268
        """Searching for something that is not a plugin returns []."""
 
269
        # test this by using a name that cannot be a plugin - its not
 
270
        # a valid python identifier.
 
271
        index = plugin.PluginsHelpIndex()
 
272
        self.assertEqual([], index.get_topics('nothing by this name'))
 
273
 
 
274
    def test_prefix(self):
 
275
        """PluginsHelpIndex has a prefix of 'plugins/'."""
 
276
        index = plugin.PluginsHelpIndex()
 
277
        self.assertEqual('plugins/', index.prefix)
 
278
 
 
279
    def test_get_topic_with_prefix(self):
 
280
        """Searching for plugins/launchpad returns launchpad module help."""
 
281
        index = plugin.PluginsHelpIndex()
 
282
        self.assertFalse(sys.modules.has_key('bzrlib.plugins.get_topics'))
 
283
        demo_module = FakeModule('', 'bzrlib.plugins.get_topics')
 
284
        sys.modules['bzrlib.plugins.get_topics'] = demo_module
 
285
        try:
 
286
            topics = index.get_topics('plugins/get_topics')
 
287
            self.assertEqual(1, len(topics))
 
288
            self.assertIsInstance(topics[0], plugin.ModuleHelpTopic)
 
289
            self.assertEqual(demo_module, topics[0].module)
 
290
        finally:
 
291
            del sys.modules['bzrlib.plugins.get_topics']
 
292
 
 
293
 
 
294
class FakeModule(object):
 
295
    """A fake module to test with."""
 
296
 
 
297
    def __init__(self, doc, name):
 
298
        self.__doc__ = doc
 
299
        self.__name__ = name
 
300
 
 
301
 
 
302
class TestModuleHelpTopic(tests.TestCase):
 
303
    """Tests for the ModuleHelpTopic class."""
 
304
 
 
305
    def test_contruct(self):
 
306
        """Construction takes the module to document."""
 
307
        mod = FakeModule('foo', 'foo')
 
308
        topic = plugin.ModuleHelpTopic(mod)
 
309
        self.assertEqual(mod, topic.module)
 
310
 
 
311
    def test_get_help_text_None(self):
 
312
        """A ModuleHelpTopic returns the docstring for get_help_text."""
 
313
        mod = FakeModule(None, 'demo')
 
314
        topic = plugin.ModuleHelpTopic(mod)
 
315
        self.assertEqual("Plugin 'demo' has no docstring.\n",
 
316
            topic.get_help_text())
 
317
 
 
318
    def test_get_help_text_no_carriage_return(self):
 
319
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
 
320
        mod = FakeModule('one line of help', 'demo')
 
321
        topic = plugin.ModuleHelpTopic(mod)
 
322
        self.assertEqual("one line of help\n",
 
323
            topic.get_help_text())
 
324
 
 
325
    def test_get_help_text_carriage_return(self):
 
326
        """ModuleHelpTopic.get_help_text adds a \n if needed."""
 
327
        mod = FakeModule('two lines of help\nand more\n', 'demo')
 
328
        topic = plugin.ModuleHelpTopic(mod)
 
329
        self.assertEqual("two lines of help\nand more\n",
 
330
            topic.get_help_text())
 
331
 
 
332
    def test_get_help_text_with_additional_see_also(self):
 
333
        mod = FakeModule('two lines of help\nand more', 'demo')
 
334
        topic = plugin.ModuleHelpTopic(mod)
 
335
        self.assertEqual("two lines of help\nand more\nSee also: bar, foo\n",
 
336
            topic.get_help_text(['foo', 'bar']))
 
337
 
 
338
    def test_get_help_topic(self):
 
339
        """The help topic for a plugin is its module name."""
 
340
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.demo')
 
341
        topic = plugin.ModuleHelpTopic(mod)
 
342
        self.assertEqual('demo', topic.get_help_topic())
 
343
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
 
344
        topic = plugin.ModuleHelpTopic(mod)
 
345
        self.assertEqual('foo_bar', topic.get_help_topic())