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
-q, --quiet Only display errors and warnings.
54
-v, --verbose Display more information.
55
-h, --help Show help message.
39
cmd = cmd_WithSeeAlso()
40
helptext = cmd.get_help_text()
43
' -v, --verbose Display more information.\n'
44
' -q, --quiet Only display errors and warnings.\n'
45
' -h, --help Show help message.\n'
47
'See also: bar, foo\n')
61
49
def test_get_help_text(self):
62
50
"""Commands have a get_help_text method which returns their help."""
63
51
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.
52
"""A sample command."""
78
54
helptext = cmd.get_help_text()
79
55
self.assertStartsWith(helptext,
112
88
def test_get_help_topic(self):
113
89
"""The help topic for a Command is its name()."""
114
90
class cmd_foo_bar(commands.Command):
115
__doc__ = """A sample command."""
91
"""A sample command."""
116
92
cmd = cmd_foo_bar()
117
93
self.assertEqual(cmd.name(), cmd.get_help_topic())
119
95
def test_formatted_help_text(self):
120
96
"""Help text should be plain text by default."""
121
97
class cmd_Demo(commands.Command):
122
__doc__ = """A sample command.
133
A code block follows.
140
110
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.
113
'Purpose: A sample command.\n'
117
' -v, --verbose Display more information.\n'
118
' -q, --quiet Only display errors and warnings.\n'
119
' -h, --help Show help message.\n'
166
130
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.
195
def test_concise_help_text(self):
196
"""Concise help text excludes the descriptive sections."""
197
class cmd_Demo(commands.Command):
198
__doc__ = """A sample command.
208
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.
229
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.
428
Interesting stuff about formats.
436
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.}}
465
def test_help_text_custom_usage(self):
466
"""Help text may contain a custom usage section."""
467
class cmd_Demo(commands.Command):
468
__doc__ = """A sample command.
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()
506
class TestRegisteredTopic(TestHelp):
131
self.assertEquals(helptext,
132
':Purpose: A sample command.\n'
136
' -v, --verbose Display more information.\n'
137
' -q, --quiet Only display errors and warnings.\n'
138
' -h, --help Show help message.\n'
150
def test_help_text_custom_usage(self):
151
"""Help text may contain a custom usage section."""
152
class cmd_Demo(commands.Command):
163
helptext = cmd.get_help_text()
164
self.assertEquals(helptext,
165
'Purpose: A sample command.\n'
167
' cmd Demo [opts] args\n'
173
' -v, --verbose Display more information.\n'
174
' -q, --quiet Only display errors and warnings.\n'
175
' -h, --help Show help message.\n'
178
' Blah blah blah.\n\n')
181
class TestRegisteredTopic(tests.TestCase):
507
182
"""Tests for the RegisteredTopic class."""
509
184
def test_contruct(self):
510
185
"""Construction takes the help topic name for the registered item."""
512
187
self.assertTrue('basic' in help_topics.topic_registry)
513
188
topic = help_topics.RegisteredTopic('basic')
514
189
self.assertEqual('basic', topic.topic)
516
191
def test_get_help_text(self):
517
"""RegisteredTopic returns the get_detail results for get_help_text."""
192
"""A RegisteredTopic returns the get_detail results for get_help_text."""
518
193
topic = help_topics.RegisteredTopic('commands')
519
194
self.assertEqual(help_topics.topic_registry.get_detail('commands'),
520
topic.get_help_text())
195
topic.get_help_text())
522
197
def test_get_help_text_with_additional_see_also(self):
523
198
topic = help_topics.RegisteredTopic('commands')