17
17
"""Unit tests for the bzrlib.help module."""
19
from cStringIO import StringIO
21
21
from bzrlib import (
33
from bzrlib.tests.test_i18n import ZzzTranslations
32
class TestHelp(tests.TestCase):
35
tests.TestCase.setUp(self)
36
commands.install_bzr_command_hooks()
37
39
class TestCommandHelp(tests.TestCase):
38
40
"""Tests for help on commands."""
40
def assertCmdHelp(self, expected, cmd):
41
self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
43
42
def test_command_help_includes_see_also(self):
44
43
class cmd_WithSeeAlso(commands.Command):
45
__doc__ = """A sample command."""
44
"""A sample command."""
46
45
_see_also = ['foo', 'bar']
47
self.assertCmdHelp('''\
48
Purpose: A sample command.
49
Usage: bzr WithSeeAlso
52
--usage Show usage message and options.
53
-q, --quiet Only display errors and warnings.
54
-v, --verbose Display more information.
55
-h, --help Show help message.
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')
61
56
def test_get_help_text(self):
62
57
"""Commands have a get_help_text method which returns their help."""
63
58
class cmd_Demo(commands.Command):
64
__doc__ = """A sample command."""
65
self.assertCmdHelp('''\
66
Purpose: A sample command.
70
--usage Show usage message and options.
71
-q, --quiet Only display errors and warnings.
72
-v, --verbose Display more information.
73
-h, --help Show help message.
59
"""A sample command."""
78
61
helptext = cmd.get_help_text()
79
62
self.assertStartsWith(helptext,
85
68
def test_command_with_additional_see_also(self):
86
69
class cmd_WithSeeAlso(commands.Command):
87
__doc__ = """A sample command."""
70
"""A sample command."""
88
71
_see_also = ['foo', 'bar']
89
72
cmd = cmd_WithSeeAlso()
90
73
helptext = cmd.get_help_text(['gam'])
91
74
self.assertEndsWith(
76
' -v, --verbose Display more information.\n'
93
77
' -q, --quiet Only display errors and warnings.\n'
94
' -v, --verbose Display more information.\n'
95
78
' -h, --help Show help message.\n'
97
80
'See also: bar, foo, gam\n')
99
82
def test_command_only_additional_see_also(self):
100
83
class cmd_WithSeeAlso(commands.Command):
101
__doc__ = """A sample command."""
84
"""A sample command."""
102
85
cmd = cmd_WithSeeAlso()
103
86
helptext = cmd.get_help_text(['gam'])
104
87
self.assertEndsWith(
89
' -v, --verbose Display more information.\n'
106
90
' -q, --quiet Only display errors and warnings.\n'
107
' -v, --verbose Display more information.\n'
108
91
' -h, --help Show help message.\n'
110
93
'See also: gam\n')
133
A code block follows.
140
117
helptext = cmd.get_help_text()
141
self.assertEqualDiff('''\
142
Purpose: A sample command.
146
--usage Show usage message and options.
147
-q, --quiet Only display errors and warnings.
148
-v, --verbose Display more information.
149
-h, --help Show help message.
160
A code block follows.
120
'Purpose: A sample command.\n'
124
' --usage Show usage message and options.\n'
125
' -v, --verbose Display more information.\n'
126
' -q, --quiet Only display errors and warnings.\n'
127
' -h, --help Show help message.\n'
166
138
helptext = cmd.get_help_text(plain=False)
167
self.assertEqualDiff('''\
168
:Purpose: A sample command.
172
--usage Show usage message and options.
173
-q, --quiet Only display errors and warnings.
174
-v, --verbose Display more information.
175
-h, --help Show help message.
186
A code block follows.
139
self.assertEquals(helptext,
140
':Purpose: A sample command.\n'
144
' --usage Show usage message and options.\n'
145
' -v, --verbose Display more information.\n'
146
' -q, --quiet Only display errors and warnings.\n'
147
' -h, --help Show help message.\n'
195
159
def test_concise_help_text(self):
196
160
"""Concise help text excludes the descriptive sections."""
197
161
class cmd_Demo(commands.Command):
198
__doc__ = """A sample command.
208
172
helptext = cmd.get_help_text()
209
self.assertEqualDiff('''\
210
Purpose: A sample command.
214
--usage Show usage message and options.
215
-q, --quiet Only display errors and warnings.
216
-v, --verbose Display more information.
217
-h, --help Show help message.
173
self.assertEqualDiff(
175
'Purpose: A sample command.\n'
179
' --usage Show usage message and options.\n'
180
' -v, --verbose Display more information.\n'
181
' -q, --quiet Only display errors and warnings.\n'
182
' -h, --help Show help message.\n'
229
192
helptext = cmd.get_help_text(verbose=False)
230
self.assertEqualDiff('''\
231
Purpose: A sample command.
235
--usage Show usage message and options.
236
-q, --quiet Only display errors and warnings.
237
-v, --verbose Display more information.
238
-h, --help Show help message.
240
See bzr help Demo for more details and examples.
245
def test_help_custom_section_ordering(self):
246
"""Custom descriptive sections should remain in the order given."""
247
class cmd_Demo(commands.Command):
254
Interesting stuff about formats.
262
Clever things to keep in mind.
265
helptext = cmd.get_help_text()
266
self.assertEqualDiff('''\
267
Purpose: A sample command.
271
--usage Show usage message and options.
272
-q, --quiet Only display errors and warnings.
273
-v, --verbose Display more information.
274
-h, --help Show help message.
280
Interesting stuff about formats.
288
Clever things to keep in mind.
293
def test_help_text_custom_usage(self):
294
"""Help text may contain a custom usage section."""
295
class cmd_Demo(commands.Command):
296
__doc__ = """A sample command.
306
helptext = cmd.get_help_text()
307
self.assertEqualDiff('''\
308
Purpose: A sample command.
316
--usage Show usage message and options.
317
-q, --quiet Only display errors and warnings.
318
-v, --verbose Display more information.
319
-h, --help Show help message.
328
class ZzzTranslationsForDoc(ZzzTranslations):
330
_section_pat = re.compile(':\w+:\\n\\s+')
331
_indent_pat = re.compile('\\s+')
334
m = self._section_pat.match(s)
336
m = self._indent_pat.match(s)
338
return u'%szz{{%s}}' % (m.group(0), s[m.end():])
339
return u'zz{{%s}}' % s
342
class TestCommandHelpI18n(tests.TestCase):
343
"""Tests for help on translated commands."""
346
super(TestCommandHelpI18n, self).setUp()
347
self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
349
def assertCmdHelp(self, expected, cmd):
350
self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
352
def test_command_help_includes_see_also(self):
353
class cmd_WithSeeAlso(commands.Command):
354
__doc__ = """A sample command."""
355
_see_also = ['foo', 'bar']
356
self.assertCmdHelp('''\
357
zz{{:Purpose: zz{{A sample command.}}
358
}}zz{{:Usage: bzr WithSeeAlso
361
--usage zz{{Show usage message and options.}}
362
-q, --quiet zz{{Only display errors and warnings.}}
363
-v, --verbose zz{{Display more information.}}
364
-h, --help zz{{Show help message.}}
366
zz{{:See also: bar, foo}}
370
def test_get_help_text(self):
371
"""Commands have a get_help_text method which returns their help."""
372
class cmd_Demo(commands.Command):
373
__doc__ = """A sample command."""
374
self.assertCmdHelp('''\
375
zz{{:Purpose: zz{{A sample command.}}
376
}}zz{{:Usage: bzr Demo
379
--usage zz{{Show usage message and options.}}
380
-q, --quiet zz{{Only display errors and warnings.}}
381
-v, --verbose zz{{Display more information.}}
382
-h, --help zz{{Show help message.}}
387
def test_command_with_additional_see_also(self):
388
class cmd_WithSeeAlso(commands.Command):
389
__doc__ = """A sample command."""
390
_see_also = ['foo', 'bar']
391
cmd = cmd_WithSeeAlso()
392
helptext = cmd.get_help_text(['gam'])
395
-q, --quiet zz{{Only display errors and warnings.}}
396
-v, --verbose zz{{Display more information.}}
397
-h, --help zz{{Show help message.}}
399
zz{{:See also: bar, foo, gam}}
402
def test_command_only_additional_see_also(self):
403
class cmd_WithSeeAlso(commands.Command):
404
__doc__ = """A sample command."""
405
cmd = cmd_WithSeeAlso()
406
helptext = cmd.get_help_text(['gam'])
410
--usage zz{{Show usage message and options.}}
411
-q, --quiet zz{{Only display errors and warnings.}}
412
-v, --verbose zz{{Display more information.}}
413
-h, --help zz{{Show help message.}}
419
def test_help_custom_section_ordering(self):
420
"""Custom descriptive sections should remain in the order given."""
421
# The help formatter expect the class name to start with 'cmd_'
422
class cmd_Demo(commands.Command):
423
__doc__ = """A sample command.
193
self.assertEquals(helptext,
194
'Purpose: A sample command.\n'
198
' --usage Show usage message and options.\n'
199
' -v, --verbose Display more information.\n'
200
' -q, --quiet Only display errors and warnings.\n'
201
' -h, --help Show help message.\n'
203
'See bzr help Demo for more details and examples.\n'
206
def test_help_custom_section_ordering(self):
207
"""Custom descriptive sections should remain in the order given."""
208
class cmd_Demo(commands.Command):
436
222
Clever things to keep in mind.
438
self.assertCmdHelp('''\
439
zz{{:Purpose: zz{{A sample command.}}
440
}}zz{{:Usage: bzr Demo
443
--usage zz{{Show usage message and options.}}
444
-q, --quiet zz{{Only display errors and warnings.}}
445
-v, --verbose zz{{Display more information.}}
446
-h, --help zz{{Show help message.}}
449
zz{{zz{{Blah blah blah.}}
452
zz{{Interesting stuff about formats.}}
460
zz{{Clever things to keep in mind.}}
225
helptext = cmd.get_help_text()
226
self.assertEqualDiff(
228
'Purpose: A sample command.\n'
232
' --usage Show usage message and options.\n'
233
' -v, --verbose Display more information.\n'
234
' -q, --quiet Only display errors and warnings.\n'
235
' -h, --help Show help message.\n'
241
' Interesting stuff about formats.\n'
249
' Clever things to keep in mind.\n'
465
252
def test_help_text_custom_usage(self):
466
253
"""Help text may contain a custom usage section."""
467
254
class cmd_Demo(commands.Command):
468
__doc__ = """A sample command.
471
258
cmd Demo [opts] args
477
self.assertCmdHelp('''\
478
zz{{:Purpose: zz{{A sample command.}}
480
zz{{cmd Demo [opts] args}}
486
--usage zz{{Show usage message and options.}}
487
-q, --quiet zz{{Only display errors and warnings.}}
488
-v, --verbose zz{{Display more information.}}
489
-h, --help zz{{Show help message.}}
492
zz{{zz{{Blah blah blah.}}
499
class TestHelp(tests.TestCase):
502
super(TestHelp, self).setUp()
503
commands.install_bzr_command_hooks()
265
helptext = cmd.get_help_text()
266
self.assertEquals(helptext,
267
'Purpose: A sample command.\n'
269
' cmd Demo [opts] args\n'
275
' --usage Show usage message and options.\n'
276
' -v, --verbose Display more information.\n'
277
' -q, --quiet Only display errors and warnings.\n'
278
' -h, --help Show help message.\n'
281
' Blah blah blah.\n\n')
506
284
class TestRegisteredTopic(TestHelp):
642
395
def test_default_search_path(self):
643
396
"""The default search path should include internal indexs."""
644
397
indices = help.HelpIndices()
645
self.assertEqual(4, len(indices.search_path))
398
self.assertEqual(3, len(indices.search_path))
646
399
# help topics should be searched in first.
647
400
self.assertIsInstance(indices.search_path[0],
648
help_topics.HelpTopicIndex)
401
help_topics.HelpTopicIndex)
649
402
# with commands being search second.
650
403
self.assertIsInstance(indices.search_path[1],
651
commands.HelpCommandIndex)
652
# plugins are a third index.
404
commands.HelpCommandIndex)
405
# and plugins are a third index.
653
406
self.assertIsInstance(indices.search_path[2],
654
plugin.PluginsHelpIndex)
655
# config options are a fourth index
656
self.assertIsInstance(indices.search_path[3],
657
help_topics.ConfigOptionHelpIndex)
407
plugin.PluginsHelpIndex)
659
409
def test_search_for_unknown_topic_raises(self):
660
410
"""Searching for an unknown topic should raise NoHelpTopic."""