~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Martin Pool
  • Date: 2010-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the formatting and construction of errors."""
18
18
 
19
 
import inspect
20
 
import re
21
19
import socket
22
20
import sys
23
21
 
28
26
    symbol_versioning,
29
27
    urlutils,
30
28
    )
31
 
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
 
29
from bzrlib.tests import TestCase, TestCaseWithTransport
32
30
 
33
31
 
34
32
class TestErrors(TestCaseWithTransport):
35
33
 
36
 
    def test_no_arg_named_message(self):
37
 
        """Ensure the __init__ and _fmt in errors do not have "message" arg.
38
 
 
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.
42
 
        See bug #603461
43
 
        """
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)
52
 
            if init:
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__))
60
 
 
61
34
    def test_bad_filename_encoding(self):
62
35
        error = errors.BadFilenameEncoding('bad/filen\xe5me', 'UTF-8')
63
36
        self.assertEqualDiff(
682
655
        str(err)
683
656
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
684
657
 
685
 
    def test_invalid_pattern(self):
686
 
        error = errors.InvalidPattern('Bad pattern msg.')
687
 
        self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
688
 
            str(error))
689
 
 
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))
695
 
 
696
658
 
697
659
class PassThroughError(errors.BzrError):
698
660
 
708
670
 
709
671
 
710
672
class ErrorWithNoFormat(errors.BzrError):
711
 
    __doc__ = """This class has a docstring but no format string."""
 
673
    """This class has a docstring but no format string."""
712
674
 
713
675
 
714
676
class TestErrorFormatting(TestCase):
741
703
            str(e), 'Unprintable exception ErrorWithBadFormat')
742
704
 
743
705
    def test_cannot_bind_address(self):
744
 
        # see <https://bugs.launchpad.net/bzr/+bug/286871>
 
706
        # see <https://bugs.edge.launchpad.net/bzr/+bug/286871>
745
707
        e = errors.CannotBindAddress('example.com', 22,
746
708
            socket.error(13, 'Permission denied'))
747
709
        self.assertContainsRe(str(e),
751
713
        e = errors.FileTimestampUnavailable("/path/foo")
752
714
        self.assertEquals("The filestamp for /path/foo is not available.",
753
715
            str(e))
754
 
            
755
 
    def test_transform_rename_failed(self):
756
 
        e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
757
 
        self.assertEquals(
758
 
            u"Failed to rename from to to: readonly file",
759
 
            str(e))