~bzr-pqm/bzr/bzr.dev

5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2007-2011 Canonical Ltd
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
16
17
"""Unit tests for the bzrlib.help module."""
18
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
19
import textwrap
20
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
21
from bzrlib import (
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
22
    builtins,
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
23
    commands,
2432.1.5 by Robert Collins
Initial stub for topic searching.
24
    errors,
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
25
    help,
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
26
    help_topics,
5875.3.12 by INADA Naoki
Add test for command help translation.
27
    i18n,
2432.1.24 by Robert Collins
Add plugins as a help index.
28
    plugin,
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
29
    tests,
30
    )
31
5875.3.12 by INADA Naoki
Add test for command help translation.
32
from bzrlib.tests.test_i18n import ZzzTranslations
33
import re
34
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
35
36
class TestCommandHelp(tests.TestCase):
37
    """Tests for help on commands."""
38
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
39
    def assertCmdHelp(self, expected, cmd):
40
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
41
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
42
    def test_command_help_includes_see_also(self):
43
        class cmd_WithSeeAlso(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
44
            __doc__ = """A sample command."""
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
45
            _see_also = ['foo', 'bar']
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
46
        self.assertCmdHelp('''\
47
            Purpose: A sample command.
48
            Usage:   bzr WithSeeAlso
49
            
50
            Options:
51
              --usage        Show usage message and options.
52
              -v, --verbose  Display more information.
53
              -q, --quiet    Only display errors and warnings.
54
              -h, --help     Show help message.
55
            
56
            See also: bar, foo
57
            ''',
58
                           cmd_WithSeeAlso())
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
59
2432.1.12 by Robert Collins
Relocate command help onto Command.
60
    def test_get_help_text(self):
61
        """Commands have a get_help_text method which returns their help."""
62
        class cmd_Demo(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
63
            __doc__ = """A sample command."""
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
64
        self.assertCmdHelp('''\
65
            Purpose: A sample command.
66
            Usage:   bzr Demo
67
            
68
            Options:
69
              --usage        Show usage message and options.
70
              -v, --verbose  Display more information.
71
              -q, --quiet    Only display errors and warnings.
72
              -h, --help     Show help message.
73
74
            ''',
75
                           cmd_Demo())
2432.1.12 by Robert Collins
Relocate command help onto Command.
76
        cmd = cmd_Demo()
77
        helptext = cmd.get_help_text()
2666.1.4 by Ian Clatworthy
Add help formatting tests
78
        self.assertStartsWith(helptext,
79
            'Purpose: A sample command.\n'
80
            'Usage:   bzr Demo')
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
81
        self.assertEndsWith(helptext,
82
            '  -h, --help     Show help message.\n\n')
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
83
84
    def test_command_with_additional_see_also(self):
85
        class cmd_WithSeeAlso(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
86
            __doc__ = """A sample command."""
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
87
            _see_also = ['foo', 'bar']
88
        cmd = cmd_WithSeeAlso()
89
        helptext = cmd.get_help_text(['gam'])
90
        self.assertEndsWith(
91
            helptext,
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
92
            '  -v, --verbose  Display more information.\n'
93
            '  -q, --quiet    Only display errors and warnings.\n'
94
            '  -h, --help     Show help message.\n'
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
95
            '\n'
96
            'See also: bar, foo, gam\n')
97
98
    def test_command_only_additional_see_also(self):
99
        class cmd_WithSeeAlso(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
100
            __doc__ = """A sample command."""
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
101
        cmd = cmd_WithSeeAlso()
102
        helptext = cmd.get_help_text(['gam'])
103
        self.assertEndsWith(
104
            helptext,
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
105
            '  -v, --verbose  Display more information.\n'
106
            '  -q, --quiet    Only display errors and warnings.\n'
107
            '  -h, --help     Show help message.\n'
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
108
            '\n'
109
            'See also: gam\n')
2432.1.28 by Robert Collins
Add a get_help_topic method to commands.Command.
110
111
    def test_get_help_topic(self):
112
        """The help topic for a Command is its name()."""
113
        class cmd_foo_bar(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
114
            __doc__ = """A sample command."""
2432.1.28 by Robert Collins
Add a get_help_topic method to commands.Command.
115
        cmd = cmd_foo_bar()
116
        self.assertEqual(cmd.name(), cmd.get_help_topic())
2666.1.4 by Ian Clatworthy
Add help formatting tests
117
118
    def test_formatted_help_text(self):
119
        """Help text should be plain text by default."""
120
        class cmd_Demo(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
121
            __doc__ = """A sample command.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
122
2666.1.4 by Ian Clatworthy
Add help formatting tests
123
            :Examples:
124
                Example 1::
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
125
2666.1.4 by Ian Clatworthy
Add help formatting tests
126
                    cmd arg1
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
127
2666.1.4 by Ian Clatworthy
Add help formatting tests
128
                Example 2::
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
129
2666.1.4 by Ian Clatworthy
Add help formatting tests
130
                    cmd arg2
5215.2.3 by John Szakmeister
Test help formatting of standalone code blocks.
131
132
                A code block follows.
133
134
                ::
135
136
                    bzr Demo something
2666.1.4 by Ian Clatworthy
Add help formatting tests
137
            """
138
        cmd = cmd_Demo()
139
        helptext = cmd.get_help_text()
140
        self.assertEquals(
141
            helptext,
142
            'Purpose: A sample command.\n'
143
            'Usage:   bzr Demo\n'
144
            '\n'
145
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
146
            '  --usage        Show usage message and options.\n'
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
147
            '  -v, --verbose  Display more information.\n'
148
            '  -q, --quiet    Only display errors and warnings.\n'
149
            '  -h, --help     Show help message.\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
150
            '\n'
151
            'Examples:\n'
152
            '    Example 1:\n'
153
            '\n'
154
            '        cmd arg1\n'
155
            '\n'
156
            '    Example 2:\n'
157
            '\n'
158
            '        cmd arg2\n'
5215.2.3 by John Szakmeister
Test help formatting of standalone code blocks.
159
            '\n'
160
            '    A code block follows.\n'
161
            '\n'
162
            '        bzr Demo something\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
163
            '\n')
164
        helptext = cmd.get_help_text(plain=False)
165
        self.assertEquals(helptext,
166
            ':Purpose: A sample command.\n'
167
            ':Usage:   bzr Demo\n'
168
            '\n'
169
            ':Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
170
            '  --usage        Show usage message and options.\n'
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
171
            '  -v, --verbose  Display more information.\n'
172
            '  -q, --quiet    Only display errors and warnings.\n'
173
            '  -h, --help     Show help message.\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
174
            '\n'
175
            ':Examples:\n'
176
            '    Example 1::\n'
177
            '\n'
178
            '        cmd arg1\n'
179
            '\n'
180
            '    Example 2::\n'
181
            '\n'
182
            '        cmd arg2\n'
5215.2.3 by John Szakmeister
Test help formatting of standalone code blocks.
183
            '\n'
184
            '    A code block follows.\n'
185
            '\n'
186
            '    ::\n'
187
            '\n'
188
            '        bzr Demo something\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
189
            '\n')
190
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
191
    def test_concise_help_text(self):
192
        """Concise help text excludes the descriptive sections."""
193
        class cmd_Demo(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
194
            __doc__ = """A sample command.
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
195
 
196
            Blah blah blah.
197
198
            :Examples:
199
                Example 1::
200
 
201
                    cmd arg1
202
            """
203
        cmd = cmd_Demo()
204
        helptext = cmd.get_help_text()
205
        self.assertEqualDiff(
206
            helptext,
207
            'Purpose: A sample command.\n'
208
            'Usage:   bzr Demo\n'
209
            '\n'
210
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
211
            '  --usage        Show usage message and options.\n'
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
212
            '  -v, --verbose  Display more information.\n'
213
            '  -q, --quiet    Only display errors and warnings.\n'
214
            '  -h, --help     Show help message.\n'
215
            '\n'
216
            'Description:\n'
217
            '  Blah blah blah.\n'
218
            '\n'
219
            'Examples:\n'
220
            '    Example 1:\n'
221
            '\n'
222
            '        cmd arg1\n'
223
            '\n')
224
        helptext = cmd.get_help_text(verbose=False)
225
        self.assertEquals(helptext,
226
            'Purpose: A sample command.\n'
227
            'Usage:   bzr Demo\n'
228
            '\n'
229
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
230
            '  --usage        Show usage message and options.\n'
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
231
            '  -v, --verbose  Display more information.\n'
232
            '  -q, --quiet    Only display errors and warnings.\n'
233
            '  -h, --help     Show help message.\n'
234
            '\n'
3984.4.5 by Ian Clatworthy
help xxx is full help; xxx -h is concise help
235
            'See bzr help Demo for more details and examples.\n'
3984.4.2 by Ian Clatworthy
make help on commands concise by default
236
            '\n')
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
237
238
    def test_help_custom_section_ordering(self):
239
        """Custom descriptive sections should remain in the order given."""
240
        class cmd_Demo(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
241
            __doc__ = """A sample command.
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
242
 
243
            Blah blah blah.
244
245
            :Formats:
246
              Interesting stuff about formats.
247
248
            :Examples:
249
              Example 1::
250
 
251
                cmd arg1
252
253
            :Tips:
254
              Clever things to keep in mind.
255
            """
256
        cmd = cmd_Demo()
257
        helptext = cmd.get_help_text()
258
        self.assertEqualDiff(
259
            helptext,
260
            'Purpose: A sample command.\n'
261
            'Usage:   bzr Demo\n'
262
            '\n'
263
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
264
            '  --usage        Show usage message and options.\n'
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
265
            '  -v, --verbose  Display more information.\n'
266
            '  -q, --quiet    Only display errors and warnings.\n'
267
            '  -h, --help     Show help message.\n'
268
            '\n'
269
            'Description:\n'
270
            '  Blah blah blah.\n'
271
            '\n'
272
            'Formats:\n'
273
            '  Interesting stuff about formats.\n'
274
            '\n'
275
            'Examples:\n'
276
            '  Example 1:\n'
277
            '\n'
278
            '    cmd arg1\n'
279
            '\n'
280
            'Tips:\n'
281
            '  Clever things to keep in mind.\n'
282
            '\n')
283
2666.1.4 by Ian Clatworthy
Add help formatting tests
284
    def test_help_text_custom_usage(self):
285
        """Help text may contain a custom usage section."""
286
        class cmd_Demo(commands.Command):
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
287
            __doc__ = """A sample command.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
288
2666.1.4 by Ian Clatworthy
Add help formatting tests
289
            :Usage:
290
                cmd Demo [opts] args
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
291
2666.1.4 by Ian Clatworthy
Add help formatting tests
292
                cmd Demo -h
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
293
2666.1.4 by Ian Clatworthy
Add help formatting tests
294
            Blah blah blah.
295
            """
296
        cmd = cmd_Demo()
297
        helptext = cmd.get_help_text()
298
        self.assertEquals(helptext,
299
            'Purpose: A sample command.\n'
300
            'Usage:\n'
301
            '    cmd Demo [opts] args\n'
302
            '\n'
303
            '    cmd Demo -h\n'
304
            '\n'
305
            '\n'
306
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
307
            '  --usage        Show usage message and options.\n'
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
308
            '  -v, --verbose  Display more information.\n'
309
            '  -q, --quiet    Only display errors and warnings.\n'
310
            '  -h, --help     Show help message.\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
311
            '\n'
312
            'Description:\n'
313
            '  Blah blah blah.\n\n')
314
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
315
5875.3.12 by INADA Naoki
Add test for command help translation.
316
class ZzzTranslationsForDoc(ZzzTranslations):
317
318
    _section_pat = re.compile(':\w+:\\n\\s+')
319
    _indent_pat = re.compile('\\s+')
320
321
    def zzz(self, s):
322
        m = self._section_pat.match(s)
323
        if m is None:
324
            m = self._indent_pat.match(s)
325
        if m:
326
            return u'%szz{{%s}}' % (m.group(0), s[m.end():])
327
        return u'zz{{%s}}' % s
328
329
330
class TestCommandHelpI18n(tests.TestCase):
331
    """Tests for help on translated commands."""
332
333
    def setUp(self):
334
        super(TestCommandHelpI18n, self).setUp()
5875.3.25 by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation.
335
        self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
5875.3.12 by INADA Naoki
Add test for command help translation.
336
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
337
    def assertCmdHelp(self, expected, cmd):
338
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
5875.3.12 by INADA Naoki
Add test for command help translation.
339
340
    def test_command_help_includes_see_also(self):
341
        class cmd_WithSeeAlso(commands.Command):
342
            __doc__ = """A sample command."""
343
            _see_also = ['foo', 'bar']
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
344
        self.assertCmdHelp('''\
345
            zz{{:Purpose: zz{{A sample command.}}
346
            }}zz{{:Usage:   bzr WithSeeAlso
347
            }}
348
            zz{{:Options:
5875.3.25 by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation.
349
              --usage        Show usage message and options.
350
              -v, --verbose  Display more information.
351
              -q, --quiet    Only display errors and warnings.
352
              -h, --help     Show help message.
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
353
            }}
354
            zz{{:See also: bar, foo}}
355
            ''',
356
                           cmd_WithSeeAlso())
5875.3.12 by INADA Naoki
Add test for command help translation.
357
358
    def test_get_help_text(self):
359
        """Commands have a get_help_text method which returns their help."""
360
        class cmd_Demo(commands.Command):
361
            __doc__ = """A sample command."""
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
362
        self.assertCmdHelp('''\
363
            zz{{:Purpose: zz{{A sample command.}}
364
            }}zz{{:Usage:   bzr Demo
365
            }}
366
            zz{{:Options:
5875.3.25 by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation.
367
              --usage        Show usage message and options.
368
              -v, --verbose  Display more information.
369
              -q, --quiet    Only display errors and warnings.
370
              -h, --help     Show help message.
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
371
            }}
372
            ''',
373
                           cmd_Demo())
5875.3.12 by INADA Naoki
Add test for command help translation.
374
375
    def test_command_with_additional_see_also(self):
376
        class cmd_WithSeeAlso(commands.Command):
377
            __doc__ = """A sample command."""
378
            _see_also = ['foo', 'bar']
379
        cmd = cmd_WithSeeAlso()
380
        helptext = cmd.get_help_text(['gam'])
381
        self.assertEndsWith(
382
            helptext,
5875.4.5 by INADA Naoki
Fix tests.
383
            '  -v, --verbose  Display more information.\n'
384
            '  -q, --quiet    Only display errors and warnings.\n'
385
            '  -h, --help     Show help message.\n'
5875.3.14 by INADA Naoki
Change how to use gettext for section titles.
386
            '}}\n'
387
            'zz{{:See also: bar, foo, gam}}\n')
5875.3.12 by INADA Naoki
Add test for command help translation.
388
389
    def test_command_only_additional_see_also(self):
390
        class cmd_WithSeeAlso(commands.Command):
391
            __doc__ = """A sample command."""
392
        cmd = cmd_WithSeeAlso()
393
        helptext = cmd.get_help_text(['gam'])
394
        self.assertEndsWith(
395
            helptext,
5875.3.14 by INADA Naoki
Change how to use gettext for section titles.
396
            'zz{{:Options:\n'
5875.4.5 by INADA Naoki
Fix tests.
397
            '  --usage        Show usage message and options.\n'
398
            '  -v, --verbose  Display more information.\n'
399
            '  -q, --quiet    Only display errors and warnings.\n'
400
            '  -h, --help     Show help message.\n'
5875.3.14 by INADA Naoki
Change how to use gettext for section titles.
401
            '}}\n'
402
            'zz{{:See also: gam}}\n')
5875.3.12 by INADA Naoki
Add test for command help translation.
403
404
405
    def test_help_custom_section_ordering(self):
406
        """Custom descriptive sections should remain in the order given."""
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
407
        # The help formatter expect the class name to start with 'cmd_'
5875.3.12 by INADA Naoki
Add test for command help translation.
408
        class cmd_Demo(commands.Command):
409
            __doc__ = """A sample command.
410
 
411
            Blah blah blah.
412
413
            :Formats:
414
              Interesting stuff about formats.
415
416
            :Examples:
417
              Example 1::
418
 
419
                cmd arg1
420
421
            :Tips:
422
              Clever things to keep in mind.
423
            """
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
424
        self.assertCmdHelp('''\
425
            zz{{:Purpose: zz{{A sample command.}}
426
            }}zz{{:Usage:   bzr Demo
427
            }}
428
            zz{{:Options:
5875.3.25 by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation.
429
              --usage        Show usage message and options.
430
              -v, --verbose  Display more information.
431
              -q, --quiet    Only display errors and warnings.
432
              -h, --help     Show help message.
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
433
            }}
434
            Description:
435
              zz{{zz{{Blah blah blah.}}
436
            
437
            }}:Formats:
438
              zz{{Interesting stuff about formats.}}
439
            
440
            Examples:
441
              zz{{Example 1::}}
442
            
443
                zz{{cmd arg1}}
444
            
445
            Tips:
446
              zz{{Clever things to keep in mind.}}
447
448
            ''',
449
                           cmd_Demo())
5875.3.12 by INADA Naoki
Add test for command help translation.
450
451
    def test_help_text_custom_usage(self):
452
        """Help text may contain a custom usage section."""
453
        class cmd_Demo(commands.Command):
454
            __doc__ = """A sample command.
455
456
            :Usage:
457
                cmd Demo [opts] args
458
459
                cmd Demo -h
460
461
            Blah blah blah.
462
            """
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
463
        self.assertCmdHelp('''\
464
            zz{{:Purpose: zz{{A sample command.}}
465
            }}zz{{:Usage:
466
                zz{{cmd Demo [opts] args}}
467
            
468
                zz{{cmd Demo -h}}
469
470
            }}
471
            zz{{:Options:
5875.3.25 by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation.
472
              --usage        Show usage message and options.
473
              -v, --verbose  Display more information.
474
              -q, --quiet    Only display errors and warnings.
475
              -h, --help     Show help message.
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
476
            }}
477
            Description:
478
              zz{{zz{{Blah blah blah.}}
479
480
            }}
481
            ''',
482
                           cmd_Demo())
5875.3.12 by INADA Naoki
Add test for command help translation.
483
484
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
485
class TestHelp(tests.TestCase):
486
487
    def setUp(self):
488
        tests.TestCase.setUp(self)
489
        commands.install_bzr_command_hooks()
490
491
4119.3.15 by Robert Collins
And fix tests for bzr help.
492
class TestRegisteredTopic(TestHelp):
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
493
    """Tests for the RegisteredTopic class."""
494
495
    def test_contruct(self):
496
        """Construction takes the help topic name for the registered item."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
497
        # validate our test
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
498
        self.assertTrue('basic' in help_topics.topic_registry)
499
        topic = help_topics.RegisteredTopic('basic')
500
        self.assertEqual('basic', topic.topic)
501
2432.1.10 by Robert Collins
Add get_help_text() to RegisteredTopic to get the help as a string.
502
    def test_get_help_text(self):
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
503
        """RegisteredTopic returns the get_detail results for get_help_text."""
2432.1.10 by Robert Collins
Add get_help_text() to RegisteredTopic to get the help as a string.
504
        topic = help_topics.RegisteredTopic('commands')
505
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
506
                         topic.get_help_text())
2432.1.10 by Robert Collins
Add get_help_text() to RegisteredTopic to get the help as a string.
507
2432.1.22 by Robert Collins
Teach RegisteredTopic to support the additional_see_also list of related help terms.
508
    def test_get_help_text_with_additional_see_also(self):
509
        topic = help_topics.RegisteredTopic('commands')
510
        self.assertEndsWith(
511
            topic.get_help_text(['foo', 'bar']),
512
            '\n'
513
            'See also: bar, foo\n')
514
3089.3.5 by Ian Clatworthy
add test for loading help from a file
515
    def test_get_help_text_loaded_from_file(self):
516
        # Pick a known topic stored in an external file
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
517
        topic = help_topics.RegisteredTopic('authentication')
3089.3.5 by Ian Clatworthy
add test for loading help from a file
518
        self.assertStartsWith(topic.get_help_text(),
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
519
            'Authentication Settings\n'
520
            '=======================\n'
3089.3.5 by Ian Clatworthy
add test for loading help from a file
521
            '\n')
522
2432.1.27 by Robert Collins
Add a get_help_topic method to RegisteredTopic.
523
    def test_get_help_topic(self):
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
524
        """The help topic for RegisteredTopic is its topic from construction."""
2432.1.27 by Robert Collins
Add a get_help_topic method to RegisteredTopic.
525
        topic = help_topics.RegisteredTopic('foobar')
526
        self.assertEqual('foobar', topic.get_help_topic())
527
        topic = help_topics.RegisteredTopic('baz')
528
        self.assertEqual('baz', topic.get_help_topic())
529
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
530
4119.3.15 by Robert Collins
And fix tests for bzr help.
531
class TestTopicIndex(TestHelp):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
532
    """Tests for the HelpTopicIndex class."""
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
533
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
534
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
535
        index = help_topics.HelpTopicIndex()
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
536
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
537
    def test_get_topics_None(self):
538
        """Searching for None returns the basic help topic."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
539
        index = help_topics.HelpTopicIndex()
540
        topics = index.get_topics(None)
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
541
        self.assertEqual(1, len(topics))
542
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
543
        self.assertEqual('basic', topics[0].topic)
544
545
    def test_get_topics_topics(self):
546
        """Searching for a string returns the matching string."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
547
        index = help_topics.HelpTopicIndex()
548
        topics = index.get_topics('topics')
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
549
        self.assertEqual(1, len(topics))
550
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
551
        self.assertEqual('topics', topics[0].topic)
552
553
    def test_get_topics_no_topic(self):
554
        """Searching for something not registered returns []."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
555
        index = help_topics.HelpTopicIndex()
556
        self.assertEqual([], index.get_topics('nothing by this name'))
557
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
558
    def test_prefix(self):
559
        """TopicIndex has a prefix of ''."""
560
        index = help_topics.HelpTopicIndex()
561
        self.assertEqual('', index.prefix)
562
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
563
4119.3.15 by Robert Collins
And fix tests for bzr help.
564
class TestCommandIndex(TestHelp):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
565
    """Tests for the HelpCommandIndex class."""
2432.1.2 by Robert Collins
Add a HelpCommandContext class for help from commands.
566
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
567
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
568
        index = commands.HelpCommandIndex()
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
569
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
570
    def test_get_topics_None(self):
571
        """Searching for None returns an empty list."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
572
        index = commands.HelpCommandIndex()
573
        self.assertEqual([], index.get_topics(None))
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
574
575
    def test_get_topics_rocks(self):
576
        """Searching for 'rocks' returns the cmd_rocks command instance."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
577
        index = commands.HelpCommandIndex()
578
        topics = index.get_topics('rocks')
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
579
        self.assertEqual(1, len(topics))
580
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
581
582
    def test_get_topics_no_topic(self):
583
        """Searching for something that is not a command returns []."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
584
        index = commands.HelpCommandIndex()
585
        self.assertEqual([], index.get_topics('nothing by this name'))
586
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
587
    def test_prefix(self):
588
        """CommandIndex has a prefix of 'commands/'."""
589
        index = commands.HelpCommandIndex()
590
        self.assertEqual('commands/', index.prefix)
591
2432.1.18 by Robert Collins
Add support for doing bzr help commands/COMMANDNAME.
592
    def test_get_topic_with_prefix(self):
593
        """Searching for commands/rocks returns the rocks command object."""
594
        index = commands.HelpCommandIndex()
595
        topics = index.get_topics('commands/rocks')
596
        self.assertEqual(1, len(topics))
597
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
598
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
599
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
600
class TestHelpIndices(tests.TestCase):
601
    """Tests for the HelpIndices class."""
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
602
603
    def test_default_search_path(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
604
        """The default search path should include internal indexs."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
605
        indices = help.HelpIndices()
2432.1.24 by Robert Collins
Add plugins as a help index.
606
        self.assertEqual(3, len(indices.search_path))
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
607
        # help topics should be searched in first.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
608
        self.assertIsInstance(indices.search_path[0],
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
609
            help_topics.HelpTopicIndex)
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
610
        # with commands being search second.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
611
        self.assertIsInstance(indices.search_path[1],
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
612
            commands.HelpCommandIndex)
2432.1.24 by Robert Collins
Add plugins as a help index.
613
        # and plugins are a third index.
614
        self.assertIsInstance(indices.search_path[2],
615
            plugin.PluginsHelpIndex)
2432.1.5 by Robert Collins
Initial stub for topic searching.
616
617
    def test_search_for_unknown_topic_raises(self):
618
        """Searching for an unknown topic should raise NoHelpTopic."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
619
        indices = help.HelpIndices()
620
        indices.search_path = []
621
        error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
2432.1.5 by Robert Collins
Initial stub for topic searching.
622
        self.assertEqual('foo', error.topic)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
623
624
    def test_search_calls_get_topic(self):
625
        """Searching should call get_topics in all indexes in order."""
626
        calls = []
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
627
        class RecordingIndex(object):
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
628
            def __init__(self, name):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
629
                self.prefix = name
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
630
            def get_topics(self, topic):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
631
                calls.append(('get_topics', self.prefix, topic))
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
632
                return ['something']
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
633
        index = help.HelpIndices()
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
634
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
635
        # try with None
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
636
        index.search(None)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
637
        self.assertEqual([
638
            ('get_topics', '1', None),
639
            ('get_topics', '2', None),
640
            ],
641
            calls)
642
        # and with a string
643
        del calls[:]
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
644
        index.search('bar')
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
645
        self.assertEqual([
646
            ('get_topics', '1', 'bar'),
647
            ('get_topics', '2', 'bar'),
648
            ],
649
            calls)
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
650
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
651
    def test_search_returns_index_and_results(self):
652
        """Searching should return help topics with their index"""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
653
        class CannedIndex(object):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
654
            def __init__(self, prefix, search_result):
655
                self.prefix = prefix
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
656
                self.result = search_result
657
            def get_topics(self, topic):
658
                return self.result
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
659
        index = help.HelpIndices()
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
660
        index_one = CannedIndex('1', ['a'])
661
        index_two = CannedIndex('2', ['b', 'c'])
662
        index.search_path = [index_one, index_two]
663
        self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
664
            index.search(None))
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
665
666
    def test_search_checks_for_duplicate_prefixes(self):
667
        """Its an error when there are multiple indices with the same prefix."""
668
        indices = help.HelpIndices()
669
        indices.search_path = [help_topics.HelpTopicIndex(),
670
            help_topics.HelpTopicIndex()]
671
        self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)