/brz/remove-bazaar

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