1
# Copyright (C) 2004, 2005 Canonical Ltd
1
# Copyright (C) 2004, 2005 by Canonical Ltd
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from cStringIO import StringIO
17
from bzrlib.tests import TestCase
18
from bzrlib.commands import display_command
27
from bzrlib.commands import display_command
28
from bzrlib.tests import TestSkipped
31
class TestCommands(tests.TestCase):
21
class TestCommands(TestCase):
33
22
def test_display_command(self):
34
23
"""EPIPE message is selectively suppressed"""
35
24
def pipe_thrower():
44
33
raise IOError(errno.ESPIPE, "Bogus pipe error")
45
34
self.assertRaises(IOError, other_thrower)
47
def test_unicode_command(self):
48
# This error is thrown when we can't find the command in the
49
# list of available commands
50
self.assertRaises(errors.BzrCommandError,
51
commands.run_bzr, [u'cmd\xb5'])
53
def test_unicode_option(self):
54
# This error is actually thrown by optparse, when it
55
# can't find the given option
57
if optparse.__version__ == "1.5.3":
58
raise TestSkipped("optparse 1.5.3 can't handle unicode options")
59
self.assertRaises(errors.BzrCommandError,
60
commands.run_bzr, ['log', u'--option\xb5'])
63
class TestGetAlias(tests.TestCase):
65
def _get_config(self, config_text):
66
my_config = config.GlobalConfig()
67
config_file = StringIO(config_text.encode('utf-8'))
68
my_config._parser = my_config._get_parser(file=config_file)
71
def test_simple(self):
72
my_config = self._get_config("[ALIASES]\n"
73
"diff=diff -r -2..-1\n")
74
self.assertEqual([u'diff', u'-r', u'-2..-1'],
75
commands.get_alias("diff", config=my_config))
77
def test_single_quotes(self):
78
my_config = self._get_config("[ALIASES]\n"
79
"diff=diff -r -2..-1 --diff-options "
80
"'--strip-trailing-cr -wp'\n")
81
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
82
u'--strip-trailing-cr -wp'],
83
commands.get_alias("diff", config=my_config))
85
def test_double_quotes(self):
86
my_config = self._get_config("[ALIASES]\n"
87
"diff=diff -r -2..-1 --diff-options "
88
"\"--strip-trailing-cr -wp\"\n")
89
self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
90
u'--strip-trailing-cr -wp'],
91
commands.get_alias("diff", config=my_config))
93
def test_unicode(self):
94
my_config = self._get_config("[ALIASES]\n"
95
u"iam=whoami 'Erik B\u00e5gfors <erik@bagfors.nu>'\n")
96
self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
97
commands.get_alias("iam", config=my_config))
100
class TestSeeAlso(tests.TestCase):
101
"""Tests for the see also functional of Command."""
103
def test_default_subclass_no_see_also(self):
104
class ACommand(commands.Command):
105
"""A sample command."""
107
self.assertEqual([], command.get_see_also())
109
def test__see_also(self):
110
"""When _see_also is defined, it sets the result of get_see_also()."""
111
class ACommand(commands.Command):
112
_see_also = ['bar', 'foo']
114
self.assertEqual(['bar', 'foo'], command.get_see_also())
116
def test_deduplication(self):
117
"""Duplicates in _see_also are stripped out."""
118
class ACommand(commands.Command):
119
_see_also = ['foo', 'foo']
121
self.assertEqual(['foo'], command.get_see_also())
123
def test_sorted(self):
124
"""_see_also is sorted by get_see_also."""
125
class ACommand(commands.Command):
126
_see_also = ['foo', 'bar']
128
self.assertEqual(['bar', 'foo'], command.get_see_also())
130
def test_additional_terms(self):
131
"""Additional terms can be supplied and are deduped and sorted."""
132
class ACommand(commands.Command):
133
_see_also = ['foo', 'bar']
135
self.assertEqual(['bar', 'foo', 'gam'],
136
command.get_see_also(['gam', 'bar', 'gam']))
139
class TestRegisterLazy(tests.TestCase):
142
import bzrlib.tests.fake_command
143
del sys.modules['bzrlib.tests.fake_command']
144
global lazy_command_imported
145
lazy_command_imported = False
149
commands.plugin_cmds.remove('fake')
151
def assertIsFakeCommand(self, cmd_obj):
152
from bzrlib.tests.fake_command import cmd_fake
153
self.assertIsInstance(cmd_obj, cmd_fake)
155
def test_register_lazy(self):
156
"""Ensure lazy registration works"""
157
commands.plugin_cmds.register_lazy('cmd_fake', [],
158
'bzrlib.tests.fake_command')
159
self.addCleanup(self.remove_fake)
160
self.assertFalse(lazy_command_imported)
161
fake_instance = commands.get_cmd_object('fake')
162
self.assertTrue(lazy_command_imported)
163
self.assertIsFakeCommand(fake_instance)
165
def test_get_unrelated_does_not_import(self):
166
commands.plugin_cmds.register_lazy('cmd_fake', [],
167
'bzrlib.tests.fake_command')
168
self.addCleanup(self.remove_fake)
169
commands.get_cmd_object('status')
170
self.assertFalse(lazy_command_imported)
172
def test_aliases(self):
173
commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
174
'bzrlib.tests.fake_command')
175
self.addCleanup(self.remove_fake)
176
fake_instance = commands.get_cmd_object('fake_alias')
177
self.assertIsFakeCommand(fake_instance)