~bzr-pqm/bzr/bzr.dev

6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2007-2012, 2016 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,
6059.3.2 by Vincent Ladeuil
Cough, with tests.
24
    config,
2432.1.5 by Robert Collins
Initial stub for topic searching.
25
    errors,
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
26
    help,
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
27
    help_topics,
5875.3.12 by INADA Naoki
Add test for command help translation.
28
    i18n,
2432.1.24 by Robert Collins
Add plugins as a help index.
29
    plugin,
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
30
    tests,
31
    )
32
5875.3.12 by INADA Naoki
Add test for command help translation.
33
from bzrlib.tests.test_i18n import ZzzTranslations
34
import re
35
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
36
37
class TestCommandHelp(tests.TestCase):
38
    """Tests for help on commands."""
39
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
40
    def assertCmdHelp(self, expected, cmd):
41
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
42
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
43
    def test_command_help_includes_see_also(self):
44
        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
45
            __doc__ = """A sample command."""
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
46
            _see_also = ['foo', 'bar']
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
47
        self.assertCmdHelp('''\
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
48
Purpose: A sample command.
49
Usage:   bzr WithSeeAlso
50
51
Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
56
57
See also: bar, foo
58
''',
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
59
                           cmd_WithSeeAlso())
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
60
2432.1.12 by Robert Collins
Relocate command help onto Command.
61
    def test_get_help_text(self):
62
        """Commands have a get_help_text method which returns their help."""
63
        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
64
            __doc__ = """A sample command."""
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
65
        self.assertCmdHelp('''\
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
66
Purpose: A sample command.
67
Usage:   bzr Demo
68
69
Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
74
75
''',
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
76
                           cmd_Demo())
2432.1.12 by Robert Collins
Relocate command help onto Command.
77
        cmd = cmd_Demo()
78
        helptext = cmd.get_help_text()
2666.1.4 by Ian Clatworthy
Add help formatting tests
79
        self.assertStartsWith(helptext,
80
            'Purpose: A sample command.\n'
81
            'Usage:   bzr Demo')
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
82
        self.assertEndsWith(helptext,
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
83
            '  -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.
84
85
    def test_command_with_additional_see_also(self):
86
        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
87
            __doc__ = """A sample command."""
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
88
            _see_also = ['foo', 'bar']
89
        cmd = cmd_WithSeeAlso()
90
        helptext = cmd.get_help_text(['gam'])
91
        self.assertEndsWith(
92
            helptext,
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
93
            '  -q, --quiet    Only display errors and warnings.\n'
94
            '  -v, --verbose  Display more information.\n'
95
            '  -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.
96
            '\n'
97
            'See also: bar, foo, gam\n')
98
99
    def test_command_only_additional_see_also(self):
100
        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
101
            __doc__ = """A sample command."""
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
102
        cmd = cmd_WithSeeAlso()
103
        helptext = cmd.get_help_text(['gam'])
104
        self.assertEndsWith(
105
            helptext,
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
106
            '  -q, --quiet    Only display errors and warnings.\n'
107
            '  -v, --verbose  Display more information.\n'
108
            '  -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.
109
            '\n'
110
            'See also: gam\n')
2432.1.28 by Robert Collins
Add a get_help_topic method to commands.Command.
111
112
    def test_get_help_topic(self):
113
        """The help topic for a Command is its name()."""
114
        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
115
            __doc__ = """A sample command."""
2432.1.28 by Robert Collins
Add a get_help_topic method to commands.Command.
116
        cmd = cmd_foo_bar()
117
        self.assertEqual(cmd.name(), cmd.get_help_topic())
2666.1.4 by Ian Clatworthy
Add help formatting tests
118
119
    def test_formatted_help_text(self):
120
        """Help text should be plain text by default."""
121
        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
122
            __doc__ = """A sample command.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
123
2666.1.4 by Ian Clatworthy
Add help formatting tests
124
            :Examples:
125
                Example 1::
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
126
2666.1.4 by Ian Clatworthy
Add help formatting tests
127
                    cmd arg1
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
128
2666.1.4 by Ian Clatworthy
Add help formatting tests
129
                Example 2::
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
130
2666.1.4 by Ian Clatworthy
Add help formatting tests
131
                    cmd arg2
5215.2.3 by John Szakmeister
Test help formatting of standalone code blocks.
132
133
                A code block follows.
134
135
                ::
136
137
                    bzr Demo something
2666.1.4 by Ian Clatworthy
Add help formatting tests
138
            """
139
        cmd = cmd_Demo()
140
        helptext = cmd.get_help_text()
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
141
        self.assertEqualDiff('''\
142
Purpose: A sample command.
143
Usage:   bzr Demo
144
145
Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
150
151
Examples:
152
    Example 1:
153
154
        cmd arg1
155
156
    Example 2:
157
158
        cmd arg2
159
160
    A code block follows.
161
162
        bzr Demo something
163
164
''',
165
                                         helptext)
2666.1.4 by Ian Clatworthy
Add help formatting tests
166
        helptext = cmd.get_help_text(plain=False)
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
167
        self.assertEqualDiff('''\
168
:Purpose: A sample command.
169
:Usage:   bzr Demo
170
171
:Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
176
177
:Examples:
178
    Example 1::
179
180
        cmd arg1
181
182
    Example 2::
183
184
        cmd arg2
185
186
    A code block follows.
187
188
    ::
189
190
        bzr Demo something
191
192
''',
193
                             helptext)
2666.1.4 by Ian Clatworthy
Add help formatting tests
194
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
195
    def test_concise_help_text(self):
196
        """Concise help text excludes the descriptive sections."""
197
        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
198
            __doc__ = """A sample command.
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
199
 
200
            Blah blah blah.
201
202
            :Examples:
203
                Example 1::
204
 
205
                    cmd arg1
206
            """
207
        cmd = cmd_Demo()
208
        helptext = cmd.get_help_text()
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
209
        self.assertEqualDiff('''\
210
Purpose: A sample command.
211
Usage:   bzr Demo
212
213
Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
218
219
Description:
220
  Blah blah blah.
221
222
Examples:
223
    Example 1:
224
225
        cmd arg1
226
227
''',
228
                             helptext)
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
229
        helptext = cmd.get_help_text(verbose=False)
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
230
        self.assertEqualDiff('''\
231
Purpose: A sample command.
232
Usage:   bzr Demo
233
234
Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
239
240
See bzr help Demo for more details and examples.
241
242
''',
243
                             helptext)
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
244
245
    def test_help_custom_section_ordering(self):
246
        """Custom descriptive sections should remain in the order given."""
247
        class cmd_Demo(commands.Command):
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
248
            __doc__ = """\
249
A sample command.
250
251
Blah blah blah.
252
253
:Formats:
254
  Interesting stuff about formats.
255
256
:Examples:
257
  Example 1::
258
259
    cmd arg1
260
261
:Tips:
262
  Clever things to keep in mind.
263
"""
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
264
        cmd = cmd_Demo()
265
        helptext = cmd.get_help_text()
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
266
        self.assertEqualDiff('''\
267
Purpose: A sample command.
268
Usage:   bzr Demo
269
270
Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
275
276
Description:
277
  Blah blah blah.
278
279
Formats:
280
  Interesting stuff about formats.
281
282
Examples:
283
  Example 1:
284
285
    cmd arg1
286
287
Tips:
288
  Clever things to keep in mind.
289
290
''',
291
                             helptext)
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
292
2666.1.4 by Ian Clatworthy
Add help formatting tests
293
    def test_help_text_custom_usage(self):
294
        """Help text may contain a custom usage section."""
295
        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
296
            __doc__ = """A sample command.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
297
2666.1.4 by Ian Clatworthy
Add help formatting tests
298
            :Usage:
299
                cmd Demo [opts] args
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
300
2666.1.4 by Ian Clatworthy
Add help formatting tests
301
                cmd Demo -h
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
302
2666.1.4 by Ian Clatworthy
Add help formatting tests
303
            Blah blah blah.
304
            """
305
        cmd = cmd_Demo()
306
        helptext = cmd.get_help_text()
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
307
        self.assertEqualDiff('''\
308
Purpose: A sample command.
309
Usage:
310
    cmd Demo [opts] args
311
312
    cmd Demo -h
313
314
315
Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
320
321
Description:
322
  Blah blah blah.
323
324
''',
325
                             helptext)
2666.1.4 by Ian Clatworthy
Add help formatting tests
326
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
327
5875.3.12 by INADA Naoki
Add test for command help translation.
328
class ZzzTranslationsForDoc(ZzzTranslations):
329
330
    _section_pat = re.compile(':\w+:\\n\\s+')
331
    _indent_pat = re.compile('\\s+')
332
333
    def zzz(self, s):
334
        m = self._section_pat.match(s)
335
        if m is None:
336
            m = self._indent_pat.match(s)
337
        if m:
338
            return u'%szz{{%s}}' % (m.group(0), s[m.end():])
339
        return u'zz{{%s}}' % s
340
341
342
class TestCommandHelpI18n(tests.TestCase):
343
    """Tests for help on translated commands."""
344
345
    def setUp(self):
346
        super(TestCommandHelpI18n, self).setUp()
5875.3.25 by Vincent Ladeuil
Fix test failures and make sure we don't rely on a default translation.
347
        self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
5875.3.12 by INADA Naoki
Add test for command help translation.
348
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
349
    def assertCmdHelp(self, expected, cmd):
350
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
5875.3.12 by INADA Naoki
Add test for command help translation.
351
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']
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
356
        self.assertCmdHelp('''\
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
357
zz{{:Purpose: zz{{A sample command.}}
358
}}zz{{:Usage:   bzr WithSeeAlso
359
}}
360
zz{{:Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.}}
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
365
}}
366
zz{{:See also: bar, foo}}
367
''',
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
368
                           cmd_WithSeeAlso())
5875.3.12 by INADA Naoki
Add test for command help translation.
369
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."""
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
374
        self.assertCmdHelp('''\
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
375
zz{{:Purpose: zz{{A sample command.}}
376
}}zz{{:Usage:   bzr Demo
377
}}
378
zz{{:Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.}}
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
383
}}
384
''',
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
385
                           cmd_Demo())
5875.3.12 by INADA Naoki
Add test for command help translation.
386
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'])
393
        self.assertEndsWith(
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
394
            helptext,'''\
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
395
  -q, --quiet    zz{{Only display errors and warnings.}}
396
  -v, --verbose  zz{{Display more information.}}
397
  -h, --help     zz{{Show help message.}}
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
398
}}
399
zz{{:See also: bar, foo, gam}}
400
''')
5875.3.12 by INADA Naoki
Add test for command help translation.
401
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'])
407
        self.assertEndsWith(
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
408
            helptext, '''\
409
zz{{:Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.}}
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
414
}}
415
zz{{:See also: gam}}
416
''')
5875.3.12 by INADA Naoki
Add test for command help translation.
417
418
419
    def test_help_custom_section_ordering(self):
420
        """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).
421
        # The help formatter expect the class name to start with 'cmd_'
5875.3.12 by INADA Naoki
Add test for command help translation.
422
        class cmd_Demo(commands.Command):
423
            __doc__ = """A sample command.
424
 
425
            Blah blah blah.
426
427
            :Formats:
428
              Interesting stuff about formats.
429
430
            :Examples:
431
              Example 1::
432
 
433
                cmd arg1
434
435
            :Tips:
436
              Clever things to keep in mind.
437
            """
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
438
        self.assertCmdHelp('''\
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
439
zz{{:Purpose: zz{{A sample command.}}
440
}}zz{{:Usage:   bzr Demo
441
}}
442
zz{{:Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.}}
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
447
}}
448
Description:
449
  zz{{zz{{Blah blah blah.}}
450
 
451
}}:Formats:
452
  zz{{Interesting stuff about formats.}}
453
 
454
Examples:
455
  zz{{Example 1::}}
456
 
457
    zz{{cmd arg1}}
458
 
459
Tips:
460
  zz{{Clever things to keep in mind.}}
461
 
462
''',
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
463
                           cmd_Demo())
5875.3.12 by INADA Naoki
Add test for command help translation.
464
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.
469
470
            :Usage:
471
                cmd Demo [opts] args
472
473
                cmd Demo -h
474
475
            Blah blah blah.
476
            """
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
477
        self.assertCmdHelp('''\
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
478
zz{{:Purpose: zz{{A sample command.}}
479
}}zz{{:Usage:
480
    zz{{cmd Demo [opts] args}}
481
482
    zz{{cmd Demo -h}}
483
484
}}
485
zz{{:Options:
6161.1.4 by Vincent Ladeuil
Follow the lead of other 'global' options which are not declared in option.py but handled in bzrlib.commands.run_bzr instead.
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.}}
6161.1.1 by Vincent Ladeuil
Allow config options to be overridden from the command line
490
}}
491
Description:
492
  zz{{zz{{Blah blah blah.}}
493
494
}}
495
''',
5875.3.23 by Vincent Ladeuil
Put the '\n' back into the formats and fix tests accordingly (reducing code duplication).
496
                           cmd_Demo())
5875.3.12 by INADA Naoki
Add test for command help translation.
497
498
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
499
class TestHelp(tests.TestCase):
500
501
    def setUp(self):
6552.1.3 by Vincent Ladeuil
Use super() instead of calling <base>.setup(self), as the original fix illustrated a too-easy-to-fall-into trap.
502
        super(TestHelp, self).setUp()
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
503
        commands.install_bzr_command_hooks()
504
505
4119.3.15 by Robert Collins
And fix tests for bzr help.
506
class TestRegisteredTopic(TestHelp):
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
507
    """Tests for the RegisteredTopic class."""
508
509
    def test_contruct(self):
510
        """Construction takes the help topic name for the registered item."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
511
        # validate our test
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
512
        self.assertTrue('basic' in help_topics.topic_registry)
513
        topic = help_topics.RegisteredTopic('basic')
514
        self.assertEqual('basic', topic.topic)
515
2432.1.10 by Robert Collins
Add get_help_text() to RegisteredTopic to get the help as a string.
516
    def test_get_help_text(self):
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
517
        """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.
518
        topic = help_topics.RegisteredTopic('commands')
519
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
520
                         topic.get_help_text())
2432.1.10 by Robert Collins
Add get_help_text() to RegisteredTopic to get the help as a string.
521
2432.1.22 by Robert Collins
Teach RegisteredTopic to support the additional_see_also list of related help terms.
522
    def test_get_help_text_with_additional_see_also(self):
523
        topic = help_topics.RegisteredTopic('commands')
524
        self.assertEndsWith(
525
            topic.get_help_text(['foo', 'bar']),
526
            '\n'
527
            'See also: bar, foo\n')
528
3089.3.5 by Ian Clatworthy
add test for loading help from a file
529
    def test_get_help_text_loaded_from_file(self):
530
        # Pick a known topic stored in an external file
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
531
        topic = help_topics.RegisteredTopic('authentication')
3089.3.5 by Ian Clatworthy
add test for loading help from a file
532
        self.assertStartsWith(topic.get_help_text(),
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
533
            'Authentication Settings\n'
534
            '=======================\n'
3089.3.5 by Ian Clatworthy
add test for loading help from a file
535
            '\n')
536
2432.1.27 by Robert Collins
Add a get_help_topic method to RegisteredTopic.
537
    def test_get_help_topic(self):
5875.3.26 by Vincent Ladeuil
Tweak test_help some more.
538
        """The help topic for RegisteredTopic is its topic from construction."""
2432.1.27 by Robert Collins
Add a get_help_topic method to RegisteredTopic.
539
        topic = help_topics.RegisteredTopic('foobar')
540
        self.assertEqual('foobar', topic.get_help_topic())
541
        topic = help_topics.RegisteredTopic('baz')
542
        self.assertEqual('baz', topic.get_help_topic())
543
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
544
4119.3.15 by Robert Collins
And fix tests for bzr help.
545
class TestTopicIndex(TestHelp):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
546
    """Tests for the HelpTopicIndex class."""
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
547
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
548
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
549
        index = help_topics.HelpTopicIndex()
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
550
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
551
    def test_get_topics_None(self):
552
        """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.
553
        index = help_topics.HelpTopicIndex()
554
        topics = index.get_topics(None)
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
555
        self.assertEqual(1, len(topics))
556
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
557
        self.assertEqual('basic', topics[0].topic)
558
559
    def test_get_topics_topics(self):
560
        """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.
561
        index = help_topics.HelpTopicIndex()
562
        topics = index.get_topics('topics')
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
563
        self.assertEqual(1, len(topics))
564
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
565
        self.assertEqual('topics', topics[0].topic)
566
567
    def test_get_topics_no_topic(self):
568
        """Searching for something not registered returns []."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
569
        index = help_topics.HelpTopicIndex()
570
        self.assertEqual([], index.get_topics('nothing by this name'))
571
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
572
    def test_prefix(self):
573
        """TopicIndex has a prefix of ''."""
574
        index = help_topics.HelpTopicIndex()
575
        self.assertEqual('', index.prefix)
576
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
577
6059.3.3 by Vincent Ladeuil
Fix typos caught by Jelmer.
578
class TestConfigOptionIndex(TestHelp):
6059.3.2 by Vincent Ladeuil
Cough, with tests.
579
    """Tests for the HelpCommandIndex class."""
580
581
    def setUp(self):
6059.3.3 by Vincent Ladeuil
Fix typos caught by Jelmer.
582
        super(TestConfigOptionIndex, self).setUp()
6059.3.2 by Vincent Ladeuil
Cough, with tests.
583
        self.index = help_topics.ConfigOptionHelpIndex()
584
585
    def test_get_topics_None(self):
586
        """Searching for None returns an empty list."""
587
        self.assertEqual([], self.index.get_topics(None))
588
589
    def test_get_topics_no_topic(self):
590
        self.assertEqual([], self.index.get_topics('nothing by this name'))
591
592
    def test_prefix(self):
593
        self.assertEqual('configuration/', self.index.prefix)
594
595
    def test_get_topic_with_prefix(self):
596
        topics = self.index.get_topics('configuration/default_format')
597
        self.assertLength(1, topics)
598
        opt = topics[0]
599
        self.assertIsInstance(opt, config.Option)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
600
        self.assertEqual('default_format', opt.name)
6059.3.2 by Vincent Ladeuil
Cough, with tests.
601
602
4119.3.15 by Robert Collins
And fix tests for bzr help.
603
class TestCommandIndex(TestHelp):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
604
    """Tests for the HelpCommandIndex class."""
2432.1.2 by Robert Collins
Add a HelpCommandContext class for help from commands.
605
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
606
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
607
        index = commands.HelpCommandIndex()
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
608
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
609
    def test_get_topics_None(self):
610
        """Searching for None returns an empty list."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
611
        index = commands.HelpCommandIndex()
612
        self.assertEqual([], index.get_topics(None))
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
613
614
    def test_get_topics_rocks(self):
615
        """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.
616
        index = commands.HelpCommandIndex()
617
        topics = index.get_topics('rocks')
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
618
        self.assertEqual(1, len(topics))
619
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
620
621
    def test_get_topics_no_topic(self):
622
        """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.
623
        index = commands.HelpCommandIndex()
624
        self.assertEqual([], index.get_topics('nothing by this name'))
625
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
626
    def test_prefix(self):
627
        """CommandIndex has a prefix of 'commands/'."""
628
        index = commands.HelpCommandIndex()
629
        self.assertEqual('commands/', index.prefix)
630
2432.1.18 by Robert Collins
Add support for doing bzr help commands/COMMANDNAME.
631
    def test_get_topic_with_prefix(self):
632
        """Searching for commands/rocks returns the rocks command object."""
633
        index = commands.HelpCommandIndex()
634
        topics = index.get_topics('commands/rocks')
635
        self.assertEqual(1, len(topics))
636
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
637
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
638
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
639
class TestHelpIndices(tests.TestCase):
640
    """Tests for the HelpIndices class."""
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
641
642
    def test_default_search_path(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
643
        """The default search path should include internal indexs."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
644
        indices = help.HelpIndices()
6059.3.1 by Vincent Ladeuil
Provide per-config option help
645
        self.assertEqual(4, len(indices.search_path))
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
646
        # help topics should be searched in first.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
647
        self.assertIsInstance(indices.search_path[0],
6059.3.1 by Vincent Ladeuil
Provide per-config option help
648
                              help_topics.HelpTopicIndex)
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
649
        # with commands being search second.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
650
        self.assertIsInstance(indices.search_path[1],
6059.3.1 by Vincent Ladeuil
Provide per-config option help
651
                              commands.HelpCommandIndex)
652
        # plugins are a third index.
2432.1.24 by Robert Collins
Add plugins as a help index.
653
        self.assertIsInstance(indices.search_path[2],
6059.3.1 by Vincent Ladeuil
Provide per-config option help
654
                              plugin.PluginsHelpIndex)
655
        # config options are a fourth index
656
        self.assertIsInstance(indices.search_path[3],
657
                              help_topics.ConfigOptionHelpIndex)
2432.1.5 by Robert Collins
Initial stub for topic searching.
658
659
    def test_search_for_unknown_topic_raises(self):
660
        """Searching for an unknown topic should raise NoHelpTopic."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
661
        indices = help.HelpIndices()
662
        indices.search_path = []
663
        error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
2432.1.5 by Robert Collins
Initial stub for topic searching.
664
        self.assertEqual('foo', error.topic)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
665
666
    def test_search_calls_get_topic(self):
667
        """Searching should call get_topics in all indexes in order."""
668
        calls = []
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
669
        class RecordingIndex(object):
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
670
            def __init__(self, name):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
671
                self.prefix = name
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
672
            def get_topics(self, topic):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
673
                calls.append(('get_topics', self.prefix, topic))
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
674
                return ['something']
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
675
        index = help.HelpIndices()
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
676
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
677
        # try with None
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
678
        index.search(None)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
679
        self.assertEqual([
680
            ('get_topics', '1', None),
681
            ('get_topics', '2', None),
682
            ],
683
            calls)
684
        # and with a string
685
        del calls[:]
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
686
        index.search('bar')
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
687
        self.assertEqual([
688
            ('get_topics', '1', 'bar'),
689
            ('get_topics', '2', 'bar'),
690
            ],
691
            calls)
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
692
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
693
    def test_search_returns_index_and_results(self):
694
        """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.
695
        class CannedIndex(object):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
696
            def __init__(self, prefix, search_result):
697
                self.prefix = prefix
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
698
                self.result = search_result
699
            def get_topics(self, topic):
700
                return self.result
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
701
        index = help.HelpIndices()
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
702
        index_one = CannedIndex('1', ['a'])
703
        index_two = CannedIndex('2', ['b', 'c'])
704
        index.search_path = [index_one, index_two]
705
        self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
706
            index.search(None))
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
707
708
    def test_search_checks_for_duplicate_prefixes(self):
709
        """Its an error when there are multiple indices with the same prefix."""
710
        indices = help.HelpIndices()
711
        indices.search_path = [help_topics.HelpTopicIndex(),
712
            help_topics.HelpTopicIndex()]
713
        self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)