~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

Merge bzr.dev, update to use new hooks.

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
25
25
    bzrdir,
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):
297
300
            str(error))
298
301
 
299
302
    def test_up_to_date(self):
300
 
        error = errors.UpToDateFormat(bzrdir.BzrDirFormat4())
301
 
        self.assertEqualDiff("The branch format All-in-one "
302
 
                             "format 4 is already at the most "
303
 
                             "recent format.",
304
 
                             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))
305
307
 
306
308
    def test_corrupt_repository(self):
307
309
        repo = self.make_repository('.')
347
349
        self.assertEqual("The value 'foo' is not a valid value.",
348
350
            str(error))
349
351
 
350
 
    def test_bzrnewerror_is_deprecated(self):
351
 
        class DeprecatedError(errors.BzrNewError):
352
 
            pass
353
 
        self.callDeprecated(['BzrNewError was deprecated in bzr 0.13; '
354
 
             'please convert DeprecatedError to use BzrError instead'],
355
 
            DeprecatedError)
356
 
 
357
352
    def test_bzrerror_from_literal_string(self):
358
353
        # Some code constructs BzrError from a literal string, in which case
359
354
        # no further formatting is done.  (I'm not sure raising the base class
570
565
        err = errors.UnknownRules(['foo', 'bar'])
571
566
        self.assertEquals("Unknown rules detected: foo, bar.", str(err))
572
567
 
573
 
    def test_hook_failed(self):
574
 
        # Create an exc_info tuple by raising and catching an exception.
575
 
        try:
576
 
            1/0
577
 
        except ZeroDivisionError:
578
 
            exc_info = sys.exc_info()
579
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
580
 
        self.assertStartsWith(
581
 
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
582
 
        self.assertEndsWith(
583
 
            str(err), 'integer division or modulo by zero')
584
 
 
585
568
    def test_tip_change_rejected(self):
586
569
        err = errors.TipChangeRejected(u'Unicode message\N{INTERROBANG}')
587
570
        self.assertEquals(
609
592
        try:
610
593
            raise Exception("example error")
611
594
        except Exception:
612
 
            exc_info = sys.exc_info()
613
 
        err = errors.SmartMessageHandlerError(exc_info)
614
 
        self.assertStartsWith(
615
 
            str(err), "The message handler raised an exception:\n")
616
 
        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
617
603
 
618
604
    def test_must_have_working_tree(self):
619
605
        err = errors.MustHaveWorkingTree('foo', 'bar')
672
658
        err = errors.NotBranchError('path', bzrdir=bzrdir)
673
659
        self.assertEqual('Not a branch: "path".', str(err))
674
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
 
675
670
    def test_not_branch_laziness(self):
676
671
        real_bzrdir = self.make_bzrdir('path')
677
672
        class FakeBzrDir(object):
700
695
            'Please use `bzr unbind` to fix.')
701
696
        self.assertEqualDiff(msg, str(error))
702
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
 
703
706
 
704
707
class PassThroughError(errors.BzrError):
705
708
 
732
735
 
733
736
    def test_missing_format_string(self):
734
737
        e = ErrorWithNoFormat(param='randomvalue')
735
 
        s = self.callDeprecated(
736
 
                ['ErrorWithNoFormat uses its docstring as a format, it should use _fmt instead'],
737
 
                lambda x: str(x), e)
738
 
        ## s = str(e)
739
 
        self.assertEqual(s,
740
 
                "This class has a docstring but no format string.")
 
738
        self.assertStartsWith(str(e),
 
739
            "Unprintable exception ErrorWithNoFormat")
741
740
 
742
741
    def test_mismatched_format_args(self):
743
742
        # Even though ErrorWithBadFormat's format string does not match the