~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Vincent Ladeuil
  • Date: 2010-04-23 08:51:52 UTC
  • mfrom: (5131.2.6 support_OO_flag)
  • mto: This revision was merged to the branch mainline in revision 5179.
  • Revision ID: v.ladeuil+lp@free.fr-20100423085152-uoewc1vnkwqhw0pj
Manually assign docstrings to command objects, so that they work with python -OO

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-2010 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 socket
21
20
import sys
 
21
 
22
22
from bzrlib import (
23
23
    bzrdir,
24
24
    errors,
87
87
            "reason: reason for foo",
88
88
            str(error))
89
89
 
 
90
    def test_inconsistent_delta_delta(self):
 
91
        error = errors.InconsistentDeltaDelta([], 'reason')
 
92
        self.assertEqualDiff(
 
93
            "An inconsistent delta was supplied: []\nreason: reason",
 
94
            str(error))
 
95
 
90
96
    def test_in_process_transport(self):
91
97
        error = errors.InProcessTransport('fpp')
92
98
        self.assertEqualDiff(
114
120
            "read without data loss.",
115
121
            str(error))
116
122
 
117
 
    def test_install_failed(self):
118
 
        error = errors.InstallFailed(['rev-one'])
119
 
        self.assertEqual("Could not install revisions:\nrev-one", str(error))
120
 
        error = errors.InstallFailed(['rev-one', 'rev-two'])
121
 
        self.assertEqual("Could not install revisions:\nrev-one, rev-two",
122
 
                         str(error))
123
 
        error = errors.InstallFailed([None])
124
 
        self.assertEqual("Could not install revisions:\nNone", str(error))
 
123
    def test_jail_break(self):
 
124
        error = errors.JailBreak("some url")
 
125
        self.assertEqualDiff("An attempt to access a url outside the server"
 
126
            " jail was made: 'some url'.",
 
127
            str(error))
125
128
 
126
129
    def test_lock_active(self):
127
130
        error = errors.LockActive("lock description")
245
248
            "You will need to upgrade the branch to permit branch stacking.",
246
249
            str(error))
247
250
 
 
251
    def test_unstackable_location(self):
 
252
        error = errors.UnstackableLocationError('foo', 'bar')
 
253
        self.assertEqualDiff("The branch 'foo' cannot be stacked on 'bar'.",
 
254
            str(error))
 
255
 
248
256
    def test_unstackable_repository_format(self):
249
257
        format = u'foo'
250
258
        url = "/foo"
534
542
            1/0
535
543
        except ZeroDivisionError:
536
544
            exc_info = sys.exc_info()
537
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info)
 
545
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
538
546
        self.assertStartsWith(
539
547
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
540
548
        self.assertEndsWith(
615
623
        self.assertEqual(
616
624
            'Repository dummy repo cannot suspend a write group.', str(err))
617
625
 
 
626
    def test_not_branch_no_args(self):
 
627
        err = errors.NotBranchError('path')
 
628
        self.assertEqual('Not a branch: "path".', str(err))
 
629
 
 
630
    def test_not_branch_bzrdir_with_repo(self):
 
631
        bzrdir = self.make_repository('repo').bzrdir
 
632
        err = errors.NotBranchError('path', bzrdir=bzrdir)
 
633
        self.assertEqual(
 
634
            'Not a branch: "path": location is a repository.', str(err))
 
635
 
 
636
    def test_not_branch_bzrdir_without_repo(self):
 
637
        bzrdir = self.make_bzrdir('bzrdir')
 
638
        err = errors.NotBranchError('path', bzrdir=bzrdir)
 
639
        self.assertEqual('Not a branch: "path".', str(err))
 
640
 
 
641
    def test_not_branch_laziness(self):
 
642
        real_bzrdir = self.make_bzrdir('path')
 
643
        class FakeBzrDir(object):
 
644
            def __init__(self):
 
645
                self.calls = []
 
646
            def open_repository(self):
 
647
                self.calls.append('open_repository')
 
648
                raise errors.NoRepositoryPresent(real_bzrdir)
 
649
        fake_bzrdir = FakeBzrDir()
 
650
        err = errors.NotBranchError('path', bzrdir=fake_bzrdir)
 
651
        self.assertEqual([], fake_bzrdir.calls)
 
652
        str(err)
 
653
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
 
654
        # Stringifying twice doesn't try to open a repository twice.
 
655
        str(err)
 
656
        self.assertEqual(['open_repository'], fake_bzrdir.calls)
 
657
 
618
658
 
619
659
class PassThroughError(errors.BzrError):
620
660
 
630
670
 
631
671
 
632
672
class ErrorWithNoFormat(errors.BzrError):
633
 
    """This class has a docstring but no format string."""
 
673
    __doc__ = """This class has a docstring but no format string."""
634
674
 
635
675
 
636
676
class TestErrorFormatting(TestCase):
661
701
        e = ErrorWithBadFormat(not_thing='x')
662
702
        self.assertStartsWith(
663
703
            str(e), 'Unprintable exception ErrorWithBadFormat')
 
704
 
 
705
    def test_cannot_bind_address(self):
 
706
        # see <https://bugs.edge.launchpad.net/bzr/+bug/286871>
 
707
        e = errors.CannotBindAddress('example.com', 22,
 
708
            socket.error(13, 'Permission denied'))
 
709
        self.assertContainsRe(str(e),
 
710
            r'Cannot bind address "example\.com:22":.*Permission denied')
 
711
 
 
712
    def test_file_timestamp_unavailable(self):            
 
713
        e = errors.FileTimestampUnavailable("/path/foo")
 
714
        self.assertEquals("The filestamp for /path/foo is not available.",
 
715
            str(e))