~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Jonathan Lange
  • Date: 2009-06-10 08:05:05 UTC
  • mto: This revision was merged to the branch mainline in revision 4433.
  • Revision ID: jml@canonical.com-20090610080505-lmch4q01z1q7deu2
MoreĀ unusedĀ imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#            and others
2
4
#
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
16
18
 
17
19
"""Tests for the formatting and construction of errors."""
18
20
 
19
 
import inspect
20
 
import re
21
 
import socket
22
21
import sys
23
 
 
24
22
from bzrlib import (
25
23
    bzrdir,
26
24
    errors,
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(
114
87
            "reason: reason for foo",
115
88
            str(error))
116
89
 
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
 
 
123
90
    def test_in_process_transport(self):
124
91
        error = errors.InProcessTransport('fpp')
125
92
        self.assertEqualDiff(
159
126
            "cannot be broken.",
160
127
            str(error))
161
128
 
162
 
    def test_lock_corrupt(self):
163
 
        error = errors.LockCorrupt("corruption info")
164
 
        self.assertEqualDiff("Lock is apparently held, but corrupted: "
165
 
            "corruption info\n"
166
 
            "Use 'bzr break-lock' to clear it",
167
 
            str(error))
168
 
 
169
129
    def test_knit_data_stream_incompatible(self):
170
130
        error = errors.KnitDataStreamIncompatible(
171
131
            'stream format', 'target format')
282
242
            "You will need to upgrade the branch to permit branch stacking.",
283
243
            str(error))
284
244
 
285
 
    def test_unstackable_location(self):
286
 
        error = errors.UnstackableLocationError('foo', 'bar')
287
 
        self.assertEqualDiff("The branch 'foo' cannot be stacked on 'bar'.",
288
 
            str(error))
289
 
 
290
245
    def test_unstackable_repository_format(self):
291
246
        format = u'foo'
292
247
        url = "/foo"
576
531
            1/0
577
532
        except ZeroDivisionError:
578
533
            exc_info = sys.exc_info()
579
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
 
534
        err = errors.HookFailed('hook stage', 'hook name', exc_info)
580
535
        self.assertStartsWith(
581
536
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
582
537
        self.assertEndsWith(
657
612
        self.assertEqual(
658
613
            'Repository dummy repo cannot suspend a write group.', str(err))
659
614
 
660
 
    def test_not_branch_no_args(self):
661
 
        err = errors.NotBranchError('path')
662
 
        self.assertEqual('Not a branch: "path".', str(err))
663
 
 
664
 
    def test_not_branch_bzrdir_with_repo(self):
665
 
        bzrdir = self.make_repository('repo').bzrdir
666
 
        err = errors.NotBranchError('path', bzrdir=bzrdir)
667
 
        self.assertEqual(
668
 
            'Not a branch: "path": location is a repository.', str(err))
669
 
 
670
 
    def test_not_branch_bzrdir_without_repo(self):
671
 
        bzrdir = self.make_bzrdir('bzrdir')
672
 
        err = errors.NotBranchError('path', bzrdir=bzrdir)
673
 
        self.assertEqual('Not a branch: "path".', str(err))
674
 
 
675
 
    def test_not_branch_laziness(self):
676
 
        real_bzrdir = self.make_bzrdir('path')
677
 
        class FakeBzrDir(object):
678
 
            def __init__(self):
679
 
                self.calls = []
680
 
            def open_repository(self):
681
 
                self.calls.append('open_repository')
682
 
                raise errors.NoRepositoryPresent(real_bzrdir)
683
 
        fake_bzrdir = FakeBzrDir()
684
 
        err = errors.NotBranchError('path', bzrdir=fake_bzrdir)
685
 
        self.assertEqual([], fake_bzrdir.calls)
686
 
        str(err)
687
 
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
688
 
        # Stringifying twice doesn't try to open a repository twice.
689
 
        str(err)
690
 
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
691
 
 
692
 
    def test_invalid_pattern(self):
693
 
        error = errors.InvalidPattern('Bad pattern msg.')
694
 
        self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
695
 
            str(error))
696
 
 
697
 
    def test_recursive_bind(self):
698
 
        error = errors.RecursiveBind('foo_bar_branch')
699
 
        msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
700
 
            'Please use `bzr unbind` to fix.')
701
 
        self.assertEqualDiff(msg, str(error))
702
 
 
703
615
 
704
616
class PassThroughError(errors.BzrError):
705
617
 
715
627
 
716
628
 
717
629
class ErrorWithNoFormat(errors.BzrError):
718
 
    __doc__ = """This class has a docstring but no format string."""
 
630
    """This class has a docstring but no format string."""
719
631
 
720
632
 
721
633
class TestErrorFormatting(TestCase):
746
658
        e = ErrorWithBadFormat(not_thing='x')
747
659
        self.assertStartsWith(
748
660
            str(e), 'Unprintable exception ErrorWithBadFormat')
749
 
 
750
 
    def test_cannot_bind_address(self):
751
 
        # see <https://bugs.launchpad.net/bzr/+bug/286871>
752
 
        e = errors.CannotBindAddress('example.com', 22,
753
 
            socket.error(13, 'Permission denied'))
754
 
        self.assertContainsRe(str(e),
755
 
            r'Cannot bind address "example\.com:22":.*Permission denied')
756
 
 
757
 
    def test_file_timestamp_unavailable(self):            
758
 
        e = errors.FileTimestampUnavailable("/path/foo")
759
 
        self.assertEquals("The filestamp for /path/foo is not available.",
760
 
            str(e))
761
 
            
762
 
    def test_transform_rename_failed(self):
763
 
        e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
764
 
        self.assertEquals(
765
 
            u"Failed to rename from to to: readonly file",
766
 
            str(e))