~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: INADA Naoki
  • Date: 2011-05-05 09:15:34 UTC
  • mto: (5830.3.3 i18n-msgfmt)
  • mto: This revision was merged to the branch mainline in revision 5873.
  • Revision ID: songofacandy@gmail.com-20110505091534-7sv835xpofwrmpt4
Add update-pot command to Makefile and tools/bzrgettext script that
extracts help text from bzr commands.

Show diffs side-by-side

added added

removed removed

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