~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Jelmer Vernooij
  • Date: 2012-03-15 02:42:48 UTC
  • mto: This revision was merged to the branch mainline in revision 6510.
  • Revision ID: jelmer@samba.org-20120315024248-7nyk2zxc52i1u8gj
extention -> extension.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008, 2009 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
 
22
24
from bzrlib import (
23
 
    bzrdir,
 
25
    controldir,
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')
170
207
            'There is no public branch set for "%s".' % url, str(error))
171
208
 
172
209
    def test_no_repo(self):
173
 
        dir = bzrdir.BzrDir.create(self.get_url())
 
210
        dir = controldir.ControlDir.create(self.get_url())
174
211
        error = errors.NoRepositoryPresent(dir)
175
212
        self.assertNotEqual(-1, str(error).find((dir.transport.clone('..').base)))
176
213
        self.assertEqual(-1, str(error).find((dir.transport.base)))
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('.')
313
349
        self.assertEqual("The value 'foo' is not a valid value.",
314
350
            str(error))
315
351
 
316
 
    def test_bzrnewerror_is_deprecated(self):
317
 
        class DeprecatedError(errors.BzrNewError):
318
 
            pass
319
 
        self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
320
 
             'please convert DeprecatedError to use BzrError instead'],
321
 
            DeprecatedError)
322
 
 
323
352
    def test_bzrerror_from_literal_string(self):
324
353
        # Some code constructs BzrError from a literal string, in which case
325
354
        # no further formatting is done.  (I'm not sure raising the base class
536
565
        err = errors.UnknownRules(['foo', 'bar'])
537
566
        self.assertEquals("Unknown rules detected: foo, bar.", str(err))
538
567
 
539
 
    def test_hook_failed(self):
540
 
        # Create an exc_info tuple by raising and catching an exception.
541
 
        try:
542
 
            1/0
543
 
        except ZeroDivisionError:
544
 
            exc_info = sys.exc_info()
545
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
546
 
        self.assertStartsWith(
547
 
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
548
 
        self.assertEndsWith(
549
 
            str(err), 'integer division or modulo by zero')
550
 
 
551
568
    def test_tip_change_rejected(self):
552
569
        err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
553
570
        self.assertEquals(
575
592
        try:
576
593
            raise Exception("example error")
577
594
        except Exception:
578
 
            exc_info = sys.exc_info()
579
 
        err = errors.SmartMessageHandlerError(exc_info)
580
 
        self.assertStartsWith(
581
 
            str(err), "The message handler raised an exception:\n")
582
 
        self.assertEndsWith(str(err), "Exception: example error\n")
 
595
            err = errors.SmartMessageHandlerError(sys.exc_info())
 
596
        # GZ 2010-11-08: Should not store exc_info in exception instances.
 
597
        try:
 
598
            self.assertStartsWith(
 
599
                str(err), "The message handler raised an exception:\n")
 
600
            self.assertEndsWith(str(err), "Exception: example error\n")
 
601
        finally:
 
602
            del err
583
603
 
584
604
    def test_must_have_working_tree(self):
585
605
        err = errors.MustHaveWorkingTree('foo', 'bar')
638
658
        err = errors.NotBranchError('path', bzrdir=bzrdir)
639
659
        self.assertEqual('Not a branch: "path".', str(err))
640
660
 
 
661
    def test_not_branch_bzrdir_with_recursive_not_branch_error(self):
 
662
        class FakeBzrDir(object):
 
663
            def open_repository(self):
 
664
                # str() on the NotBranchError will trigger a call to this,
 
665
                # which in turn will another, identical NotBranchError.
 
666
                raise errors.NotBranchError('path', bzrdir=FakeBzrDir())
 
667
        err = errors.NotBranchError('path', bzrdir=FakeBzrDir())
 
668
        self.assertEqual('Not a branch: "path".', str(err))
 
669
 
641
670
    def test_not_branch_laziness(self):
642
671
        real_bzrdir = self.make_bzrdir('path')
643
672
        class FakeBzrDir(object):
655
684
        str(err)
656
685
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
657
686
 
 
687
    def test_invalid_pattern(self):
 
688
        error = errors.InvalidPattern('Bad pattern msg.')
 
689
        self.assertEqualDiff("Invalid pattern(s) found. Bad pattern msg.",
 
690
            str(error))
 
691
 
 
692
    def test_recursive_bind(self):
 
693
        error = errors.RecursiveBind('foo_bar_branch')
 
694
        msg = ('Branch "foo_bar_branch" appears to be bound to itself. '
 
695
            'Please use `bzr unbind` to fix.')
 
696
        self.assertEqualDiff(msg, str(error))
 
697
 
 
698
    def test_retry_with_new_packs(self):
 
699
        fake_exc_info = ('{exc type}', '{exc value}', '{exc traceback}')
 
700
        error = errors.RetryWithNewPacks(
 
701
            '{context}', reload_occurred=False, exc_info=fake_exc_info)
 
702
        self.assertEqual(
 
703
            'Pack files have changed, reload and retry. context: '
 
704
            '{context} {exc value}', str(error))
 
705
 
658
706
 
659
707
class PassThroughError(errors.BzrError):
660
708
 
670
718
 
671
719
 
672
720
class ErrorWithNoFormat(errors.BzrError):
673
 
    """This class has a docstring but no format string."""
 
721
    __doc__ = """This class has a docstring but no format string."""
674
722
 
675
723
 
676
724
class TestErrorFormatting(TestCase):
687
735
 
688
736
    def test_missing_format_string(self):
689
737
        e = ErrorWithNoFormat(param='randomvalue')
690
 
        s = self.callDeprecated(
691
 
                ['ErrorWithNoFormat uses its docstring as a format, it should use _fmt instead'],
692
 
                lambda x: str(x), e)
693
 
        ## s = str(e)
694
 
        self.assertEqual(s,
695
 
                "This class has a docstring but no format string.")
 
738
        self.assertStartsWith(str(e),
 
739
            "Unprintable exception ErrorWithNoFormat")
696
740
 
697
741
    def test_mismatched_format_args(self):
698
742
        # Even though ErrorWithBadFormat's format string does not match the
703
747
            str(e), 'Unprintable exception ErrorWithBadFormat')
704
748
 
705
749
    def test_cannot_bind_address(self):
706
 
        # see <https://bugs.edge.launchpad.net/bzr/+bug/286871>
 
750
        # see <https://bugs.launchpad.net/bzr/+bug/286871>
707
751
        e = errors.CannotBindAddress('example.com', 22,
708
752
            socket.error(13, 'Permission denied'))
709
753
        self.assertContainsRe(str(e),
713
757
        e = errors.FileTimestampUnavailable("/path/foo")
714
758
        self.assertEquals("The filestamp for /path/foo is not available.",
715
759
            str(e))
 
760
            
 
761
    def test_transform_rename_failed(self):
 
762
        e = errors.TransformRenameFailed(u"from", u"to", "readonly file", 2)
 
763
        self.assertEquals(
 
764
            u"Failed to rename from to to: readonly file",
 
765
            str(e))