/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
32
class TestCommandHelp(tests.TestCase):
33
    """Tests for help on commands."""
34
35
    def test_command_help_includes_see_also(self):
36
        class cmd_WithSeeAlso(commands.Command):
37
            """A sample command."""
38
            _see_also = ['foo', 'bar']
39
        cmd = cmd_WithSeeAlso()
2432.1.12 by Robert Collins
Relocate command help onto Command.
40
        helptext = cmd.get_help_text()
2425.1.3 by Robert Collins
Python 2.4 compatability change for the new help see-also tests.
41
        self.assertEndsWith(
2432.1.12 by Robert Collins
Relocate command help onto Command.
42
            helptext,
2598.1.4 by Martin Pool
Fix up tests for option help cleanups
43
            '  -h, --help  Show help message.\n'
2425.2.2 by Robert Collins
``bzr help`` now provides cross references to other help topics using the
44
            '\n'
2425.1.3 by Robert Collins
Python 2.4 compatability change for the new help see-also tests.
45
            'See also: bar, foo\n')
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
46
2432.1.12 by Robert Collins
Relocate command help onto Command.
47
    def test_get_help_text(self):
48
        """Commands have a get_help_text method which returns their help."""
49
        class cmd_Demo(commands.Command):
50
            """A sample command."""
51
        cmd = cmd_Demo()
52
        helptext = cmd.get_help_text()
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
53
        self.assertStartsWith(helptext, 'Purpose: A sample command.')
54
        self.assertContainsRe(helptext, 'Usage:   bzr Demo')
55
        self.assertEndsWith(helptext, 'Show help message.\n\n')
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
56
57
    def test_command_with_additional_see_also(self):
58
        class cmd_WithSeeAlso(commands.Command):
59
            """A sample command."""
60
            _see_also = ['foo', 'bar']
61
        cmd = cmd_WithSeeAlso()
62
        helptext = cmd.get_help_text(['gam'])
63
        self.assertEndsWith(
64
            helptext,
2598.1.4 by Martin Pool
Fix up tests for option help cleanups
65
            '  -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.
66
            '\n'
67
            'See also: bar, foo, gam\n')
68
69
    def test_command_only_additional_see_also(self):
70
        class cmd_WithSeeAlso(commands.Command):
71
            """A sample command."""
72
        cmd = cmd_WithSeeAlso()
73
        helptext = cmd.get_help_text(['gam'])
74
        self.assertEndsWith(
75
            helptext,
2598.1.4 by Martin Pool
Fix up tests for option help cleanups
76
            '  -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.
77
            '\n'
78
            'See also: gam\n')
2432.1.28 by Robert Collins
Add a get_help_topic method to commands.Command.
79
80
    def test_get_help_topic(self):
81
        """The help topic for a Command is its name()."""
82
        class cmd_foo_bar(commands.Command):
83
            """A sample command."""
84
        cmd = cmd_foo_bar()
85
        self.assertEqual(cmd.name(), cmd.get_help_topic())
2432.1.12 by Robert Collins
Relocate command help onto Command.
86
    
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
87
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
88
class TestRegisteredTopic(tests.TestCase):
89
    """Tests for the RegisteredTopic class."""
90
91
    def test_contruct(self):
92
        """Construction takes the help topic name for the registered item."""
93
        # validate our test 
94
        self.assertTrue('basic' in help_topics.topic_registry)
95
        topic = help_topics.RegisteredTopic('basic')
96
        self.assertEqual('basic', topic.topic)
97
2432.1.10 by Robert Collins
Add get_help_text() to RegisteredTopic to get the help as a string.
98
    def test_get_help_text(self):
99
        """A RegisteredTopic returns the get_detail results for get_help_text."""
100
        topic = help_topics.RegisteredTopic('commands')
101
        self.assertEqual(help_topics.topic_registry.get_detail('commands'),
102
            topic.get_help_text())
103
2432.1.22 by Robert Collins
Teach RegisteredTopic to support the additional_see_also list of related help terms.
104
    def test_get_help_text_with_additional_see_also(self):
105
        topic = help_topics.RegisteredTopic('commands')
106
        self.assertEndsWith(
107
            topic.get_help_text(['foo', 'bar']),
108
            '\n'
109
            'See also: bar, foo\n')
110
2432.1.27 by Robert Collins
Add a get_help_topic method to RegisteredTopic.
111
    def test_get_help_topic(self):
112
        """The help topic for a RegisteredTopic is its topic from construction."""
113
        topic = help_topics.RegisteredTopic('foobar')
114
        self.assertEqual('foobar', topic.get_help_topic())
115
        topic = help_topics.RegisteredTopic('baz')
116
        self.assertEqual('baz', topic.get_help_topic())
117
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
118
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
119
class TestTopicIndex(tests.TestCase):
120
    """Tests for the HelpTopicIndex class."""
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
121
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
122
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
123
        index = help_topics.HelpTopicIndex()
2432.1.1 by Robert Collins
Add a HelpTopicContext object.
124
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
125
    def test_get_topics_None(self):
126
        """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.
127
        index = help_topics.HelpTopicIndex()
128
        topics = index.get_topics(None)
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
129
        self.assertEqual(1, len(topics))
130
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
131
        self.assertEqual('basic', topics[0].topic)
132
133
    def test_get_topics_topics(self):
134
        """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.
135
        index = help_topics.HelpTopicIndex()
136
        topics = index.get_topics('topics')
2432.1.8 by Robert Collins
HelpTopicContext now returns RegisteredTopic objects for get_topics calls.
137
        self.assertEqual(1, len(topics))
138
        self.assertIsInstance(topics[0], help_topics.RegisteredTopic)
139
        self.assertEqual('topics', topics[0].topic)
140
141
    def test_get_topics_no_topic(self):
142
        """Searching for something not registered returns []."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
143
        index = help_topics.HelpTopicIndex()
144
        self.assertEqual([], index.get_topics('nothing by this name'))
145
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
146
    def test_prefix(self):
147
        """TopicIndex has a prefix of ''."""
148
        index = help_topics.HelpTopicIndex()
149
        self.assertEqual('', index.prefix)
150
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
151
152
class TestCommandIndex(tests.TestCase):
153
    """Tests for the HelpCommandIndex class."""
2432.1.2 by Robert Collins
Add a HelpCommandContext class for help from commands.
154
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
155
    def test_default_constructable(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
156
        index = commands.HelpCommandIndex()
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
157
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
158
    def test_get_topics_None(self):
159
        """Searching for None returns an empty list."""
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
160
        index = commands.HelpCommandIndex()
161
        self.assertEqual([], index.get_topics(None))
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
162
163
    def test_get_topics_rocks(self):
164
        """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.
165
        index = commands.HelpCommandIndex()
166
        topics = index.get_topics('rocks')
2432.1.13 by Robert Collins
HelpCommandContext now implementes get_topics.
167
        self.assertEqual(1, len(topics))
168
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
169
170
    def test_get_topics_no_topic(self):
171
        """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.
172
        index = commands.HelpCommandIndex()
173
        self.assertEqual([], index.get_topics('nothing by this name'))
174
2432.1.17 by Robert Collins
Add prefixes to HelpIndexes.
175
    def test_prefix(self):
176
        """CommandIndex has a prefix of 'commands/'."""
177
        index = commands.HelpCommandIndex()
178
        self.assertEqual('commands/', index.prefix)
179
2432.1.18 by Robert Collins
Add support for doing bzr help commands/COMMANDNAME.
180
    def test_get_topic_with_prefix(self):
181
        """Searching for commands/rocks returns the rocks command object."""
182
        index = commands.HelpCommandIndex()
183
        topics = index.get_topics('commands/rocks')
184
        self.assertEqual(1, len(topics))
185
        self.assertIsInstance(topics[0], builtins.cmd_rocks)
186
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
187
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
188
class TestHelpIndices(tests.TestCase):
189
    """Tests for the HelpIndices class."""
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
190
191
    def test_default_search_path(self):
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
192
        """The default search path should include internal indexs."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
193
        indices = help.HelpIndices()
2432.1.24 by Robert Collins
Add plugins as a help index.
194
        self.assertEqual(3, len(indices.search_path))
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
195
        # help topics should be searched in first.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
196
        self.assertIsInstance(indices.search_path[0],
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
197
            help_topics.HelpTopicIndex)
2432.1.3 by Robert Collins
Create a HelpContexts object to do help lookups.
198
        # with commands being search second.
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
199
        self.assertIsInstance(indices.search_path[1],
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
200
            commands.HelpCommandIndex)
2432.1.24 by Robert Collins
Add plugins as a help index.
201
        # and plugins are a third index.
202
        self.assertIsInstance(indices.search_path[2],
203
            plugin.PluginsHelpIndex)
2432.1.5 by Robert Collins
Initial stub for topic searching.
204
205
    def test_search_for_unknown_topic_raises(self):
206
        """Searching for an unknown topic should raise NoHelpTopic."""
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
207
        indices = help.HelpIndices()
208
        indices.search_path = []
209
        error = self.assertRaises(errors.NoHelpTopic, indices.search, 'foo')
2432.1.5 by Robert Collins
Initial stub for topic searching.
210
        self.assertEqual('foo', error.topic)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
211
212
    def test_search_calls_get_topic(self):
213
        """Searching should call get_topics in all indexes in order."""
214
        calls = []
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
215
        class RecordingIndex(object):
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
216
            def __init__(self, name):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
217
                self.prefix = name
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
218
            def get_topics(self, topic):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
219
                calls.append(('get_topics', self.prefix, topic))
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
220
                return ['something']
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
221
        index = help.HelpIndices()
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
222
        index.search_path = [RecordingIndex('1'), RecordingIndex('2')]
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
223
        # try with None
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
224
        index.search(None)
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
225
        self.assertEqual([
226
            ('get_topics', '1', None),
227
            ('get_topics', '2', None),
228
            ],
229
            calls)
230
        # and with a string
231
        del calls[:]
2432.1.15 by Robert Collins
Rename Context (in bzrlib.help) to Index, for a clearer name.
232
        index.search('bar')
2432.1.6 by Robert Collins
HelpContexts.search now invokes get_topics on each context.
233
        self.assertEqual([
234
            ('get_topics', '1', 'bar'),
235
            ('get_topics', '2', 'bar'),
236
            ],
237
            calls)
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
238
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
239
    def test_search_returns_index_and_results(self):
240
        """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.
241
        class CannedIndex(object):
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
242
            def __init__(self, prefix, search_result):
243
                self.prefix = prefix
2432.1.7 by Robert Collins
HelpContexts.search now returns the found topics.
244
                self.result = search_result
245
            def get_topics(self, topic):
246
                return self.result
2432.1.16 by Robert Collins
Correct spelling of Indexs to Indices.
247
        index = help.HelpIndices()
2432.1.20 by Robert Collins
Modify the result of HelpIndices.search to include the index each result was found in.
248
        index_one = CannedIndex('1', ['a'])
249
        index_two = CannedIndex('2', ['b', 'c'])
250
        index.search_path = [index_one, index_two]
251
        self.assertEqual([(index_one, 'a'), (index_two, 'b'), (index_two, 'c')],
252
            index.search(None))
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
253
254
    def test_search_checks_for_duplicate_prefixes(self):
255
        """Its an error when there are multiple indices with the same prefix."""
256
        indices = help.HelpIndices()
257
        indices.search_path = [help_topics.HelpTopicIndex(),
258
            help_topics.HelpTopicIndex()]
259
        self.assertRaises(errors.DuplicateHelpPrefix, indices.search, None)