29
from bzrlib.tests import TestCase, TestCaseWithTransport
31
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
32
34
class TestErrors(TestCaseWithTransport):
36
def test_no_arg_named_message(self):
37
"""Ensure the __init__ and _fmt in errors do not have "message" arg.
39
This test fails if __init__ or _fmt in errors has an argument
40
named "message" as this can cause errors in some Python versions.
41
Python 2.5 uses a slot for StandardError.message.
44
fmt_pattern = re.compile("%\(message\)[sir]")
45
subclasses_present = getattr(errors.BzrError, '__subclasses__', None)
46
if not subclasses_present:
47
raise TestSkipped('__subclasses__ attribute required for classes. '
48
'Requires Python 2.5 or later.')
49
for c in errors.BzrError.__subclasses__():
50
init = getattr(c, '__init__', None)
51
fmt = getattr(c, '_fmt', None)
53
args = inspect.getargspec(init)[0]
54
self.assertFalse('message' in args,
55
('Argument name "message" not allowed for '
56
'"errors.%s.__init__"' % c.__name__))
57
if fmt and fmt_pattern.search(fmt):
58
self.assertFalse(True, ('"message" not allowed in '
59
'"errors.%s._fmt"' % c.__name__))
34
61
def test_bad_filename_encoding(self):
35
62
error = errors.BadFilenameEncoding('bad/filen\xe5me', 'UTF-8')
36
63
self.assertEqualDiff(
114
147
"read without data loss.",
117
def test_install_failed(self):
118
error = errors.InstallFailed(['rev-one'])
119
self.assertEqual("Could not install revisions:\nrev-one", str(error))
120
error = errors.InstallFailed(['rev-one', 'rev-two'])
121
self.assertEqual("Could not install revisions:\nrev-one, rev-two",
123
error = errors.InstallFailed([None])
124
self.assertEqual("Could not install revisions:\nNone", str(error))
150
def test_jail_break(self):
151
error = errors.JailBreak("some url")
152
self.assertEqualDiff("An attempt to access a url outside the server"
153
" jail was made: 'some url'.",
126
156
def test_lock_active(self):
127
157
error = errors.LockActive("lock description")
577
613
self.assertEqual(str(err), "Branching 'bar'(foo) must create a"
578
614
" working tree.")
616
def test_no_such_view(self):
617
err = errors.NoSuchView('foo')
618
self.assertEquals("No such view: foo.", str(err))
620
def test_views_not_supported(self):
621
err = errors.ViewsNotSupported('atree')
623
self.assertStartsWith(err_str, "Views are not supported by ")
624
self.assertEndsWith(err_str, "; use 'bzr upgrade' to change your "
625
"tree to a later format.")
627
def test_file_outside_view(self):
628
err = errors.FileOutsideView('baz', ['foo', 'bar'])
629
self.assertEquals('Specified file "baz" is outside the current view: '
630
'foo, bar', str(err))
580
632
def test_invalid_shelf_id(self):
581
633
invalid_id = "foo"
582
634
err = errors.InvalidShelfId(invalid_id)
583
635
self.assertEqual('"foo" is not a valid shelf id, '
584
636
'try a number instead.', str(err))
638
def test_unresumable_write_group(self):
640
wg_tokens = ['token']
642
err = errors.UnresumableWriteGroup(repo, wg_tokens, reason)
644
"Repository dummy repo cannot resume write group "
645
"['token']: a reason", str(err))
647
def test_unsuspendable_write_group(self):
649
err = errors.UnsuspendableWriteGroup(repo)
651
'Repository dummy repo cannot suspend a write group.', str(err))
653
def test_not_branch_no_args(self):
654
err = errors.NotBranchError('path')
655
self.assertEqual('Not a branch: "path".', str(err))
657
def test_not_branch_bzrdir_with_repo(self):
658
bzrdir = self.make_repository('repo').bzrdir
659
err = errors.NotBranchError('path', bzrdir=bzrdir)
661
'Not a branch: "path": location is a repository.', str(err))
663
def test_not_branch_bzrdir_without_repo(self):
664
bzrdir = self.make_bzrdir('bzrdir')
665
err = errors.NotBranchError('path', bzrdir=bzrdir)
666
self.assertEqual('Not a branch: "path".', str(err))
668
def test_not_branch_laziness(self):
669
real_bzrdir = self.make_bzrdir('path')
670
class FakeBzrDir(object):
673
def open_repository(self):
674
self.calls.append('open_repository')
675
raise errors.NoRepositoryPresent(real_bzrdir)
676
fake_bzrdir = FakeBzrDir()
677
err = errors.NotBranchError('path', bzrdir=fake_bzrdir)
678
self.assertEqual([], fake_bzrdir.calls)
680
self.assertEqual(['open_repository'], fake_bzrdir.calls)
681
# Stringifying twice doesn't try to open a repository twice.
683
self.assertEqual(['open_repository'], fake_bzrdir.calls)
685
def test_invalid_pattern(self):
686
error = errors.InvalidPattern('Bad pattern msg.')
687
self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
690
def test_recursive_bind(self):
691
error = errors.RecursiveBind('foo_bar_branch')
692
msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
693
'Please use `bzr unbind` to fix.')
694
self.assertEqualDiff(msg, str(error))
587
697
class PassThroughError(errors.BzrError):
589
699
_fmt = """Pass through %(foo)s and %(bar)s"""
591
701
def __init__(self, foo, bar):
629
739
e = ErrorWithBadFormat(not_thing='x')
630
740
self.assertStartsWith(
631
741
str(e), 'Unprintable exception ErrorWithBadFormat')
743
def test_cannot_bind_address(self):
744
# see <https://bugs.launchpad.net/bzr/+bug/286871>
745
e = errors.CannotBindAddress('example.com', 22,
746
socket.error(13, 'Permission denied'))
747
self.assertContainsRe(str(e),
748
r'Cannot bind address "example\.com:22":.*Permission denied')
750
def test_file_timestamp_unavailable(self):
751
e = errors.FileTimestampUnavailable("/path/foo")
752
self.assertEquals("The filestamp for /path/foo is not available.",
755
def test_transform_rename_failed(self):
756
e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
758
u"Failed to rename from to to: readonly file",