13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
37
32
class TestCommandHelp(tests.TestCase):
38
33
"""Tests for help on commands."""
40
def assertCmdHelp(self, expected, cmd):
41
self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
43
35
def test_command_help_includes_see_also(self):
44
36
class cmd_WithSeeAlso(commands.Command):
45
__doc__ = """A sample command."""
37
"""A sample command."""
46
38
_see_also = ['foo', 'bar']
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.
39
cmd = cmd_WithSeeAlso()
40
helptext = cmd.get_help_text()
43
' -h, --help show help message\n'
45
'See also: bar, foo\n')
61
47
def test_get_help_text(self):
62
48
"""Commands have a get_help_text method which returns their help."""
63
49
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
-v, --verbose Display more information.
72
-q, --quiet Only display errors and warnings.
73
-h, --help Show help message.
50
"""A sample command."""
78
52
helptext = cmd.get_help_text()
79
self.assertStartsWith(helptext,
80
'Purpose: A sample command.\n'
82
self.assertEndsWith(helptext,
83
' -h, --help Show help message.\n\n')
53
self.assertStartsWith(helptext, 'usage: bzr Demo')
54
self.assertEndsWith(helptext, 'show help message\n')
85
56
def test_command_with_additional_see_also(self):
86
57
class cmd_WithSeeAlso(commands.Command):
87
__doc__ = """A sample command."""
58
"""A sample command."""
88
59
_see_also = ['foo', 'bar']
89
60
cmd = cmd_WithSeeAlso()
90
61
helptext = cmd.get_help_text(['gam'])
91
62
self.assertEndsWith(
93
' -v, --verbose Display more information.\n'
94
' -q, --quiet Only display errors and warnings.\n'
95
' -h, --help Show help message.\n'
64
' -h, --help show help message\n'
97
66
'See also: bar, foo, gam\n')
99
68
def test_command_only_additional_see_also(self):
100
69
class cmd_WithSeeAlso(commands.Command):
101
__doc__ = """A sample command."""
70
"""A sample command."""
102
71
cmd = cmd_WithSeeAlso()
103
72
helptext = cmd.get_help_text(['gam'])
104
73
self.assertEndsWith(
106
' -v, --verbose Display more information.\n'
107
' -q, --quiet Only display errors and warnings.\n'
108
' -h, --help Show help message.\n'
75
' -h, --help show help message\n'
110
77
'See also: gam\n')
112
79
def test_get_help_topic(self):
113
80
"""The help topic for a Command is its name()."""
114
81
class cmd_foo_bar(commands.Command):
115
__doc__ = """A sample command."""
82
"""A sample command."""
116
83
cmd = cmd_foo_bar()
117
84
self.assertEqual(cmd.name(), cmd.get_help_topic())
119
def test_formatted_help_text(self):
120
"""Help text should be plain text by default."""
121
class cmd_Demo(commands.Command):
122
__doc__ = """A sample command.
133
A code block follows.
140
helptext = cmd.get_help_text()
143
'Purpose: A sample command.\n'
147
' --usage Show usage message and options.\n'
148
' -v, --verbose Display more information.\n'
149
' -q, --quiet Only display errors and warnings.\n'
150
' -h, --help Show help message.\n'
161
' A code block follows.\n'
163
' bzr Demo something\n'
165
helptext = cmd.get_help_text(plain=False)
166
self.assertEquals(helptext,
167
':Purpose: A sample command.\n'
171
' --usage Show usage message and options.\n'
172
' -v, --verbose Display more information.\n'
173
' -q, --quiet Only display errors and warnings.\n'
174
' -h, --help Show help message.\n'
185
' A code block follows.\n'
189
' bzr Demo something\n'
192
def test_concise_help_text(self):
193
"""Concise help text excludes the descriptive sections."""
194
class cmd_Demo(commands.Command):
195
__doc__ = """A sample command.
205
helptext = cmd.get_help_text()
206
self.assertEqualDiff(
208
'Purpose: A sample command.\n'
212
' --usage Show usage message and options.\n'
213
' -v, --verbose Display more information.\n'
214
' -q, --quiet Only display errors and warnings.\n'
215
' -h, --help Show help message.\n'
225
helptext = cmd.get_help_text(verbose=False)
226
self.assertEquals(helptext,
227
'Purpose: A sample command.\n'
231
' --usage Show usage message and options.\n'
232
' -v, --verbose Display more information.\n'
233
' -q, --quiet Only display errors and warnings.\n'
234
' -h, --help Show help message.\n'
236
'See bzr help Demo for more details and examples.\n'
239
def test_help_custom_section_ordering(self):
240
"""Custom descriptive sections should remain in the order given."""
241
class cmd_Demo(commands.Command):
242
__doc__ = """A sample command.
247
Interesting stuff about formats.
255
Clever things to keep in mind.
258
helptext = cmd.get_help_text()
259
self.assertEqualDiff(
261
'Purpose: A sample command.\n'
265
' --usage Show usage message and options.\n'
266
' -v, --verbose Display more information.\n'
267
' -q, --quiet Only display errors and warnings.\n'
268
' -h, --help Show help message.\n'
274
' Interesting stuff about formats.\n'
282
' Clever things to keep in mind.\n'
285
def test_help_text_custom_usage(self):
286
"""Help text may contain a custom usage section."""
287
class cmd_Demo(commands.Command):
288
__doc__ = """A sample command.
298
helptext = cmd.get_help_text()
299
self.assertEquals(helptext,
300
'Purpose: A sample command.\n'
302
' cmd Demo [opts] args\n'
308
' --usage Show usage message and options.\n'
309
' -v, --verbose Display more information.\n'
310
' -q, --quiet Only display errors and warnings.\n'
311
' -h, --help Show help message.\n'
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()
493
class TestRegisteredTopic(TestHelp):
87
class TestRegisteredTopic(tests.TestCase):
494
88
"""Tests for the RegisteredTopic class."""
496
90
def test_contruct(self):
497
91
"""Construction takes the help topic name for the registered item."""
499
93
self.assertTrue('basic' in help_topics.topic_registry)
500
94
topic = help_topics.RegisteredTopic('basic')
501
95
self.assertEqual('basic', topic.topic)
503
97
def test_get_help_text(self):
504
"""RegisteredTopic returns the get_detail results for get_help_text."""
98
"""A RegisteredTopic returns the get_detail results for get_help_text."""
505
99
topic = help_topics.RegisteredTopic('commands')
506
100
self.assertEqual(help_topics.topic_registry.get_detail('commands'),
507
topic.get_help_text())
101
topic.get_help_text())
509
103
def test_get_help_text_with_additional_see_also(self):
510
104
topic = help_topics.RegisteredTopic('commands')