~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_errors.py

  • Committer: Robert Collins
  • Date: 2009-07-16 23:28:49 UTC
  • mfrom: (4543 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4553.
  • Revision ID: robertc@robertcollins.net-20090716232849-u7u6g658j7qn7rp2
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com>
 
3
#            and others
2
4
#
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
16
18
 
17
19
"""Tests for the formatting and construction of errors."""
18
20
 
19
 
import socket
20
21
import sys
21
 
 
22
22
from bzrlib import (
23
23
    bzrdir,
24
24
    errors,
542
542
            1/0
543
543
        except ZeroDivisionError:
544
544
            exc_info = sys.exc_info()
545
 
        err = errors.HookFailed('hook stage', 'hook name', exc_info, warn=False)
 
545
        err = errors.HookFailed('hook stage', 'hook name', exc_info)
546
546
        self.assertStartsWith(
547
547
            str(err), 'Hook \'hook name\' during hook stage failed:\n')
548
548
        self.assertEndsWith(
623
623
        self.assertEqual(
624
624
            'Repository dummy repo cannot suspend a write group.', str(err))
625
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
 
 
658
626
 
659
627
class PassThroughError(errors.BzrError):
660
628
 
670
638
 
671
639
 
672
640
class ErrorWithNoFormat(errors.BzrError):
673
 
    __doc__ = """This class has a docstring but no format string."""
 
641
    """This class has a docstring but no format string."""
674
642
 
675
643
 
676
644
class TestErrorFormatting(TestCase):
701
669
        e = ErrorWithBadFormat(not_thing='x')
702
670
        self.assertStartsWith(
703
671
            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))