~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_exceptions.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2009, 2010, 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for display of exceptions."""
18
18
 
19
 
from cStringIO import StringIO
20
19
import os
21
 
import sys
 
20
import re
22
21
 
23
22
from bzrlib import (
24
23
    bzrdir,
 
24
    config,
 
25
    controldir,
25
26
    errors,
 
27
    osutils,
26
28
    repository,
27
 
    trace,
 
29
    tests,
28
30
    )
29
 
 
30
 
from bzrlib.tests import TestCaseInTempDir, TestCase
31
 
from bzrlib.errors import NotBranchError
32
 
 
33
 
 
34
 
class TestExceptionReporting(TestCase):
35
 
 
36
 
    def test_report_exception(self):
37
 
        """When an error occurs, display bug report details to stderr"""
38
 
        try:
39
 
            raise AssertionError("failed")
40
 
        except AssertionError, e:
41
 
            erf = StringIO()
42
 
            trace.report_exception(sys.exc_info(), erf)
43
 
        err = erf.getvalue()
44
 
        self.assertContainsRe(err,
45
 
            r'bzr: ERROR: exceptions\.AssertionError: failed\n')
46
 
        self.assertContainsRe(err,
47
 
            r'Please report a bug at https://bugs\.launchpad\.net/bzr/\+filebug')
48
 
        self.assertContainsRe(err,
49
 
            '(?m)^encoding: .*, fsenc: .*, lang: .*')
50
 
        self.assertContainsRe(err,
51
 
            '(?m)^plugins:$')
 
31
from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
 
32
 
 
33
 
 
34
class TestExceptionReporting(tests.TestCaseInTempDir):
52
35
 
53
36
    def test_exception_exitcode(self):
54
37
        # we must use a subprocess, because the normal in-memory mechanism
58
41
            retcode=errors.EXIT_INTERNAL_ERROR)
59
42
        self.assertEqual(4, errors.EXIT_INTERNAL_ERROR)
60
43
        self.assertContainsRe(err,
61
 
                r'bzr: ERROR: exceptions\.AssertionError: always fails\n')
62
 
        self.assertContainsRe(err, r'Please report a bug at')
63
 
    
64
 
 
65
 
class TestDeprecationWarning(TestCaseInTempDir):
 
44
                r'exceptions\.AssertionError: always fails\n')
 
45
        self.assertContainsRe(err, r'Bazaar has encountered an internal error')
 
46
 
 
47
    def test_undecodable_argv(self):
 
48
        """A user error must be reported if argv is not in the locale encoding
 
49
 
 
50
        A subprocess with an environment ascii-only setting is used so the test
 
51
        can run without worrying about the locale the test suite is using.
 
52
        """
 
53
        if os.name != "posix":
 
54
            raise tests.TestNotApplicable("Needs system beholden to C locales")
 
55
        out, err = self.run_bzr_subprocess(["\xa0"],
 
56
            env_changes={"LANG": "C", "LC_ALL": "C"},
 
57
            universal_newlines=True,
 
58
            retcode=errors.EXIT_ERROR)
 
59
        self.assertContainsRe(err, r"^bzr: ERROR: .*'\\xa0'.* unsupported",
 
60
            flags=re.MULTILINE)
 
61
        self.assertEquals(out, "")
 
62
 
 
63
 
 
64
class TestOptParseBugHandling(tests.TestCase):
 
65
    "Test that we handle http://bugs.python.org/issue2931"
 
66
 
 
67
    def test_nonascii_optparse(self):
 
68
        """Reasonable error raised when non-ascii in option name"""
 
69
        error_re = 'Only ASCII permitted in option names'
 
70
        out = self.run_bzr_error([error_re], ['st',u'-\xe4'])
 
71
 
 
72
 
 
73
class TestObsoleteRepoFormat(RepositoryFormat2a):
 
74
 
 
75
    @classmethod
 
76
    def get_format_string(cls):
 
77
        return "Test Obsolete Repository Format"
 
78
 
 
79
    def is_deprecated(self):
 
80
        return True
 
81
 
 
82
 
 
83
class TestDeprecationWarning(tests.TestCaseWithTransport):
 
84
    """The deprecation warning is controlled via a global variable:
 
85
    repository._deprecation_warning_done. As such, it can be emitted only once
 
86
    during a bzr invocation, no matter how many repositories are involved.
 
87
 
 
88
    It would be better if it was a repo attribute instead but that's far more
 
89
    work than I want to do right now -- vila 20091215.
 
90
    """
 
91
 
 
92
    def setUp(self):
 
93
        super(TestDeprecationWarning, self).setUp()
 
94
        self.addCleanup(repository.format_registry.remove,
 
95
            TestObsoleteRepoFormat)
 
96
        repository.format_registry.register(TestObsoleteRepoFormat)
 
97
        self.addCleanup(controldir.format_registry.remove, "testobsolete")
 
98
        bzrdir.register_metadir(controldir.format_registry, "testobsolete",
 
99
            "bzrlib.tests.blackbox.test_exceptions.TestObsoleteRepoFormat",
 
100
            branch_format='bzrlib.branch.BzrBranchFormat7',
 
101
            tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
102
            deprecated=True,
 
103
            help='Same as 2a, but with an obsolete repo format.')
 
104
        self.disable_deprecation_warning()
 
105
 
 
106
    def enable_deprecation_warning(self, repo=None):
 
107
        """repo is not used yet since _deprecation_warning_done is a global"""
 
108
        repository._deprecation_warning_done = False
 
109
 
 
110
    def disable_deprecation_warning(self, repo=None):
 
111
        """repo is not used yet since _deprecation_warning_done is a global"""
 
112
        repository._deprecation_warning_done = True
 
113
 
 
114
    def make_obsolete_repo(self, path):
 
115
        # We don't want the deprecation raising during the repo creation
 
116
        format = controldir.format_registry.make_bzrdir("testobsolete")
 
117
        tree = self.make_branch_and_tree(path, format=format)
 
118
        return tree
 
119
 
 
120
    def check_warning(self, present):
 
121
        if present:
 
122
            check = self.assertContainsRe
 
123
        else:
 
124
            check = self.assertNotContainsRe
 
125
        check(self.get_log(), 'WARNING.*bzr upgrade')
66
126
 
67
127
    def test_repository_deprecation_warning(self):
68
128
        """Old formats give a warning"""
69
 
        # the warning's normally off for testing but we reenable it
70
 
        repository._deprecation_warning_done = False
71
 
        try:
72
 
            os.mkdir('foo')
73
 
            bzrdir.BzrDirFormat5().initialize('foo')
74
 
            out, err = self.run_bzr("status foo")
75
 
            self.assertContainsRe(self._get_log(keep_log_file=True),
76
 
                                  "bzr upgrade")
77
 
        finally:
78
 
            repository._deprecation_warning_done = True
79
 
 
 
129
        self.make_obsolete_repo('foo')
 
130
        self.enable_deprecation_warning()
 
131
        out, err = self.run_bzr('status', working_dir='foo')
 
132
        self.check_warning(True)
 
133
 
 
134
    def test_repository_deprecation_warning_suppressed_global(self):
 
135
        """Old formats give a warning"""
 
136
        conf = config.GlobalConfig()
 
137
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
138
        self.make_obsolete_repo('foo')
 
139
        self.enable_deprecation_warning()
 
140
        out, err = self.run_bzr('status', working_dir='foo')
 
141
        self.check_warning(False)
 
142
 
 
143
    def test_repository_deprecation_warning_suppressed_locations(self):
 
144
        """Old formats give a warning"""
 
145
        self.make_obsolete_repo('foo')
 
146
        conf = config.LocationConfig(osutils.pathjoin(self.test_dir, 'foo'))
 
147
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
148
        self.enable_deprecation_warning()
 
149
        out, err = self.run_bzr('status', working_dir='foo')
 
150
        self.check_warning(False)
 
151
 
 
152
    def test_repository_deprecation_warning_suppressed_branch(self):
 
153
        """Old formats give a warning"""
 
154
        tree = self.make_obsolete_repo('foo')
 
155
        conf = tree.branch.get_config()
 
156
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
157
        self.enable_deprecation_warning()
 
158
        out, err = self.run_bzr('status', working_dir='foo')
 
159
        self.check_warning(False)