~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
 
    controldir,
 
25
    bzrdir,
26
26
    errors,
27
27
    osutils,
 
28
    symbol_versioning,
28
29
    urlutils,
29
30
    )
30
 
from bzrlib.tests import (
31
 
    TestCase,
32
 
    TestCaseWithTransport,
33
 
    TestSkipped,
34
 
    )
 
31
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
35
32
 
36
33
 
37
34
class TestErrors(TestCaseWithTransport):
162
159
            "cannot be broken.",
163
160
            str(error))
164
161
 
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
 
 
172
162
    def test_knit_data_stream_incompatible(self):
173
163
        error = errors.KnitDataStreamIncompatible(
174
164
            'stream format', 'target format')
207
197
            'There is no public branch set for "%s".' % url, str(error))
208
198
 
209
199
    def test_no_repo(self):
210
 
        dir = controldir.ControlDir.create(self.get_url())
 
200
        dir = bzrdir.BzrDir.create(self.get_url())
211
201
        error = errors.NoRepositoryPresent(dir)
212
202
        self.assertNotEqual(-1, str(error).find((dir.transport.clone('..').base)))
213
203
        self.assertEqual(-1, str(error).find((dir.transport.base)))
300
290
            str(error))
301
291
 
302
292
    def test_up_to_date(self):
303
 
        error = errors.UpToDateFormat("someformat")
304
 
        self.assertEqualDiff(
305
 
            "The branch format someformat is already at the most "
306
 
            "recent format.", str(error))
 
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))
307
298
 
308
299
    def test_corrupt_repository(self):
309
300
        repo = self.make_repository('.')
349
340
        self.assertEqual("The value 'foo' is not a valid value.",
350
341
            str(error))
351
342
 
 
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
 
352
350
    def test_bzrerror_from_literal_string(self):
353
351
        # Some code constructs BzrError from a literal string, in which case
354
352
        # no further formatting is done.  (I'm not sure raising the base class
565
563
        err = errors.UnknownRules(['foo', 'bar'])
566
564
        self.assertEquals("Unknown rules detected: foo, bar.", str(err))
567
565
 
 
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
 
568
578
    def test_tip_change_rejected(self):
569
579
        err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
570
580
        self.assertEquals(
592
602
        try:
593
603
            raise Exception("example error")
594
604
        except Exception:
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
 
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")
603
610
 
604
611
    def test_must_have_working_tree(self):
605
612
        err = errors.MustHaveWorkingTree('foo', 'bar')
658
665
        err = errors.NotBranchError('path', bzrdir=bzrdir)
659
666
        self.assertEqual('Not a branch: "path".', str(err))
660
667
 
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
 
 
670
668
    def test_not_branch_laziness(self):
671
669
        real_bzrdir = self.make_bzrdir('path')
672
670
        class FakeBzrDir(object):
695
693
            'Please use `bzr unbind` to fix.')
696
694
        self.assertEqualDiff(msg, str(error))
697
695
 
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
 
 
706
696
 
707
697
class PassThroughError(errors.BzrError):
708
698
 
735
725
 
736
726
    def test_missing_format_string(self):
737
727
        e = ErrorWithNoFormat(param='randomvalue')
738
 
        self.assertStartsWith(str(e),
739
 
            "Unprintable exception ErrorWithNoFormat")
 
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.")
740
734
 
741
735
    def test_mismatched_format_args(self):
742
736
        # Even though ErrorWithBadFormat's format string does not match the