~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

(vila) Fix bzrlib.tests.test_gpg.TestVerify.test_verify_revoked_signature
 with recent versions of gpg. (Vincent Ladeuil)

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
22
22
import sys
23
23
 
24
24
from bzrlib import (
25
 
    bzrdir,
 
25
    controldir,
26
26
    errors,
27
27
    osutils,
28
 
    symbol_versioning,
29
28
    urlutils,
30
29
    )
31
 
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
 
30
from bzrlib.tests import (
 
31
    TestCase,
 
32
    TestCaseWithTransport,
 
33
    TestSkipped,
 
34
    )
32
35
 
33
36
 
34
37
class TestErrors(TestCaseWithTransport):
159
162
            "cannot be broken.",
160
163
            str(error))
161
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
 
162
172
    def test_knit_data_stream_incompatible(self):
163
173
        error = errors.KnitDataStreamIncompatible(
164
174
            'stream format', 'target format')
197
207
            'There is no public branch set for "%s".' % url, str(error))
198
208
 
199
209
    def test_no_repo(self):
200
 
        dir = bzrdir.BzrDir.create(self.get_url())
 
210
        dir = controldir.ControlDir.create(self.get_url())
201
211
        error = errors.NoRepositoryPresent(dir)
202
212
        self.assertNotEqual(-1, str(error).find((dir.transport.clone('..').base)))
203
213
        self.assertEqual(-1, str(error).find((dir.transport.base)))
290
300
            str(error))
291
301
 
292
302
    def test_up_to_date(self):
293
 
        error = errors.UpToDateFormat(bzrdir.BzrDirFormat4())
294
 
        self.assertEqualDiff("The branch format All-in-one "
295
 
                             "format 4 is already at the most "
296
 
                             "recent format.",
297
 
                             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))
298
307
 
299
308
    def test_corrupt_repository(self):
300
309
        repo = self.make_repository('.')
340
349
        self.assertEqual("The value 'foo' is not a valid value.",
341
350
            str(error))
342
351
 
343
 
    def test_bzrnewerror_is_deprecated(self):
344
 
        class DeprecatedError(errors.BzrNewError):
345
 
            pass
346
 
        self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
347
 
             'please convert DeprecatedError to use BzrError instead'],
348
 
            DeprecatedError)
349
 
 
350
352
    def test_bzrerror_from_literal_string(self):
351
353
        # Some code constructs BzrError from a literal string, in which case
352
354
        # no further formatting is done.  (I'm not sure raising the base class
563
565
        err = errors.UnknownRules(['foo', 'bar'])
564
566
        self.assertEquals("Unknown rules detected: foo, bar.", str(err))
565
567
 
566
 
    def test_hook_failed(self):
567
 
        # Create an exc_info tuple by raising and catching an exception.
568
 
        try:
569
 
            1/0
570
 
        except ZeroDivisionError:
571
 
            exc_info = sys.exc_info()
572
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
573
 
        self.assertStartsWith(
574
 
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
575
 
        self.assertEndsWith(
576
 
            str(err), 'integer division or modulo by zero')
577
 
 
578
568
    def test_tip_change_rejected(self):
579
569
        err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
580
570
        self.assertEquals(
602
592
        try:
603
593
            raise Exception("example error")
604
594
        except Exception:
605
 
            exc_info = sys.exc_info()
606
 
        err = errors.SmartMessageHandlerError(exc_info)
607
 
        self.assertStartsWith(
608
 
            str(err), "The message handler raised an exception:\n")
609
 
        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
610
603
 
611
604
    def test_must_have_working_tree(self):
612
605
        err = errors.MustHaveWorkingTree('foo', 'bar')
665
658
        err = errors.NotBranchError('path', bzrdir=bzrdir)
666
659
        self.assertEqual('Not a branch: "path".', str(err))
667
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
 
668
670
    def test_not_branch_laziness(self):
669
671
        real_bzrdir = self.make_bzrdir('path')
670
672
        class FakeBzrDir(object):
693
695
            'Please use `bzr unbind` to fix.')
694
696
        self.assertEqualDiff(msg, str(error))
695
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
 
696
706
 
697
707
class PassThroughError(errors.BzrError):
698
708
 
725
735
 
726
736
    def test_missing_format_string(self):
727
737
        e = ErrorWithNoFormat(param='randomvalue')
728
 
        s = self.callDeprecated(
729
 
                ['ErrorWithNoFormat uses its docstring as a format, it should use _fmt instead'],
730
 
                lambda x: str(x), e)
731
 
        ## s = str(e)
732
 
        self.assertEqual(s,
733
 
                "This class has a docstring but no format string.")
 
738
        self.assertStartsWith(str(e),
 
739
            "Unprintable exception ErrorWithNoFormat")
734
740
 
735
741
    def test_mismatched_format_args(self):
736
742
        # Even though ErrorWithBadFormat's format string does not match the