24
24
from bzrlib.tests import TestCase, TestCaseWithTransport
27
# TODO: Make sure builtin exception class formats are consistent - e.g. should
28
# or shouldn't end with a full stop, etc.
27
31
class TestErrors(TestCaseWithTransport):
29
33
def test_inventory_modified(self):
84
88
repo.bzrdir.root_transport.base,
91
def test_bzrnewerror_is_deprecated(self):
92
class DeprecatedError(errors.BzrNewError):
94
self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
95
'please convert DeprecatedError to use BzrError instead'],
98
def test_bzrerror_from_literal_string(self):
99
# Some code constructs BzrError from a literal string, in which case
100
# no further formatting is done. (I'm not sure raising the base class
101
# is a great idea, but if the exception is not intended to be caught
102
# perhaps no more is needed.)
104
raise errors.BzrError('this is my errors; %d is not expanded')
105
except errors.BzrError, e:
106
self.assertEqual('this is my errors; %d is not expanded', str(e))
87
108
def test_reading_completed(self):
88
109
error = errors.ReadingCompleted("a request")
89
110
self.assertEqualDiff("The MediumRequest 'a request' has already had "
109
class PassThroughError(errors.BzrNewError):
110
"""Pass through %(foo)s and %(bar)s"""
130
class PassThroughError(errors.BzrError):
132
_fmt = """Pass through %(foo)s and %(bar)s"""
112
134
def __init__(self, foo, bar):
113
errors.BzrNewError.__init__(self, foo=foo, bar=bar)
116
class ErrorWithBadFormat(errors.BzrNewError):
117
"""One format specifier: %(thing)s"""
135
errors.BzrError.__init__(self, foo=foo, bar=bar)
138
class ErrorWithBadFormat(errors.BzrError):
140
_fmt = """One format specifier: %(thing)s"""
143
class ErrorWithNoFormat(errors.BzrError):
144
"""This class has a docstring but no format string."""
120
147
class TestErrorFormatting(TestCase):
130
157
self.assertEqual('Pass through \xc2\xb5 and bar', s)
159
def test_missing_format_string(self):
160
e = ErrorWithNoFormat(param='randomvalue')
161
s = self.callDeprecated(
162
['ErrorWithNoFormat uses its docstring as a format, it should use _fmt instead'],
166
"This class has a docstring but no format string.")
132
168
def test_mismatched_format_args(self):
133
169
# Even though ErrorWithBadFormat's format string does not match the
134
170
# arguments we constructing it with, we can still stringify an instance
135
171
# of this exception. The resulting string will say its unprintable.
136
172
e = ErrorWithBadFormat(not_thing='x')
137
173
self.assertStartsWith(
138
str(e), 'Unprintable exception ErrorWithBadFormat(')
174
str(e), 'Unprintable exception ErrorWithBadFormat')
141
177
class TestSpecificErrors(TestCase):
144
180
e = errors.TransportNotPossible('readonly', 'original error')
145
181
self.assertEqual('Transport operation not possible:'
146
182
' readonly original error', str(e))
184
def assertSocketConnectionError(self, expected, *args, **kwargs):
185
"""Check the formatting of a SocketConnectionError exception"""
186
e = errors.SocketConnectionError(*args, **kwargs)
187
self.assertEqual(expected, str(e))
189
def test_socket_connection_error(self):
190
"""Test the formatting of SocketConnectionError"""
192
# There should be a default msg about failing to connect
193
# we only require a host name.
194
self.assertSocketConnectionError(
195
'Failed to connect to ahost',
198
# If port is None, we don't put :None
199
self.assertSocketConnectionError(
200
'Failed to connect to ahost',
202
# But if port is supplied we include it
203
self.assertSocketConnectionError(
204
'Failed to connect to ahost:22',
207
# We can also supply extra information about the error
208
# with or without a port
209
self.assertSocketConnectionError(
210
'Failed to connect to ahost:22; bogus error',
211
'ahost', port=22, orig_error='bogus error')
212
self.assertSocketConnectionError(
213
'Failed to connect to ahost; bogus error',
214
'ahost', orig_error='bogus error')
215
# An exception object can be passed rather than a string
216
orig_error = ValueError('bad value')
217
self.assertSocketConnectionError(
218
'Failed to connect to ahost; %s' % (str(orig_error),),
219
host='ahost', orig_error=orig_error)
221
# And we can supply a custom failure message
222
self.assertSocketConnectionError(
223
'Unable to connect to ssh host ahost:444; my_error',
224
host='ahost', port=444, msg='Unable to connect to ssh host',
225
orig_error='my_error')