/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2018-05-19 13:16:11 UTC
  • mto: (6968.4.3 git-archive)
  • mto: This revision was merged to the branch mainline in revision 6972.
  • Revision ID: jelmer@jelmer.uk-20180519131611-l9h9ud41j7qg1m03
Move tar/zip to breezy.archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007, 2009-2012, 2016 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Tests for display of exceptions."""
 
18
 
 
19
import os
 
20
import re
 
21
 
 
22
from breezy import (
 
23
    bzr,
 
24
    config,
 
25
    controldir,
 
26
    errors,
 
27
    osutils,
 
28
    repository,
 
29
    tests,
 
30
    )
 
31
from breezy.bzr.groupcompress_repo import RepositoryFormat2a
 
32
 
 
33
 
 
34
class TestExceptionReporting(tests.TestCaseInTempDir):
 
35
 
 
36
    def test_exception_exitcode(self):
 
37
        # we must use a subprocess, because the normal in-memory mechanism
 
38
        # allows errors to propagate up through the test suite
 
39
        out, err = self.run_bzr_subprocess(['assert-fail'],
 
40
            universal_newlines=True,
 
41
            retcode=errors.EXIT_INTERNAL_ERROR)
 
42
        self.assertEqual(4, errors.EXIT_INTERNAL_ERROR)
 
43
        self.assertContainsRe(err, r'\nAssertionError: always fails\n')
 
44
        self.assertContainsRe(err, r'Bazaar has encountered an internal error')
 
45
 
 
46
    def test_undecodable_argv(self):
 
47
        """A user error must be reported if argv is not in the locale encoding
 
48
 
 
49
        A subprocess with an environment ascii-only setting is used so the test
 
50
        can run without worrying about the locale the test suite is using.
 
51
        """
 
52
        if os.name != "posix":
 
53
            raise tests.TestNotApplicable("Needs system beholden to C locales")
 
54
        out, err = self.run_bzr_subprocess(["\xa0"],
 
55
            env_changes={"LANG": "C", "LC_ALL": "C"},
 
56
            universal_newlines=True,
 
57
            retcode=errors.EXIT_ERROR)
 
58
        self.assertContainsRe(err, r"^brz: ERROR: .*'\\xa0'.* unsupported",
 
59
            flags=re.MULTILINE)
 
60
        self.assertEqual(out, "")
 
61
 
 
62
    def test_utf8_default_fs_enc(self):
 
63
        """In the C locale brz treats a posix filesystem as UTF-8 encoded"""
 
64
        if os.name != "posix":
 
65
            raise tests.TestNotApplicable("Needs system beholden to C locales")
 
66
        out, err = self.run_bzr_subprocess(["init", "file:%C2%A7"],
 
67
            env_changes={"LANG": "C", "LC_ALL": "C"})
 
68
        self.assertContainsRe(out, "^Created a standalone tree .*$")
 
69
 
 
70
 
 
71
class TestOptParseBugHandling(tests.TestCase):
 
72
    "Test that we handle http://bugs.python.org/issue2931"
 
73
 
 
74
    def test_nonascii_optparse(self):
 
75
        """Reasonable error raised when non-ascii in option name"""
 
76
        error_re = 'Only ASCII permitted in option names'
 
77
        out = self.run_bzr_error([error_re], ['st', u'-\xe4'])
 
78
 
 
79
 
 
80
class TestObsoleteRepoFormat(RepositoryFormat2a):
 
81
 
 
82
    @classmethod
 
83
    def get_format_string(cls):
 
84
        return b"Test Obsolete Repository Format"
 
85
 
 
86
    def is_deprecated(self):
 
87
        return True
 
88
 
 
89
 
 
90
class TestDeprecationWarning(tests.TestCaseWithTransport):
 
91
    """The deprecation warning is controlled via a global variable:
 
92
    repository._deprecation_warning_done. As such, it can be emitted only once
 
93
    during a brz invocation, no matter how many repositories are involved.
 
94
 
 
95
    It would be better if it was a repo attribute instead but that's far more
 
96
    work than I want to do right now -- vila 20091215.
 
97
    """
 
98
 
 
99
    def setUp(self):
 
100
        super(TestDeprecationWarning, self).setUp()
 
101
        self.addCleanup(repository.format_registry.remove,
 
102
            TestObsoleteRepoFormat)
 
103
        repository.format_registry.register(TestObsoleteRepoFormat)
 
104
        self.addCleanup(controldir.format_registry.remove, "testobsolete")
 
105
        bzr.register_metadir(controldir.format_registry, "testobsolete",
 
106
            "breezy.tests.blackbox.test_exceptions.TestObsoleteRepoFormat",
 
107
            branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
108
            tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat6',
 
109
            deprecated=True,
 
110
            help='Same as 2a, but with an obsolete repo format.')
 
111
        self.disable_deprecation_warning()
 
112
 
 
113
    def enable_deprecation_warning(self, repo=None):
 
114
        """repo is not used yet since _deprecation_warning_done is a global"""
 
115
        repository._deprecation_warning_done = False
 
116
 
 
117
    def disable_deprecation_warning(self, repo=None):
 
118
        """repo is not used yet since _deprecation_warning_done is a global"""
 
119
        repository._deprecation_warning_done = True
 
120
 
 
121
    def make_obsolete_repo(self, path):
 
122
        # We don't want the deprecation raising during the repo creation
 
123
        format = controldir.format_registry.make_controldir("testobsolete")
 
124
        tree = self.make_branch_and_tree(path, format=format)
 
125
        return tree
 
126
 
 
127
    def check_warning(self, present):
 
128
        if present:
 
129
            check = self.assertContainsRe
 
130
        else:
 
131
            check = self.assertNotContainsRe
 
132
        check(self.get_log(), 'WARNING.*brz upgrade')
 
133
 
 
134
    def test_repository_deprecation_warning(self):
 
135
        """Old formats give a warning"""
 
136
        self.make_obsolete_repo('foo')
 
137
        self.enable_deprecation_warning()
 
138
        out, err = self.run_bzr('status', working_dir='foo')
 
139
        self.check_warning(True)
 
140
 
 
141
    def test_repository_deprecation_warning_suppressed_global(self):
 
142
        """Old formats give a warning"""
 
143
        conf = config.GlobalStack()
 
144
        conf.set('suppress_warnings', 'format_deprecation')
 
145
        self.make_obsolete_repo('foo')
 
146
        self.enable_deprecation_warning()
 
147
        out, err = self.run_bzr('status', working_dir='foo')
 
148
        self.check_warning(False)
 
149
 
 
150
    def test_repository_deprecation_warning_suppressed_locations(self):
 
151
        """Old formats give a warning"""
 
152
        self.make_obsolete_repo('foo')
 
153
        conf = config.LocationStack(osutils.pathjoin(self.test_dir, 'foo'))
 
154
        conf.set('suppress_warnings', 'format_deprecation')
 
155
        self.enable_deprecation_warning()
 
156
        out, err = self.run_bzr('status', working_dir='foo')
 
157
        self.check_warning(False)
 
158
 
 
159
    def test_repository_deprecation_warning_suppressed_branch(self):
 
160
        """Old formats give a warning"""
 
161
        tree = self.make_obsolete_repo('foo')
 
162
        conf = tree.branch.get_config_stack()
 
163
        conf.set('suppress_warnings', 'format_deprecation')
 
164
        self.enable_deprecation_warning()
 
165
        out, err = self.run_bzr('status', working_dir='foo')
 
166
        self.check_warning(False)