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