17
17
"""Tests for the formatting and construction of errors."""
22
24
from bzrlib import (
29
from bzrlib.tests import TestCase, TestCaseWithTransport
30
from bzrlib.tests import (
32
TestCaseWithTransport,
32
37
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__))
34
64
def test_bad_filename_encoding(self):
35
65
error = errors.BadFilenameEncoding('bad/filen\xe5me', 'UTF-8')
36
66
self.assertEqualDiff(
103
133
error = errors.InvalidHttpRange('path',
104
134
'Content-Range: potatoes 0-00/o0oo0',
106
self.assertEquals("Invalid http range"
107
" 'Content-Range: potatoes 0-00/o0oo0'"
108
" for path: bad range",
136
self.assertEqual("Invalid http range"
137
" 'Content-Range: potatoes 0-00/o0oo0'"
138
" for path: bad range",
111
141
def test_invalid_range(self):
112
142
error = errors.InvalidRange('path', 12, 'bad range')
113
self.assertEquals("Invalid range access in path at 12: bad range",
143
self.assertEqual("Invalid range access in path at 12: bad range",
116
146
def test_inventory_modified(self):
117
147
error = errors.InventoryModified("a tree to be repred")
132
162
"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",
135
172
def test_knit_data_stream_incompatible(self):
136
173
error = errors.KnitDataStreamIncompatible(
137
174
'stream format', 'target format')
170
207
'There is no public branch set for "%s".' % url, str(error))
172
209
def test_no_repo(self):
173
dir = bzrdir.BzrDir.create(self.get_url())
210
dir = controldir.ControlDir.create(self.get_url())
174
211
error = errors.NoRepositoryPresent(dir)
175
212
self.assertNotEqual(-1, str(error).find((dir.transport.clone('..').base)))
176
213
self.assertEqual(-1, str(error).find((dir.transport.base)))
265
302
def test_up_to_date(self):
266
error = errors.UpToDateFormat(bzrdir.BzrDirFormat4())
267
self.assertEqualDiff("The branch format All-in-one "
268
"format 4 is already at the most "
303
error = errors.UpToDateFormat("someformat")
304
self.assertEqualDiff(
305
"The branch format someformat is already at the most "
306
"recent format.", str(error))
272
308
def test_corrupt_repository(self):
273
309
repo = self.make_repository('.')
313
349
self.assertEqual("The value 'foo' is not a valid value.",
316
def test_bzrnewerror_is_deprecated(self):
317
class DeprecatedError(errors.BzrNewError):
319
self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
320
'please convert DeprecatedError to use BzrError instead'],
323
352
def test_bzrerror_from_literal_string(self):
324
353
# Some code constructs BzrError from a literal string, in which case
325
354
# no further formatting is done. (I'm not sure raising the base class
497
526
def test_unable_create_symlink(self):
498
527
err = errors.UnableCreateSymlink()
500
529
"Unable to create symlink on this platform",
502
531
err = errors.UnableCreateSymlink(path=u'foo')
504
533
"Unable to create symlink 'foo' on this platform",
506
535
err = errors.UnableCreateSymlink(path=u'\xb5')
508
537
"Unable to create symlink u'\\xb5' on this platform",
518
547
def test_incorrect_url(self):
519
548
err = errors.InvalidBugTrackerURL('foo', 'http://bug.com/')
521
550
("The URL for bug tracker \"foo\" doesn't contain {id}: "
522
551
"http://bug.com/"),
525
554
def test_unable_encode_path(self):
526
555
err = errors.UnableEncodePath('foo', 'executable')
527
self.assertEquals("Unable to encode executable path 'foo' in "
528
"user encoding " + osutils.get_user_encoding(),
556
self.assertEqual("Unable to encode executable path 'foo' in "
557
"user encoding " + osutils.get_user_encoding(),
531
560
def test_unknown_format(self):
532
561
err = errors.UnknownFormatError('bar', kind='foo')
533
self.assertEquals("Unknown foo format: 'bar'", str(err))
562
self.assertEqual("Unknown foo format: 'bar'", str(err))
535
564
def test_unknown_rules(self):
536
565
err = errors.UnknownRules(['foo', 'bar'])
537
self.assertEquals("Unknown rules detected: foo, bar.", str(err))
539
def test_hook_failed(self):
540
# Create an exc_info tuple by raising and catching an exception.
543
except ZeroDivisionError:
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')
566
self.assertEqual("Unknown rules detected: foo, bar.", str(err))
551
568
def test_tip_change_rejected(self):
552
569
err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
554
571
u'Tip change rejected: Unicode message\N{INTERROBANG}',
557
574
'Tip change rejected: Unicode message\xe2\x80\xbd',
560
577
def test_error_from_smart_server(self):
561
578
error_tuple = ('error', 'tuple')
562
579
err = errors.ErrorFromSmartServer(error_tuple)
564
581
"Error received from smart server: ('error', 'tuple')", str(err))
566
583
def test_untranslateable_error_from_smart_server(self):
567
584
error_tuple = ('error', 'tuple')
568
585
orig_err = errors.ErrorFromSmartServer(error_tuple)
569
586
err = errors.UnknownErrorFromSmartServer(orig_err)
571
588
"Server sent an unexpected error: ('error', 'tuple')", str(err))
573
590
def test_smart_message_handler_error(self):
576
593
raise Exception("example error")
577
594
except Exception:
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")
595
err = errors.SmartMessageHandlerError(sys.exc_info())
596
# GZ 2010-11-08: Should not store exc_info in exception instances.
598
self.assertStartsWith(
599
str(err), "The message handler raised an exception:\n")
600
self.assertEndsWith(str(err), "Exception: example error\n")
584
604
def test_must_have_working_tree(self):
585
605
err = errors.MustHaveWorkingTree('foo', 'bar')
589
609
def test_no_such_view(self):
590
610
err = errors.NoSuchView('foo')
591
self.assertEquals("No such view: foo.", str(err))
611
self.assertEqual("No such view: foo.", str(err))
593
613
def test_views_not_supported(self):
594
614
err = errors.ViewsNotSupported('atree')
600
620
def test_file_outside_view(self):
601
621
err = errors.FileOutsideView('baz', ['foo', 'bar'])
602
self.assertEquals('Specified file "baz" is outside the current view: '
603
'foo, bar', str(err))
622
self.assertEqual('Specified file "baz" is outside the current view: '
623
'foo, bar', str(err))
605
625
def test_invalid_shelf_id(self):
606
626
invalid_id = "foo"
607
627
err = errors.InvalidShelfId(invalid_id)
608
628
self.assertEqual('"foo" is not a valid shelf id, '
609
'try a number instead.', str(err))
629
'try a number instead.', str(err))
611
631
def test_unresumable_write_group(self):
612
632
repo = "dummy repo"
638
658
err = errors.NotBranchError('path', bzrdir=bzrdir)
639
659
self.assertEqual('Not a branch: "path".', str(err))
661
def test_not_branch_bzrdir_with_recursive_not_branch_error(self):
662
class FakeBzrDir(object):
663
def open_repository(self):
664
# str() on the NotBranchError will trigger a call to this,
665
# which in turn will another, identical NotBranchError.
666
raise errors.NotBranchError('path', bzrdir=FakeBzrDir())
667
err = errors.NotBranchError('path', bzrdir=FakeBzrDir())
668
self.assertEqual('Not a branch: "path".', str(err))
641
670
def test_not_branch_laziness(self):
642
671
real_bzrdir = self.make_bzrdir('path')
643
672
class FakeBzrDir(object):
656
685
self.assertEqual(['open_repository'], fake_bzrdir.calls)
687
def test_invalid_pattern(self):
688
error = errors.InvalidPattern('Bad pattern msg.')
689
self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
692
def test_recursive_bind(self):
693
error = errors.RecursiveBind('foo_bar_branch')
694
msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
695
'Please use `bzr unbind` to fix.')
696
self.assertEqualDiff(msg, str(error))
698
def test_retry_with_new_packs(self):
699
fake_exc_info = ('{exc type}', '{exc value}', '{exc traceback}')
700
error = errors.RetryWithNewPacks(
701
'{context}', reload_occurred=False, exc_info=fake_exc_info)
703
'Pack files have changed, reload and retry. context: '
704
'{context} {exc value}', str(error))
659
707
class PassThroughError(errors.BzrError):
688
736
def test_missing_format_string(self):
689
737
e = ErrorWithNoFormat(param='randomvalue')
690
s = self.callDeprecated(
691
['ErrorWithNoFormat uses its docstring as a format, it should use _fmt instead'],
695
"This class has a docstring but no format string.")
738
self.assertStartsWith(str(e),
739
"Unprintable exception ErrorWithNoFormat")
697
741
def test_mismatched_format_args(self):
698
742
# Even though ErrorWithBadFormat's format string does not match the
703
747
str(e), 'Unprintable exception ErrorWithBadFormat')
705
749
def test_cannot_bind_address(self):
706
# see <https://bugs.edge.launchpad.net/bzr/+bug/286871>
750
# see <https://bugs.launchpad.net/bzr/+bug/286871>
707
751
e = errors.CannotBindAddress('example.com', 22,
708
socket.error(13, 'Permission denied'))
709
self.assertContainsRe(str(e),
752
socket.error(13, 'Permission denied'))
753
self.assertContainsRe(
710
755
r'Cannot bind address "example\.com:22":.*Permission denied')
712
def test_file_timestamp_unavailable(self):
757
def test_file_timestamp_unavailable(self):
713
758
e = errors.FileTimestampUnavailable("/path/foo")
714
self.assertEquals("The filestamp for /path/foo is not available.",
759
self.assertEqual("The filestamp for /path/foo is not available.",
762
def test_transform_rename_failed(self):
763
e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
765
u"Failed to rename from to to: readonly file",