17
17
"""Tests for display of exceptions."""
 
 
19
from cStringIO import StringIO
 
31
 
from breezy.sixish import PY3
 
32
 
from breezy.bzr.groupcompress_repo import RepositoryFormat2a
 
35
 
class TestExceptionReporting(tests.TestCaseInTempDir):
 
 
33
from bzrlib.tests import TestCaseInTempDir, TestCase
 
 
34
from bzrlib.errors import NotBranchError
 
 
37
class TestExceptionReporting(TestCase):
 
37
39
    def test_exception_exitcode(self):
 
38
40
        # we must use a subprocess, because the normal in-memory mechanism
 
39
41
        # allows errors to propagate up through the test suite
 
40
42
        out, err = self.run_bzr_subprocess(['assert-fail'],
 
41
 
                                           universal_newlines=True,
 
42
 
                                           retcode=errors.EXIT_INTERNAL_ERROR)
 
 
43
            universal_newlines=True,
 
 
44
            retcode=errors.EXIT_INTERNAL_ERROR)
 
43
45
        self.assertEqual(4, errors.EXIT_INTERNAL_ERROR)
 
44
 
        self.assertContainsRe(err, br'\nAssertionError: always fails\n')
 
45
 
        self.assertContainsRe(
 
46
 
            err, br'Bazaar has encountered an internal error')
 
48
 
    def test_undecodable_argv(self):
 
49
 
        """A user error must be reported if argv is not in the locale encoding
 
51
 
        A subprocess with an environment ascii-only setting is used so the test
 
52
 
        can run without worrying about the locale the test suite is using.
 
54
 
        if os.name != "posix":
 
55
 
            raise tests.TestNotApplicable("Needs system beholden to C locales")
 
57
 
            raise tests.TestNotApplicable(
 
58
 
                "Unable to pass argv to subprocess as bytes")
 
59
 
        out, err = self.run_bzr_subprocess([b"\xa0"],
 
61
 
                                               "LANG": "C", "LC_ALL": "C"},
 
62
 
                                           universal_newlines=True,
 
63
 
                                           retcode=errors.EXIT_ERROR)
 
64
 
        self.assertContainsRe(err, br"^brz: ERROR: .*'\\xa0'.* unsupported",
 
66
 
        self.assertEqual(out, b"")
 
68
 
    def test_utf8_default_fs_enc(self):
 
69
 
        """In the C locale brz treats a posix filesystem as UTF-8 encoded"""
 
70
 
        if os.name != "posix":
 
71
 
            raise tests.TestNotApplicable("Needs system beholden to C locales")
 
72
 
        out, err = self.run_bzr_subprocess(["init", "file:%C2%A7"],
 
73
 
                                           env_changes={"LANG": "C", "LC_ALL": "C"})
 
74
 
        self.assertContainsRe(out, b"^Created a standalone tree .*$")
 
77
 
class TestOptParseBugHandling(tests.TestCase):
 
78
 
    "Test that we handle http://bugs.python.org/issue2931"
 
80
 
    def test_nonascii_optparse(self):
 
81
 
        """Reasonable error raised when non-ascii in option name on Python 2"""
 
83
 
            error_re = u'no such option: -\xe4'
 
85
 
            error_re = 'Only ASCII permitted in option names'
 
86
 
        out = self.run_bzr_error([error_re], ['st', u'-\xe4'])
 
89
 
class TestObsoleteRepoFormat(RepositoryFormat2a):
 
92
 
    def get_format_string(cls):
 
93
 
        return b"Test Obsolete Repository Format"
 
95
 
    def is_deprecated(self):
 
 
46
        self.assertContainsRe(err,
 
 
47
                r'exceptions\.AssertionError: always fails\n')
 
 
48
        self.assertContainsRe(err, r'Bazaar has encountered an internal error')
 
99
51
class TestDeprecationWarning(tests.TestCaseWithTransport):
 
100
52
    """The deprecation warning is controlled via a global variable:
 
101
53
    repository._deprecation_warning_done. As such, it can be emitted only once
 
102
 
    during a brz invocation, no matter how many repositories are involved.
 
 
54
    during a bzr invocation, no matter how many repositories are involved.
 
104
56
    It would be better if it was a repo attribute instead but that's far more
 
105
57
    work than I want to do right now -- vila 20091215.
 
 
109
61
        super(TestDeprecationWarning, self).setUp()
 
110
 
        self.addCleanup(repository.format_registry.remove,
 
111
 
                        TestObsoleteRepoFormat)
 
112
 
        repository.format_registry.register(TestObsoleteRepoFormat)
 
113
 
        self.addCleanup(controldir.format_registry.remove, "testobsolete")
 
114
 
        bzr.register_metadir(controldir.format_registry, "testobsolete",
 
115
 
                             "breezy.tests.blackbox.test_exceptions.TestObsoleteRepoFormat",
 
116
 
                             branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
117
 
                             tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat6',
 
119
 
                             help='Same as 2a, but with an obsolete repo format.')
 
120
62
        self.disable_deprecation_warning()
 
122
64
    def enable_deprecation_warning(self, repo=None):
 
 
150
91
    def test_repository_deprecation_warning_suppressed_global(self):
 
151
92
        """Old formats give a warning"""
 
152
 
        conf = config.GlobalStack()
 
153
 
        conf.set('suppress_warnings', 'format_deprecation')
 
 
93
        conf = config.GlobalConfig()
 
 
94
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
154
95
        self.make_obsolete_repo('foo')
 
155
96
        self.enable_deprecation_warning()
 
156
97
        out, err = self.run_bzr('status', working_dir='foo')
 
 
159
100
    def test_repository_deprecation_warning_suppressed_locations(self):
 
160
101
        """Old formats give a warning"""
 
161
102
        self.make_obsolete_repo('foo')
 
162
 
        conf = config.LocationStack(osutils.pathjoin(self.test_dir, 'foo'))
 
163
 
        conf.set('suppress_warnings', 'format_deprecation')
 
 
103
        conf = config.LocationConfig(osutils.pathjoin(self.test_dir, 'foo'))
 
 
104
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
164
105
        self.enable_deprecation_warning()
 
165
106
        out, err = self.run_bzr('status', working_dir='foo')
 
166
107
        self.check_warning(False)
 
 
168
109
    def test_repository_deprecation_warning_suppressed_branch(self):
 
169
110
        """Old formats give a warning"""
 
170
111
        tree = self.make_obsolete_repo('foo')
 
171
 
        conf = tree.branch.get_config_stack()
 
172
 
        conf.set('suppress_warnings', 'format_deprecation')
 
 
112
        conf = tree.branch.get_config()
 
 
113
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
173
114
        self.enable_deprecation_warning()
 
174
115
        out, err = self.run_bzr('status', working_dir='foo')
 
175
116
        self.check_warning(False)