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_disabled_method(self):
38
42
"read without data loss.",
45
def test_install_failed(self):
46
error = errors.InstallFailed(['rev-one'])
47
self.assertEqual("Could not install revisions:\nrev-one", str(error))
48
error = errors.InstallFailed(['rev-one', 'rev-two'])
49
self.assertEqual("Could not install revisions:\nrev-one, rev-two",
51
error = errors.InstallFailed([None])
52
self.assertEqual("Could not install revisions:\nNone", str(error))
41
54
def test_medium_not_connected(self):
42
55
error = errors.MediumNotConnected("a medium")
43
56
self.assertEqualDiff(
89
102
repo.bzrdir.root_transport.base,
105
def test_bzrnewerror_is_deprecated(self):
106
class DeprecatedError(errors.BzrNewError):
108
self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
109
'please convert DeprecatedError to use BzrError instead'],
112
def test_bzrerror_from_literal_string(self):
113
# Some code constructs BzrError from a literal string, in which case
114
# no further formatting is done. (I'm not sure raising the base class
115
# is a great idea, but if the exception is not intended to be caught
116
# perhaps no more is needed.)
118
raise errors.BzrError('this is my errors; %d is not expanded')
119
except errors.BzrError, e:
120
self.assertEqual('this is my errors; %d is not expanded', str(e))
92
122
def test_reading_completed(self):
93
123
error = errors.ReadingCompleted("a request")
94
124
self.assertEqualDiff("The MediumRequest 'a request' has already had "
110
140
" no data may be read.",
114
class PassThroughError(errors.BzrNewError):
115
"""Pass through %(foo)s and %(bar)s"""
117
def __init__(self, foo, bar):
118
errors.BzrNewError.__init__(self, foo=foo, bar=bar)
121
class ErrorWithBadFormat(errors.BzrNewError):
122
"""One format specifier: %(thing)s"""
125
class TestErrorFormatting(TestCase):
127
def test_always_str(self):
128
e = PassThroughError(u'\xb5', 'bar')
129
self.assertIsInstance(e.__str__(), str)
130
# In Python str(foo) *must* return a real byte string
131
# not a Unicode string. The following line would raise a
132
# Unicode error, because it tries to call str() on the string
133
# returned from e.__str__(), and it has non ascii characters
135
self.assertEqual('Pass through \xc2\xb5 and bar', s)
137
def test_mismatched_format_args(self):
138
# Even though ErrorWithBadFormat's format string does not match the
139
# arguments we constructing it with, we can still stringify an instance
140
# of this exception. The resulting string will say its unprintable.
141
e = ErrorWithBadFormat(not_thing='x')
142
self.assertStartsWith(
143
str(e), 'Unprintable exception ErrorWithBadFormat(')
146
class TestSpecificErrors(TestCase):
148
143
def test_transport_not_possible(self):
149
144
e = errors.TransportNotPossible('readonly', 'original error')
150
145
self.assertEqual('Transport operation not possible:'
194
189
orig_error='my_error')
193
class PassThroughError(errors.BzrError):
195
_fmt = """Pass through %(foo)s and %(bar)s"""
197
def __init__(self, foo, bar):
198
errors.BzrError.__init__(self, foo=foo, bar=bar)
201
class ErrorWithBadFormat(errors.BzrError):
203
_fmt = """One format specifier: %(thing)s"""
206
class ErrorWithNoFormat(errors.BzrError):
207
"""This class has a docstring but no format string."""
210
class TestErrorFormatting(TestCase):
212
def test_always_str(self):
213
e = PassThroughError(u'\xb5', 'bar')
214
self.assertIsInstance(e.__str__(), str)
215
# In Python str(foo) *must* return a real byte string
216
# not a Unicode string. The following line would raise a
217
# Unicode error, because it tries to call str() on the string
218
# returned from e.__str__(), and it has non ascii characters
220
self.assertEqual('Pass through \xc2\xb5 and bar', s)
222
def test_missing_format_string(self):
223
e = ErrorWithNoFormat(param='randomvalue')
224
s = self.callDeprecated(
225
['ErrorWithNoFormat uses its docstring as a format, it should use _fmt instead'],
229
"This class has a docstring but no format string.")
231
def test_mismatched_format_args(self):
232
# Even though ErrorWithBadFormat's format string does not match the
233
# arguments we constructing it with, we can still stringify an instance
234
# of this exception. The resulting string will say its unprintable.
235
e = ErrorWithBadFormat(not_thing='x')
236
self.assertStartsWith(
237
str(e), 'Unprintable exception ErrorWithBadFormat')