14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Unit tests for the breezy.help module."""
17
"""Unit tests for the bzrlib.help module."""
19
from cStringIO import StringIO
33
from .test_i18n import ZzzTranslations
37
class TestErrors(tests.TestCase):
39
def test_no_help_topic(self):
40
error = help.NoHelpTopic("topic")
41
self.assertEqualDiff("No help could be found for 'topic'. "
42
"Please use 'brz help topics' to obtain a list of topics.",
32
class TestHelp(tests.TestCase):
35
tests.TestCase.setUp(self)
36
commands.install_bzr_command_hooks()
46
39
class TestCommandHelp(tests.TestCase):
47
40
"""Tests for help on commands."""
49
def assertCmdHelp(self, expected, cmd):
50
self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
52
42
def test_command_help_includes_see_also(self):
53
43
class cmd_WithSeeAlso(commands.Command):
54
44
__doc__ = """A sample command."""
55
45
_see_also = ['foo', 'bar']
56
self.assertCmdHelp('''\
57
Purpose: A sample command.
58
Usage: brz WithSeeAlso
61
-h, --help Show help message.
62
-q, --quiet Only display errors and warnings.
63
--usage Show usage message and options.
64
-v, --verbose Display more information.
46
cmd = cmd_WithSeeAlso()
47
helptext = cmd.get_help_text()
50
' -v, --verbose Display more information.\n'
51
' -q, --quiet Only display errors and warnings.\n'
52
' -h, --help Show help message.\n'
54
'See also: bar, foo\n')
70
56
def test_get_help_text(self):
71
57
"""Commands have a get_help_text method which returns their help."""
72
58
class cmd_Demo(commands.Command):
73
59
__doc__ = """A sample command."""
74
self.assertCmdHelp('''\
75
Purpose: A sample command.
79
-h, --help Show help message.
80
-q, --quiet Only display errors and warnings.
81
--usage Show usage message and options.
82
-v, --verbose Display more information.
87
61
helptext = cmd.get_help_text()
88
62
self.assertStartsWith(helptext,
89
'Purpose: A sample command.\n'
63
'Purpose: A sample command.\n'
91
65
self.assertEndsWith(helptext,
92
' -v, --verbose Display more information.\n\n')
66
' -h, --help Show help message.\n\n')
94
68
def test_command_with_additional_see_also(self):
95
69
class cmd_WithSeeAlso(commands.Command):
148
123
helptext = cmd.get_help_text()
149
self.assertEqualDiff('''\
150
Purpose: A sample command.
154
-h, --help Show help message.
155
-q, --quiet Only display errors and warnings.
156
--usage Show usage message and options.
157
-v, --verbose Display more information.
168
A code block follows.
126
'Purpose: A sample command.\n'
130
' --usage Show usage message and options.\n'
131
' -v, --verbose Display more information.\n'
132
' -q, --quiet Only display errors and warnings.\n'
133
' -h, --help Show help message.\n'
144
' A code block follows.\n'
146
' bzr Demo something\n'
174
148
helptext = cmd.get_help_text(plain=False)
175
self.assertEqualDiff('''\
176
:Purpose: A sample command.
180
-h, --help Show help message.
181
-q, --quiet Only display errors and warnings.
182
--usage Show usage message and options.
183
-v, --verbose Display more information.
194
A code block follows.
149
self.assertEquals(helptext,
150
':Purpose: A sample command.\n'
154
' --usage Show usage message and options.\n'
155
' -v, --verbose Display more information.\n'
156
' -q, --quiet Only display errors and warnings.\n'
157
' -h, --help Show help message.\n'
168
' A code block follows.\n'
172
' bzr Demo something\n'
203
175
def test_concise_help_text(self):
204
176
"""Concise help text excludes the descriptive sections."""
205
177
class cmd_Demo(commands.Command):
206
178
__doc__ = """A sample command.
216
188
helptext = cmd.get_help_text()
217
self.assertEqualDiff('''\
218
Purpose: A sample command.
222
-h, --help Show help message.
223
-q, --quiet Only display errors and warnings.
224
--usage Show usage message and options.
225
-v, --verbose Display more information.
189
self.assertEqualDiff(
191
'Purpose: A sample command.\n'
195
' --usage Show usage message and options.\n'
196
' -v, --verbose Display more information.\n'
197
' -q, --quiet Only display errors and warnings.\n'
198
' -h, --help Show help message.\n'
237
208
helptext = cmd.get_help_text(verbose=False)
238
self.assertEqualDiff('''\
239
Purpose: A sample command.
243
-h, --help Show help message.
244
-q, --quiet Only display errors and warnings.
245
--usage Show usage message and options.
246
-v, --verbose Display more information.
248
See brz help Demo for more details and examples.
253
def test_help_custom_section_ordering(self):
254
"""Custom descriptive sections should remain in the order given."""
255
class cmd_Demo(commands.Command):
262
Interesting stuff about formats.
270
Clever things to keep in mind.
273
helptext = cmd.get_help_text()
274
self.assertEqualDiff('''\
275
Purpose: A sample command.
279
-h, --help Show help message.
280
-q, --quiet Only display errors and warnings.
281
--usage Show usage message and options.
282
-v, --verbose Display more information.
288
Interesting stuff about formats.
296
Clever things to keep in mind.
301
def test_help_text_custom_usage(self):
302
"""Help text may contain a custom usage section."""
303
class cmd_Demo(commands.Command):
304
__doc__ = """A sample command.
314
helptext = cmd.get_help_text()
315
self.assertEqualDiff('''\
316
Purpose: A sample command.
324
-h, --help Show help message.
325
-q, --quiet Only display errors and warnings.
326
--usage Show usage message and options.
327
-v, --verbose Display more information.
336
class ZzzTranslationsForDoc(ZzzTranslations):
338
_section_pat = re.compile(':\\w+:\\n\\s+')
339
_indent_pat = re.compile('\\s+')
342
m = self._section_pat.match(s)
344
m = self._indent_pat.match(s)
346
return u'%szz{{%s}}' % (m.group(0), s[m.end():])
347
return u'zz{{%s}}' % s
350
class TestCommandHelpI18n(tests.TestCase):
351
"""Tests for help on translated commands."""
354
super(TestCommandHelpI18n, self).setUp()
355
self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
357
def assertCmdHelp(self, expected, cmd):
358
self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
360
def test_command_help_includes_see_also(self):
361
class cmd_WithSeeAlso(commands.Command):
362
__doc__ = """A sample command."""
363
_see_also = ['foo', 'bar']
364
self.assertCmdHelp('''\
365
zz{{:Purpose: zz{{A sample command.}}
366
}}zz{{:Usage: brz WithSeeAlso
369
-h, --help zz{{Show help message.}}
370
-q, --quiet zz{{Only display errors and warnings.}}
371
--usage zz{{Show usage message and options.}}
372
-v, --verbose zz{{Display more information.}}
374
zz{{:See also: bar, foo}}
378
def test_get_help_text(self):
379
"""Commands have a get_help_text method which returns their help."""
380
class cmd_Demo(commands.Command):
381
__doc__ = """A sample command."""
382
self.assertCmdHelp('''\
383
zz{{:Purpose: zz{{A sample command.}}
384
}}zz{{:Usage: brz Demo
387
-h, --help zz{{Show help message.}}
388
-q, --quiet zz{{Only display errors and warnings.}}
389
--usage zz{{Show usage message and options.}}
390
-v, --verbose zz{{Display more information.}}
395
def test_command_with_additional_see_also(self):
396
class cmd_WithSeeAlso(commands.Command):
397
__doc__ = """A sample command."""
398
_see_also = ['foo', 'bar']
399
cmd = cmd_WithSeeAlso()
400
helptext = cmd.get_help_text(['gam'])
403
-h, --help zz{{Show help message.}}
404
-q, --quiet zz{{Only display errors and warnings.}}
405
--usage zz{{Show usage message and options.}}
406
-v, --verbose zz{{Display more information.}}
408
zz{{:See also: bar, foo, gam}}
411
def test_command_only_additional_see_also(self):
412
class cmd_WithSeeAlso(commands.Command):
413
__doc__ = """A sample command."""
414
cmd = cmd_WithSeeAlso()
415
helptext = cmd.get_help_text(['gam'])
419
-h, --help zz{{Show help message.}}
420
-q, --quiet zz{{Only display errors and warnings.}}
421
--usage zz{{Show usage message and options.}}
422
-v, --verbose zz{{Display more information.}}
427
def test_help_custom_section_ordering(self):
428
"""Custom descriptive sections should remain in the order given."""
429
# The help formatter expect the class name to start with 'cmd_'
430
class cmd_Demo(commands.Command):
431
__doc__ = """A sample command.
209
self.assertEquals(helptext,
210
'Purpose: A sample command.\n'
214
' --usage Show usage message and options.\n'
215
' -v, --verbose Display more information.\n'
216
' -q, --quiet Only display errors and warnings.\n'
217
' -h, --help Show help message.\n'
219
'See bzr help Demo for more details and examples.\n'
222
def test_help_custom_section_ordering(self):
223
"""Custom descriptive sections should remain in the order given."""
224
class cmd_Demo(commands.Command):
225
__doc__ = """A sample command.
650
411
def test_default_search_path(self):
651
412
"""The default search path should include internal indexs."""
652
413
indices = help.HelpIndices()
653
self.assertEqual(4, len(indices.search_path))
414
self.assertEqual(3, len(indices.search_path))
654
415
# help topics should be searched in first.
655
416
self.assertIsInstance(indices.search_path[0],
656
help_topics.HelpTopicIndex)
417
help_topics.HelpTopicIndex)
657
418
# with commands being search second.
658
419
self.assertIsInstance(indices.search_path[1],
659
commands.HelpCommandIndex)
660
# plugins are a third index.
420
commands.HelpCommandIndex)
421
# and plugins are a third index.
661
422
self.assertIsInstance(indices.search_path[2],
662
plugin.PluginsHelpIndex)
663
# config options are a fourth index
664
self.assertIsInstance(indices.search_path[3],
665
help_topics.ConfigOptionHelpIndex)
423
plugin.PluginsHelpIndex)
667
425
def test_search_for_unknown_topic_raises(self):
668
426
"""Searching for an unknown topic should raise NoHelpTopic."""
669
427
indices = help.HelpIndices()
670
428
indices.search_path = []
671
error = self.assertRaises(help.NoHelpTopic, indices.search, 'foo')
429
error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
672
430
self.assertEqual('foo', error.topic)
674
432
def test_search_calls_get_topic(self):
675
433
"""Searching should call get_topics in all indexes in order."""
678
435
class RecordingIndex(object):
679
436
def __init__(self, name):
680
437
self.prefix = name
682
438
def get_topics(self, topic):
683
439
calls.append(('get_topics', self.prefix, topic))
684
440
return ['something']