~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for display of exceptions."""
18
18
 
 
19
from cStringIO import StringIO
19
20
import os
20
 
import re
 
21
import sys
21
22
 
22
23
from bzrlib import (
23
24
    bzrdir,
24
 
    config,
25
 
    controldir,
26
25
    errors,
27
 
    osutils,
28
26
    repository,
29
 
    tests,
 
27
    trace,
30
28
    )
31
 
from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
32
 
 
33
 
 
34
 
class TestExceptionReporting(tests.TestCaseInTempDir):
 
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:$')
35
52
 
36
53
    def test_exception_exitcode(self):
37
54
        # we must use a subprocess, because the normal in-memory mechanism
41
58
            retcode=errors.EXIT_INTERNAL_ERROR)
42
59
        self.assertEqual(4, errors.EXIT_INTERNAL_ERROR)
43
60
        self.assertContainsRe(err,
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.assertEqual(out, "")
62
 
 
63
 
    def test_utf8_default_fs_enc(self):
64
 
        """In the C locale bzr treats a posix filesystem as UTF-8 encoded"""
65
 
        if os.name != "posix":
66
 
            raise tests.TestNotApplicable("Needs system beholden to C locales")
67
 
        out, err = self.run_bzr_subprocess(["init", "file:%C2%A7"],
68
 
            env_changes={"LANG": "C", "LC_ALL": "C"})
69
 
        self.assertContainsRe(out, "^Created a standalone tree .*$")
70
 
 
71
 
 
72
 
class TestOptParseBugHandling(tests.TestCase):
73
 
    "Test that we handle http://bugs.python.org/issue2931"
74
 
 
75
 
    def test_nonascii_optparse(self):
76
 
        """Reasonable error raised when non-ascii in option name"""
77
 
        error_re = 'Only ASCII permitted in option names'
78
 
        out = self.run_bzr_error([error_re], ['st',u'-\xe4'])
79
 
 
80
 
 
81
 
class TestObsoleteRepoFormat(RepositoryFormat2a):
82
 
 
83
 
    @classmethod
84
 
    def get_format_string(cls):
85
 
        return "Test Obsolete Repository Format"
86
 
 
87
 
    def is_deprecated(self):
88
 
        return True
89
 
 
90
 
 
91
 
class TestDeprecationWarning(tests.TestCaseWithTransport):
92
 
    """The deprecation warning is controlled via a global variable:
93
 
    repository._deprecation_warning_done. As such, it can be emitted only once
94
 
    during a bzr invocation, no matter how many repositories are involved.
95
 
 
96
 
    It would be better if it was a repo attribute instead but that's far more
97
 
    work than I want to do right now -- vila 20091215.
98
 
    """
99
 
 
100
 
    def setUp(self):
101
 
        super(TestDeprecationWarning, self).setUp()
102
 
        self.addCleanup(repository.format_registry.remove,
103
 
            TestObsoleteRepoFormat)
104
 
        repository.format_registry.register(TestObsoleteRepoFormat)
105
 
        self.addCleanup(controldir.format_registry.remove, "testobsolete")
106
 
        bzrdir.register_metadir(controldir.format_registry, "testobsolete",
107
 
            "bzrlib.tests.blackbox.test_exceptions.TestObsoleteRepoFormat",
108
 
            branch_format='bzrlib.branch.BzrBranchFormat7',
109
 
            tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
110
 
            deprecated=True,
111
 
            help='Same as 2a, but with an obsolete repo format.')
112
 
        self.disable_deprecation_warning()
113
 
 
114
 
    def enable_deprecation_warning(self, repo=None):
115
 
        """repo is not used yet since _deprecation_warning_done is a global"""
 
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):
 
66
 
 
67
    def test_repository_deprecation_warning(self):
 
68
        """Old formats give a warning"""
 
69
        # the warning's normally off for testing but we reenable it
116
70
        repository._deprecation_warning_done = False
117
 
 
118
 
    def disable_deprecation_warning(self, repo=None):
119
 
        """repo is not used yet since _deprecation_warning_done is a global"""
120
 
        repository._deprecation_warning_done = True
121
 
 
122
 
    def make_obsolete_repo(self, path):
123
 
        # We don't want the deprecation raising during the repo creation
124
 
        format = controldir.format_registry.make_bzrdir("testobsolete")
125
 
        tree = self.make_branch_and_tree(path, format=format)
126
 
        return tree
127
 
 
128
 
    def check_warning(self, present):
129
 
        if present:
130
 
            check = self.assertContainsRe
131
 
        else:
132
 
            check = self.assertNotContainsRe
133
 
        check(self.get_log(), 'WARNING.*bzr upgrade')
134
 
 
135
 
    def test_repository_deprecation_warning(self):
136
 
        """Old formats give a warning"""
137
 
        self.make_obsolete_repo('foo')
138
 
        self.enable_deprecation_warning()
139
 
        out, err = self.run_bzr('status', working_dir='foo')
140
 
        self.check_warning(True)
141
 
 
142
 
    def test_repository_deprecation_warning_suppressed_global(self):
143
 
        """Old formats give a warning"""
144
 
        conf = config.GlobalStack()
145
 
        conf.set('suppress_warnings', 'format_deprecation')
146
 
        self.make_obsolete_repo('foo')
147
 
        self.enable_deprecation_warning()
148
 
        out, err = self.run_bzr('status', working_dir='foo')
149
 
        self.check_warning(False)
150
 
 
151
 
    def test_repository_deprecation_warning_suppressed_locations(self):
152
 
        """Old formats give a warning"""
153
 
        self.make_obsolete_repo('foo')
154
 
        conf = config.LocationStack(osutils.pathjoin(self.test_dir, 'foo'))
155
 
        conf.set('suppress_warnings', 'format_deprecation')
156
 
        self.enable_deprecation_warning()
157
 
        out, err = self.run_bzr('status', working_dir='foo')
158
 
        self.check_warning(False)
159
 
 
160
 
    def test_repository_deprecation_warning_suppressed_branch(self):
161
 
        """Old formats give a warning"""
162
 
        tree = self.make_obsolete_repo('foo')
163
 
        conf = tree.branch.get_config_stack()
164
 
        conf.set('suppress_warnings', 'format_deprecation')
165
 
        self.enable_deprecation_warning()
166
 
        out, err = self.run_bzr('status', working_dir='foo')
167
 
        self.check_warning(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