~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Vincent Ladeuil
  • Date: 2011-01-27 14:27:18 UTC
  • mto: (5609.10.1 2.3b5-dev)
  • mto: This revision was merged to the branch mainline in revision 5635.
  • Revision ID: v.ladeuil+lp@free.fr-20110127142718-1ik7fyjfflgj91j5
Use self.get_transport instead of transport.get_transport where possible.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
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
16
16
 
17
17
"""Tests for the formatting and construction of errors."""
18
18
 
 
19
import inspect
 
20
import re
19
21
import socket
20
22
import sys
21
23
 
23
25
    bzrdir,
24
26
    errors,
25
27
    osutils,
26
 
    symbol_versioning,
27
28
    urlutils,
28
29
    )
29
 
from bzrlib.tests import TestCase, TestCaseWithTransport
 
30
from bzrlib.tests import (
 
31
    TestCase,
 
32
    TestCaseWithTransport,
 
33
    TestSkipped,
 
34
    )
30
35
 
31
36
 
32
37
class TestErrors(TestCaseWithTransport):
33
38
 
 
39
    def test_no_arg_named_message(self):
 
40
        """Ensure the __init__ and _fmt in errors do not have "message" arg.
 
41
 
 
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.
 
45
        See bug #603461
 
46
        """
 
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)
 
55
            if init:
 
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__))
 
63
 
34
64
    def test_bad_filename_encoding(self):
35
65
        error = errors.BadFilenameEncoding('bad/filen\xe5me', 'UTF-8')
36
66
        self.assertEqualDiff(
132
162
            "cannot be broken.",
133
163
            str(error))
134
164
 
 
165
    def test_lock_corrupt(self):
 
166
        error = errors.LockCorrupt("corruption info")
 
167
        self.assertEqualDiff("Lock is apparently held, but corrupted: "
 
168
            "corruption info\n"
 
169
            "Use 'bzr break-lock' to clear it",
 
170
            str(error))
 
171
 
135
172
    def test_knit_data_stream_incompatible(self):
136
173
        error = errors.KnitDataStreamIncompatible(
137
174
            'stream format', 'target format')
638
675
        err = errors.NotBranchError('path', bzrdir=bzrdir)
639
676
        self.assertEqual('Not a branch: "path".', str(err))
640
677
 
 
678
    def test_not_branch_bzrdir_with_recursive_not_branch_error(self):
 
679
        class FakeBzrDir(object):
 
680
            def open_repository(self):
 
681
                # str() on the NotBranchError will trigger a call to this,
 
682
                # which in turn will another, identical NotBranchError.
 
683
                raise errors.NotBranchError('path', bzrdir=FakeBzrDir())
 
684
        err = errors.NotBranchError('path', bzrdir=FakeBzrDir())
 
685
        self.assertEqual('Not a branch: "path".', str(err))
 
686
 
641
687
    def test_not_branch_laziness(self):
642
688
        real_bzrdir = self.make_bzrdir('path')
643
689
        class FakeBzrDir(object):
655
701
        str(err)
656
702
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
657
703
 
 
704
    def test_invalid_pattern(self):
 
705
        error = errors.InvalidPattern('Bad pattern msg.')
 
706
        self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
 
707
            str(error))
 
708
 
 
709
    def test_recursive_bind(self):
 
710
        error = errors.RecursiveBind('foo_bar_branch')
 
711
        msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
 
712
            'Please use `bzr unbind` to fix.')
 
713
        self.assertEqualDiff(msg, str(error))
 
714
 
658
715
 
659
716
class PassThroughError(errors.BzrError):
660
717
 
703
760
            str(e), 'Unprintable exception ErrorWithBadFormat')
704
761
 
705
762
    def test_cannot_bind_address(self):
706
 
        # see <https://bugs.edge.launchpad.net/bzr/+bug/286871>
 
763
        # see <https://bugs.launchpad.net/bzr/+bug/286871>
707
764
        e = errors.CannotBindAddress('example.com', 22,
708
765
            socket.error(13, 'Permission denied'))
709
766
        self.assertContainsRe(str(e),
713
770
        e = errors.FileTimestampUnavailable("/path/foo")
714
771
        self.assertEquals("The filestamp for /path/foo is not available.",
715
772
            str(e))
 
773
            
 
774
    def test_transform_rename_failed(self):
 
775
        e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
 
776
        self.assertEquals(
 
777
            u"Failed to rename from to to: readonly file",
 
778
            str(e))