/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_help.py

Add test for exporting command help.
It checks ":Usage:" is not exported.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Unit tests for the bzrlib.help module."""
18
18
 
19
 
from cStringIO import StringIO
20
 
 
21
19
from bzrlib import (
22
20
    builtins,
23
21
    commands,
24
22
    errors,
25
23
    help,
26
24
    help_topics,
 
25
    i18n,
27
26
    plugin,
28
27
    tests,
29
28
    )
30
29
 
 
30
from bzrlib.tests.test_i18n import ZzzTranslations
 
31
import re
 
32
 
31
33
 
32
34
class TestHelp(tests.TestCase):
33
35
 
112
114
                Example 2::
113
115
 
114
116
                    cmd arg2
 
117
 
 
118
                A code block follows.
 
119
 
 
120
                ::
 
121
 
 
122
                    bzr Demo something
115
123
            """
116
124
        cmd = cmd_Demo()
117
125
        helptext = cmd.get_help_text()
134
142
            '    Example 2:\n'
135
143
            '\n'
136
144
            '        cmd arg2\n'
 
145
            '\n'
 
146
            '    A code block follows.\n'
 
147
            '\n'
 
148
            '        bzr Demo something\n'
137
149
            '\n')
138
150
        helptext = cmd.get_help_text(plain=False)
139
151
        self.assertEquals(helptext,
154
166
            '    Example 2::\n'
155
167
            '\n'
156
168
            '        cmd arg2\n'
 
169
            '\n'
 
170
            '    A code block follows.\n'
 
171
            '\n'
 
172
            '    ::\n'
 
173
            '\n'
 
174
            '        bzr Demo something\n'
157
175
            '\n')
158
176
 
159
177
    def test_concise_help_text(self):
281
299
            '  Blah blah blah.\n\n')
282
300
 
283
301
 
 
302
class ZzzTranslationsForDoc(ZzzTranslations):
 
303
 
 
304
    _section_pat = re.compile(':\w+:\\n\\s+')
 
305
    _indent_pat = re.compile('\\s+')
 
306
 
 
307
    def zzz(self, s):
 
308
        m = self._section_pat.match(s)
 
309
        if m is None:
 
310
            m = self._indent_pat.match(s)
 
311
        if m:
 
312
            return u'%szz{{%s}}' % (m.group(0), s[m.end():])
 
313
        return u'zz{{%s}}' % s
 
314
 
 
315
 
 
316
class TestCommandHelpI18n(tests.TestCase):
 
317
    """Tests for help on translated commands."""
 
318
 
 
319
    def setUp(self):
 
320
        super(TestCommandHelpI18n, self).setUp()
 
321
        self.overrideAttr(i18n, '_translation', ZzzTranslationsForDoc())
 
322
 
 
323
 
 
324
    def test_command_help_includes_see_also(self):
 
325
        class cmd_WithSeeAlso(commands.Command):
 
326
            __doc__ = """A sample command."""
 
327
            _see_also = ['foo', 'bar']
 
328
        cmd = cmd_WithSeeAlso()
 
329
        helptext = cmd.get_help_text()
 
330
        self.assertEndsWith(
 
331
            helptext,
 
332
            '  -v, --verbose  zz{{Display more information.}}\n'
 
333
            '  -q, --quiet    zz{{Only display errors and warnings.}}\n'
 
334
            '  -h, --help     zz{{Show help message.}}\n'
 
335
            '}}\n'
 
336
            'zz{{:See also: bar, foo}}\n')
 
337
 
 
338
    def test_get_help_text(self):
 
339
        """Commands have a get_help_text method which returns their help."""
 
340
        class cmd_Demo(commands.Command):
 
341
            __doc__ = """A sample command."""
 
342
        cmd = cmd_Demo()
 
343
        helptext = cmd.get_help_text()
 
344
        self.assertStartsWith(helptext,
 
345
            'zz{{:Purpose: zz{{A sample command.}}}}\n'
 
346
            'zz{{:Usage:   bzr Demo}}\n')
 
347
        self.assertEndsWith(helptext,
 
348
            '  -h, --help     zz{{Show help message.}}\n}}\n')
 
349
 
 
350
    def test_command_with_additional_see_also(self):
 
351
        class cmd_WithSeeAlso(commands.Command):
 
352
            __doc__ = """A sample command."""
 
353
            _see_also = ['foo', 'bar']
 
354
        cmd = cmd_WithSeeAlso()
 
355
        helptext = cmd.get_help_text(['gam'])
 
356
        self.assertEndsWith(
 
357
            helptext,
 
358
            '  -v, --verbose  zz{{Display more information.}}\n'
 
359
            '  -q, --quiet    zz{{Only display errors and warnings.}}\n'
 
360
            '  -h, --help     zz{{Show help message.}}\n'
 
361
            '}}\n'
 
362
            'zz{{:See also: bar, foo, gam}}\n')
 
363
 
 
364
    def test_command_only_additional_see_also(self):
 
365
        class cmd_WithSeeAlso(commands.Command):
 
366
            __doc__ = """A sample command."""
 
367
        cmd = cmd_WithSeeAlso()
 
368
        helptext = cmd.get_help_text(['gam'])
 
369
        self.assertEndsWith(
 
370
            helptext,
 
371
            'zz{{:Options:\n'
 
372
            '  --usage        zz{{Show usage message and options.}}\n'
 
373
            '  -v, --verbose  zz{{Display more information.}}\n'
 
374
            '  -q, --quiet    zz{{Only display errors and warnings.}}\n'
 
375
            '  -h, --help     zz{{Show help message.}}\n'
 
376
            '}}\n'
 
377
            'zz{{:See also: gam}}\n')
 
378
 
 
379
 
 
380
    def test_help_custom_section_ordering(self):
 
381
        """Custom descriptive sections should remain in the order given."""
 
382
        class cmd_Demo(commands.Command):
 
383
            __doc__ = """A sample command.
 
384
 
 
385
            Blah blah blah.
 
386
 
 
387
            :Formats:
 
388
              Interesting stuff about formats.
 
389
 
 
390
            :Examples:
 
391
              Example 1::
 
392
 
 
393
                cmd arg1
 
394
 
 
395
            :Tips:
 
396
              Clever things to keep in mind.
 
397
            """
 
398
        cmd = cmd_Demo()
 
399
        helptext = cmd.get_help_text()
 
400
        self.assertEqualDiff(
 
401
            helptext,
 
402
            'zz{{:Purpose: zz{{A sample command.}}}}\n'
 
403
            'zz{{:Usage:   bzr Demo}}\n'
 
404
            '\n'
 
405
            'zz{{:Options:\n'
 
406
            '  --usage        zz{{Show usage message and options.}}\n'
 
407
            '  -v, --verbose  zz{{Display more information.}}\n'
 
408
            '  -q, --quiet    zz{{Only display errors and warnings.}}\n'
 
409
            '  -h, --help     zz{{Show help message.}}\n'
 
410
            '}}\n'
 
411
            'Description:\n'
 
412
            '  zz{{zz{{Blah blah blah.}}}}\n'
 
413
            '\n'
 
414
            'Formats:\n'
 
415
            '  zz{{Interesting stuff about formats.}}\n'
 
416
            '\n'
 
417
            'Examples:\n'
 
418
            '  zz{{Example 1::}}\n'
 
419
            '\n'
 
420
            '    zz{{cmd arg1}}\n'
 
421
            '\n'
 
422
            'Tips:\n'
 
423
            '  zz{{Clever things to keep in mind.}}\n'
 
424
            '\n')
 
425
 
 
426
    def test_help_text_custom_usage(self):
 
427
        """Help text may contain a custom usage section."""
 
428
        class cmd_Demo(commands.Command):
 
429
            __doc__ = """A sample command.
 
430
 
 
431
            :Usage:
 
432
                cmd Demo [opts] args
 
433
 
 
434
                cmd Demo -h
 
435
 
 
436
            Blah blah blah.
 
437
            """
 
438
        cmd = cmd_Demo()
 
439
        helptext = cmd.get_help_text()
 
440
        self.assertEquals(helptext,
 
441
            'zz{{:Purpose: zz{{A sample command.}}}}\n'
 
442
            'zz{{:Usage:\n'
 
443
            '    zz{{cmd Demo [opts] args}}\n'
 
444
            '\n'
 
445
            '    zz{{cmd Demo -h}}\n'
 
446
            '}}\n'
 
447
            '\n'
 
448
            'zz{{:Options:\n'
 
449
            '  --usage        zz{{Show usage message and options.}}\n'
 
450
            '  -v, --verbose  zz{{Display more information.}}\n'
 
451
            '  -q, --quiet    zz{{Only display errors and warnings.}}\n'
 
452
            '  -h, --help     zz{{Show help message.}}\n'
 
453
            '}}\n'
 
454
            'Description:\n'
 
455
            '  zz{{zz{{Blah blah blah.}}}}\n\n')
 
456
 
 
457
 
284
458
class TestRegisteredTopic(TestHelp):
285
459
    """Tests for the RegisteredTopic class."""
286
460