/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5177.1.1 by Vincent Ladeuil
Manually assign docstrings to command objects, so that they work with python -OO
1
# Copyright (C) 2007-2010 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
17
"""Unit tests for the bzrlib.help module."""
18
19
from cStringIO import StringIO
20
21
from bzrlib 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,
2432.1.5 by Robert Collins
Initial stub for topic searching.
24
    errors,
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
25
    help,
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
26
    help_topics,
2432.1.24 by Robert Collins
Add plugins as a help index.
27
    plugin,
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
28
    tests,
29
    )
30
31
4119.3.15 by Robert Collins
And fix tests for bzr help.
32
class TestHelp(tests.TestCase):
33
34
    def setUp(self):
35
        tests.TestCase.setUp(self)
36
        commands.install_bzr_command_hooks()
37
38
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
39
class TestCommandHelp(tests.TestCase):
40
    """Tests for help on commands."""
41
42
    def test_command_help_includes_see_also(self):
43
        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
44
            __doc__ = """A sample command."""
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
45
            _see_also = ['foo', 'bar']
46
        cmd = cmd_WithSeeAlso()
2432.1.12 by Robert Collins
Relocate command help onto Command.
47
        helptext = cmd.get_help_text()
2425.1.3 by Robert Collins
Python 2.4 compatability change for the new help see-also tests.
48
        self.assertEndsWith(
2432.1.12 by Robert Collins
Relocate command help onto Command.
49
            helptext,
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
50
            '  -v, --verbose  Display more information.\n'
51
            '  -q, --quiet    Only display errors and warnings.\n'
52
            '  -h, --help     Show help message.\n'
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
53
            '\n'
2425.1.3 by Robert Collins
Python 2.4 compatability change for the new help see-also tests.
54
            'See also: bar, foo\n')
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
55
2432.1.12 by Robert Collins
Relocate command help onto Command.
56
    def test_get_help_text(self):
57
        """Commands have a get_help_text method which returns their help."""
58
        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
59
            __doc__ = """A sample command."""
2432.1.12 by Robert Collins
Relocate command help onto Command.
60
        cmd = cmd_Demo()
61
        helptext = cmd.get_help_text()
2666.1.4 by Ian Clatworthy
Add help formatting tests
62
        self.assertStartsWith(helptext,
63
            'Purpose: A sample command.\n'
64
            'Usage:   bzr Demo')
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
65
        self.assertEndsWith(helptext,
66
            '  -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.
67
68
    def test_command_with_additional_see_also(self):
69
        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
70
            __doc__ = """A sample command."""
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
71
            _see_also = ['foo', 'bar']
72
        cmd = cmd_WithSeeAlso()
73
        helptext = cmd.get_help_text(['gam'])
74
        self.assertEndsWith(
75
            helptext,
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
76
            '  -v, --verbose  Display more information.\n'
77
            '  -q, --quiet    Only display errors and warnings.\n'
78
            '  -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.
79
            '\n'
80
            'See also: bar, foo, gam\n')
81
82
    def test_command_only_additional_see_also(self):
83
        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
84
            __doc__ = """A sample command."""
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
85
        cmd = cmd_WithSeeAlso()
86
        helptext = cmd.get_help_text(['gam'])
87
        self.assertEndsWith(
88
            helptext,
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
89
            '  -v, --verbose  Display more information.\n'
90
            '  -q, --quiet    Only display errors and warnings.\n'
91
            '  -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.
92
            '\n'
93
            'See also: gam\n')
2432.1.28 by Robert Collins
Add a get_help_topic method to commands.Command.
94
95
    def test_get_help_topic(self):
96
        """The help topic for a Command is its name()."""
97
        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
98
            __doc__ = """A sample command."""
2432.1.28 by Robert Collins
Add a get_help_topic method to commands.Command.
99
        cmd = cmd_foo_bar()
100
        self.assertEqual(cmd.name(), cmd.get_help_topic())
2666.1.4 by Ian Clatworthy
Add help formatting tests
101
102
    def test_formatted_help_text(self):
103
        """Help text should be plain text by default."""
104
        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
105
            __doc__ = """A sample command.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
106
2666.1.4 by Ian Clatworthy
Add help formatting tests
107
            :Examples:
108
                Example 1::
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
109
2666.1.4 by Ian Clatworthy
Add help formatting tests
110
                    cmd arg1
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
111
2666.1.4 by Ian Clatworthy
Add help formatting tests
112
                Example 2::
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
113
2666.1.4 by Ian Clatworthy
Add help formatting tests
114
                    cmd arg2
5215.2.3 by John Szakmeister
Test help formatting of standalone code blocks.
115
116
                A code block follows.
117
118
                ::
119
120
                    bzr Demo something
2666.1.4 by Ian Clatworthy
Add help formatting tests
121
            """
122
        cmd = cmd_Demo()
123
        helptext = cmd.get_help_text()
124
        self.assertEquals(
125
            helptext,
126
            'Purpose: A sample command.\n'
127
            'Usage:   bzr Demo\n'
128
            '\n'
129
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
130
            '  --usage        Show usage message and options.\n'
5193.3.9 by Parth Malwankar
updated test_help to have --null (which is a standard option now)
131
            '  -0, --null     Use an ASCII NUL (\\0) separator rather than a newline.\n'
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
132
            '  -v, --verbose  Display more information.\n'
133
            '  -q, --quiet    Only display errors and warnings.\n'
134
            '  -h, --help     Show help message.\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
135
            '\n'
136
            'Examples:\n'
137
            '    Example 1:\n'
138
            '\n'
139
            '        cmd arg1\n'
140
            '\n'
141
            '    Example 2:\n'
142
            '\n'
143
            '        cmd arg2\n'
5215.2.3 by John Szakmeister
Test help formatting of standalone code blocks.
144
            '\n'
145
            '    A code block follows.\n'
146
            '\n'
147
            '        bzr Demo something\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
148
            '\n')
149
        helptext = cmd.get_help_text(plain=False)
150
        self.assertEquals(helptext,
151
            ':Purpose: A sample command.\n'
152
            ':Usage:   bzr Demo\n'
153
            '\n'
154
            ':Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
155
            '  --usage        Show usage message and options.\n'
5193.3.9 by Parth Malwankar
updated test_help to have --null (which is a standard option now)
156
            '  -0, --null     Use an ASCII NUL (\\0) separator rather than a newline.\n'
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
157
            '  -v, --verbose  Display more information.\n'
158
            '  -q, --quiet    Only display errors and warnings.\n'
159
            '  -h, --help     Show help message.\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
160
            '\n'
161
            ':Examples:\n'
162
            '    Example 1::\n'
163
            '\n'
164
            '        cmd arg1\n'
165
            '\n'
166
            '    Example 2::\n'
167
            '\n'
168
            '        cmd arg2\n'
5215.2.3 by John Szakmeister
Test help formatting of standalone code blocks.
169
            '\n'
170
            '    A code block follows.\n'
171
            '\n'
172
            '    ::\n'
173
            '\n'
174
            '        bzr Demo something\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
175
            '\n')
176
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
177
    def test_concise_help_text(self):
178
        """Concise help text excludes the descriptive sections."""
179
        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
180
            __doc__ = """A sample command.
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
181
 
182
            Blah blah blah.
183
184
            :Examples:
185
                Example 1::
186
 
187
                    cmd arg1
188
            """
189
        cmd = cmd_Demo()
190
        helptext = cmd.get_help_text()
191
        self.assertEqualDiff(
192
            helptext,
193
            'Purpose: A sample command.\n'
194
            'Usage:   bzr Demo\n'
195
            '\n'
196
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
197
            '  --usage        Show usage message and options.\n'
5193.3.9 by Parth Malwankar
updated test_help to have --null (which is a standard option now)
198
            '  -0, --null     Use an ASCII NUL (\\0) separator rather than a newline.\n'
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
199
            '  -v, --verbose  Display more information.\n'
200
            '  -q, --quiet    Only display errors and warnings.\n'
201
            '  -h, --help     Show help message.\n'
202
            '\n'
203
            'Description:\n'
204
            '  Blah blah blah.\n'
205
            '\n'
206
            'Examples:\n'
207
            '    Example 1:\n'
208
            '\n'
209
            '        cmd arg1\n'
210
            '\n')
211
        helptext = cmd.get_help_text(verbose=False)
212
        self.assertEquals(helptext,
213
            'Purpose: A sample command.\n'
214
            'Usage:   bzr Demo\n'
215
            '\n'
216
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
217
            '  --usage        Show usage message and options.\n'
5193.3.9 by Parth Malwankar
updated test_help to have --null (which is a standard option now)
218
            '  -0, --null     Use an ASCII NUL (\\0) separator rather than a newline.\n'
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
219
            '  -v, --verbose  Display more information.\n'
220
            '  -q, --quiet    Only display errors and warnings.\n'
221
            '  -h, --help     Show help message.\n'
222
            '\n'
3984.4.5 by Ian Clatworthy
help xxx is full help; xxx -h is concise help
223
            'See bzr help Demo for more details and examples.\n'
3984.4.2 by Ian Clatworthy
make help on commands concise by default
224
            '\n')
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
225
226
    def test_help_custom_section_ordering(self):
227
        """Custom descriptive sections should remain in the order given."""
228
        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
229
            __doc__ = """A sample command.
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
230
 
231
            Blah blah blah.
232
233
            :Formats:
234
              Interesting stuff about formats.
235
236
            :Examples:
237
              Example 1::
238
 
239
                cmd arg1
240
241
            :Tips:
242
              Clever things to keep in mind.
243
            """
244
        cmd = cmd_Demo()
245
        helptext = cmd.get_help_text()
246
        self.assertEqualDiff(
247
            helptext,
248
            'Purpose: A sample command.\n'
249
            'Usage:   bzr Demo\n'
250
            '\n'
251
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
252
            '  --usage        Show usage message and options.\n'
5193.3.9 by Parth Malwankar
updated test_help to have --null (which is a standard option now)
253
            '  -0, --null     Use an ASCII NUL (\\0) separator rather than a newline.\n'
3984.4.1 by Ian Clatworthy
get_help_text() verbose parameter & keep custom sections ordered
254
            '  -v, --verbose  Display more information.\n'
255
            '  -q, --quiet    Only display errors and warnings.\n'
256
            '  -h, --help     Show help message.\n'
257
            '\n'
258
            'Description:\n'
259
            '  Blah blah blah.\n'
260
            '\n'
261
            'Formats:\n'
262
            '  Interesting stuff about formats.\n'
263
            '\n'
264
            'Examples:\n'
265
            '  Example 1:\n'
266
            '\n'
267
            '    cmd arg1\n'
268
            '\n'
269
            'Tips:\n'
270
            '  Clever things to keep in mind.\n'
271
            '\n')
272
2666.1.4 by Ian Clatworthy
Add help formatting tests
273
    def test_help_text_custom_usage(self):
274
        """Help text may contain a custom usage section."""
275
        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
276
            __doc__ = """A sample command.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
277
2666.1.4 by Ian Clatworthy
Add help formatting tests
278
            :Usage:
279
                cmd Demo [opts] args
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
280
2666.1.4 by Ian Clatworthy
Add help formatting tests
281
                cmd Demo -h
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
282
2666.1.4 by Ian Clatworthy
Add help formatting tests
283
            Blah blah blah.
284
            """
285
        cmd = cmd_Demo()
286
        helptext = cmd.get_help_text()
287
        self.assertEquals(helptext,
288
            'Purpose: A sample command.\n'
289
            'Usage:\n'
290
            '    cmd Demo [opts] args\n'
291
            '\n'
292
            '    cmd Demo -h\n'
293
            '\n'
294
            '\n'
295
            'Options:\n'
3984.4.6 by Ian Clatworthy
Show usage on --usage, not -h
296
            '  --usage        Show usage message and options.\n'
5193.3.9 by Parth Malwankar
updated test_help to have --null (which is a standard option now)
297
            '  -0, --null     Use an ASCII NUL (\\0) separator rather than a newline.\n'
2768.1.6 by Ian Clatworthy
Fix existing option and help tests
298
            '  -v, --verbose  Display more information.\n'
299
            '  -q, --quiet    Only display errors and warnings.\n'
300
            '  -h, --help     Show help message.\n'
2666.1.4 by Ian Clatworthy
Add help formatting tests
301
            '\n'
302
            'Description:\n'
303
            '  Blah blah blah.\n\n')
304
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
305
4119.3.15 by Robert Collins
And fix tests for bzr help.
306
class TestRegisteredTopic(TestHelp):
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
307
    """Tests for the RegisteredTopic class."""
308
309
    def test_contruct(self):
310
        """Construction takes the help topic name for the registered item."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
311
        # validate our test
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
312
        self.assertTrue('basic' in help_topics.topic_registry)
313
        topic = help_topics.RegisteredTopic('basic')
314
        self.assertEqual('basic', topic.topic)
315
2432.1.10 by Robert Collins
Add get_help_text() to RegisteredTopic to get the help as a string.
316
    def test_get_help_text(self):
317
        """A RegisteredTopic returns the get_detail results for get_help_text."""
318
        topic = help_topics.RegisteredTopic('commands')
319
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
320
            topic.get_help_text())
321
2432.1.22 by Robert Collins
Teach RegisteredTopic to support the additional_see_also list of related help terms.
322
    def test_get_help_text_with_additional_see_also(self):
323
        topic = help_topics.RegisteredTopic('commands')
324
        self.assertEndsWith(
325
            topic.get_help_text(['foo', 'bar']),
326
            '\n'
327
            'See also: bar, foo\n')
328
3089.3.5 by Ian Clatworthy
add test for loading help from a file
329
    def test_get_help_text_loaded_from_file(self):
330
        # Pick a known topic stored in an external file
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
331
        topic = help_topics.RegisteredTopic('authentication')
3089.3.5 by Ian Clatworthy
add test for loading help from a file
332
        self.assertStartsWith(topic.get_help_text(),
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
333
            'Authentication Settings\n'
334
            '=======================\n'
3089.3.5 by Ian Clatworthy
add test for loading help from a file
335
            '\n')
336
2432.1.27 by Robert Collins
Add a get_help_topic method to RegisteredTopic.
337
    def test_get_help_topic(self):
338
        """The help topic for a RegisteredTopic is its topic from construction."""
339
        topic = help_topics.RegisteredTopic('foobar')
340
        self.assertEqual('foobar', topic.get_help_topic())
341
        topic = help_topics.RegisteredTopic('baz')
342
        self.assertEqual('baz', topic.get_help_topic())
343
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
344
4119.3.15 by Robert Collins
And fix tests for bzr help.
345
class TestTopicIndex(TestHelp):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
346
    """Tests for the HelpTopicIndex class."""
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
347
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
348
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
349
        index = help_topics.HelpTopicIndex()
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
350
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
351
    def test_get_topics_None(self):
352
        """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.
353
        index = help_topics.HelpTopicIndex()
354
        topics = index.get_topics(None)
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
355
        self.assertEqual(1, len(topics))
356
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
357
        self.assertEqual('basic', topics[0].topic)
358
359
    def test_get_topics_topics(self):
360
        """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.
361
        index = help_topics.HelpTopicIndex()
362
        topics = index.get_topics('topics')
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
363
        self.assertEqual(1, len(topics))
364
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
365
        self.assertEqual('topics', topics[0].topic)
366
367
    def test_get_topics_no_topic(self):
368
        """Searching for something not registered returns []."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
369
        index = help_topics.HelpTopicIndex()
370
        self.assertEqual([], index.get_topics('nothing by this name'))
371
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
372
    def test_prefix(self):
373
        """TopicIndex has a prefix of ''."""
374
        index = help_topics.HelpTopicIndex()
375
        self.assertEqual('', index.prefix)
376
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
377
4119.3.15 by Robert Collins
And fix tests for bzr help.
378
class TestCommandIndex(TestHelp):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
379
    """Tests for the HelpCommandIndex class."""
2432.1.2 by Robert Collins
Add a HelpCommandContext class for help from commands.
380
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
381
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
382
        index = commands.HelpCommandIndex()
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
383
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
384
    def test_get_topics_None(self):
385
        """Searching for None returns an empty list."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
386
        index = commands.HelpCommandIndex()
387
        self.assertEqual([], index.get_topics(None))
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
388
389
    def test_get_topics_rocks(self):
390
        """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.
391
        index = commands.HelpCommandIndex()
392
        topics = index.get_topics('rocks')
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
393
        self.assertEqual(1, len(topics))
394
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
395
396
    def test_get_topics_no_topic(self):
397
        """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.
398
        index = commands.HelpCommandIndex()
399
        self.assertEqual([], index.get_topics('nothing by this name'))
400
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
401
    def test_prefix(self):
402
        """CommandIndex has a prefix of 'commands/'."""
403
        index = commands.HelpCommandIndex()
404
        self.assertEqual('commands/', index.prefix)
405
2432.1.18 by Robert Collins
Add support for doing bzr help commands/COMMANDNAME.
406
    def test_get_topic_with_prefix(self):
407
        """Searching for commands/rocks returns the rocks command object."""
408
        index = commands.HelpCommandIndex()
409
        topics = index.get_topics('commands/rocks')
410
        self.assertEqual(1, len(topics))
411
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
412
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
413
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
414
class TestHelpIndices(tests.TestCase):
415
    """Tests for the HelpIndices class."""
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
416
417
    def test_default_search_path(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
418
        """The default search path should include internal indexs."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
419
        indices = help.HelpIndices()
2432.1.24 by Robert Collins
Add plugins as a help index.
420
        self.assertEqual(3, len(indices.search_path))
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
421
        # help topics should be searched in first.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
422
        self.assertIsInstance(indices.search_path[0],
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
423
            help_topics.HelpTopicIndex)
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
424
        # with commands being search second.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
425
        self.assertIsInstance(indices.search_path[1],
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
426
            commands.HelpCommandIndex)
2432.1.24 by Robert Collins
Add plugins as a help index.
427
        # and plugins are a third index.
428
        self.assertIsInstance(indices.search_path[2],
429
            plugin.PluginsHelpIndex)
2432.1.5 by Robert Collins
Initial stub for topic searching.
430
431
    def test_search_for_unknown_topic_raises(self):
432
        """Searching for an unknown topic should raise NoHelpTopic."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
433
        indices = help.HelpIndices()
434
        indices.search_path = []
435
        error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
2432.1.5 by Robert Collins
Initial stub for topic searching.
436
        self.assertEqual('foo', error.topic)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
437
438
    def test_search_calls_get_topic(self):
439
        """Searching should call get_topics in all indexes in order."""
440
        calls = []
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
441
        class RecordingIndex(object):
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
442
            def __init__(self, name):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
443
                self.prefix = name
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
444
            def get_topics(self, topic):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
445
                calls.append(('get_topics', self.prefix, topic))
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
446
                return ['something']
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
447
        index = help.HelpIndices()
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
448
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
449
        # try with None
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
450
        index.search(None)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
451
        self.assertEqual([
452
            ('get_topics', '1', None),
453
            ('get_topics', '2', None),
454
            ],
455
            calls)
456
        # and with a string
457
        del calls[:]
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
458
        index.search('bar')
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
459
        self.assertEqual([
460
            ('get_topics', '1', 'bar'),
461
            ('get_topics', '2', 'bar'),
462
            ],
463
            calls)
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
464
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
465
    def test_search_returns_index_and_results(self):
466
        """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.
467
        class CannedIndex(object):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
468
            def __init__(self, prefix, search_result):
469
                self.prefix = prefix
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
470
                self.result = search_result
471
            def get_topics(self, topic):
472
                return self.result
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
473
        index = help.HelpIndices()
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
474
        index_one = CannedIndex('1', ['a'])
475
        index_two = CannedIndex('2', ['b', 'c'])
476
        index.search_path = [index_one, index_two]
477
        self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
478
            index.search(None))
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
479
480
    def test_search_checks_for_duplicate_prefixes(self):
481
        """Its an error when there are multiple indices with the same prefix."""
482
        indices = help.HelpIndices()
483
        indices.search_path = [help_topics.HelpTopicIndex(),
484
            help_topics.HelpTopicIndex()]
485
        self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)