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