1
# Copyright (C) 2006-2011 Canonical Ltd
1
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
30
from bzrlib.tests import (
32
TestCaseWithTransport,
29
from bzrlib.tests import TestCase, TestCaseWithTransport
37
32
class TestErrors(TestCaseWithTransport):
39
def test_no_arg_named_message(self):
40
"""Ensure the __init__ and _fmt in errors do not have "message" arg.
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.
47
fmt_pattern = re.compile("%\(message\)[sir]")
48
subclasses_present = getattr(errors.BzrError, '__subclasses__', None)
49
if not subclasses_present:
50
raise TestSkipped('__subclasses__ attribute required for classes. '
51
'Requires Python 2.5 or later.')
52
for c in errors.BzrError.__subclasses__():
53
init = getattr(c, '__init__', None)
54
fmt = getattr(c, '_fmt', None)
56
args = inspect.getargspec(init)[0]
57
self.assertFalse('message' in args,
58
('Argument name "message" not allowed for '
59
'"errors.%s.__init__"' % c.__name__))
60
if fmt and fmt_pattern.search(fmt):
61
self.assertFalse(True, ('"message" not allowed in '
62
'"errors.%s._fmt"' % c.__name__))
64
34
def test_bad_filename_encoding(self):
65
35
error = errors.BadFilenameEncoding('bad/filen\xe5me', 'UTF-8')
66
36
self.assertEqualDiff(
162
132
"cannot be broken.",
165
def test_lock_corrupt(self):
166
error = errors.LockCorrupt("corruption info")
167
self.assertEqualDiff("Lock is apparently held, but corrupted: "
169
"Use 'bzr break-lock' to clear it",
172
135
def test_knit_data_stream_incompatible(self):
173
136
error = errors.KnitDataStreamIncompatible(
174
137
'stream format', 'target format')
302
265
def test_up_to_date(self):
303
error = errors.UpToDateFormat("someformat")
304
self.assertEqualDiff(
305
"The branch format someformat is already at the most "
306
"recent format.", str(error))
266
error = errors.UpToDateFormat(bzrdir.BzrDirFormat4())
267
self.assertEqualDiff("The branch format All-in-one "
268
"format 4 is already at the most "
308
272
def test_corrupt_repository(self):
309
273
repo = self.make_repository('.')
579
543
except ZeroDivisionError:
580
err = errors.HookFailed('hook stage', 'hook name', sys.exc_info(),
582
# GZ 2010-11-08: Should not store exc_info in exception instances, but
583
# HookFailed is deprecated anyway and could maybe go.
585
self.assertStartsWith(
586
str(err), 'Hook \'hook name\' during hook stage failed:\n')
588
str(err), 'integer division or modulo by zero')
544
exc_info = sys.exc_info()
545
err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
546
self.assertStartsWith(
547
str(err), 'Hook \'hook name\' during hook stage failed:\n')
549
str(err), 'integer division or modulo by zero')
592
551
def test_tip_change_rejected(self):
593
552
err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
617
576
raise Exception("example error")
618
577
except Exception:
619
err = errors.SmartMessageHandlerError(sys.exc_info())
620
# GZ 2010-11-08: Should not store exc_info in exception instances.
622
self.assertStartsWith(
623
str(err), "The message handler raised an exception:\n")
624
self.assertEndsWith(str(err), "Exception: example error\n")
578
exc_info = sys.exc_info()
579
err = errors.SmartMessageHandlerError(exc_info)
580
self.assertStartsWith(
581
str(err), "The message handler raised an exception:\n")
582
self.assertEndsWith(str(err), "Exception: example error\n")
628
584
def test_must_have_working_tree(self):
629
585
err = errors.MustHaveWorkingTree('foo', 'bar')
682
638
err = errors.NotBranchError('path', bzrdir=bzrdir)
683
639
self.assertEqual('Not a branch: "path".', str(err))
685
def test_not_branch_bzrdir_with_recursive_not_branch_error(self):
686
class FakeBzrDir(object):
687
def open_repository(self):
688
# str() on the NotBranchError will trigger a call to this,
689
# which in turn will another, identical NotBranchError.
690
raise errors.NotBranchError('path', bzrdir=FakeBzrDir())
691
err = errors.NotBranchError('path', bzrdir=FakeBzrDir())
692
self.assertEqual('Not a branch: "path".', str(err))
694
641
def test_not_branch_laziness(self):
695
642
real_bzrdir = self.make_bzrdir('path')
696
643
class FakeBzrDir(object):
709
656
self.assertEqual(['open_repository'], fake_bzrdir.calls)
711
def test_invalid_pattern(self):
712
error = errors.InvalidPattern('Bad pattern msg.')
713
self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
716
def test_recursive_bind(self):
717
error = errors.RecursiveBind('foo_bar_branch')
718
msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
719
'Please use `bzr unbind` to fix.')
720
self.assertEqualDiff(msg, str(error))
723
659
class PassThroughError(errors.BzrError):
767
703
str(e), 'Unprintable exception ErrorWithBadFormat')
769
705
def test_cannot_bind_address(self):
770
# see <https://bugs.launchpad.net/bzr/+bug/286871>
706
# see <https://bugs.edge.launchpad.net/bzr/+bug/286871>
771
707
e = errors.CannotBindAddress('example.com', 22,
772
708
socket.error(13, 'Permission denied'))
773
709
self.assertContainsRe(str(e),
777
713
e = errors.FileTimestampUnavailable("/path/foo")
778
714
self.assertEquals("The filestamp for /path/foo is not available.",
781
def test_transform_rename_failed(self):
782
e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
784
u"Failed to rename from to to: readonly file",