17
17
"""Unit tests for the bzrlib.help module."""
19
21
from bzrlib import (
30
class TestHelp(tests.TestCase):
33
tests.TestCase.setUp(self)
34
commands.install_bzr_command_hooks()
33
from bzrlib.tests.test_i18n import ZzzTranslations
37
37
class TestCommandHelp(tests.TestCase):
38
38
"""Tests for help on commands."""
40
def assertCmdHelp(self, expected, cmd):
41
self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
40
43
def test_command_help_includes_see_also(self):
41
44
class cmd_WithSeeAlso(commands.Command):
42
45
__doc__ = """A sample command."""
43
46
_see_also = ['foo', 'bar']
44
cmd = cmd_WithSeeAlso()
45
helptext = cmd.get_help_text()
48
' -v, --verbose Display more information.\n'
49
' -q, --quiet Only display errors and warnings.\n'
50
' -h, --help Show help message.\n'
52
'See also: bar, foo\n')
47
self.assertCmdHelp('''\
48
Purpose: A sample command.
49
Usage: bzr WithSeeAlso
52
--usage Show usage message and options.
53
-v, --verbose Display more information.
54
-q, --quiet Only display errors and warnings.
55
-h, --help Show help message.
54
61
def test_get_help_text(self):
55
62
"""Commands have a get_help_text method which returns their help."""
56
63
class cmd_Demo(commands.Command):
57
64
__doc__ = """A sample command."""
65
self.assertCmdHelp('''\
66
Purpose: A sample command.
70
--usage Show usage message and options.
71
-v, --verbose Display more information.
72
-q, --quiet Only display errors and warnings.
73
-h, --help Show help message.
59
78
helptext = cmd.get_help_text()
60
79
self.assertStartsWith(helptext,
295
314
' Blah blah blah.\n\n')
317
class ZzzTranslationsForDoc(ZzzTranslations):
319
_section_pat = re.compile(':\w+:\\n\\s+')
320
_indent_pat = re.compile('\\s+')
323
m = self._section_pat.match(s)
325
m = self._indent_pat.match(s)
327
return u'%szz{{%s}}' % (m.group(0), s[m.end():])
328
return u'zz{{%s}}' % s
331
class TestCommandHelpI18n(tests.TestCase):
332
"""Tests for help on translated commands."""
335
super(TestCommandHelpI18n, self).setUp()
336
self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
338
def assertCmdHelp(self, expected, cmd):
339
self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
341
def test_command_help_includes_see_also(self):
342
class cmd_WithSeeAlso(commands.Command):
343
__doc__ = """A sample command."""
344
_see_also = ['foo', 'bar']
345
self.assertCmdHelp('''\
346
zz{{:Purpose: zz{{A sample command.}}
347
}}zz{{:Usage: bzr WithSeeAlso
350
--usage zz{{Show usage message and options.}}
351
-v, --verbose zz{{Display more information.}}
352
-q, --quiet zz{{Only display errors and warnings.}}
353
-h, --help zz{{Show help message.}}
355
zz{{:See also: bar, foo}}
359
def test_get_help_text(self):
360
"""Commands have a get_help_text method which returns their help."""
361
class cmd_Demo(commands.Command):
362
__doc__ = """A sample command."""
363
self.assertCmdHelp('''\
364
zz{{:Purpose: zz{{A sample command.}}
365
}}zz{{:Usage: bzr Demo
368
--usage zz{{Show usage message and options.}}
369
-v, --verbose zz{{Display more information.}}
370
-q, --quiet zz{{Only display errors and warnings.}}
371
-h, --help zz{{Show help message.}}
376
def test_command_with_additional_see_also(self):
377
class cmd_WithSeeAlso(commands.Command):
378
__doc__ = """A sample command."""
379
_see_also = ['foo', 'bar']
380
cmd = cmd_WithSeeAlso()
381
helptext = cmd.get_help_text(['gam'])
384
' -v, --verbose zz{{Display more information.}}\n'
385
' -q, --quiet zz{{Only display errors and warnings.}}\n'
386
' -h, --help zz{{Show help message.}}\n'
388
'zz{{:See also: bar, foo, gam}}\n')
390
def test_command_only_additional_see_also(self):
391
class cmd_WithSeeAlso(commands.Command):
392
__doc__ = """A sample command."""
393
cmd = cmd_WithSeeAlso()
394
helptext = cmd.get_help_text(['gam'])
398
' --usage zz{{Show usage message and options.}}\n'
399
' -v, --verbose zz{{Display more information.}}\n'
400
' -q, --quiet zz{{Only display errors and warnings.}}\n'
401
' -h, --help zz{{Show help message.}}\n'
403
'zz{{:See also: gam}}\n')
406
def test_help_custom_section_ordering(self):
407
"""Custom descriptive sections should remain in the order given."""
408
# The help formatter expect the class name to start with 'cmd_'
409
class cmd_Demo(commands.Command):
410
__doc__ = """A sample command.
415
Interesting stuff about formats.
423
Clever things to keep in mind.
425
self.assertCmdHelp('''\
426
zz{{:Purpose: zz{{A sample command.}}
427
}}zz{{:Usage: bzr Demo
430
--usage zz{{Show usage message and options.}}
431
-v, --verbose zz{{Display more information.}}
432
-q, --quiet zz{{Only display errors and warnings.}}
433
-h, --help zz{{Show help message.}}
436
zz{{zz{{Blah blah blah.}}
439
zz{{Interesting stuff about formats.}}
447
zz{{Clever things to keep in mind.}}
452
def test_help_text_custom_usage(self):
453
"""Help text may contain a custom usage section."""
454
class cmd_Demo(commands.Command):
455
__doc__ = """A sample command.
464
self.assertCmdHelp('''\
465
zz{{:Purpose: zz{{A sample command.}}
467
zz{{cmd Demo [opts] args}}
473
--usage zz{{Show usage message and options.}}
474
-v, --verbose zz{{Display more information.}}
475
-q, --quiet zz{{Only display errors and warnings.}}
476
-h, --help zz{{Show help message.}}
479
zz{{zz{{Blah blah blah.}}
486
class TestHelp(tests.TestCase):
489
tests.TestCase.setUp(self)
490
commands.install_bzr_command_hooks()
298
493
class TestRegisteredTopic(TestHelp):
299
494
"""Tests for the RegisteredTopic class."""
367
562
self.assertEqual('', index.prefix)
565
class TestConfigOptionIndex(TestHelp):
566
"""Tests for the HelpCommandIndex class."""
569
super(TestConfigOptionIndex, self).setUp()
570
self.index = help_topics.ConfigOptionHelpIndex()
572
def test_get_topics_None(self):
573
"""Searching for None returns an empty list."""
574
self.assertEqual([], self.index.get_topics(None))
576
def test_get_topics_no_topic(self):
577
self.assertEqual([], self.index.get_topics('nothing by this name'))
579
def test_prefix(self):
580
self.assertEqual('configuration/', self.index.prefix)
582
def test_get_topic_with_prefix(self):
583
topics = self.index.get_topics('configuration/default_format')
584
self.assertLength(1, topics)
586
self.assertIsInstance(opt, config.Option)
587
self.assertEquals('default_format', opt.name)
370
590
class TestCommandIndex(TestHelp):
371
591
"""Tests for the HelpCommandIndex class."""
409
629
def test_default_search_path(self):
410
630
"""The default search path should include internal indexs."""
411
631
indices = help.HelpIndices()
412
self.assertEqual(3, len(indices.search_path))
632
self.assertEqual(4, len(indices.search_path))
413
633
# help topics should be searched in first.
414
634
self.assertIsInstance(indices.search_path[0],
415
help_topics.HelpTopicIndex)
635
help_topics.HelpTopicIndex)
416
636
# with commands being search second.
417
637
self.assertIsInstance(indices.search_path[1],
418
commands.HelpCommandIndex)
419
# and plugins are a third index.
638
commands.HelpCommandIndex)
639
# plugins are a third index.
420
640
self.assertIsInstance(indices.search_path[2],
421
plugin.PluginsHelpIndex)
641
plugin.PluginsHelpIndex)
642
# config options are a fourth index
643
self.assertIsInstance(indices.search_path[3],
644
help_topics.ConfigOptionHelpIndex)
423
646
def test_search_for_unknown_topic_raises(self):
424
647
"""Searching for an unknown topic should raise NoHelpTopic."""