~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commands.py

  • Committer: Vincent Ladeuil
  • Date: 2011-12-21 14:25:26 UTC
  • mto: This revision was merged to the branch mainline in revision 6397.
  • Revision ID: v.ladeuil+lp@free.fr-20111221142526-pnwau0xnalimujts
Provides MemoryStack to simplify configuration setup in tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from cStringIO import StringIO
18
17
import errno
 
18
import inspect
19
19
import sys
20
20
 
21
21
from bzrlib import (
32
32
 
33
33
class TestCommands(tests.TestCase):
34
34
 
 
35
    def test_all_commands_have_help(self):
 
36
        commands._register_builtin_commands()
 
37
        commands_without_help = set()
 
38
        base_doc = inspect.getdoc(commands.Command)
 
39
        for cmd_name in commands.all_command_names():
 
40
            cmd = commands.get_cmd_object(cmd_name)
 
41
            cmd_help = cmd.help()
 
42
            if not cmd_help or cmd_help == base_doc:
 
43
                commands_without_help.append(cmd_name)
 
44
        self.assertLength(0, commands_without_help)
 
45
 
35
46
    def test_display_command(self):
36
47
        """EPIPE message is selectively suppressed"""
37
48
        def pipe_thrower():
64
75
    @staticmethod
65
76
    def get_command(options):
66
77
        class cmd_foo(commands.Command):
67
 
            'Bar'
 
78
            __doc__ = 'Bar'
68
79
 
69
80
            takes_options = options
70
81
 
79
90
        self.assertContainsRe(c.get_help_text(), '--foo')
80
91
 
81
92
 
 
93
class TestInsideCommand(tests.TestCaseInTempDir):
 
94
 
 
95
    def test_command_see_config_overrides(self):
 
96
        def run(cmd):
 
97
            # We override the run() command method so we can observe the
 
98
            # overrides from inside.
 
99
            c = config.GlobalStack()
 
100
            self.assertEquals('12', c.get('xx'))
 
101
            self.assertEquals('foo', c.get('yy'))
 
102
        self.overrideAttr(builtins.cmd_rocks, 'run', run)
 
103
        self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo'])
 
104
        c = config.GlobalStack()
 
105
        # Ensure that we don't leak outside of the command
 
106
        self.assertEquals(None, c.get('xx'))
 
107
        self.assertEquals(None, c.get('yy'))
 
108
 
 
109
 
 
110
class TestInvokedAs(tests.TestCase):
 
111
 
 
112
    def test_invoked_as(self):
 
113
        """The command object knows the actual name used to invoke it."""
 
114
        commands.install_bzr_command_hooks()
 
115
        commands._register_builtin_commands()
 
116
        # get one from the real get_cmd_object.
 
117
        c = commands.get_cmd_object('ci')
 
118
        self.assertIsInstance(c, builtins.cmd_commit)
 
119
        self.assertEquals(c.invoked_as, 'ci')
 
120
 
 
121
 
82
122
class TestGetAlias(tests.TestCase):
83
123
 
84
124
    def _get_config(self, config_text):
85
 
        my_config = config.GlobalConfig()
86
 
        config_file = StringIO(config_text.encode('utf-8'))
87
 
        my_config._parser = my_config._get_parser(file=config_file)
 
125
        my_config = config.GlobalConfig.from_string(config_text)
88
126
        return my_config
89
127
 
90
128
    def test_simple(self):
111
149
 
112
150
    def test_unicode(self):
113
151
        my_config = self._get_config("[ALIASES]\n"
114
 
            u"iam=whoami 'Erik B\u00e5gfors <erik@bagfors.nu>'\n")
 
152
            u'iam=whoami "Erik B\u00e5gfors <erik@bagfors.nu>"\n')
115
153
        self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
116
154
                          commands.get_alias("iam", config=my_config))
117
155
 
119
157
class TestSeeAlso(tests.TestCase):
120
158
    """Tests for the see also functional of Command."""
121
159
 
 
160
    @staticmethod
 
161
    def _get_command_with_see_also(see_also):
 
162
        class ACommand(commands.Command):
 
163
            __doc__ = """A sample command."""
 
164
            _see_also = see_also
 
165
        return ACommand()
 
166
 
122
167
    def test_default_subclass_no_see_also(self):
123
 
        class ACommand(commands.Command):
124
 
            """A sample command."""
125
 
        command = ACommand()
 
168
        command = self._get_command_with_see_also([])
126
169
        self.assertEqual([], command.get_see_also())
127
170
 
128
171
    def test__see_also(self):
129
172
        """When _see_also is defined, it sets the result of get_see_also()."""
130
 
        class ACommand(commands.Command):
131
 
            _see_also = ['bar', 'foo']
132
 
        command = ACommand()
 
173
        command = self._get_command_with_see_also(['bar', 'foo'])
133
174
        self.assertEqual(['bar', 'foo'], command.get_see_also())
134
175
 
135
176
    def test_deduplication(self):
136
177
        """Duplicates in _see_also are stripped out."""
137
 
        class ACommand(commands.Command):
138
 
            _see_also = ['foo', 'foo']
139
 
        command = ACommand()
 
178
        command = self._get_command_with_see_also(['foo', 'foo'])
140
179
        self.assertEqual(['foo'], command.get_see_also())
141
180
 
142
181
    def test_sorted(self):
143
182
        """_see_also is sorted by get_see_also."""
144
 
        class ACommand(commands.Command):
145
 
            _see_also = ['foo', 'bar']
146
 
        command = ACommand()
 
183
        command = self._get_command_with_see_also(['foo', 'bar'])
147
184
        self.assertEqual(['bar', 'foo'], command.get_see_also())
148
185
 
149
186
    def test_additional_terms(self):
150
187
        """Additional terms can be supplied and are deduped and sorted."""
151
 
        class ACommand(commands.Command):
152
 
            _see_also = ['foo', 'bar']
153
 
        command = ACommand()
 
188
        command = self._get_command_with_see_also(['foo', 'bar'])
154
189
        self.assertEqual(['bar', 'foo', 'gam'],
155
190
            command.get_see_also(['gam', 'bar', 'gam']))
156
191
 
210
245
        commands.Command.hooks.install_named_hook(
211
246
            "extend_command", hook_calls.append, None)
212
247
        # create a command, should not fire
213
 
        class ACommand(commands.Command):
214
 
            """A sample command."""
215
 
        cmd = ACommand()
 
248
        class cmd_test_extend_command_hook(commands.Command):
 
249
            __doc__ = """A sample command."""
216
250
        self.assertEqual([], hook_calls)
217
251
        # -- as a builtin
218
252
        # register the command class, should not fire
219
253
        try:
220
 
            builtins.cmd_test_extend_command_hook = ACommand
 
254
            commands.builtin_command_registry.register(cmd_test_extend_command_hook)
221
255
            self.assertEqual([], hook_calls)
222
256
            # and ask for the object, should fire
223
257
            cmd = commands.get_cmd_object('test-extend-command-hook')
227
261
            self.assertSubset([cmd], hook_calls)
228
262
            del hook_calls[:]
229
263
        finally:
230
 
            del builtins.cmd_test_extend_command_hook
 
264
            commands.builtin_command_registry.remove('test-extend-command-hook')
231
265
        # -- as a plugin lazy registration
232
266
        try:
233
267
            # register the command class, should not fire
249
283
        commands.install_bzr_command_hooks()
250
284
        hook_calls = []
251
285
        class ACommand(commands.Command):
252
 
            """A sample command."""
 
286
            __doc__ = """A sample command."""
253
287
        def get_cmd(cmd_or_None, cmd_name):
254
288
            hook_calls.append(('called', cmd_or_None, cmd_name))
255
289
            if cmd_name in ('foo', 'info'):
276
310
 
277
311
class TestGetMissingCommandHook(tests.TestCase):
278
312
 
279
 
    def test_fires_on_get_cmd_object(self):
280
 
        # The get_missing_command(cmd) hook fires when commands are delivered to the
281
 
        # ui.
282
 
        hook_calls = []
 
313
    def hook_missing(self):
 
314
        """Hook get_missing_command for testing."""
 
315
        self.hook_calls = []
283
316
        class ACommand(commands.Command):
284
 
            """A sample command."""
 
317
            __doc__ = """A sample command."""
285
318
        def get_missing_cmd(cmd_name):
286
 
            hook_calls.append(('called', cmd_name))
 
319
            self.hook_calls.append(('called', cmd_name))
287
320
            if cmd_name in ('foo', 'info'):
288
321
                return ACommand()
289
322
        commands.Command.hooks.install_named_hook(
290
323
            "get_missing_command", get_missing_cmd, None)
 
324
        self.ACommand = ACommand
 
325
 
 
326
    def test_fires_on_get_cmd_object(self):
 
327
        # The get_missing_command(cmd) hook fires when commands are delivered to the
 
328
        # ui.
 
329
        self.hook_missing()
291
330
        # create a command directly, should not fire
292
 
        cmd = ACommand()
293
 
        self.assertEqual([], hook_calls)
 
331
        self.cmd = self.ACommand()
 
332
        self.assertEqual([], self.hook_calls)
294
333
        # ask by name, should fire and give us our command
295
334
        cmd = commands.get_cmd_object('foo')
296
 
        self.assertEqual([('called', 'foo')], hook_calls)
297
 
        self.assertIsInstance(cmd, ACommand)
298
 
        del hook_calls[:]
 
335
        self.assertEqual([('called', 'foo')], self.hook_calls)
 
336
        self.assertIsInstance(cmd, self.ACommand)
 
337
        del self.hook_calls[:]
299
338
        # ask by a name that is supplied by a builtin - the hook should not
300
339
        # fire and we still get our object.
301
340
        commands.install_bzr_command_hooks()
302
341
        cmd = commands.get_cmd_object('info')
303
342
        self.assertNotEqual(None, cmd)
304
 
        self.assertEqual(0, len(hook_calls))
 
343
        self.assertEqual(0, len(self.hook_calls))
 
344
 
 
345
    def test_skipped_on_HelpCommandIndex_get_topics(self):
 
346
        # The get_missing_command(cmd_name) hook is not fired when
 
347
        # looking up help topics.
 
348
        self.hook_missing()
 
349
        topic = commands.HelpCommandIndex()
 
350
        topics = topic.get_topics('foo')
 
351
        self.assertEqual([], self.hook_calls)
305
352
 
306
353
 
307
354
class TestListCommandHook(tests.TestCase):