/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 breezy/tests/test_help.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:26:22 UTC
  • mfrom: (7167.1.4 run-flake8)
  • Revision ID: breezy.the.bot@gmail.com-20181116182622-qw3gan3hz78a2imw
Add a flake8 test.

Merged from https://code.launchpad.net/~jelmer/brz/run-flake8/+merge/358902

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2012, 2016 Canonical Ltd
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Unit tests for the breezy.help module."""
 
18
 
 
19
import textwrap
 
20
 
 
21
from .. import (
 
22
    builtins,
 
23
    commands,
 
24
    config,
 
25
    errors,
 
26
    help,
 
27
    help_topics,
 
28
    i18n,
 
29
    plugin,
 
30
    tests,
 
31
    )
 
32
 
 
33
from .test_i18n import ZzzTranslations
 
34
import re
 
35
 
 
36
 
 
37
class TestErrors(tests.TestCase):
 
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
 
 
46
class TestCommandHelp(tests.TestCase):
 
47
    """Tests for help on commands."""
 
48
 
 
49
    def assertCmdHelp(self, expected, cmd):
 
50
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
 
51
 
 
52
    def test_command_help_includes_see_also(self):
 
53
        class cmd_WithSeeAlso(commands.Command):
 
54
            __doc__ = """A sample command."""
 
55
            _see_also = ['foo', 'bar']
 
56
        self.assertCmdHelp('''\
 
57
Purpose: A sample command.
 
58
Usage:   brz WithSeeAlso
 
59
 
 
60
Options:
 
61
  -h, --help     Show help message.
 
62
  -q, --quiet    Only display errors and warnings.
 
63
  --usage        Show usage message and options.
 
64
  -v, --verbose  Display more information.
 
65
 
 
66
See also: bar, foo
 
67
''',
 
68
                           cmd_WithSeeAlso())
 
69
 
 
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):
 
73
            __doc__ = """A sample command."""
 
74
        self.assertCmdHelp('''\
 
75
Purpose: A sample command.
 
76
Usage:   brz Demo
 
77
 
 
78
Options:
 
79
  -h, --help     Show help message.
 
80
  -q, --quiet    Only display errors and warnings.
 
81
  --usage        Show usage message and options.
 
82
  -v, --verbose  Display more information.
 
83
 
 
84
''',
 
85
                           cmd_Demo())
 
86
        cmd = cmd_Demo()
 
87
        helptext = cmd.get_help_text()
 
88
        self.assertStartsWith(helptext,
 
89
            'Purpose: A sample command.\n'
 
90
            'Usage:   brz Demo')
 
91
        self.assertEndsWith(helptext,
 
92
            '  -v, --verbose  Display more information.\n\n')
 
93
 
 
94
    def test_command_with_additional_see_also(self):
 
95
        class cmd_WithSeeAlso(commands.Command):
 
96
            __doc__ = """A sample command."""
 
97
            _see_also = ['foo', 'bar']
 
98
        cmd = cmd_WithSeeAlso()
 
99
        helptext = cmd.get_help_text(['gam'])
 
100
        self.assertEndsWith(
 
101
            helptext,
 
102
            '  -h, --help     Show help message.\n'
 
103
            '  -q, --quiet    Only display errors and warnings.\n'
 
104
            '  --usage        Show usage message and options.\n'
 
105
            '  -v, --verbose  Display more information.\n'
 
106
            '\n'
 
107
            'See also: bar, foo, gam\n')
 
108
 
 
109
    def test_command_only_additional_see_also(self):
 
110
        class cmd_WithSeeAlso(commands.Command):
 
111
            __doc__ = """A sample command."""
 
112
        cmd = cmd_WithSeeAlso()
 
113
        helptext = cmd.get_help_text(['gam'])
 
114
        self.assertEndsWith(
 
115
            helptext,
 
116
            '  -v, --verbose  Display more information.\n'
 
117
            '\n'
 
118
            'See also: gam\n')
 
119
 
 
120
    def test_get_help_topic(self):
 
121
        """The help topic for a Command is its name()."""
 
122
        class cmd_foo_bar(commands.Command):
 
123
            __doc__ = """A sample command."""
 
124
        cmd = cmd_foo_bar()
 
125
        self.assertEqual(cmd.name(), cmd.get_help_topic())
 
126
 
 
127
    def test_formatted_help_text(self):
 
128
        """Help text should be plain text by default."""
 
129
        class cmd_Demo(commands.Command):
 
130
            __doc__ = """A sample command.
 
131
 
 
132
            :Examples:
 
133
                Example 1::
 
134
 
 
135
                    cmd arg1
 
136
 
 
137
                Example 2::
 
138
 
 
139
                    cmd arg2
 
140
 
 
141
                A code block follows.
 
142
 
 
143
                ::
 
144
 
 
145
                    brz Demo something
 
146
            """
 
147
        cmd = cmd_Demo()
 
148
        helptext = cmd.get_help_text()
 
149
        self.assertEqualDiff('''\
 
150
Purpose: A sample command.
 
151
Usage:   brz Demo
 
152
 
 
153
Options:
 
154
  -h, --help     Show help message.
 
155
  -q, --quiet    Only display errors and warnings.
 
156
  --usage        Show usage message and options.
 
157
  -v, --verbose  Display more information.
 
158
 
 
159
Examples:
 
160
    Example 1:
 
161
 
 
162
        cmd arg1
 
163
 
 
164
    Example 2:
 
165
 
 
166
        cmd arg2
 
167
 
 
168
    A code block follows.
 
169
 
 
170
        brz Demo something
 
171
 
 
172
''',
 
173
                                         helptext)
 
174
        helptext = cmd.get_help_text(plain=False)
 
175
        self.assertEqualDiff('''\
 
176
:Purpose: A sample command.
 
177
:Usage:   brz Demo
 
178
 
 
179
:Options:
 
180
  -h, --help     Show help message.
 
181
  -q, --quiet    Only display errors and warnings.
 
182
  --usage        Show usage message and options.
 
183
  -v, --verbose  Display more information.
 
184
 
 
185
:Examples:
 
186
    Example 1::
 
187
 
 
188
        cmd arg1
 
189
 
 
190
    Example 2::
 
191
 
 
192
        cmd arg2
 
193
 
 
194
    A code block follows.
 
195
 
 
196
    ::
 
197
 
 
198
        brz Demo something
 
199
 
 
200
''',
 
201
                             helptext)
 
202
 
 
203
    def test_concise_help_text(self):
 
204
        """Concise help text excludes the descriptive sections."""
 
205
        class cmd_Demo(commands.Command):
 
206
            __doc__ = """A sample command.
 
207
 
 
208
            Blah blah blah.
 
209
 
 
210
            :Examples:
 
211
                Example 1::
 
212
 
 
213
                    cmd arg1
 
214
            """
 
215
        cmd = cmd_Demo()
 
216
        helptext = cmd.get_help_text()
 
217
        self.assertEqualDiff('''\
 
218
Purpose: A sample command.
 
219
Usage:   brz Demo
 
220
 
 
221
Options:
 
222
  -h, --help     Show help message.
 
223
  -q, --quiet    Only display errors and warnings.
 
224
  --usage        Show usage message and options.
 
225
  -v, --verbose  Display more information.
 
226
 
 
227
Description:
 
228
  Blah blah blah.
 
229
 
 
230
Examples:
 
231
    Example 1:
 
232
 
 
233
        cmd arg1
 
234
 
 
235
''',
 
236
                             helptext)
 
237
        helptext = cmd.get_help_text(verbose=False)
 
238
        self.assertEqualDiff('''\
 
239
Purpose: A sample command.
 
240
Usage:   brz Demo
 
241
 
 
242
Options:
 
243
  -h, --help     Show help message.
 
244
  -q, --quiet    Only display errors and warnings.
 
245
  --usage        Show usage message and options.
 
246
  -v, --verbose  Display more information.
 
247
 
 
248
See brz help Demo for more details and examples.
 
249
 
 
250
''',
 
251
                             helptext)
 
252
 
 
253
    def test_help_custom_section_ordering(self):
 
254
        """Custom descriptive sections should remain in the order given."""
 
255
        class cmd_Demo(commands.Command):
 
256
            __doc__ = """\
 
257
A sample command.
 
258
 
 
259
Blah blah blah.
 
260
 
 
261
:Formats:
 
262
  Interesting stuff about formats.
 
263
 
 
264
:Examples:
 
265
  Example 1::
 
266
 
 
267
    cmd arg1
 
268
 
 
269
:Tips:
 
270
  Clever things to keep in mind.
 
271
"""
 
272
        cmd = cmd_Demo()
 
273
        helptext = cmd.get_help_text()
 
274
        self.assertEqualDiff('''\
 
275
Purpose: A sample command.
 
276
Usage:   brz Demo
 
277
 
 
278
Options:
 
279
  -h, --help     Show help message.
 
280
  -q, --quiet    Only display errors and warnings.
 
281
  --usage        Show usage message and options.
 
282
  -v, --verbose  Display more information.
 
283
 
 
284
Description:
 
285
  Blah blah blah.
 
286
 
 
287
Formats:
 
288
  Interesting stuff about formats.
 
289
 
 
290
Examples:
 
291
  Example 1:
 
292
 
 
293
    cmd arg1
 
294
 
 
295
Tips:
 
296
  Clever things to keep in mind.
 
297
 
 
298
''',
 
299
                             helptext)
 
300
 
 
301
    def test_help_text_custom_usage(self):
 
302
        """Help text may contain a custom usage section."""
 
303
        class cmd_Demo(commands.Command):
 
304
            __doc__ = """A sample command.
 
305
 
 
306
            :Usage:
 
307
                cmd Demo [opts] args
 
308
 
 
309
                cmd Demo -h
 
310
 
 
311
            Blah blah blah.
 
312
            """
 
313
        cmd = cmd_Demo()
 
314
        helptext = cmd.get_help_text()
 
315
        self.assertEqualDiff('''\
 
316
Purpose: A sample command.
 
317
Usage:
 
318
    cmd Demo [opts] args
 
319
 
 
320
    cmd Demo -h
 
321
 
 
322
 
 
323
Options:
 
324
  -h, --help     Show help message.
 
325
  -q, --quiet    Only display errors and warnings.
 
326
  --usage        Show usage message and options.
 
327
  -v, --verbose  Display more information.
 
328
 
 
329
Description:
 
330
  Blah blah blah.
 
331
 
 
332
''',
 
333
                             helptext)
 
334
 
 
335
 
 
336
class ZzzTranslationsForDoc(ZzzTranslations):
 
337
 
 
338
    _section_pat = re.compile(':\\w+:\\n\\s+')
 
339
    _indent_pat = re.compile('\\s+')
 
340
 
 
341
    def zzz(self, s):
 
342
        m = self._section_pat.match(s)
 
343
        if m is None:
 
344
            m = self._indent_pat.match(s)
 
345
        if m:
 
346
            return u'%szz{{%s}}' % (m.group(0), s[m.end():])
 
347
        return u'zz{{%s}}' % s
 
348
 
 
349
 
 
350
class TestCommandHelpI18n(tests.TestCase):
 
351
    """Tests for help on translated commands."""
 
352
 
 
353
    def setUp(self):
 
354
        super(TestCommandHelpI18n, self).setUp()
 
355
        self.overrideAttr(i18n, '_translations', ZzzTranslationsForDoc())
 
356
 
 
357
    def assertCmdHelp(self, expected, cmd):
 
358
        self.assertEqualDiff(textwrap.dedent(expected), cmd.get_help_text())
 
359
 
 
360
    def test_command_help_includes_see_also(self):
 
361
        class cmd_WithSeeAlso(commands.Command):
 
362
            __doc__ = """A sample command."""
 
363
            _see_also = ['foo', 'bar']
 
364
        self.assertCmdHelp('''\
 
365
zz{{:Purpose: zz{{A sample command.}}
 
366
}}zz{{:Usage:   brz WithSeeAlso
 
367
}}
 
368
zz{{:Options:
 
369
  -h, --help     zz{{Show help message.}}
 
370
  -q, --quiet    zz{{Only display errors and warnings.}}
 
371
  --usage        zz{{Show usage message and options.}}
 
372
  -v, --verbose  zz{{Display more information.}}
 
373
}}
 
374
zz{{:See also: bar, foo}}
 
375
''',
 
376
                           cmd_WithSeeAlso())
 
377
 
 
378
    def test_get_help_text(self):
 
379
        """Commands have a get_help_text method which returns their help."""
 
380
        class cmd_Demo(commands.Command):
 
381
            __doc__ = """A sample command."""
 
382
        self.assertCmdHelp('''\
 
383
zz{{:Purpose: zz{{A sample command.}}
 
384
}}zz{{:Usage:   brz Demo
 
385
}}
 
386
zz{{:Options:
 
387
  -h, --help     zz{{Show help message.}}
 
388
  -q, --quiet    zz{{Only display errors and warnings.}}
 
389
  --usage        zz{{Show usage message and options.}}
 
390
  -v, --verbose  zz{{Display more information.}}
 
391
}}
 
392
''',
 
393
                           cmd_Demo())
 
394
 
 
395
    def test_command_with_additional_see_also(self):
 
396
        class cmd_WithSeeAlso(commands.Command):
 
397
            __doc__ = """A sample command."""
 
398
            _see_also = ['foo', 'bar']
 
399
        cmd = cmd_WithSeeAlso()
 
400
        helptext = cmd.get_help_text(['gam'])
 
401
        self.assertEndsWith(
 
402
            helptext, '''\
 
403
  -h, --help     zz{{Show help message.}}
 
404
  -q, --quiet    zz{{Only display errors and warnings.}}
 
405
  --usage        zz{{Show usage message and options.}}
 
406
  -v, --verbose  zz{{Display more information.}}
 
407
}}
 
408
zz{{:See also: bar, foo, gam}}
 
409
''')
 
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(
 
417
            helptext, '''\
 
418
zz{{:Options:
 
419
  -h, --help     zz{{Show help message.}}
 
420
  -q, --quiet    zz{{Only display errors and warnings.}}
 
421
  --usage        zz{{Show usage message and options.}}
 
422
  -v, --verbose  zz{{Display more information.}}
 
423
}}
 
424
zz{{:See also: gam}}
 
425
''')
 
426
 
 
427
 
 
428
    def test_help_custom_section_ordering(self):
 
429
        """Custom descriptive sections should remain in the order given."""
 
430
        # The help formatter expect the class name to start with 'cmd_'
 
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
            """
 
447
        self.assertCmdHelp('''\
 
448
zz{{:Purpose: zz{{A sample command.}}
 
449
}}zz{{:Usage:   brz Demo
 
450
}}
 
451
zz{{:Options:
 
452
  -h, --help     zz{{Show help message.}}
 
453
  -q, --quiet    zz{{Only display errors and warnings.}}
 
454
  --usage        zz{{Show usage message and options.}}
 
455
  -v, --verbose  zz{{Display more information.}}
 
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
''',
 
472
                           cmd_Demo())
 
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
            """
 
486
        self.assertCmdHelp('''\
 
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:
 
495
  -h, --help     zz{{Show help message.}}
 
496
  -q, --quiet    zz{{Only display errors and warnings.}}
 
497
  --usage        zz{{Show usage message and options.}}
 
498
  -v, --verbose  zz{{Display more information.}}
 
499
}}
 
500
Description:
 
501
  zz{{zz{{Blah blah blah.}}
 
502
 
 
503
}}
 
504
''',
 
505
                           cmd_Demo())
 
506
 
 
507
 
 
508
class TestHelp(tests.TestCase):
 
509
 
 
510
    def setUp(self):
 
511
        super(TestHelp, self).setUp()
 
512
        commands.install_bzr_command_hooks()
 
513
 
 
514
 
 
515
class TestRegisteredTopic(TestHelp):
 
516
    """Tests for the RegisteredTopic class."""
 
517
 
 
518
    def test_contruct(self):
 
519
        """Construction takes the help topic name for the registered item."""
 
520
        # validate our test
 
521
        self.assertTrue('basic' in help_topics.topic_registry)
 
522
        topic = help_topics.RegisteredTopic('basic')
 
523
        self.assertEqual('basic', topic.topic)
 
524
 
 
525
    def test_get_help_text(self):
 
526
        """RegisteredTopic returns the get_detail results for get_help_text."""
 
527
        topic = help_topics.RegisteredTopic('commands')
 
528
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
 
529
                         topic.get_help_text())
 
530
 
 
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
 
 
538
    def test_get_help_text_loaded_from_file(self):
 
539
        # Pick a known topic stored in an external file
 
540
        topic = help_topics.RegisteredTopic('authentication')
 
541
        self.assertStartsWith(topic.get_help_text(),
 
542
            'Authentication Settings\n'
 
543
            '=======================\n'
 
544
            '\n')
 
545
 
 
546
    def test_get_help_topic(self):
 
547
        """The help topic for RegisteredTopic is its topic from construction."""
 
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
 
 
553
 
 
554
class TestTopicIndex(TestHelp):
 
555
    """Tests for the HelpTopicIndex class."""
 
556
 
 
557
    def test_default_constructable(self):
 
558
        index = help_topics.HelpTopicIndex()
 
559
 
 
560
    def test_get_topics_None(self):
 
561
        """Searching for None returns the basic help topic."""
 
562
        index = help_topics.HelpTopicIndex()
 
563
        topics = index.get_topics(None)
 
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."""
 
570
        index = help_topics.HelpTopicIndex()
 
571
        topics = index.get_topics('topics')
 
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 []."""
 
578
        index = help_topics.HelpTopicIndex()
 
579
        self.assertEqual([], index.get_topics('nothing by this name'))
 
580
 
 
581
    def test_prefix(self):
 
582
        """TopicIndex has a prefix of ''."""
 
583
        index = help_topics.HelpTopicIndex()
 
584
        self.assertEqual('', index.prefix)
 
585
 
 
586
 
 
587
class TestConfigOptionIndex(TestHelp):
 
588
    """Tests for the HelpCommandIndex class."""
 
589
 
 
590
    def setUp(self):
 
591
        super(TestConfigOptionIndex, self).setUp()
 
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)
 
609
        self.assertEqual('default_format', opt.name)
 
610
 
 
611
 
 
612
class TestCommandIndex(TestHelp):
 
613
    """Tests for the HelpCommandIndex class."""
 
614
 
 
615
    def test_default_constructable(self):
 
616
        index = commands.HelpCommandIndex()
 
617
 
 
618
    def test_get_topics_None(self):
 
619
        """Searching for None returns an empty list."""
 
620
        index = commands.HelpCommandIndex()
 
621
        self.assertEqual([], index.get_topics(None))
 
622
 
 
623
    def test_get_topics_rocks(self):
 
624
        """Searching for 'rocks' returns the cmd_rocks command instance."""
 
625
        index = commands.HelpCommandIndex()
 
626
        topics = index.get_topics('rocks')
 
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 []."""
 
632
        index = commands.HelpCommandIndex()
 
633
        self.assertEqual([], index.get_topics('nothing by this name'))
 
634
 
 
635
    def test_prefix(self):
 
636
        """CommandIndex has a prefix of 'commands/'."""
 
637
        index = commands.HelpCommandIndex()
 
638
        self.assertEqual('commands/', index.prefix)
 
639
 
 
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
 
 
647
 
 
648
class TestHelpIndices(tests.TestCase):
 
649
    """Tests for the HelpIndices class."""
 
650
 
 
651
    def test_default_search_path(self):
 
652
        """The default search path should include internal indexs."""
 
653
        indices = help.HelpIndices()
 
654
        self.assertEqual(4, len(indices.search_path))
 
655
        # help topics should be searched in first.
 
656
        self.assertIsInstance(indices.search_path[0],
 
657
                              help_topics.HelpTopicIndex)
 
658
        # with commands being search second.
 
659
        self.assertIsInstance(indices.search_path[1],
 
660
                              commands.HelpCommandIndex)
 
661
        # plugins are a third index.
 
662
        self.assertIsInstance(indices.search_path[2],
 
663
                              plugin.PluginsHelpIndex)
 
664
        # config options are a fourth index
 
665
        self.assertIsInstance(indices.search_path[3],
 
666
                              help_topics.ConfigOptionHelpIndex)
 
667
 
 
668
    def test_search_for_unknown_topic_raises(self):
 
669
        """Searching for an unknown topic should raise NoHelpTopic."""
 
670
        indices = help.HelpIndices()
 
671
        indices.search_path = []
 
672
        error = self.assertRaises(help.NoHelpTopic, indices.search, 'foo')
 
673
        self.assertEqual('foo', error.topic)
 
674
 
 
675
    def test_search_calls_get_topic(self):
 
676
        """Searching should call get_topics in all indexes in order."""
 
677
        calls = []
 
678
        class RecordingIndex(object):
 
679
            def __init__(self, name):
 
680
                self.prefix = name
 
681
            def get_topics(self, topic):
 
682
                calls.append(('get_topics', self.prefix, topic))
 
683
                return ['something']
 
684
        index = help.HelpIndices()
 
685
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
 
686
        # try with None
 
687
        index.search(None)
 
688
        self.assertEqual([
 
689
            ('get_topics', '1', None),
 
690
            ('get_topics', '2', None),
 
691
            ],
 
692
            calls)
 
693
        # and with a string
 
694
        del calls[:]
 
695
        index.search('bar')
 
696
        self.assertEqual([
 
697
            ('get_topics', '1', 'bar'),
 
698
            ('get_topics', '2', 'bar'),
 
699
            ],
 
700
            calls)
 
701
 
 
702
    def test_search_returns_index_and_results(self):
 
703
        """Searching should return help topics with their index"""
 
704
        class CannedIndex(object):
 
705
            def __init__(self, prefix, search_result):
 
706
                self.prefix = prefix
 
707
                self.result = search_result
 
708
            def get_topics(self, topic):
 
709
                return self.result
 
710
        index = help.HelpIndices()
 
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))
 
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)