/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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, br'\nAssertionError: always fails\n')
 
44
        self.assertContainsRe(
 
45
            err, br'Bazaar has encountered an internal error')
 
46
 
 
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 .*$")
 
54
 
 
55
 
 
56
class TestOptParseBugHandling(tests.TestCase):
 
57
    "Test that we handle http://bugs.python.org/issue2931"
 
58
 
 
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'])
 
63
 
 
64
 
 
65
class TestObsoleteRepoFormat(RepositoryFormat2a):
 
66
 
 
67
    @classmethod
 
68
    def get_format_string(cls):
 
69
        return b"Test Obsolete Repository Format"
 
70
 
 
71
    def is_deprecated(self):
 
72
        return True
 
73
 
 
74
 
 
75
class TestDeprecationWarning(tests.TestCaseWithTransport):
 
76
    """The deprecation warning is controlled via a global variable:
 
77
    repository._deprecation_warning_done. As such, it can be emitted only once
 
78
    during a brz invocation, no matter how many repositories are involved.
 
79
 
 
80
    It would be better if it was a repo attribute instead but that's far more
 
81
    work than I want to do right now -- vila 20091215.
 
82
    """
 
83
 
 
84
    def setUp(self):
 
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',
 
94
                             deprecated=True,
 
95
                             help='Same as 2a, but with an obsolete repo format.')
 
96
        self.disable_deprecation_warning()
 
97
 
 
98
    def enable_deprecation_warning(self, repo=None):
 
99
        """repo is not used yet since _deprecation_warning_done is a global"""
 
100
        repository._deprecation_warning_done = False
 
101
 
 
102
    def disable_deprecation_warning(self, repo=None):
 
103
        """repo is not used yet since _deprecation_warning_done is a global"""
 
104
        repository._deprecation_warning_done = True
 
105
 
 
106
    def make_obsolete_repo(self, path):
 
107
        # We don't want the deprecation raising during the repo creation
 
108
        format = controldir.format_registry.make_controldir("testobsolete")
 
109
        tree = self.make_branch_and_tree(path, format=format)
 
110
        return tree
 
111
 
 
112
    def check_warning(self, present):
 
113
        if present:
 
114
            check = self.assertContainsRe
 
115
        else:
 
116
            check = self.assertNotContainsRe
 
117
        check(self.get_log(), 'WARNING.*brz upgrade')
 
118
 
 
119
    def test_repository_deprecation_warning(self):
 
120
        """Old formats give a warning"""
 
121
        self.make_obsolete_repo('foo')
 
122
        self.enable_deprecation_warning()
 
123
        out, err = self.run_bzr('status', working_dir='foo')
 
124
        self.check_warning(True)
 
125
 
 
126
    def test_repository_deprecation_warning_suppressed_global(self):
 
127
        """Old formats give a warning"""
 
128
        conf = config.GlobalStack()
 
129
        conf.set('suppress_warnings', 'format_deprecation')
 
130
        self.make_obsolete_repo('foo')
 
131
        self.enable_deprecation_warning()
 
132
        out, err = self.run_bzr('status', working_dir='foo')
 
133
        self.check_warning(False)
 
134
 
 
135
    def test_repository_deprecation_warning_suppressed_locations(self):
 
136
        """Old formats give a warning"""
 
137
        self.make_obsolete_repo('foo')
 
138
        conf = config.LocationStack(osutils.pathjoin(self.test_dir, 'foo'))
 
139
        conf.set('suppress_warnings', 'format_deprecation')
 
140
        self.enable_deprecation_warning()
 
141
        out, err = self.run_bzr('status', working_dir='foo')
 
142
        self.check_warning(False)
 
143
 
 
144
    def test_repository_deprecation_warning_suppressed_branch(self):
 
145
        """Old formats give a warning"""
 
146
        tree = self.make_obsolete_repo('foo')
 
147
        conf = tree.branch.get_config_stack()
 
148
        conf.set('suppress_warnings', 'format_deprecation')
 
149
        self.enable_deprecation_warning()
 
150
        out, err = self.run_bzr('status', working_dir='foo')
 
151
        self.check_warning(False)