31
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
28
from bzrlib.tests import TestCase, TestCaseWithTransport
34
31
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__))
61
def test_bad_filename_encoding(self):
62
error = errors.BadFilenameEncoding('bad/filen\xe5me', 'UTF-8')
64
"Filename 'bad/filen\\xe5me' is not valid in your current"
65
" filesystem encoding UTF-8",
68
33
def test_corrupt_dirstate(self):
69
34
error = errors.CorruptDirstate('path/to/dirstate', 'the reason why')
70
35
self.assertEqualDiff(
226
175
" tree atree.", str(error))
227
176
self.assertIsInstance(error, errors.NoSuchRevision)
229
def test_not_stacked(self):
230
error = errors.NotStacked('a branch')
231
self.assertEqualDiff("The branch 'a branch' is not stacked.",
234
178
def test_not_write_locked(self):
235
179
error = errors.NotWriteLocked('a thing to repr')
236
180
self.assertEqualDiff("'a thing to repr' is not write locked but needs "
184
def test_read_only_lock_error(self):
185
error = self.applyDeprecated(symbol_versioning.zero_ninetytwo,
186
errors.ReadOnlyLockError, 'filename', 'error message')
187
self.assertEqualDiff("Cannot acquire write lock on filename."
188
" error message", str(error))
240
190
def test_lock_failed(self):
241
191
error = errors.LockFailed('http://canonical.com/', 'readonly transport')
242
192
self.assertEqualDiff("Cannot lock http://canonical.com/: readonly transport",
269
def test_unstackable_branch_format(self):
272
error = errors.UnstackableBranchFormat(format, url)
273
self.assertEqualDiff(
274
"The branch '/foo'(foo) is not a stackable format. "
275
"You will need to upgrade the branch to permit branch stacking.",
278
def test_unstackable_location(self):
279
error = errors.UnstackableLocationError('foo', 'bar')
280
self.assertEqualDiff("The branch 'foo' cannot be stacked on 'bar'.",
283
def test_unstackable_repository_format(self):
286
error = errors.UnstackableRepositoryFormat(format, url)
287
self.assertEqualDiff(
288
"The repository '/foo'(foo) is not a stackable format. "
289
"You will need to upgrade the repository to permit branch stacking.",
292
213
def test_up_to_date(self):
293
214
error = errors.UpToDateFormat(bzrdir.BzrDirFormat4())
294
self.assertEqualDiff("The branch format All-in-one "
295
"format 4 is already at the most "
215
self.assertEqualDiff("The branch format Bazaar-NG branch, "
216
"format 0.0.4 is already at the most "
296
217
"recent format.",
559
470
err = errors.UnknownFormatError('bar', kind='foo')
560
471
self.assertEquals("Unknown foo format: 'bar'", str(err))
562
def test_unknown_rules(self):
563
err = errors.UnknownRules(['foo', 'bar'])
564
self.assertEquals("Unknown rules detected: foo, bar.", str(err))
566
def test_hook_failed(self):
567
# Create an exc_info tuple by raising and catching an exception.
570
except ZeroDivisionError:
571
exc_info = sys.exc_info()
572
err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
573
self.assertStartsWith(
574
str(err), 'Hook \'hook name\' during hook stage failed:\n')
576
str(err), 'integer division or modulo by zero')
578
def test_tip_change_rejected(self):
579
err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
581
u'Tip change rejected: Unicode message\N{INTERROBANG}',
584
'Tip change rejected: Unicode message\xe2\x80\xbd',
587
def test_error_from_smart_server(self):
588
error_tuple = ('error', 'tuple')
589
err = errors.ErrorFromSmartServer(error_tuple)
591
"Error received from smart server: ('error', 'tuple')", str(err))
593
def test_untranslateable_error_from_smart_server(self):
594
error_tuple = ('error', 'tuple')
595
orig_err = errors.ErrorFromSmartServer(error_tuple)
596
err = errors.UnknownErrorFromSmartServer(orig_err)
598
"Server sent an unexpected error: ('error', 'tuple')", str(err))
600
def test_smart_message_handler_error(self):
601
# Make an exc_info tuple.
603
raise Exception("example error")
605
exc_info = sys.exc_info()
606
err = errors.SmartMessageHandlerError(exc_info)
607
self.assertStartsWith(
608
str(err), "The message handler raised an exception:\n")
609
self.assertEndsWith(str(err), "Exception: example error\n")
611
def test_must_have_working_tree(self):
612
err = errors.MustHaveWorkingTree('foo', 'bar')
613
self.assertEqual(str(err), "Branching 'bar'(foo) must create a"
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))
632
def test_invalid_shelf_id(self):
634
err = errors.InvalidShelfId(invalid_id)
635
self.assertEqual('"foo" is not a valid shelf id, '
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))
697
474
class PassThroughError(errors.BzrError):
699
476
_fmt = """Pass through %(foo)s and %(bar)s"""
701
478
def __init__(self, foo, bar):