~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_help.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 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 bzrlib.help module."""
 
18
 
 
19
from cStringIO import StringIO
 
20
 
 
21
from bzrlib import (
 
22
    builtins,
 
23
    commands,
 
24
    errors,
 
25
    help,
 
26
    help_topics,
 
27
    plugin,
 
28
    tests,
 
29
    )
 
30
 
 
31
 
 
32
class TestHelp(tests.TestCase):
 
33
 
 
34
    def setUp(self):
 
35
        tests.TestCase.setUp(self)
 
36
        commands.install_bzr_command_hooks()
 
37
 
 
38
 
 
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):
 
44
            __doc__ = """A sample command."""
 
45
            _see_also = ['foo', 'bar']
 
46
        cmd = cmd_WithSeeAlso()
 
47
        helptext = cmd.get_help_text()
 
48
        self.assertEndsWith(
 
49
            helptext,
 
50
            '  -v, --verbose  Display more information.\n'
 
51
            '  -q, --quiet    Only display errors and warnings.\n'
 
52
            '  -h, --help     Show help message.\n'
 
53
            '\n'
 
54
            'See also: bar, foo\n')
 
55
 
 
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):
 
59
            __doc__ = """A sample command."""
 
60
        cmd = cmd_Demo()
 
61
        helptext = cmd.get_help_text()
 
62
        self.assertStartsWith(helptext,
 
63
            'Purpose: A sample command.\n'
 
64
            'Usage:   bzr Demo')
 
65
        self.assertEndsWith(helptext,
 
66
            '  -h, --help     Show help message.\n\n')
 
67
 
 
68
    def test_command_with_additional_see_also(self):
 
69
        class cmd_WithSeeAlso(commands.Command):
 
70
            __doc__ = """A sample command."""
 
71
            _see_also = ['foo', 'bar']
 
72
        cmd = cmd_WithSeeAlso()
 
73
        helptext = cmd.get_help_text(['gam'])
 
74
        self.assertEndsWith(
 
75
            helptext,
 
76
            '  -v, --verbose  Display more information.\n'
 
77
            '  -q, --quiet    Only display errors and warnings.\n'
 
78
            '  -h, --help     Show help message.\n'
 
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):
 
84
            __doc__ = """A sample command."""
 
85
        cmd = cmd_WithSeeAlso()
 
86
        helptext = cmd.get_help_text(['gam'])
 
87
        self.assertEndsWith(
 
88
            helptext,
 
89
            '  -v, --verbose  Display more information.\n'
 
90
            '  -q, --quiet    Only display errors and warnings.\n'
 
91
            '  -h, --help     Show help message.\n'
 
92
            '\n'
 
93
            'See also: gam\n')
 
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):
 
98
            __doc__ = """A sample command."""
 
99
        cmd = cmd_foo_bar()
 
100
        self.assertEqual(cmd.name(), cmd.get_help_topic())
 
101
 
 
102
    def test_formatted_help_text(self):
 
103
        """Help text should be plain text by default."""
 
104
        class cmd_Demo(commands.Command):
 
105
            __doc__ = """A sample command.
 
106
 
 
107
            :Examples:
 
108
                Example 1::
 
109
 
 
110
                    cmd arg1
 
111
 
 
112
                Example 2::
 
113
 
 
114
                    cmd arg2
 
115
 
 
116
                A code block follows.
 
117
 
 
118
                ::
 
119
 
 
120
                    bzr Demo something
 
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'
 
130
            '  --usage        Show usage message and options.\n'
 
131
            '  -v, --verbose  Display more information.\n'
 
132
            '  -q, --quiet    Only display errors and warnings.\n'
 
133
            '  -h, --help     Show help message.\n'
 
134
            '\n'
 
135
            'Examples:\n'
 
136
            '    Example 1:\n'
 
137
            '\n'
 
138
            '        cmd arg1\n'
 
139
            '\n'
 
140
            '    Example 2:\n'
 
141
            '\n'
 
142
            '        cmd arg2\n'
 
143
            '\n'
 
144
            '    A code block follows.\n'
 
145
            '\n'
 
146
            '        bzr Demo something\n'
 
147
            '\n')
 
148
        helptext = cmd.get_help_text(plain=False)
 
149
        self.assertEquals(helptext,
 
150
            ':Purpose: A sample command.\n'
 
151
            ':Usage:   bzr Demo\n'
 
152
            '\n'
 
153
            ':Options:\n'
 
154
            '  --usage        Show usage message and options.\n'
 
155
            '  -v, --verbose  Display more information.\n'
 
156
            '  -q, --quiet    Only display errors and warnings.\n'
 
157
            '  -h, --help     Show help message.\n'
 
158
            '\n'
 
159
            ':Examples:\n'
 
160
            '    Example 1::\n'
 
161
            '\n'
 
162
            '        cmd arg1\n'
 
163
            '\n'
 
164
            '    Example 2::\n'
 
165
            '\n'
 
166
            '        cmd arg2\n'
 
167
            '\n'
 
168
            '    A code block follows.\n'
 
169
            '\n'
 
170
            '    ::\n'
 
171
            '\n'
 
172
            '        bzr Demo something\n'
 
173
            '\n')
 
174
 
 
175
    def test_concise_help_text(self):
 
176
        """Concise help text excludes the descriptive sections."""
 
177
        class cmd_Demo(commands.Command):
 
178
            __doc__ = """A sample command.
 
179
 
 
180
            Blah blah blah.
 
181
 
 
182
            :Examples:
 
183
                Example 1::
 
184
 
 
185
                    cmd arg1
 
186
            """
 
187
        cmd = cmd_Demo()
 
188
        helptext = cmd.get_help_text()
 
189
        self.assertEqualDiff(
 
190
            helptext,
 
191
            'Purpose: A sample command.\n'
 
192
            'Usage:   bzr Demo\n'
 
193
            '\n'
 
194
            'Options:\n'
 
195
            '  --usage        Show usage message and options.\n'
 
196
            '  -v, --verbose  Display more information.\n'
 
197
            '  -q, --quiet    Only display errors and warnings.\n'
 
198
            '  -h, --help     Show help message.\n'
 
199
            '\n'
 
200
            'Description:\n'
 
201
            '  Blah blah blah.\n'
 
202
            '\n'
 
203
            'Examples:\n'
 
204
            '    Example 1:\n'
 
205
            '\n'
 
206
            '        cmd arg1\n'
 
207
            '\n')
 
208
        helptext = cmd.get_help_text(verbose=False)
 
209
        self.assertEquals(helptext,
 
210
            'Purpose: A sample command.\n'
 
211
            'Usage:   bzr Demo\n'
 
212
            '\n'
 
213
            'Options:\n'
 
214
            '  --usage        Show usage message and options.\n'
 
215
            '  -v, --verbose  Display more information.\n'
 
216
            '  -q, --quiet    Only display errors and warnings.\n'
 
217
            '  -h, --help     Show help message.\n'
 
218
            '\n'
 
219
            'See bzr help Demo for more details and examples.\n'
 
220
            '\n')
 
221
 
 
222
    def test_help_custom_section_ordering(self):
 
223
        """Custom descriptive sections should remain in the order given."""
 
224
        class cmd_Demo(commands.Command):
 
225
            __doc__ = """A sample command.
 
226
 
 
227
            Blah blah blah.
 
228
 
 
229
            :Formats:
 
230
              Interesting stuff about formats.
 
231
 
 
232
            :Examples:
 
233
              Example 1::
 
234
 
 
235
                cmd arg1
 
236
 
 
237
            :Tips:
 
238
              Clever things to keep in mind.
 
239
            """
 
240
        cmd = cmd_Demo()
 
241
        helptext = cmd.get_help_text()
 
242
        self.assertEqualDiff(
 
243
            helptext,
 
244
            'Purpose: A sample command.\n'
 
245
            'Usage:   bzr Demo\n'
 
246
            '\n'
 
247
            'Options:\n'
 
248
            '  --usage        Show usage message and options.\n'
 
249
            '  -v, --verbose  Display more information.\n'
 
250
            '  -q, --quiet    Only display errors and warnings.\n'
 
251
            '  -h, --help     Show help message.\n'
 
252
            '\n'
 
253
            'Description:\n'
 
254
            '  Blah blah blah.\n'
 
255
            '\n'
 
256
            'Formats:\n'
 
257
            '  Interesting stuff about formats.\n'
 
258
            '\n'
 
259
            'Examples:\n'
 
260
            '  Example 1:\n'
 
261
            '\n'
 
262
            '    cmd arg1\n'
 
263
            '\n'
 
264
            'Tips:\n'
 
265
            '  Clever things to keep in mind.\n'
 
266
            '\n')
 
267
 
 
268
    def test_help_text_custom_usage(self):
 
269
        """Help text may contain a custom usage section."""
 
270
        class cmd_Demo(commands.Command):
 
271
            __doc__ = """A sample command.
 
272
 
 
273
            :Usage:
 
274
                cmd Demo [opts] args
 
275
 
 
276
                cmd Demo -h
 
277
 
 
278
            Blah blah blah.
 
279
            """
 
280
        cmd = cmd_Demo()
 
281
        helptext = cmd.get_help_text()
 
282
        self.assertEquals(helptext,
 
283
            'Purpose: A sample command.\n'
 
284
            'Usage:\n'
 
285
            '    cmd Demo [opts] args\n'
 
286
            '\n'
 
287
            '    cmd Demo -h\n'
 
288
            '\n'
 
289
            '\n'
 
290
            'Options:\n'
 
291
            '  --usage        Show usage message and options.\n'
 
292
            '  -v, --verbose  Display more information.\n'
 
293
            '  -q, --quiet    Only display errors and warnings.\n'
 
294
            '  -h, --help     Show help message.\n'
 
295
            '\n'
 
296
            'Description:\n'
 
297
            '  Blah blah blah.\n\n')
 
298
 
 
299
 
 
300
class TestRegisteredTopic(TestHelp):
 
301
    """Tests for the RegisteredTopic class."""
 
302
 
 
303
    def test_contruct(self):
 
304
        """Construction takes the help topic name for the registered item."""
 
305
        # validate our test
 
306
        self.assertTrue('basic' in help_topics.topic_registry)
 
307
        topic = help_topics.RegisteredTopic('basic')
 
308
        self.assertEqual('basic', topic.topic)
 
309
 
 
310
    def test_get_help_text(self):
 
311
        """A RegisteredTopic returns the get_detail results for get_help_text."""
 
312
        topic = help_topics.RegisteredTopic('commands')
 
313
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
 
314
            topic.get_help_text())
 
315
 
 
316
    def test_get_help_text_with_additional_see_also(self):
 
317
        topic = help_topics.RegisteredTopic('commands')
 
318
        self.assertEndsWith(
 
319
            topic.get_help_text(['foo', 'bar']),
 
320
            '\n'
 
321
            'See also: bar, foo\n')
 
322
 
 
323
    def test_get_help_text_loaded_from_file(self):
 
324
        # Pick a known topic stored in an external file
 
325
        topic = help_topics.RegisteredTopic('authentication')
 
326
        self.assertStartsWith(topic.get_help_text(),
 
327
            'Authentication Settings\n'
 
328
            '=======================\n'
 
329
            '\n')
 
330
 
 
331
    def test_get_help_topic(self):
 
332
        """The help topic for a RegisteredTopic is its topic from construction."""
 
333
        topic = help_topics.RegisteredTopic('foobar')
 
334
        self.assertEqual('foobar', topic.get_help_topic())
 
335
        topic = help_topics.RegisteredTopic('baz')
 
336
        self.assertEqual('baz', topic.get_help_topic())
 
337
 
 
338
 
 
339
class TestTopicIndex(TestHelp):
 
340
    """Tests for the HelpTopicIndex class."""
 
341
 
 
342
    def test_default_constructable(self):
 
343
        index = help_topics.HelpTopicIndex()
 
344
 
 
345
    def test_get_topics_None(self):
 
346
        """Searching for None returns the basic help topic."""
 
347
        index = help_topics.HelpTopicIndex()
 
348
        topics = index.get_topics(None)
 
349
        self.assertEqual(1, len(topics))
 
350
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
 
351
        self.assertEqual('basic', topics[0].topic)
 
352
 
 
353
    def test_get_topics_topics(self):
 
354
        """Searching for a string returns the matching string."""
 
355
        index = help_topics.HelpTopicIndex()
 
356
        topics = index.get_topics('topics')
 
357
        self.assertEqual(1, len(topics))
 
358
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
 
359
        self.assertEqual('topics', topics[0].topic)
 
360
 
 
361
    def test_get_topics_no_topic(self):
 
362
        """Searching for something not registered returns []."""
 
363
        index = help_topics.HelpTopicIndex()
 
364
        self.assertEqual([], index.get_topics('nothing by this name'))
 
365
 
 
366
    def test_prefix(self):
 
367
        """TopicIndex has a prefix of ''."""
 
368
        index = help_topics.HelpTopicIndex()
 
369
        self.assertEqual('', index.prefix)
 
370
 
 
371
 
 
372
class TestCommandIndex(TestHelp):
 
373
    """Tests for the HelpCommandIndex class."""
 
374
 
 
375
    def test_default_constructable(self):
 
376
        index = commands.HelpCommandIndex()
 
377
 
 
378
    def test_get_topics_None(self):
 
379
        """Searching for None returns an empty list."""
 
380
        index = commands.HelpCommandIndex()
 
381
        self.assertEqual([], index.get_topics(None))
 
382
 
 
383
    def test_get_topics_rocks(self):
 
384
        """Searching for 'rocks' returns the cmd_rocks command instance."""
 
385
        index = commands.HelpCommandIndex()
 
386
        topics = index.get_topics('rocks')
 
387
        self.assertEqual(1, len(topics))
 
388
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
 
389
 
 
390
    def test_get_topics_no_topic(self):
 
391
        """Searching for something that is not a command returns []."""
 
392
        index = commands.HelpCommandIndex()
 
393
        self.assertEqual([], index.get_topics('nothing by this name'))
 
394
 
 
395
    def test_prefix(self):
 
396
        """CommandIndex has a prefix of 'commands/'."""
 
397
        index = commands.HelpCommandIndex()
 
398
        self.assertEqual('commands/', index.prefix)
 
399
 
 
400
    def test_get_topic_with_prefix(self):
 
401
        """Searching for commands/rocks returns the rocks command object."""
 
402
        index = commands.HelpCommandIndex()
 
403
        topics = index.get_topics('commands/rocks')
 
404
        self.assertEqual(1, len(topics))
 
405
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
 
406
 
 
407
 
 
408
class TestHelpIndices(tests.TestCase):
 
409
    """Tests for the HelpIndices class."""
 
410
 
 
411
    def test_default_search_path(self):
 
412
        """The default search path should include internal indexs."""
 
413
        indices = help.HelpIndices()
 
414
        self.assertEqual(3, len(indices.search_path))
 
415
        # help topics should be searched in first.
 
416
        self.assertIsInstance(indices.search_path[0],
 
417
            help_topics.HelpTopicIndex)
 
418
        # with commands being search second.
 
419
        self.assertIsInstance(indices.search_path[1],
 
420
            commands.HelpCommandIndex)
 
421
        # and plugins are a third index.
 
422
        self.assertIsInstance(indices.search_path[2],
 
423
            plugin.PluginsHelpIndex)
 
424
 
 
425
    def test_search_for_unknown_topic_raises(self):
 
426
        """Searching for an unknown topic should raise NoHelpTopic."""
 
427
        indices = help.HelpIndices()
 
428
        indices.search_path = []
 
429
        error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
 
430
        self.assertEqual('foo', error.topic)
 
431
 
 
432
    def test_search_calls_get_topic(self):
 
433
        """Searching should call get_topics in all indexes in order."""
 
434
        calls = []
 
435
        class RecordingIndex(object):
 
436
            def __init__(self, name):
 
437
                self.prefix = name
 
438
            def get_topics(self, topic):
 
439
                calls.append(('get_topics', self.prefix, topic))
 
440
                return ['something']
 
441
        index = help.HelpIndices()
 
442
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
 
443
        # try with None
 
444
        index.search(None)
 
445
        self.assertEqual([
 
446
            ('get_topics', '1', None),
 
447
            ('get_topics', '2', None),
 
448
            ],
 
449
            calls)
 
450
        # and with a string
 
451
        del calls[:]
 
452
        index.search('bar')
 
453
        self.assertEqual([
 
454
            ('get_topics', '1', 'bar'),
 
455
            ('get_topics', '2', 'bar'),
 
456
            ],
 
457
            calls)
 
458
 
 
459
    def test_search_returns_index_and_results(self):
 
460
        """Searching should return help topics with their index"""
 
461
        class CannedIndex(object):
 
462
            def __init__(self, prefix, search_result):
 
463
                self.prefix = prefix
 
464
                self.result = search_result
 
465
            def get_topics(self, topic):
 
466
                return self.result
 
467
        index = help.HelpIndices()
 
468
        index_one = CannedIndex('1', ['a'])
 
469
        index_two = CannedIndex('2', ['b', 'c'])
 
470
        index.search_path = [index_one, index_two]
 
471
        self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
 
472
            index.search(None))
 
473
 
 
474
    def test_search_checks_for_duplicate_prefixes(self):
 
475
        """Its an error when there are multiple indices with the same prefix."""
 
476
        indices = help.HelpIndices()
 
477
        indices.search_path = [help_topics.HelpTopicIndex(),
 
478
            help_topics.HelpTopicIndex()]
 
479
        self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)