~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Jelmer Vernooij
  • Date: 2010-08-23 20:37:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5389.
  • Revision ID: jelmer@samba.org-20100823203718-1fj7rrjsiaxedkp7
Provide bzrlib.bzrdir.format_registry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
3
 
#            and others
 
1
# Copyright (C) 2006-2010 Canonical Ltd
4
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
18
16
 
19
17
"""Tests for the formatting and construction of errors."""
20
18
 
 
19
import inspect
 
20
import re
 
21
import socket
21
22
import sys
 
23
 
22
24
from bzrlib import (
23
25
    bzrdir,
24
26
    errors,
26
28
    symbol_versioning,
27
29
    urlutils,
28
30
    )
29
 
from bzrlib.tests import TestCase, TestCaseWithTransport
 
31
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
30
32
 
31
33
 
32
34
class TestErrors(TestCaseWithTransport):
33
35
 
 
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
 
34
61
    def test_bad_filename_encoding(self):
35
62
        error = errors.BadFilenameEncoding('bad/filen\xe5me', 'UTF-8')
36
63
        self.assertEqualDiff(
87
114
            "reason: reason for foo",
88
115
            str(error))
89
116
 
 
117
    def test_inconsistent_delta_delta(self):
 
118
        error = errors.InconsistentDeltaDelta([], 'reason')
 
119
        self.assertEqualDiff(
 
120
            "An inconsistent delta was supplied: []\nreason: reason",
 
121
            str(error))
 
122
 
90
123
    def test_in_process_transport(self):
91
124
        error = errors.InProcessTransport('fpp')
92
125
        self.assertEqualDiff(
114
147
            "read without data loss.",
115
148
            str(error))
116
149
 
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",
122
 
                         str(error))
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'.",
 
154
            str(error))
125
155
 
126
156
    def test_lock_active(self):
127
157
        error = errors.LockActive("lock description")
245
275
            "You will need to upgrade the branch to permit branch stacking.",
246
276
            str(error))
247
277
 
 
278
    def test_unstackable_location(self):
 
279
        error = errors.UnstackableLocationError('foo', 'bar')
 
280
        self.assertEqualDiff("The branch 'foo' cannot be stacked on 'bar'.",
 
281
            str(error))
 
282
 
248
283
    def test_unstackable_repository_format(self):
249
284
        format = u'foo'
250
285
        url = "/foo"
534
569
            1/0
535
570
        except ZeroDivisionError:
536
571
            exc_info = sys.exc_info()
537
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info)
 
572
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
538
573
        self.assertStartsWith(
539
574
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
540
575
        self.assertEndsWith(
615
650
        self.assertEqual(
616
651
            'Repository dummy repo cannot suspend a write group.', str(err))
617
652
 
 
653
    def test_not_branch_no_args(self):
 
654
        err = errors.NotBranchError('path')
 
655
        self.assertEqual('Not a branch: "path".', str(err))
 
656
 
 
657
    def test_not_branch_bzrdir_with_repo(self):
 
658
        bzrdir = self.make_repository('repo').bzrdir
 
659
        err = errors.NotBranchError('path', bzrdir=bzrdir)
 
660
        self.assertEqual(
 
661
            'Not a branch: "path": location is a repository.', str(err))
 
662
 
 
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))
 
667
 
 
668
    def test_not_branch_laziness(self):
 
669
        real_bzrdir = self.make_bzrdir('path')
 
670
        class FakeBzrDir(object):
 
671
            def __init__(self):
 
672
                self.calls = []
 
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)
 
679
        str(err)
 
680
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
 
681
        # Stringifying twice doesn't try to open a repository twice.
 
682
        str(err)
 
683
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
 
684
 
 
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
 
618
696
 
619
697
class PassThroughError(errors.BzrError):
620
698
 
630
708
 
631
709
 
632
710
class ErrorWithNoFormat(errors.BzrError):
633
 
    """This class has a docstring but no format string."""
 
711
    __doc__ = """This class has a docstring but no format string."""
634
712
 
635
713
 
636
714
class TestErrorFormatting(TestCase):
661
739
        e = ErrorWithBadFormat(not_thing='x')
662
740
        self.assertStartsWith(
663
741
            str(e), 'Unprintable exception ErrorWithBadFormat')
 
742
 
 
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')
 
749
 
 
750
    def test_file_timestamp_unavailable(self):            
 
751
        e = errors.FileTimestampUnavailable("/path/foo")
 
752
        self.assertEquals("The filestamp for /path/foo is not available.",
 
753
            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))