~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# Copyright (C) 2007-2012, 2016 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Unit tests for the bzrlib.help module."""

import textwrap

from bzrlib import (
    builtins,
    commands,
    config,
    errors,
    help,
    help_topics,
    i18n,
    plugin,
    tests,
    )

from bzrlib.tests.test_i18n import ZzzTranslations
import re


class TestCommandHelp(tests.TestCase):
    """Tests for help on commands."""

    def assertCmdHelp(self, expected, cmd):
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())

    def test_command_help_includes_see_also(self):
        class cmd_WithSeeAlso(commands.Command):
            __doc__ = """A sample command."""
            _see_also = ['foo', 'bar']
        self.assertCmdHelp('''\
Purpose: A sample command.
Usage:   bzr WithSeeAlso

Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

See also: bar, foo
''',
                           cmd_WithSeeAlso())

    def test_get_help_text(self):
        """Commands have a get_help_text method which returns their help."""
        class cmd_Demo(commands.Command):
            __doc__ = """A sample command."""
        self.assertCmdHelp('''\
Purpose: A sample command.
Usage:   bzr Demo

Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

''',
                           cmd_Demo())
        cmd = cmd_Demo()
        helptext = cmd.get_help_text()
        self.assertStartsWith(helptext,
            'Purpose: A sample command.\n'
            'Usage:   bzr Demo')
        self.assertEndsWith(helptext,
            '  -h, --help     Show help message.\n\n')

    def test_command_with_additional_see_also(self):
        class cmd_WithSeeAlso(commands.Command):
            __doc__ = """A sample command."""
            _see_also = ['foo', 'bar']
        cmd = cmd_WithSeeAlso()
        helptext = cmd.get_help_text(['gam'])
        self.assertEndsWith(
            helptext,
            '  -q, --quiet    Only display errors and warnings.\n'
            '  -v, --verbose  Display more information.\n'
            '  -h, --help     Show help message.\n'
            '\n'
            'See also: bar, foo, gam\n')

    def test_command_only_additional_see_also(self):
        class cmd_WithSeeAlso(commands.Command):
            __doc__ = """A sample command."""
        cmd = cmd_WithSeeAlso()
        helptext = cmd.get_help_text(['gam'])
        self.assertEndsWith(
            helptext,
            '  -q, --quiet    Only display errors and warnings.\n'
            '  -v, --verbose  Display more information.\n'
            '  -h, --help     Show help message.\n'
            '\n'
            'See also: gam\n')

    def test_get_help_topic(self):
        """The help topic for a Command is its name()."""
        class cmd_foo_bar(commands.Command):
            __doc__ = """A sample command."""
        cmd = cmd_foo_bar()
        self.assertEqual(cmd.name(), cmd.get_help_topic())

    def test_formatted_help_text(self):
        """Help text should be plain text by default."""
        class cmd_Demo(commands.Command):
            __doc__ = """A sample command.

            :Examples:
                Example 1::

                    cmd arg1

                Example 2::

                    cmd arg2

                A code block follows.

                ::

                    bzr Demo something
            """
        cmd = cmd_Demo()
        helptext = cmd.get_help_text()
        self.assertEqualDiff('''\
Purpose: A sample command.
Usage:   bzr Demo

Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

Examples:
    Example 1:

        cmd arg1

    Example 2:

        cmd arg2

    A code block follows.

        bzr Demo something

''',
                                         helptext)
        helptext = cmd.get_help_text(plain=False)
        self.assertEqualDiff('''\
:Purpose: A sample command.
:Usage:   bzr Demo

:Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

:Examples:
    Example 1::

        cmd arg1

    Example 2::

        cmd arg2

    A code block follows.

    ::

        bzr Demo something

''',
                             helptext)

    def test_concise_help_text(self):
        """Concise help text excludes the descriptive sections."""
        class cmd_Demo(commands.Command):
            __doc__ = """A sample command.
 
            Blah blah blah.

            :Examples:
                Example 1::
 
                    cmd arg1
            """
        cmd = cmd_Demo()
        helptext = cmd.get_help_text()
        self.assertEqualDiff('''\
Purpose: A sample command.
Usage:   bzr Demo

Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

Description:
  Blah blah blah.

Examples:
    Example 1:

        cmd arg1

''',
                             helptext)
        helptext = cmd.get_help_text(verbose=False)
        self.assertEqualDiff('''\
Purpose: A sample command.
Usage:   bzr Demo

Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

See bzr help Demo for more details and examples.

''',
                             helptext)

    def test_help_custom_section_ordering(self):
        """Custom descriptive sections should remain in the order given."""
        class cmd_Demo(commands.Command):
            __doc__ = """\
A sample command.

Blah blah blah.

:Formats:
  Interesting stuff about formats.

:Examples:
  Example 1::

    cmd arg1

:Tips:
  Clever things to keep in mind.
"""
        cmd = cmd_Demo()
        helptext = cmd.get_help_text()
        self.assertEqualDiff('''\
Purpose: A sample command.
Usage:   bzr Demo

Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

Description:
  Blah blah blah.

Formats:
  Interesting stuff about formats.

Examples:
  Example 1:

    cmd arg1

Tips:
  Clever things to keep in mind.

''',
                             helptext)

    def test_help_text_custom_usage(self):
        """Help text may contain a custom usage section."""
        class cmd_Demo(commands.Command):
            __doc__ = """A sample command.

            :Usage:
                cmd Demo [opts] args

                cmd Demo -h

            Blah blah blah.
            """
        cmd = cmd_Demo()
        helptext = cmd.get_help_text()
        self.assertEqualDiff('''\
Purpose: A sample command.
Usage:
    cmd Demo [opts] args

    cmd Demo -h


Options:
  --usage        Show usage message and options.
  -q, --quiet    Only display errors and warnings.
  -v, --verbose  Display more information.
  -h, --help     Show help message.

Description:
  Blah blah blah.

''',
                             helptext)


class ZzzTranslationsForDoc(ZzzTranslations):

    _section_pat = re.compile(':\w+:\\n\\s+')
    _indent_pat = re.compile('\\s+')

    def zzz(self, s):
        m = self._section_pat.match(s)
        if m is None:
            m = self._indent_pat.match(s)
        if m:
            return u'%szz{{%s}}' % (m.group(0), s[m.end():])
        return u'zz{{%s}}' % s


class TestCommandHelpI18n(tests.TestCase):
    """Tests for help on translated commands."""

    def setUp(self):
        super(TestCommandHelpI18n, self).setUp()
        self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())

    def assertCmdHelp(self, expected, cmd):
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())

    def test_command_help_includes_see_also(self):
        class cmd_WithSeeAlso(commands.Command):
            __doc__ = """A sample command."""
            _see_also = ['foo', 'bar']
        self.assertCmdHelp('''\
zz{{:Purpose: zz{{A sample command.}}
}}zz{{:Usage:   bzr WithSeeAlso
}}
zz{{:Options:
  --usage        zz{{Show usage message and options.}}
  -q, --quiet    zz{{Only display errors and warnings.}}
  -v, --verbose  zz{{Display more information.}}
  -h, --help     zz{{Show help message.}}
}}
zz{{:See also: bar, foo}}
''',
                           cmd_WithSeeAlso())

    def test_get_help_text(self):
        """Commands have a get_help_text method which returns their help."""
        class cmd_Demo(commands.Command):
            __doc__ = """A sample command."""
        self.assertCmdHelp('''\
zz{{:Purpose: zz{{A sample command.}}
}}zz{{:Usage:   bzr Demo
}}
zz{{:Options:
  --usage        zz{{Show usage message and options.}}
  -q, --quiet    zz{{Only display errors and warnings.}}
  -v, --verbose  zz{{Display more information.}}
  -h, --help     zz{{Show help message.}}
}}
''',
                           cmd_Demo())

    def test_command_with_additional_see_also(self):
        class cmd_WithSeeAlso(commands.Command):
            __doc__ = """A sample command."""
            _see_also = ['foo', 'bar']
        cmd = cmd_WithSeeAlso()
        helptext = cmd.get_help_text(['gam'])
        self.assertEndsWith(
            helptext,'''\
  -q, --quiet    zz{{Only display errors and warnings.}}
  -v, --verbose  zz{{Display more information.}}
  -h, --help     zz{{Show help message.}}
}}
zz{{:See also: bar, foo, gam}}
''')

    def test_command_only_additional_see_also(self):
        class cmd_WithSeeAlso(commands.Command):
            __doc__ = """A sample command."""
        cmd = cmd_WithSeeAlso()
        helptext = cmd.get_help_text(['gam'])
        self.assertEndsWith(
            helptext, '''\
zz{{:Options:
  --usage        zz{{Show usage message and options.}}
  -q, --quiet    zz{{Only display errors and warnings.}}
  -v, --verbose  zz{{Display more information.}}
  -h, --help     zz{{Show help message.}}
}}
zz{{:See also: gam}}
''')


    def test_help_custom_section_ordering(self):
        """Custom descriptive sections should remain in the order given."""
        # The help formatter expect the class name to start with 'cmd_'
        class cmd_Demo(commands.Command):
            __doc__ = """A sample command.
 
            Blah blah blah.

            :Formats:
              Interesting stuff about formats.

            :Examples:
              Example 1::
 
                cmd arg1

            :Tips:
              Clever things to keep in mind.
            """
        self.assertCmdHelp('''\
zz{{:Purpose: zz{{A sample command.}}
}}zz{{:Usage:   bzr Demo
}}
zz{{:Options:
  --usage        zz{{Show usage message and options.}}
  -q, --quiet    zz{{Only display errors and warnings.}}
  -v, --verbose  zz{{Display more information.}}
  -h, --help     zz{{Show help message.}}
}}
Description:
  zz{{zz{{Blah blah blah.}}
 
}}:Formats:
  zz{{Interesting stuff about formats.}}
 
Examples:
  zz{{Example 1::}}
 
    zz{{cmd arg1}}
 
Tips:
  zz{{Clever things to keep in mind.}}
 
''',
                           cmd_Demo())

    def test_help_text_custom_usage(self):
        """Help text may contain a custom usage section."""
        class cmd_Demo(commands.Command):
            __doc__ = """A sample command.

            :Usage:
                cmd Demo [opts] args

                cmd Demo -h

            Blah blah blah.
            """
        self.assertCmdHelp('''\
zz{{:Purpose: zz{{A sample command.}}
}}zz{{:Usage:
    zz{{cmd Demo [opts] args}}

    zz{{cmd Demo -h}}

}}
zz{{:Options:
  --usage        zz{{Show usage message and options.}}
  -q, --quiet    zz{{Only display errors and warnings.}}
  -v, --verbose  zz{{Display more information.}}
  -h, --help     zz{{Show help message.}}
}}
Description:
  zz{{zz{{Blah blah blah.}}

}}
''',
                           cmd_Demo())


class TestHelp(tests.TestCase):

    def setUp(self):
        super(TestHelp, self).setUp()
        commands.install_bzr_command_hooks()


class TestRegisteredTopic(TestHelp):
    """Tests for the RegisteredTopic class."""

    def test_contruct(self):
        """Construction takes the help topic name for the registered item."""
        # validate our test
        self.assertTrue('basic' in help_topics.topic_registry)
        topic = help_topics.RegisteredTopic('basic')
        self.assertEqual('basic', topic.topic)

    def test_get_help_text(self):
        """RegisteredTopic returns the get_detail results for get_help_text."""
        topic = help_topics.RegisteredTopic('commands')
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
                         topic.get_help_text())

    def test_get_help_text_with_additional_see_also(self):
        topic = help_topics.RegisteredTopic('commands')
        self.assertEndsWith(
            topic.get_help_text(['foo', 'bar']),
            '\n'
            'See also: bar, foo\n')

    def test_get_help_text_loaded_from_file(self):
        # Pick a known topic stored in an external file
        topic = help_topics.RegisteredTopic('authentication')
        self.assertStartsWith(topic.get_help_text(),
            'Authentication Settings\n'
            '=======================\n'
            '\n')

    def test_get_help_topic(self):
        """The help topic for RegisteredTopic is its topic from construction."""
        topic = help_topics.RegisteredTopic('foobar')
        self.assertEqual('foobar', topic.get_help_topic())
        topic = help_topics.RegisteredTopic('baz')
        self.assertEqual('baz', topic.get_help_topic())


class TestTopicIndex(TestHelp):
    """Tests for the HelpTopicIndex class."""

    def test_default_constructable(self):
        index = help_topics.HelpTopicIndex()

    def test_get_topics_None(self):
        """Searching for None returns the basic help topic."""
        index = help_topics.HelpTopicIndex()
        topics = index.get_topics(None)
        self.assertEqual(1, len(topics))
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
        self.assertEqual('basic', topics[0].topic)

    def test_get_topics_topics(self):
        """Searching for a string returns the matching string."""
        index = help_topics.HelpTopicIndex()
        topics = index.get_topics('topics')
        self.assertEqual(1, len(topics))
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
        self.assertEqual('topics', topics[0].topic)

    def test_get_topics_no_topic(self):
        """Searching for something not registered returns []."""
        index = help_topics.HelpTopicIndex()
        self.assertEqual([], index.get_topics('nothing by this name'))

    def test_prefix(self):
        """TopicIndex has a prefix of ''."""
        index = help_topics.HelpTopicIndex()
        self.assertEqual('', index.prefix)


class TestConfigOptionIndex(TestHelp):
    """Tests for the HelpCommandIndex class."""

    def setUp(self):
        super(TestConfigOptionIndex, self).setUp()
        self.index = help_topics.ConfigOptionHelpIndex()

    def test_get_topics_None(self):
        """Searching for None returns an empty list."""
        self.assertEqual([], self.index.get_topics(None))

    def test_get_topics_no_topic(self):
        self.assertEqual([], self.index.get_topics('nothing by this name'))

    def test_prefix(self):
        self.assertEqual('configuration/', self.index.prefix)

    def test_get_topic_with_prefix(self):
        topics = self.index.get_topics('configuration/default_format')
        self.assertLength(1, topics)
        opt = topics[0]
        self.assertIsInstance(opt, config.Option)
        self.assertEqual('default_format', opt.name)


class TestCommandIndex(TestHelp):
    """Tests for the HelpCommandIndex class."""

    def test_default_constructable(self):
        index = commands.HelpCommandIndex()

    def test_get_topics_None(self):
        """Searching for None returns an empty list."""
        index = commands.HelpCommandIndex()
        self.assertEqual([], index.get_topics(None))

    def test_get_topics_rocks(self):
        """Searching for 'rocks' returns the cmd_rocks command instance."""
        index = commands.HelpCommandIndex()
        topics = index.get_topics('rocks')
        self.assertEqual(1, len(topics))
        self.assertIsInstance(topics[0], builtins.cmd_rocks)

    def test_get_topics_no_topic(self):
        """Searching for something that is not a command returns []."""
        index = commands.HelpCommandIndex()
        self.assertEqual([], index.get_topics('nothing by this name'))

    def test_prefix(self):
        """CommandIndex has a prefix of 'commands/'."""
        index = commands.HelpCommandIndex()
        self.assertEqual('commands/', index.prefix)

    def test_get_topic_with_prefix(self):
        """Searching for commands/rocks returns the rocks command object."""
        index = commands.HelpCommandIndex()
        topics = index.get_topics('commands/rocks')
        self.assertEqual(1, len(topics))
        self.assertIsInstance(topics[0], builtins.cmd_rocks)


class TestHelpIndices(tests.TestCase):
    """Tests for the HelpIndices class."""

    def test_default_search_path(self):
        """The default search path should include internal indexs."""
        indices = help.HelpIndices()
        self.assertEqual(4, len(indices.search_path))
        # help topics should be searched in first.
        self.assertIsInstance(indices.search_path[0],
                              help_topics.HelpTopicIndex)
        # with commands being search second.
        self.assertIsInstance(indices.search_path[1],
                              commands.HelpCommandIndex)
        # plugins are a third index.
        self.assertIsInstance(indices.search_path[2],
                              plugin.PluginsHelpIndex)
        # config options are a fourth index
        self.assertIsInstance(indices.search_path[3],
                              help_topics.ConfigOptionHelpIndex)

    def test_search_for_unknown_topic_raises(self):
        """Searching for an unknown topic should raise NoHelpTopic."""
        indices = help.HelpIndices()
        indices.search_path = []
        error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
        self.assertEqual('foo', error.topic)

    def test_search_calls_get_topic(self):
        """Searching should call get_topics in all indexes in order."""
        calls = []
        class RecordingIndex(object):
            def __init__(self, name):
                self.prefix = name
            def get_topics(self, topic):
                calls.append(('get_topics', self.prefix, topic))
                return ['something']
        index = help.HelpIndices()
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
        # try with None
        index.search(None)
        self.assertEqual([
            ('get_topics', '1', None),
            ('get_topics', '2', None),
            ],
            calls)
        # and with a string
        del calls[:]
        index.search('bar')
        self.assertEqual([
            ('get_topics', '1', 'bar'),
            ('get_topics', '2', 'bar'),
            ],
            calls)

    def test_search_returns_index_and_results(self):
        """Searching should return help topics with their index"""
        class CannedIndex(object):
            def __init__(self, prefix, search_result):
                self.prefix = prefix
                self.result = search_result
            def get_topics(self, topic):
                return self.result
        index = help.HelpIndices()
        index_one = CannedIndex('1', ['a'])
        index_two = CannedIndex('2', ['b', 'c'])
        index.search_path = [index_one, index_two]
        self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
            index.search(None))

    def test_search_checks_for_duplicate_prefixes(self):
        """Its an error when there are multiple indices with the same prefix."""
        indices = help.HelpIndices()
        indices.search_path = [help_topics.HelpTopicIndex(),
            help_topics.HelpTopicIndex()]
        self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)