~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: INADA Naoki
  • Date: 2011-05-18 06:01:08 UTC
  • mto: This revision was merged to the branch mainline in revision 5894.
  • Revision ID: songofacandy@gmail.com-20110518060108-86t2kffcrzu0nf6i
Update Japanese docs.

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-2011 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,
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')
263
300
            str(error))
264
301
 
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 "
269
 
                             "recent format.",
270
 
                             str(error))
 
303
        error = errors.UpToDateFormat("someformat")
 
304
        self.assertEqualDiff(
 
305
            "The branch format someformat is already at the most "
 
306
            "recent format.", str(error))
271
307
 
272
308
    def test_corrupt_repository(self):
273
309
        repo = self.make_repository('.')
542
578
            1/0
543
579
        except ZeroDivisionError:
544
580
            exc_info = sys.exc_info()
545
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info)
 
581
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
546
582
        self.assertStartsWith(
547
583
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
548
584
        self.assertEndsWith(
623
659
        self.assertEqual(
624
660
            'Repository dummy repo cannot suspend a write group.', str(err))
625
661
 
 
662
    def test_not_branch_no_args(self):
 
663
        err = errors.NotBranchError('path')
 
664
        self.assertEqual('Not a branch: "path".', str(err))
 
665
 
 
666
    def test_not_branch_bzrdir_with_repo(self):
 
667
        bzrdir = self.make_repository('repo').bzrdir
 
668
        err = errors.NotBranchError('path', bzrdir=bzrdir)
 
669
        self.assertEqual(
 
670
            'Not a branch: "path": location is a repository.', str(err))
 
671
 
 
672
    def test_not_branch_bzrdir_without_repo(self):
 
673
        bzrdir = self.make_bzrdir('bzrdir')
 
674
        err = errors.NotBranchError('path', bzrdir=bzrdir)
 
675
        self.assertEqual('Not a branch: "path".', str(err))
 
676
 
 
677
    def test_not_branch_bzrdir_with_recursive_not_branch_error(self):
 
678
        class FakeBzrDir(object):
 
679
            def open_repository(self):
 
680
                # str() on the NotBranchError will trigger a call to this,
 
681
                # which in turn will another, identical NotBranchError.
 
682
                raise errors.NotBranchError('path', bzrdir=FakeBzrDir())
 
683
        err = errors.NotBranchError('path', bzrdir=FakeBzrDir())
 
684
        self.assertEqual('Not a branch: "path".', str(err))
 
685
 
 
686
    def test_not_branch_laziness(self):
 
687
        real_bzrdir = self.make_bzrdir('path')
 
688
        class FakeBzrDir(object):
 
689
            def __init__(self):
 
690
                self.calls = []
 
691
            def open_repository(self):
 
692
                self.calls.append('open_repository')
 
693
                raise errors.NoRepositoryPresent(real_bzrdir)
 
694
        fake_bzrdir = FakeBzrDir()
 
695
        err = errors.NotBranchError('path', bzrdir=fake_bzrdir)
 
696
        self.assertEqual([], fake_bzrdir.calls)
 
697
        str(err)
 
698
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
 
699
        # Stringifying twice doesn't try to open a repository twice.
 
700
        str(err)
 
701
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
 
702
 
 
703
    def test_invalid_pattern(self):
 
704
        error = errors.InvalidPattern('Bad pattern msg.')
 
705
        self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
 
706
            str(error))
 
707
 
 
708
    def test_recursive_bind(self):
 
709
        error = errors.RecursiveBind('foo_bar_branch')
 
710
        msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
 
711
            'Please use `bzr unbind` to fix.')
 
712
        self.assertEqualDiff(msg, str(error))
 
713
 
626
714
 
627
715
class PassThroughError(errors.BzrError):
628
716
 
638
726
 
639
727
 
640
728
class ErrorWithNoFormat(errors.BzrError):
641
 
    """This class has a docstring but no format string."""
 
729
    __doc__ = """This class has a docstring but no format string."""
642
730
 
643
731
 
644
732
class TestErrorFormatting(TestCase):
669
757
        e = ErrorWithBadFormat(not_thing='x')
670
758
        self.assertStartsWith(
671
759
            str(e), 'Unprintable exception ErrorWithBadFormat')
 
760
 
 
761
    def test_cannot_bind_address(self):
 
762
        # see <https://bugs.launchpad.net/bzr/+bug/286871>
 
763
        e = errors.CannotBindAddress('example.com', 22,
 
764
            socket.error(13, 'Permission denied'))
 
765
        self.assertContainsRe(str(e),
 
766
            r'Cannot bind address "example\.com:22":.*Permission denied')
 
767
 
 
768
    def test_file_timestamp_unavailable(self):            
 
769
        e = errors.FileTimestampUnavailable("/path/foo")
 
770
        self.assertEquals("The filestamp for /path/foo is not available.",
 
771
            str(e))
 
772
            
 
773
    def test_transform_rename_failed(self):
 
774
        e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
 
775
        self.assertEquals(
 
776
            u"Failed to rename from to to: readonly file",
 
777
            str(e))