238
237
self.assertEqual(expected_path, bzrlib.plugins.__path__)
240
239
bzrlib.plugins.__path__ = old_path
242
class TestHelpIndex(tests.TestCase):
243
"""Tests for the PluginsHelpIndex class."""
245
def test_default_constructable(self):
246
index = plugin.PluginsHelpIndex()
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))
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
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)
265
del sys.modules['bzrlib.plugins.get_topics']
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'))
274
def test_prefix(self):
275
"""PluginsHelpIndex has a prefix of 'plugins/'."""
276
index = plugin.PluginsHelpIndex()
277
self.assertEqual('plugins/', index.prefix)
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
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)
291
del sys.modules['bzrlib.plugins.get_topics']
294
class FakeModule(object):
295
"""A fake module to test with."""
297
def __init__(self, doc, name):
302
class TestModuleHelpTopic(tests.TestCase):
303
"""Tests for the ModuleHelpTopic class."""
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)
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())
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())
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())
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']))
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())