/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
16
17
"""Tests for the formatting and construction of errors."""
18
5050.8.1 by Parth Malwankar
added test to ensure that BzrError subclasses dont use "message" as a name
19
import inspect
20
import re
4634.1.2 by Martin Pool
Add test for CannotBindAddress
21
import socket
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
22
import sys
4634.1.2 by Martin Pool
Add test for CannotBindAddress
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from .. import (
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
25
    controldir,
1948.1.6 by John Arbash Meinel
Make BzrNewError always return a str object
26
    errors,
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
27
    osutils,
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
28
    tests,
3200.2.1 by Robert Collins
* The ``register-branch`` command will now use the public url of the branch
29
    urlutils,
1948.1.6 by John Arbash Meinel
Make BzrNewError always return a str object
30
    )
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
31
from ..sixish import (
32
    text_type,
5579.3.1 by Jelmer Vernooij
Remove unused imports.
33
    )
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
34
35
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
36
class TestErrors(tests.TestCase):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
37
5050.8.1 by Parth Malwankar
added test to ensure that BzrError subclasses dont use "message" as a name
38
    def test_no_arg_named_message(self):
39
        """Ensure the __init__ and _fmt in errors do not have "message" arg.
40
41
        This test fails if __init__ or _fmt in errors has an argument
42
        named "message" as this can cause errors in some Python versions.
43
        Python 2.5 uses a slot for StandardError.message.
44
        See bug #603461
45
        """
46
        fmt_pattern = re.compile("%\(message\)[sir]")
5050.8.3 by Parth Malwankar
use __subclasses__
47
        for c in errors.BzrError.__subclasses__():
48
            init = getattr(c, '__init__', None)
49
            fmt = getattr(c, '_fmt', None)
5050.8.1 by Parth Malwankar
added test to ensure that BzrError subclasses dont use "message" as a name
50
            if init:
51
                args = inspect.getargspec(init)[0]
52
                self.assertFalse('message' in args,
53
                    ('Argument name "message" not allowed for '
5050.8.3 by Parth Malwankar
use __subclasses__
54
                    '"errors.%s.__init__"' % c.__name__))
5050.8.1 by Parth Malwankar
added test to ensure that BzrError subclasses dont use "message" as a name
55
            if fmt and fmt_pattern.search(fmt):
56
                self.assertFalse(True, ('"message" not allowed in '
5050.8.3 by Parth Malwankar
use __subclasses__
57
                    '"errors.%s._fmt"' % c.__name__))
5050.8.1 by Parth Malwankar
added test to ensure that BzrError subclasses dont use "message" as a name
58
3287.20.2 by John Arbash Meinel
Raise a clear error about the offending filename when there is a filename with bad characters.
59
    def test_bad_filename_encoding(self):
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
60
        error = errors.BadFilenameEncoding(b'bad/filen\xe5me', 'UTF-8')
6670.3.2 by Martin
Avoid PendingDeprecationWarning from assertRegexpMatches
61
        self.assertContainsRe(
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
62
            str(error),
63
            "^Filename b?'bad/filen\\\\xe5me' is not valid in your current"
64
            " filesystem encoding UTF-8$")
3287.20.2 by John Arbash Meinel
Raise a clear error about the offending filename when there is a filename with bad characters.
65
2255.7.16 by John Arbash Meinel
Make sure adding a duplicate file_id raises DuplicateFileId.
66
    def test_duplicate_file_id(self):
67
        error = errors.DuplicateFileId('a_file_id', 'foo')
68
        self.assertEqualDiff('File id {a_file_id} already exists in inventory'
69
                             ' as foo', str(error))
70
2432.1.19 by Robert Collins
Ensure each HelpIndex has a unique prefix.
71
    def test_duplicate_help_prefix(self):
72
        error = errors.DuplicateHelpPrefix('foo')
73
        self.assertEqualDiff('The prefix foo is in the help search path twice.',
74
            str(error))
75
3445.1.1 by John Arbash Meinel
Start working on a new Graph api to make finding revision numbers faster.
76
    def test_ghost_revisions_have_no_revno(self):
77
        error = errors.GhostRevisionsHaveNoRevno('target', 'ghost_rev')
78
        self.assertEqualDiff("Could not determine revno for {target} because"
79
                             " its ancestry shows a ghost at {ghost_rev}",
80
                             str(error))
81
6672.1.2 by Jelmer Vernooij
Remove breezy.api.
82
    def test_incompatibleVersion(self):
83
        error = errors.IncompatibleVersion("module", [(4, 5, 6), (7, 8, 9)],
84
                (1, 2, 3))
2550.2.3 by Robert Collins
Add require_api API.
85
        self.assertEqualDiff(
6672.1.2 by Jelmer Vernooij
Remove breezy.api.
86
            'API module is not compatible; one of versions '
87
            '[(4, 5, 6), (7, 8, 9)] is required, but current version is '
88
            '(1, 2, 3).',
2550.2.3 by Robert Collins
Add require_api API.
89
            str(error))
90
3207.2.2 by John Arbash Meinel
Fix bug #187169, when an invalid delta is supplied to update_basis_by_delta
91
    def test_inconsistent_delta(self):
92
        error = errors.InconsistentDelta('path', 'file-id', 'reason for foo')
93
        self.assertEqualDiff(
3221.1.8 by Martin Pool
Update error format in test_inconsistent_delta
94
            "An inconsistent delta was supplied involving 'path', 'file-id'\n"
3207.2.2 by John Arbash Meinel
Fix bug #187169, when an invalid delta is supplied to update_basis_by_delta
95
            "reason: reason for foo",
96
            str(error))
97
4505.5.1 by Robert Collins
Add more generic InconsistentDeltaDelta error class for use when the exact cause of an inconsistent delta isn't trivially accessible.
98
    def test_inconsistent_delta_delta(self):
99
        error = errors.InconsistentDeltaDelta([], 'reason')
100
        self.assertEqualDiff(
101
            "An inconsistent delta was supplied: []\nreason: reason",
102
            str(error))
103
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
104
    def test_in_process_transport(self):
105
        error = errors.InProcessTransport('fpp')
106
        self.assertEqualDiff(
107
            "The transport 'fpp' is only accessible within this process.",
108
            str(error))
109
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
110
    def test_invalid_http_range(self):
111
        error = errors.InvalidHttpRange('path',
112
                                        'Content-Range: potatoes 0-00/o0oo0',
113
                                        'bad range')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
114
        self.assertEqual("Invalid http range"
115
                         " 'Content-Range: potatoes 0-00/o0oo0'"
116
                         " for path: bad range",
117
                         str(error))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
118
119
    def test_invalid_range(self):
120
        error = errors.InvalidRange('path', 12, 'bad range')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
121
        self.assertEqual("Invalid range access in path at 12: bad range",
122
                         str(error))
3059.2.12 by Vincent Ladeuil
Spiv review feedback.
123
1986.5.3 by Robert Collins
New method ``WorkingTree.flush()`` which will write the current memory
124
    def test_inventory_modified(self):
125
        error = errors.InventoryModified("a tree to be repred")
126
        self.assertEqualDiff("The current inventory for the tree 'a tree to "
127
            "be repred' has been modified, so a clean inventory cannot be "
128
            "read without data loss.",
129
            str(error))
130
4294.2.8 by Robert Collins
Reduce round trips pushing new branches substantially.
131
    def test_jail_break(self):
132
        error = errors.JailBreak("some url")
133
        self.assertEqualDiff("An attempt to access a url outside the server"
134
            " jail was made: 'some url'.",
135
            str(error))
136
2255.2.145 by Robert Collins
Support unbreakable locks for trees.
137
    def test_lock_active(self):
138
        error = errors.LockActive("lock description")
139
        self.assertEqualDiff("The lock for 'lock description' is in use and "
140
            "cannot be broken.",
141
            str(error))
142
4634.161.1 by Andrew Bennetts
Add LockCorrupt error, and use it to provide nicer handling of unparseable lock/held/info files.
143
    def test_lock_corrupt(self):
144
        error = errors.LockCorrupt("corruption info")
145
        self.assertEqualDiff("Lock is apparently held, but corrupted: "
146
            "corruption info\n"
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
147
            "Use 'brz break-lock' to clear it",
4634.161.1 by Andrew Bennetts
Add LockCorrupt error, and use it to provide nicer handling of unparseable lock/held/info files.
148
            str(error))
149
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
150
    def test_knit_data_stream_incompatible(self):
151
        error = errors.KnitDataStreamIncompatible(
152
            'stream format', 'target format')
153
        self.assertEqual('Cannot insert knit data stream of format '
154
                         '"stream format" into knit of format '
155
                         '"target format".', str(error))
156
3052.2.1 by Robert Collins
Add a new KnitDataStreamUnknown error class for showing formats we can't understand.
157
    def test_knit_data_stream_unknown(self):
158
        error = errors.KnitDataStreamUnknown(
159
            'stream format')
160
        self.assertEqual('Cannot parse knit data stream of format '
161
                         '"stream format".', str(error))
162
2171.1.1 by John Arbash Meinel
Knit index files should ignore empty indexes rather than consider them corrupt.
163
    def test_knit_header_error(self):
164
        error = errors.KnitHeaderError('line foo\n', 'path/to/file')
165
        self.assertEqual("Knit header error: 'line foo\\n' unexpected"
2745.3.2 by Daniel Watkins
Updated tests to reflect new error text.
166
                         " for file \"path/to/file\".", str(error))
2171.1.1 by John Arbash Meinel
Knit index files should ignore empty indexes rather than consider them corrupt.
167
2196.2.5 by John Arbash Meinel
Add an exception class when the knit index storage method is unknown, and properly test for it
168
    def test_knit_index_unknown_method(self):
169
        error = errors.KnitIndexUnknownMethod('http://host/foo.kndx',
170
                                              ['bad', 'no-eol'])
171
        self.assertEqual("Knit index http://host/foo.kndx does not have a"
172
                         " known method in options: ['bad', 'no-eol']",
173
                         str(error))
174
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
175
    def test_medium_not_connected(self):
176
        error = errors.MediumNotConnected("a medium")
177
        self.assertEqualDiff(
178
            "The medium 'a medium' is not connected.", str(error))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
179
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
180
    def test_no_smart_medium(self):
181
        error = errors.NoSmartMedium("a transport")
182
        self.assertEqualDiff("The transport 'a transport' cannot tunnel the "
183
            "smart protocol.",
184
            str(error))
185
1988.2.1 by Robert Collins
WorkingTree has a new api ``unversion`` which allow the unversioning of
186
    def test_no_such_id(self):
187
        error = errors.NoSuchId("atree", "anid")
2745.3.2 by Daniel Watkins
Updated tests to reflect new error text.
188
        self.assertEqualDiff("The file id \"anid\" is not present in the tree "
2745.3.3 by Daniel Watkins
Changed to remove need for escaping of quotes.
189
            "atree.",
1988.2.1 by Robert Collins
WorkingTree has a new api ``unversion`` which allow the unversioning of
190
            str(error))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
191
1908.11.1 by Robert Collins
Add a new method ``Tree.revision_tree`` which allows access to cached
192
    def test_no_such_revision_in_tree(self):
193
        error = errors.NoSuchRevisionInTree("atree", "anid")
2745.3.3 by Daniel Watkins
Changed to remove need for escaping of quotes.
194
        self.assertEqualDiff("The revision id {anid} is not present in the"
195
                             " tree atree.", str(error))
1908.11.1 by Robert Collins
Add a new method ``Tree.revision_tree`` which allows access to cached
196
        self.assertIsInstance(error, errors.NoSuchRevision)
197
3221.11.2 by Robert Collins
Create basic stackable branch facility.
198
    def test_not_stacked(self):
199
        error = errors.NotStacked('a branch')
200
        self.assertEqualDiff("The branch 'a branch' is not stacked.",
201
            str(error))
202
1986.5.3 by Robert Collins
New method ``WorkingTree.flush()`` which will write the current memory
203
    def test_not_write_locked(self):
204
        error = errors.NotWriteLocked('a thing to repr')
205
        self.assertEqualDiff("'a thing to repr' is not write locked but needs "
206
            "to be.",
207
            str(error))
208
2872.5.1 by Martin Pool
Avoid internal error tracebacks on failure to lock on readonly transport (#129701).
209
    def test_lock_failed(self):
210
        error = errors.LockFailed('http://canonical.com/', 'readonly transport')
211
        self.assertEqualDiff("Cannot lock http://canonical.com/: readonly transport",
212
            str(error))
213
        self.assertFalse(error.internal_error)
214
2018.2.4 by Robert Collins
separate out the client medium from the client encoding protocol for the smart server.
215
    def test_too_many_concurrent_requests(self):
216
        error = errors.TooManyConcurrentRequests("a medium")
217
        self.assertEqualDiff("The medium 'a medium' has reached its concurrent "
218
            "request limit. Be sure to finish_writing and finish_reading on "
2018.5.134 by Andrew Bennetts
Fix the TooManyConcurrentRequests error message.
219
            "the currently open request.",
2018.2.4 by Robert Collins
separate out the client medium from the client encoding protocol for the smart server.
220
            str(error))
221
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
222
    def test_unavailable_representation(self):
223
        error = errors.UnavailableRepresentation(('key',), "mpdiff", "fulltext")
224
        self.assertEqualDiff("The encoding 'mpdiff' is not available for key "
225
            "('key',) which is encoded as 'fulltext'.",
226
            str(error))
227
4462.3.2 by Robert Collins
Do not stack on the same branch/repository anymore. This was never supported and would generally result in infinite recursion. Fixes bug 376243.
228
    def test_unstackable_location(self):
229
        error = errors.UnstackableLocationError('foo', 'bar')
230
        self.assertEqualDiff("The branch 'foo' cannot be stacked on 'bar'.",
231
            str(error))
232
3221.11.2 by Robert Collins
Create basic stackable branch facility.
233
    def test_unstackable_repository_format(self):
234
        format = u'foo'
235
        url = "/foo"
236
        error = errors.UnstackableRepositoryFormat(format, url)
237
        self.assertEqualDiff(
238
            "The repository '/foo'(foo) is not a stackable format. "
239
            "You will need to upgrade the repository to permit branch stacking.",
240
            str(error))
241
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
242
    def test_up_to_date(self):
5582.10.51 by Jelmer Vernooij
Remove use of BzrDirFormat4 in test_errors.
243
        error = errors.UpToDateFormat("someformat")
244
        self.assertEqualDiff(
245
            "The branch format someformat is already at the most "
246
            "recent format.", str(error))
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
247
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
248
    def test_read_error(self):
249
        # a unicode path to check that %r is being used.
250
        path = u'a path'
251
        error = errors.ReadError(path)
6670.3.2 by Martin
Avoid PendingDeprecationWarning from assertRegexpMatches
252
        self.assertContainsRe(str(error), "^Error reading from u?'a path'.$")
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
253
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
254
    def test_bzrerror_from_literal_string(self):
255
        # Some code constructs BzrError from a literal string, in which case
256
        # no further formatting is done.  (I'm not sure raising the base class
257
        # is a great idea, but if the exception is not intended to be caught
258
        # perhaps no more is needed.)
259
        try:
260
            raise errors.BzrError('this is my errors; %d is not expanded')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
261
        except errors.BzrError as e:
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
262
            self.assertEqual('this is my errors; %d is not expanded', str(e))
263
2018.2.4 by Robert Collins
separate out the client medium from the client encoding protocol for the smart server.
264
    def test_reading_completed(self):
265
        error = errors.ReadingCompleted("a request")
266
        self.assertEqualDiff("The MediumRequest 'a request' has already had "
267
            "finish_reading called upon it - the request has been completed and"
268
            " no more data may be read.",
269
            str(error))
270
271
    def test_writing_completed(self):
272
        error = errors.WritingCompleted("a request")
273
        self.assertEqualDiff("The MediumRequest 'a request' has already had "
274
            "finish_writing called upon it - accept bytes may not be called "
275
            "anymore.",
276
            str(error))
277
278
    def test_writing_not_completed(self):
279
        error = errors.WritingNotComplete("a request")
280
        self.assertEqualDiff("The MediumRequest 'a request' has not has "
281
            "finish_writing called upon it - until the write phase is complete"
282
            " no data may be read.",
283
            str(error))
284
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
285
    def test_transport_not_possible(self):
286
        error = errors.TransportNotPossible('readonly', 'original error')
287
        self.assertEqualDiff('Transport operation not possible:'
288
                         ' readonly original error', str(error))
2052.4.4 by John Arbash Meinel
Create a SocketConnectionError to make creating nice errors easier
289
290
    def assertSocketConnectionError(self, expected, *args, **kwargs):
291
        """Check the formatting of a SocketConnectionError exception"""
292
        e = errors.SocketConnectionError(*args, **kwargs)
293
        self.assertEqual(expected, str(e))
294
295
    def test_socket_connection_error(self):
296
        """Test the formatting of SocketConnectionError"""
297
298
        # There should be a default msg about failing to connect
299
        # we only require a host name.
300
        self.assertSocketConnectionError(
301
            'Failed to connect to ahost',
302
            'ahost')
303
304
        # If port is None, we don't put :None
305
        self.assertSocketConnectionError(
306
            'Failed to connect to ahost',
307
            'ahost', port=None)
308
        # But if port is supplied we include it
309
        self.assertSocketConnectionError(
310
            'Failed to connect to ahost:22',
311
            'ahost', port=22)
312
313
        # We can also supply extra information about the error
314
        # with or without a port
315
        self.assertSocketConnectionError(
316
            'Failed to connect to ahost:22; bogus error',
317
            'ahost', port=22, orig_error='bogus error')
318
        self.assertSocketConnectionError(
319
            'Failed to connect to ahost; bogus error',
320
            'ahost', orig_error='bogus error')
321
        # An exception object can be passed rather than a string
322
        orig_error = ValueError('bad value')
323
        self.assertSocketConnectionError(
324
            'Failed to connect to ahost; %s' % (str(orig_error),),
325
            host='ahost', orig_error=orig_error)
326
327
        # And we can supply a custom failure message
328
        self.assertSocketConnectionError(
329
            'Unable to connect to ssh host ahost:444; my_error',
330
            host='ahost', port=444, msg='Unable to connect to ssh host',
331
            orig_error='my_error')
332
3535.8.2 by James Westby
Incorporate spiv's feedback.
333
    def test_target_not_branch(self):
334
        """Test the formatting of TargetNotBranch."""
335
        error = errors.TargetNotBranch('foo')
336
        self.assertEqual(
337
            "Your branch does not have all of the revisions required in "
3535.8.4 by James Westby
Replace "however" with "and" at John's request.
338
            "order to merge this merge directive and the target "
3535.8.3 by James Westby
Use location instead of branch as suggested by Robert.
339
            "location specified in the merge directive is not a branch: "
3535.8.2 by James Westby
Incorporate spiv's feedback.
340
            "foo.", str(error))
341
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
342
    def test_unexpected_smart_server_response(self):
343
        e = errors.UnexpectedSmartServerResponse(('not yes',))
344
        self.assertEqual(
345
            "Could not understand response from smart server: ('not yes',)",
346
            str(e))
2052.4.4 by John Arbash Meinel
Create a SocketConnectionError to make creating nice errors easier
347
2506.2.1 by Andrew Bennetts
Start implementing container format reading and writing.
348
    def test_unknown_container_format(self):
349
        """Test the formatting of UnknownContainerFormatError."""
350
        e = errors.UnknownContainerFormatError('bad format string')
351
        self.assertEqual(
352
            "Unrecognised container format: 'bad format string'",
353
            str(e))
354
355
    def test_unexpected_end_of_container(self):
356
        """Test the formatting of UnexpectedEndOfContainerError."""
357
        e = errors.UnexpectedEndOfContainerError()
358
        self.assertEqual(
359
            "Unexpected end of container stream", str(e))
360
361
    def test_unknown_record_type(self):
362
        """Test the formatting of UnknownRecordTypeError."""
363
        e = errors.UnknownRecordTypeError("X")
364
        self.assertEqual(
365
            "Unknown record type: 'X'",
366
            str(e))
367
2506.3.1 by Andrew Bennetts
More progress:
368
    def test_invalid_record(self):
369
        """Test the formatting of InvalidRecordError."""
370
        e = errors.InvalidRecordError("xxx")
371
        self.assertEqual(
372
            "Invalid record: xxx",
373
            str(e))
374
2506.2.6 by Andrew Bennetts
Add validate method to ContainerReader and BytesRecordReader.
375
    def test_container_has_excess_data(self):
376
        """Test the formatting of ContainerHasExcessDataError."""
377
        e = errors.ContainerHasExcessDataError("excess bytes")
378
        self.assertEqual(
379
            "Container has data after end marker: 'excess bytes'",
380
            str(e))
381
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
382
    def test_duplicate_record_name_error(self):
383
        """Test the formatting of DuplicateRecordNameError."""
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
384
        e = errors.DuplicateRecordNameError(b"n\xc3\xa5me")
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
385
        self.assertEqual(
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
386
            u"Container has multiple records with the same name: n\xe5me",
387
            text_type(e))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
388
2854.1.1 by Martin Pool
Fix "unprintable error" message for BzrCheckError and others
389
    def test_check_error(self):
390
        e = errors.BzrCheckError('example check failure')
391
        self.assertEqual(
392
            "Internal check failed: example check failure",
393
            str(e))
394
        self.assertTrue(e.internal_error)
2506.6.1 by Andrew Bennetts
Return a callable instead of a str from read, and add more validation.
395
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
396
    def test_repository_data_stream_error(self):
397
        """Test the formatting of RepositoryDataStreamError."""
398
        e = errors.RepositoryDataStreamError(u"my reason")
399
        self.assertEqual(
400
            "Corrupt or incompatible data stream: my reason", str(e))
401
2978.2.1 by Alexander Belchenko
fix formatting of ImmortalPendingDeletion error message.
402
    def test_immortal_pending_deletion_message(self):
403
        err = errors.ImmortalPendingDeletion('foo')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
404
        self.assertEqual(
2978.2.1 by Alexander Belchenko
fix formatting of ImmortalPendingDeletion error message.
405
            "Unable to delete transform temporary directory foo.  "
406
            "Please examine foo to see if it contains any files "
407
            "you wish to keep, and delete it when you are done.",
408
            str(err))
409
3006.2.2 by Alexander Belchenko
tests added.
410
    def test_unable_create_symlink(self):
411
        err = errors.UnableCreateSymlink()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
412
        self.assertEqual(
3006.2.2 by Alexander Belchenko
tests added.
413
            "Unable to create symlink on this platform",
414
            str(err))
415
        err = errors.UnableCreateSymlink(path=u'foo')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
416
        self.assertEqual(
3006.2.2 by Alexander Belchenko
tests added.
417
            "Unable to create symlink 'foo' on this platform",
418
            str(err))
419
        err = errors.UnableCreateSymlink(path=u'\xb5')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
420
        self.assertEqual(
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
421
            "Unable to create symlink %s on this platform" % repr(u'\xb5'),
3006.2.2 by Alexander Belchenko
tests added.
422
            str(err))
423
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
424
    def test_invalid_url_join(self):
425
        """Test the formatting of InvalidURLJoin."""
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
426
        e = urlutils.InvalidURLJoin('Reason', 'base path', ('args',))
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
427
        self.assertEqual(
428
            "Invalid URL join request: Reason: 'base path' + ('args',)",
429
            str(e))
430
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
431
    def test_unable_encode_path(self):
432
        err = errors.UnableEncodePath('foo', 'executable')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
433
        self.assertEqual("Unable to encode executable path 'foo' in "
434
                         "user encoding " + osutils.get_user_encoding(),
435
                         str(err))
3234.2.6 by Alexander Belchenko
because every mail client has different rules to compose command line we should encode arguments to 8 bit string only when needed.
436
3246.3.4 by Daniel Watkins
Added test.
437
    def test_unknown_format(self):
438
        err = errors.UnknownFormatError('bar', kind='foo')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
439
        self.assertEqual("Unknown foo format: 'bar'", str(err))
3246.3.4 by Daniel Watkins
Added test.
440
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
441
    def test_tip_change_rejected(self):
442
        err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
443
        self.assertEqual(
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
444
            u'Tip change rejected: Unicode message\N{INTERROBANG}',
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
445
            text_type(err))
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
446
3690.1.1 by Andrew Bennetts
Unexpected error responses from a smart server no longer cause the client to traceback.
447
    def test_error_from_smart_server(self):
448
        error_tuple = ('error', 'tuple')
449
        err = errors.ErrorFromSmartServer(error_tuple)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
450
        self.assertEqual(
3690.1.1 by Andrew Bennetts
Unexpected error responses from a smart server no longer cause the client to traceback.
451
            "Error received from smart server: ('error', 'tuple')", str(err))
452
453
    def test_untranslateable_error_from_smart_server(self):
454
        error_tuple = ('error', 'tuple')
455
        orig_err = errors.ErrorFromSmartServer(error_tuple)
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
456
        err = errors.UnknownErrorFromSmartServer(orig_err)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
457
        self.assertEqual(
3690.1.1 by Andrew Bennetts
Unexpected error responses from a smart server no longer cause the client to traceback.
458
            "Server sent an unexpected error: ('error', 'tuple')", str(err))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
459
3883.2.3 by Andrew Bennetts
Add test, tweak traceback formatting.
460
    def test_smart_message_handler_error(self):
461
        # Make an exc_info tuple.
462
        try:
463
            raise Exception("example error")
464
        except Exception:
5340.15.2 by John Arbash Meinel
supercede 2.4-613247-cleanup-tests
465
            err = errors.SmartMessageHandlerError(sys.exc_info())
466
        # GZ 2010-11-08: Should not store exc_info in exception instances.
467
        try:
468
            self.assertStartsWith(
469
                str(err), "The message handler raised an exception:\n")
470
            self.assertEndsWith(str(err), "Exception: example error\n")
471
        finally:
472
            del err
3690.1.1 by Andrew Bennetts
Unexpected error responses from a smart server no longer cause the client to traceback.
473
4002.1.7 by Andrew Bennetts
Rename UnresumableWriteGroups to UnresumableWriteGroup.
474
    def test_unresumable_write_group(self):
4002.1.1 by Andrew Bennetts
Implement suspend_write_group/resume_write_group.
475
        repo = "dummy repo"
476
        wg_tokens = ['token']
477
        reason = "a reason"
4002.1.7 by Andrew Bennetts
Rename UnresumableWriteGroups to UnresumableWriteGroup.
478
        err = errors.UnresumableWriteGroup(repo, wg_tokens, reason)
4002.1.1 by Andrew Bennetts
Implement suspend_write_group/resume_write_group.
479
        self.assertEqual(
4002.1.7 by Andrew Bennetts
Rename UnresumableWriteGroups to UnresumableWriteGroup.
480
            "Repository dummy repo cannot resume write group "
4002.1.1 by Andrew Bennetts
Implement suspend_write_group/resume_write_group.
481
            "['token']: a reason", str(err))
482
483
    def test_unsuspendable_write_group(self):
484
        repo = "dummy repo"
485
        err = errors.UnsuspendableWriteGroup(repo)
486
        self.assertEqual(
487
            'Repository dummy repo cannot suspend a write group.', str(err))
488
4734.4.9 by Andrew Bennetts
More tests and comments.
489
    def test_not_branch_no_args(self):
490
        err = errors.NotBranchError('path')
491
        self.assertEqual('Not a branch: "path".', str(err))
492
5050.46.1 by Andrew Bennetts
Suppress unexpected errors during NotBranchError's call to open_repository.
493
    def test_not_branch_bzrdir_with_recursive_not_branch_error(self):
494
        class FakeBzrDir(object):
495
            def open_repository(self):
496
                # str() on the NotBranchError will trigger a call to this,
497
                # which in turn will another, identical NotBranchError.
6653.6.6 by Jelmer Vernooij
Fix remaining tests.
498
                raise errors.NotBranchError('path', controldir=FakeBzrDir())
499
        err = errors.NotBranchError('path', controldir=FakeBzrDir())
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
500
        self.assertEqual('Not a branch: "path": NotBranchError.', str(err))
4734.4.9 by Andrew Bennetts
More tests and comments.
501
5050.7.6 by Parth Malwankar
fixed test name
502
    def test_recursive_bind(self):
5050.7.5 by Parth Malwankar
better error message for RecursiveBind
503
        error = errors.RecursiveBind('foo_bar_branch')
504
        msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
505
            'Please use `brz unbind` to fix.')
5050.7.5 by Parth Malwankar
better error message for RecursiveBind
506
        self.assertEqualDiff(msg, str(error))
507
5609.58.1 by Andrew Bennetts
Fix 'Unprintable exception' when displaying RetryWithNewPacks error.
508
    def test_retry_with_new_packs(self):
509
        fake_exc_info = ('{exc type}', '{exc value}', '{exc traceback}')
510
        error = errors.RetryWithNewPacks(
511
            '{context}', reload_occurred=False, exc_info=fake_exc_info)
512
        self.assertEqual(
513
            'Pack files have changed, reload and retry. context: '
514
            '{context} {exc value}', str(error))
515
2116.3.1 by John Arbash Meinel
Cleanup error tests
516
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
517
class PassThroughError(errors.BzrError):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
518
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
519
    _fmt = """Pass through %(foo)s and %(bar)s"""
2116.3.1 by John Arbash Meinel
Cleanup error tests
520
521
    def __init__(self, foo, bar):
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
522
        errors.BzrError.__init__(self, foo=foo, bar=bar)
523
524
525
class ErrorWithBadFormat(errors.BzrError):
526
527
    _fmt = """One format specifier: %(thing)s"""
528
529
530
class ErrorWithNoFormat(errors.BzrError):
5131.2.6 by Martin
Fix more tests which were failing under -OO that had been missed earlier
531
    __doc__ = """This class has a docstring but no format string."""
2116.3.1 by John Arbash Meinel
Cleanup error tests
532
533
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
534
class TestErrorFormatting(tests.TestCase):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
535
2116.3.1 by John Arbash Meinel
Cleanup error tests
536
    def test_always_str(self):
537
        e = PassThroughError(u'\xb5', 'bar')
538
        self.assertIsInstance(e.__str__(), str)
539
        # In Python str(foo) *must* return a real byte string
540
        # not a Unicode string. The following line would raise a
541
        # Unicode error, because it tries to call str() on the string
542
        # returned from e.__str__(), and it has non ascii characters
543
        s = str(e)
544
        self.assertEqual('Pass through \xc2\xb5 and bar', s)
545
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
546
    def test_missing_format_string(self):
547
        e = ErrorWithNoFormat(param='randomvalue')
6318.2.1 by Martin Packman
Remove deprecated classes and practices from bzrlib.errors
548
        self.assertStartsWith(str(e),
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
549
                              "Unprintable exception ErrorWithNoFormat")
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
550
2116.3.1 by John Arbash Meinel
Cleanup error tests
551
    def test_mismatched_format_args(self):
552
        # Even though ErrorWithBadFormat's format string does not match the
553
        # arguments we constructing it with, we can still stringify an instance
554
        # of this exception. The resulting string will say its unprintable.
555
        e = ErrorWithBadFormat(not_thing='x')
556
        self.assertStartsWith(
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
557
            str(e), 'Unprintable exception ErrorWithBadFormat')
4634.1.2 by Martin Pool
Add test for CannotBindAddress
558
559
    def test_cannot_bind_address(self):
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
560
        # see <https://bugs.launchpad.net/bzr/+bug/286871>
4634.1.2 by Martin Pool
Add test for CannotBindAddress
561
        e = errors.CannotBindAddress('example.com', 22,
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
562
                                     socket.error(13, 'Permission denied'))
563
        self.assertContainsRe(
564
            str(e),
4634.1.2 by Martin Pool
Add test for CannotBindAddress
565
            r'Cannot bind address "example\.com:22":.*Permission denied')
4976.1.1 by Jelmer Vernooij
Add FileTimestampUnavailable exception.
566
5186.2.6 by Martin Pool
Add formatting test for TransformRenameFailed
567
    def test_transform_rename_failed(self):
5186.2.7 by Martin Pool
Update other cases where transform detects failure to rename
568
        e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
569
        self.assertEqual(
5186.2.6 by Martin Pool
Add formatting test for TransformRenameFailed
570
            u"Failed to rename from to to: readonly file",
571
            str(e))
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
572
573
574
class TestErrorsUsingTransport(tests.TestCaseWithMemoryTransport):
575
    """Tests for errors that need to use a branch or repo."""
576
577
    def test_no_public_branch(self):
578
        b = self.make_branch('.')
579
        error = errors.NoPublicBranch(b)
580
        url = urlutils.unescape_for_display(b.base, 'ascii')
581
        self.assertEqualDiff(
582
            'There is no public branch set for "%s".' % url, str(error))
583
584
    def test_no_repo(self):
585
        dir = controldir.ControlDir.create(self.get_url())
586
        error = errors.NoRepositoryPresent(dir)
587
        self.assertNotEqual(-1, str(error).find((dir.transport.clone('..').base)))
588
        self.assertEqual(-1, str(error).find((dir.transport.base)))
589
590
    def test_corrupt_repository(self):
591
        repo = self.make_repository('.')
592
        error = errors.CorruptRepository(repo)
593
        self.assertEqualDiff("An error has been detected in the repository %s.\n"
594
                             "Please run brz reconcile on this repository." %
6653.6.6 by Jelmer Vernooij
Fix remaining tests.
595
                             repo.controldir.root_transport.base,
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
596
                             str(error))
597
598
    def test_not_branch_bzrdir_with_repo(self):
6653.6.6 by Jelmer Vernooij
Fix remaining tests.
599
        controldir = self.make_repository('repo').controldir
600
        err = errors.NotBranchError('path', controldir=controldir)
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
601
        self.assertEqual(
602
            'Not a branch: "path": location is a repository.', str(err))
603
604
    def test_not_branch_bzrdir_without_repo(self):
6653.6.6 by Jelmer Vernooij
Fix remaining tests.
605
        controldir = self.make_controldir('bzrdir')
606
        err = errors.NotBranchError('path', controldir=controldir)
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
607
        self.assertEqual('Not a branch: "path".', str(err))
608
609
    def test_not_branch_laziness(self):
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
610
        real_bzrdir = self.make_controldir('path')
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
611
        class FakeBzrDir(object):
612
            def __init__(self):
613
                self.calls = []
614
            def open_repository(self):
615
                self.calls.append('open_repository')
616
                raise errors.NoRepositoryPresent(real_bzrdir)
617
        fake_bzrdir = FakeBzrDir()
6653.6.6 by Jelmer Vernooij
Fix remaining tests.
618
        err = errors.NotBranchError('path', controldir=fake_bzrdir)
6670.3.1 by Martin
Initial work to make errors module Python 3 compatible
619
        self.assertEqual([], fake_bzrdir.calls)
620
        str(err)
621
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
622
        # Stringifying twice doesn't try to open a repository twice.
623
        str(err)
624
        self.assertEqual(['open_repository'], fake_bzrdir.calls)