1
# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
1
# Copyright (C) 2006, 2007, 2009-2012, 2016 Canonical Ltd
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
17
17
"""Tests for display of exceptions."""
19
from cStringIO import StringIO
33
from bzrlib.tests import TestCaseInTempDir, TestCase
34
from bzrlib.errors import NotBranchError
37
class TestExceptionReporting(TestCase):
31
from breezy.bzr.groupcompress_repo import RepositoryFormat2a
34
class TestExceptionReporting(tests.TestCaseInTempDir):
39
36
def test_exception_exitcode(self):
40
37
# we must use a subprocess, because the normal in-memory mechanism
41
38
# allows errors to propagate up through the test suite
42
39
out, err = self.run_bzr_subprocess(['assert-fail'],
43
universal_newlines=True,
44
retcode=errors.EXIT_INTERNAL_ERROR)
40
universal_newlines=True,
41
retcode=errors.EXIT_INTERNAL_ERROR)
45
42
self.assertEqual(4, errors.EXIT_INTERNAL_ERROR)
46
self.assertContainsRe(err,
47
r'exceptions\.AssertionError: always fails\n')
48
self.assertContainsRe(err, r'Bazaar has encountered an internal error')
43
self.assertContainsRe(err, br'\nAssertionError: always fails\n')
44
self.assertContainsRe(
45
err, br'Bazaar has encountered an internal error')
47
def test_utf8_default_fs_enc(self):
48
"""In the C locale brz treats a posix filesystem as UTF-8 encoded"""
49
if os.name != "posix":
50
raise tests.TestNotApplicable("Needs system beholden to C locales")
51
out, err = self.run_bzr_subprocess(["init", "file:%C2%A7"],
52
env_changes={"LANG": "C", "LC_ALL": "C"})
53
self.assertContainsRe(out, b"^Created a standalone tree .*$")
56
class TestOptParseBugHandling(tests.TestCase):
57
"Test that we handle http://bugs.python.org/issue2931"
59
def test_nonascii_optparse(self):
60
"""Reasonable error raised when non-ascii in option name on Python 2"""
61
error_re = u'no such option: -\xe4'
62
out = self.run_bzr_error([error_re], ['st', u'-\xe4'])
65
class TestObsoleteRepoFormat(RepositoryFormat2a):
68
def get_format_string(cls):
69
return b"Test Obsolete Repository Format"
71
def is_deprecated(self):
51
75
class TestDeprecationWarning(tests.TestCaseWithTransport):
52
76
"""The deprecation warning is controlled via a global variable:
53
77
repository._deprecation_warning_done. As such, it can be emitted only once
54
during a bzr invocation, no matter how many repositories are involved.
78
during a brz invocation, no matter how many repositories are involved.
56
80
It would be better if it was a repo attribute instead but that's far more
57
81
work than I want to do right now -- vila 20091215.
61
85
super(TestDeprecationWarning, self).setUp()
86
self.addCleanup(repository.format_registry.remove,
87
TestObsoleteRepoFormat)
88
repository.format_registry.register(TestObsoleteRepoFormat)
89
self.addCleanup(controldir.format_registry.remove, "testobsolete")
90
bzr.register_metadir(controldir.format_registry, "testobsolete",
91
"breezy.tests.blackbox.test_exceptions.TestObsoleteRepoFormat",
92
branch_format='breezy.bzr.branch.BzrBranchFormat7',
93
tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat6',
95
help='Same as 2a, but with an obsolete repo format.')
62
96
self.disable_deprecation_warning()
64
98
def enable_deprecation_warning(self, repo=None):
72
106
def make_obsolete_repo(self, path):
73
107
# We don't want the deprecation raising during the repo creation
74
tree = self.make_branch_and_tree(path, format=bzrdir.BzrDirFormat5())
108
format = controldir.format_registry.make_controldir("testobsolete")
109
tree = self.make_branch_and_tree(path, format=format)
77
112
def check_warning(self, present):
79
114
check = self.assertContainsRe
81
116
check = self.assertNotContainsRe
82
check(self._get_log(keep_log_file=True), 'WARNING.*bzr upgrade')
117
check(self.get_log(), 'WARNING.*brz upgrade')
84
119
def test_repository_deprecation_warning(self):
85
120
"""Old formats give a warning"""
91
126
def test_repository_deprecation_warning_suppressed_global(self):
92
127
"""Old formats give a warning"""
93
conf = config.GlobalConfig()
94
conf.set_user_option('suppress_warnings', 'format_deprecation')
128
conf = config.GlobalStack()
129
conf.set('suppress_warnings', 'format_deprecation')
95
130
self.make_obsolete_repo('foo')
96
131
self.enable_deprecation_warning()
97
132
out, err = self.run_bzr('status', working_dir='foo')
100
135
def test_repository_deprecation_warning_suppressed_locations(self):
101
136
"""Old formats give a warning"""
102
137
self.make_obsolete_repo('foo')
103
conf = config.LocationConfig(osutils.pathjoin(self.test_dir, 'foo'))
104
conf.set_user_option('suppress_warnings', 'format_deprecation')
138
conf = config.LocationStack(osutils.pathjoin(self.test_dir, 'foo'))
139
conf.set('suppress_warnings', 'format_deprecation')
105
140
self.enable_deprecation_warning()
106
141
out, err = self.run_bzr('status', working_dir='foo')
107
142
self.check_warning(False)
109
144
def test_repository_deprecation_warning_suppressed_branch(self):
110
145
"""Old formats give a warning"""
111
146
tree = self.make_obsolete_repo('foo')
112
conf = tree.branch.get_config()
113
conf.set_user_option('suppress_warnings', 'format_deprecation')
147
conf = tree.branch.get_config_stack()
148
conf.set('suppress_warnings', 'format_deprecation')
114
149
self.enable_deprecation_warning()
115
150
out, err = self.run_bzr('status', working_dir='foo')
116
151
self.check_warning(False)