/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: v.ladeuil+lp at free
  • Date: 2006-11-08 07:44:30 UTC
  • mfrom: (2123 +trunk)
  • mto: (2145.1.1 keepalive)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: v.ladeuil+lp@free.fr-20061108074430-a9c08d4a475bd97f
Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from bzrlib.tests import TestCase, TestCaseWithTransport
25
25
 
26
26
 
 
27
# TODO: Make sure builtin exception class formats are consistent - e.g. should
 
28
# or shouldn't end with a full stop, etc.
 
29
 
 
30
 
27
31
class TestErrors(TestCaseWithTransport):
28
32
 
29
33
    def test_inventory_modified(self):
84
88
                             repo.bzrdir.root_transport.base,
85
89
                             str(error))
86
90
 
 
91
    def test_bzrnewerror_is_deprecated(self):
 
92
        class DeprecatedError(errors.BzrNewError):
 
93
            pass
 
94
        self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
 
95
             'please convert DeprecatedError to use BzrError instead'],
 
96
            DeprecatedError)
 
97
 
 
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.)
 
103
        try:
 
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))
 
107
 
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 "
106
127
            str(error))
107
128
 
108
129
 
109
 
class PassThroughError(errors.BzrNewError):
110
 
    """Pass through %(foo)s and %(bar)s"""
 
130
class PassThroughError(errors.BzrError):
 
131
    
 
132
    _fmt = """Pass through %(foo)s and %(bar)s"""
111
133
 
112
134
    def __init__(self, foo, bar):
113
 
        errors.BzrNewError.__init__(self, foo=foo, bar=bar)
114
 
 
115
 
 
116
 
class ErrorWithBadFormat(errors.BzrNewError):
117
 
    """One format specifier: %(thing)s"""
 
135
        errors.BzrError.__init__(self, foo=foo, bar=bar)
 
136
 
 
137
 
 
138
class ErrorWithBadFormat(errors.BzrError):
 
139
 
 
140
    _fmt = """One format specifier: %(thing)s"""
 
141
 
 
142
 
 
143
class ErrorWithNoFormat(errors.BzrError):
 
144
    """This class has a docstring but no format string."""
118
145
 
119
146
 
120
147
class TestErrorFormatting(TestCase):
129
156
        s = str(e)
130
157
        self.assertEqual('Pass through \xc2\xb5 and bar', s)
131
158
 
 
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'],
 
163
                lambda x: str(x), e)
 
164
        ## s = str(e)
 
165
        self.assertEqual(s, 
 
166
                "This class has a docstring but no format string.")
 
167
 
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')
139
175
 
140
176
 
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))
 
183
 
 
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))
 
188
 
 
189
    def test_socket_connection_error(self):
 
190
        """Test the formatting of SocketConnectionError"""
 
191
 
 
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',
 
196
            'ahost')
 
197
 
 
198
        # If port is None, we don't put :None
 
199
        self.assertSocketConnectionError(
 
200
            'Failed to connect to ahost',
 
201
            'ahost', port=None)
 
202
        # But if port is supplied we include it
 
203
        self.assertSocketConnectionError(
 
204
            'Failed to connect to ahost:22',
 
205
            'ahost', port=22)
 
206
 
 
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)
 
220
 
 
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')
 
226
 
 
227