~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_help.py

  • Committer: John Arbash Meinel
  • Date: 2009-06-12 18:05:15 UTC
  • mto: (4371.4.5 vila-better-heads)
  • mto: This revision was merged to the branch mainline in revision 4449.
  • Revision ID: john@arbash-meinel.com-20090612180515-t0cwbjsnve094oik
Add a failing test for handling nodes that are in the same linear chain.

It fails because the ancestry skipping causes us to miss the fact that the two nodes
are actually directly related. We could check at the beginning, as the 
code used to do, but I think that will be incomplete for the more-than-two
heads cases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007 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
 
import textwrap
 
19
from cStringIO import StringIO
20
20
 
21
21
from bzrlib import (
22
22
    builtins,
23
23
    commands,
24
 
    config,
25
24
    errors,
26
25
    help,
27
26
    help_topics,
28
 
    i18n,
29
27
    plugin,
30
28
    tests,
31
29
    )
32
30
 
33
 
from bzrlib.tests.test_i18n import ZzzTranslations
34
 
import re
35
 
 
36
31
 
37
32
class TestCommandHelp(tests.TestCase):
38
33
    """Tests for help on commands."""
39
34
 
40
 
    def assertCmdHelp(self, expected, cmd):
41
 
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
42
 
 
43
35
    def test_command_help_includes_see_also(self):
44
36
        class cmd_WithSeeAlso(commands.Command):
45
 
            __doc__ = """A sample command."""
 
37
            """A sample command."""
46
38
            _see_also = ['foo', 'bar']
47
 
        self.assertCmdHelp('''\
48
 
            Purpose: A sample command.
49
 
            Usage:   bzr WithSeeAlso
50
 
            
51
 
            Options:
52
 
              --usage        Show usage message and options.
53
 
              -v, --verbose  Display more information.
54
 
              -q, --quiet    Only display errors and warnings.
55
 
              -h, --help     Show help message.
56
 
            
57
 
            See also: bar, foo
58
 
            ''',
59
 
                           cmd_WithSeeAlso())
 
39
        cmd = cmd_WithSeeAlso()
 
40
        helptext = cmd.get_help_text()
 
41
        self.assertEndsWith(
 
42
            helptext,
 
43
            '  -v, --verbose  Display more information.\n'
 
44
            '  -q, --quiet    Only display errors and warnings.\n'
 
45
            '  -h, --help     Show help message.\n'
 
46
            '\n'
 
47
            'See also: bar, foo\n')
60
48
 
61
49
    def test_get_help_text(self):
62
50
        """Commands have a get_help_text method which returns their help."""
63
51
        class cmd_Demo(commands.Command):
64
 
            __doc__ = """A sample command."""
65
 
        self.assertCmdHelp('''\
66
 
            Purpose: A sample command.
67
 
            Usage:   bzr Demo
68
 
            
69
 
            Options:
70
 
              --usage        Show usage message and options.
71
 
              -v, --verbose  Display more information.
72
 
              -q, --quiet    Only display errors and warnings.
73
 
              -h, --help     Show help message.
74
 
 
75
 
            ''',
76
 
                           cmd_Demo())
 
52
            """A sample command."""
77
53
        cmd = cmd_Demo()
78
54
        helptext = cmd.get_help_text()
79
55
        self.assertStartsWith(helptext,
84
60
 
85
61
    def test_command_with_additional_see_also(self):
86
62
        class cmd_WithSeeAlso(commands.Command):
87
 
            __doc__ = """A sample command."""
 
63
            """A sample command."""
88
64
            _see_also = ['foo', 'bar']
89
65
        cmd = cmd_WithSeeAlso()
90
66
        helptext = cmd.get_help_text(['gam'])
98
74
 
99
75
    def test_command_only_additional_see_also(self):
100
76
        class cmd_WithSeeAlso(commands.Command):
101
 
            __doc__ = """A sample command."""
 
77
            """A sample command."""
102
78
        cmd = cmd_WithSeeAlso()
103
79
        helptext = cmd.get_help_text(['gam'])
104
80
        self.assertEndsWith(
112
88
    def test_get_help_topic(self):
113
89
        """The help topic for a Command is its name()."""
114
90
        class cmd_foo_bar(commands.Command):
115
 
            __doc__ = """A sample command."""
 
91
            """A sample command."""
116
92
        cmd = cmd_foo_bar()
117
93
        self.assertEqual(cmd.name(), cmd.get_help_topic())
118
94
 
119
95
    def test_formatted_help_text(self):
120
96
        """Help text should be plain text by default."""
121
97
        class cmd_Demo(commands.Command):
122
 
            __doc__ = """A sample command.
 
98
            """A sample command.
123
99
 
124
100
            :Examples:
125
101
                Example 1::
129
105
                Example 2::
130
106
 
131
107
                    cmd arg2
132
 
 
133
 
                A code block follows.
134
 
 
135
 
                ::
136
 
 
137
 
                    bzr Demo something
138
108
            """
139
109
        cmd = cmd_Demo()
140
110
        helptext = cmd.get_help_text()
157
127
            '    Example 2:\n'
158
128
            '\n'
159
129
            '        cmd arg2\n'
160
 
            '\n'
161
 
            '    A code block follows.\n'
162
 
            '\n'
163
 
            '        bzr Demo something\n'
164
130
            '\n')
165
131
        helptext = cmd.get_help_text(plain=False)
166
132
        self.assertEquals(helptext,
181
147
            '    Example 2::\n'
182
148
            '\n'
183
149
            '        cmd arg2\n'
184
 
            '\n'
185
 
            '    A code block follows.\n'
186
 
            '\n'
187
 
            '    ::\n'
188
 
            '\n'
189
 
            '        bzr Demo something\n'
190
150
            '\n')
191
151
 
192
152
    def test_concise_help_text(self):
193
153
        """Concise help text excludes the descriptive sections."""
194
154
        class cmd_Demo(commands.Command):
195
 
            __doc__ = """A sample command.
 
155
            """A sample command.
196
156
 
197
157
            Blah blah blah.
198
158
 
239
199
    def test_help_custom_section_ordering(self):
240
200
        """Custom descriptive sections should remain in the order given."""
241
201
        class cmd_Demo(commands.Command):
242
 
            __doc__ = """A sample command.
 
202
            """A sample command.
243
203
 
244
204
            Blah blah blah.
245
205
 
285
245
    def test_help_text_custom_usage(self):
286
246
        """Help text may contain a custom usage section."""
287
247
        class cmd_Demo(commands.Command):
288
 
            __doc__ = """A sample command.
 
248
            """A sample command.
289
249
 
290
250
            :Usage:
291
251
                cmd Demo [opts] args
314
274
            '  Blah blah blah.\n\n')
315
275
 
316
276
 
317
 
class ZzzTranslationsForDoc(ZzzTranslations):
318
 
 
319
 
    _section_pat = re.compile(':\w+:\\n\\s+')
320
 
    _indent_pat = re.compile('\\s+')
321
 
 
322
 
    def zzz(self, s):
323
 
        m = self._section_pat.match(s)
324
 
        if m is None:
325
 
            m = self._indent_pat.match(s)
326
 
        if m:
327
 
            return u'%szz{{%s}}' % (m.group(0), s[m.end():])
328
 
        return u'zz{{%s}}' % s
329
 
 
330
 
 
331
 
class TestCommandHelpI18n(tests.TestCase):
332
 
    """Tests for help on translated commands."""
333
 
 
334
 
    def setUp(self):
335
 
        super(TestCommandHelpI18n, self).setUp()
336
 
        self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
337
 
 
338
 
    def assertCmdHelp(self, expected, cmd):
339
 
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
340
 
 
341
 
    def test_command_help_includes_see_also(self):
342
 
        class cmd_WithSeeAlso(commands.Command):
343
 
            __doc__ = """A sample command."""
344
 
            _see_also = ['foo', 'bar']
345
 
        self.assertCmdHelp('''\
346
 
            zz{{:Purpose: zz{{A sample command.}}
347
 
            }}zz{{:Usage:   bzr WithSeeAlso
348
 
            }}
349
 
            zz{{:Options:
350
 
              --usage        Show usage message and options.
351
 
              -v, --verbose  Display more information.
352
 
              -q, --quiet    Only display errors and warnings.
353
 
              -h, --help     Show help message.
354
 
            }}
355
 
            zz{{:See also: bar, foo}}
356
 
            ''',
357
 
                           cmd_WithSeeAlso())
358
 
 
359
 
    def test_get_help_text(self):
360
 
        """Commands have a get_help_text method which returns their help."""
361
 
        class cmd_Demo(commands.Command):
362
 
            __doc__ = """A sample command."""
363
 
        self.assertCmdHelp('''\
364
 
            zz{{:Purpose: zz{{A sample command.}}
365
 
            }}zz{{:Usage:   bzr Demo
366
 
            }}
367
 
            zz{{:Options:
368
 
              --usage        Show usage message and options.
369
 
              -v, --verbose  Display more information.
370
 
              -q, --quiet    Only display errors and warnings.
371
 
              -h, --help     Show help message.
372
 
            }}
373
 
            ''',
374
 
                           cmd_Demo())
375
 
 
376
 
    def test_command_with_additional_see_also(self):
377
 
        class cmd_WithSeeAlso(commands.Command):
378
 
            __doc__ = """A sample command."""
379
 
            _see_also = ['foo', 'bar']
380
 
        cmd = cmd_WithSeeAlso()
381
 
        helptext = cmd.get_help_text(['gam'])
382
 
        self.assertEndsWith(
383
 
            helptext,
384
 
            '  -v, --verbose  Display more information.\n'
385
 
            '  -q, --quiet    Only display errors and warnings.\n'
386
 
            '  -h, --help     Show help message.\n'
387
 
            '}}\n'
388
 
            'zz{{:See also: bar, foo, gam}}\n')
389
 
 
390
 
    def test_command_only_additional_see_also(self):
391
 
        class cmd_WithSeeAlso(commands.Command):
392
 
            __doc__ = """A sample command."""
393
 
        cmd = cmd_WithSeeAlso()
394
 
        helptext = cmd.get_help_text(['gam'])
395
 
        self.assertEndsWith(
396
 
            helptext,
397
 
            'zz{{:Options:\n'
398
 
            '  --usage        Show usage message and options.\n'
399
 
            '  -v, --verbose  Display more information.\n'
400
 
            '  -q, --quiet    Only display errors and warnings.\n'
401
 
            '  -h, --help     Show help message.\n'
402
 
            '}}\n'
403
 
            'zz{{:See also: gam}}\n')
404
 
 
405
 
 
406
 
    def test_help_custom_section_ordering(self):
407
 
        """Custom descriptive sections should remain in the order given."""
408
 
        # The help formatter expect the class name to start with 'cmd_'
409
 
        class cmd_Demo(commands.Command):
410
 
            __doc__ = """A sample command.
411
 
 
412
 
            Blah blah blah.
413
 
 
414
 
            :Formats:
415
 
              Interesting stuff about formats.
416
 
 
417
 
            :Examples:
418
 
              Example 1::
419
 
 
420
 
                cmd arg1
421
 
 
422
 
            :Tips:
423
 
              Clever things to keep in mind.
424
 
            """
425
 
        self.assertCmdHelp('''\
426
 
            zz{{:Purpose: zz{{A sample command.}}
427
 
            }}zz{{:Usage:   bzr Demo
428
 
            }}
429
 
            zz{{:Options:
430
 
              --usage        Show usage message and options.
431
 
              -v, --verbose  Display more information.
432
 
              -q, --quiet    Only display errors and warnings.
433
 
              -h, --help     Show help message.
434
 
            }}
435
 
            Description:
436
 
              zz{{zz{{Blah blah blah.}}
437
 
            
438
 
            }}:Formats:
439
 
              zz{{Interesting stuff about formats.}}
440
 
            
441
 
            Examples:
442
 
              zz{{Example 1::}}
443
 
            
444
 
                zz{{cmd arg1}}
445
 
            
446
 
            Tips:
447
 
              zz{{Clever things to keep in mind.}}
448
 
 
449
 
            ''',
450
 
                           cmd_Demo())
451
 
 
452
 
    def test_help_text_custom_usage(self):
453
 
        """Help text may contain a custom usage section."""
454
 
        class cmd_Demo(commands.Command):
455
 
            __doc__ = """A sample command.
456
 
 
457
 
            :Usage:
458
 
                cmd Demo [opts] args
459
 
 
460
 
                cmd Demo -h
461
 
 
462
 
            Blah blah blah.
463
 
            """
464
 
        self.assertCmdHelp('''\
465
 
            zz{{:Purpose: zz{{A sample command.}}
466
 
            }}zz{{:Usage:
467
 
                zz{{cmd Demo [opts] args}}
468
 
            
469
 
                zz{{cmd Demo -h}}
470
 
 
471
 
            }}
472
 
            zz{{:Options:
473
 
              --usage        Show usage message and options.
474
 
              -v, --verbose  Display more information.
475
 
              -q, --quiet    Only display errors and warnings.
476
 
              -h, --help     Show help message.
477
 
            }}
478
 
            Description:
479
 
              zz{{zz{{Blah blah blah.}}
480
 
 
481
 
            }}
482
 
            ''',
483
 
                           cmd_Demo())
484
 
 
485
 
 
486
 
class TestHelp(tests.TestCase):
487
 
 
488
 
    def setUp(self):
489
 
        tests.TestCase.setUp(self)
490
 
        commands.install_bzr_command_hooks()
491
 
 
492
 
 
493
 
class TestRegisteredTopic(TestHelp):
 
277
class TestRegisteredTopic(tests.TestCase):
494
278
    """Tests for the RegisteredTopic class."""
495
279
 
496
280
    def test_contruct(self):
501
285
        self.assertEqual('basic', topic.topic)
502
286
 
503
287
    def test_get_help_text(self):
504
 
        """RegisteredTopic returns the get_detail results for get_help_text."""
 
288
        """A RegisteredTopic returns the get_detail results for get_help_text."""
505
289
        topic = help_topics.RegisteredTopic('commands')
506
290
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
507
 
                         topic.get_help_text())
 
291
            topic.get_help_text())
508
292
 
509
293
    def test_get_help_text_with_additional_see_also(self):
510
294
        topic = help_topics.RegisteredTopic('commands')
522
306
            '\n')
523
307
 
524
308
    def test_get_help_topic(self):
525
 
        """The help topic for RegisteredTopic is its topic from construction."""
 
309
        """The help topic for a RegisteredTopic is its topic from construction."""
526
310
        topic = help_topics.RegisteredTopic('foobar')
527
311
        self.assertEqual('foobar', topic.get_help_topic())
528
312
        topic = help_topics.RegisteredTopic('baz')
529
313
        self.assertEqual('baz', topic.get_help_topic())
530
314
 
531
315
 
532
 
class TestTopicIndex(TestHelp):
 
316
class TestTopicIndex(tests.TestCase):
533
317
    """Tests for the HelpTopicIndex class."""
534
318
 
535
319
    def test_default_constructable(self):
562
346
        self.assertEqual('', index.prefix)
563
347
 
564
348
 
565
 
class TestConfigOptionIndex(TestHelp):
566
 
    """Tests for the HelpCommandIndex class."""
567
 
 
568
 
    def setUp(self):
569
 
        super(TestConfigOptionIndex, self).setUp()
570
 
        self.index = help_topics.ConfigOptionHelpIndex()
571
 
 
572
 
    def test_get_topics_None(self):
573
 
        """Searching for None returns an empty list."""
574
 
        self.assertEqual([], self.index.get_topics(None))
575
 
 
576
 
    def test_get_topics_no_topic(self):
577
 
        self.assertEqual([], self.index.get_topics('nothing by this name'))
578
 
 
579
 
    def test_prefix(self):
580
 
        self.assertEqual('configuration/', self.index.prefix)
581
 
 
582
 
    def test_get_topic_with_prefix(self):
583
 
        topics = self.index.get_topics('configuration/default_format')
584
 
        self.assertLength(1, topics)
585
 
        opt = topics[0]
586
 
        self.assertIsInstance(opt, config.Option)
587
 
        self.assertEquals('default_format', opt.name)
588
 
 
589
 
 
590
 
class TestCommandIndex(TestHelp):
 
349
class TestCommandIndex(tests.TestCase):
591
350
    """Tests for the HelpCommandIndex class."""
592
351
 
593
352
    def test_default_constructable(self):
629
388
    def test_default_search_path(self):
630
389
        """The default search path should include internal indexs."""
631
390
        indices = help.HelpIndices()
632
 
        self.assertEqual(4, len(indices.search_path))
 
391
        self.assertEqual(3, len(indices.search_path))
633
392
        # help topics should be searched in first.
634
393
        self.assertIsInstance(indices.search_path[0],
635
 
                              help_topics.HelpTopicIndex)
 
394
            help_topics.HelpTopicIndex)
636
395
        # with commands being search second.
637
396
        self.assertIsInstance(indices.search_path[1],
638
 
                              commands.HelpCommandIndex)
639
 
        # plugins are a third index.
 
397
            commands.HelpCommandIndex)
 
398
        # and plugins are a third index.
640
399
        self.assertIsInstance(indices.search_path[2],
641
 
                              plugin.PluginsHelpIndex)
642
 
        # config options are a fourth index
643
 
        self.assertIsInstance(indices.search_path[3],
644
 
                              help_topics.ConfigOptionHelpIndex)
 
400
            plugin.PluginsHelpIndex)
645
401
 
646
402
    def test_search_for_unknown_topic_raises(self):
647
403
        """Searching for an unknown topic should raise NoHelpTopic."""