~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-18 06:27:34 UTC
  • mfrom: (5887 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5894.
  • Revision ID: songofacandy@gmail.com-20110518062734-1ilhll0rrqyyp8um
merge from lp:bzr and resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 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
16
16
 
17
17
"""Tests for display of exceptions."""
18
18
 
19
 
from cStringIO import StringIO
20
19
import os
21
20
import sys
22
21
 
23
22
from bzrlib import (
24
23
    bzrdir,
 
24
    config,
 
25
    controldir,
25
26
    errors,
 
27
    osutils,
26
28
    repository,
27
 
    trace,
 
29
    tests,
28
30
    )
 
31
from bzrlib.repofmt.groupcompress_repo import RepositoryFormat2a
29
32
 
30
 
from bzrlib.tests import TestCaseInTempDir, TestCase
31
 
from bzrlib.errors import NotBranchError
 
33
from bzrlib.tests import TestCase
32
34
 
33
35
 
34
36
class TestExceptionReporting(TestCase):
35
37
 
36
 
    def test_report_exception(self):
37
 
        """When an error occurs, display bug report details to stderr"""
38
 
        try:
39
 
            raise AssertionError("failed")
40
 
        except AssertionError, e:
41
 
            erf = StringIO()
42
 
            trace.report_exception(sys.exc_info(), erf)
43
 
        err = erf.getvalue()
44
 
        self.assertContainsRe(err,
45
 
            r'bzr: ERROR: exceptions\.AssertionError: failed\n')
46
 
        self.assertContainsRe(err,
47
 
            r'Please report a bug at https://bugs\.launchpad\.net/bzr/\+filebug')
48
 
        self.assertContainsRe(err,
49
 
            '(?m)^encoding: .*, fsenc: .*, lang: .*')
50
 
        self.assertContainsRe(err,
51
 
            '(?m)^plugins:$')
52
 
 
53
38
    def test_exception_exitcode(self):
54
39
        # we must use a subprocess, because the normal in-memory mechanism
55
40
        # allows errors to propagate up through the test suite
58
43
            retcode=errors.EXIT_INTERNAL_ERROR)
59
44
        self.assertEqual(4, errors.EXIT_INTERNAL_ERROR)
60
45
        self.assertContainsRe(err,
61
 
                r'bzr: ERROR: exceptions\.AssertionError: always fails\n')
62
 
        self.assertContainsRe(err, r'Please report a bug at')
63
 
 
64
 
 
65
 
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
        error_re = 'Only ASCII permitted in option names'
 
71
        out = self.run_bzr_error([error_re], ['st',u'-\xe4'])
 
72
 
 
73
 
 
74
class TestObsoleteRepoFormat(RepositoryFormat2a):
 
75
 
 
76
    @classmethod
 
77
    def get_format_string(cls):
 
78
        return "Test Obsolete Repository Format"
 
79
 
 
80
    def is_deprecated(self):
 
81
        return True
 
82
 
 
83
 
 
84
class TestDeprecationWarning(tests.TestCaseWithTransport):
 
85
    """The deprecation warning is controlled via a global variable:
 
86
    repository._deprecation_warning_done. As such, it can be emitted only once
 
87
    during a bzr invocation, no matter how many repositories are involved.
 
88
 
 
89
    It would be better if it was a repo attribute instead but that's far more
 
90
    work than I want to do right now -- vila 20091215.
 
91
    """
 
92
 
 
93
    def setUp(self):
 
94
        super(TestDeprecationWarning, self).setUp()
 
95
        self.addCleanup(repository.format_registry.remove,
 
96
            TestObsoleteRepoFormat)
 
97
        repository.format_registry.register(TestObsoleteRepoFormat)
 
98
        self.addCleanup(controldir.format_registry.remove, "testobsolete")
 
99
        bzrdir.register_metadir(controldir.format_registry, "testobsolete",
 
100
            "bzrlib.tests.blackbox.test_exceptions.TestObsoleteRepoFormat",
 
101
            branch_format='bzrlib.branch.BzrBranchFormat7',
 
102
            tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
103
            deprecated=True,
 
104
            help='Same as 2a, but with an obsolete repo format.')
 
105
        self.disable_deprecation_warning()
 
106
 
 
107
    def enable_deprecation_warning(self, repo=None):
 
108
        """repo is not used yet since _deprecation_warning_done is a global"""
 
109
        repository._deprecation_warning_done = False
 
110
 
 
111
    def disable_deprecation_warning(self, repo=None):
 
112
        """repo is not used yet since _deprecation_warning_done is a global"""
 
113
        repository._deprecation_warning_done = True
 
114
 
 
115
    def make_obsolete_repo(self, path):
 
116
        # We don't want the deprecation raising during the repo creation
 
117
        format = controldir.format_registry.make_bzrdir("testobsolete")
 
118
        tree = self.make_branch_and_tree(path, format=format)
 
119
        return tree
 
120
 
 
121
    def check_warning(self, present):
 
122
        if present:
 
123
            check = self.assertContainsRe
 
124
        else:
 
125
            check = self.assertNotContainsRe
 
126
        check(self._get_log(keep_log_file=True), 'WARNING.*bzr upgrade')
66
127
 
67
128
    def test_repository_deprecation_warning(self):
68
129
        """Old formats give a warning"""
69
 
        # the warning's normally off for testing but we reenable it
70
 
        repository._deprecation_warning_done = False
71
 
        try:
72
 
            os.mkdir('foo')
73
 
            bzrdir.BzrDirFormat5().initialize('foo')
74
 
            out, err = self.run_bzr("status foo")
75
 
            self.assertContainsRe(self._get_log(keep_log_file=True),
76
 
                                  "bzr upgrade")
77
 
        finally:
78
 
            repository._deprecation_warning_done = True
79
 
 
 
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(True)
 
134
 
 
135
    def test_repository_deprecation_warning_suppressed_global(self):
 
136
        """Old formats give a warning"""
 
137
        conf = config.GlobalConfig()
 
138
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
139
        self.make_obsolete_repo('foo')
 
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_locations(self):
 
145
        """Old formats give a warning"""
 
146
        self.make_obsolete_repo('foo')
 
147
        conf = config.LocationConfig(osutils.pathjoin(self.test_dir, 'foo'))
 
148
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
149
        self.enable_deprecation_warning()
 
150
        out, err = self.run_bzr('status', working_dir='foo')
 
151
        self.check_warning(False)
 
152
 
 
153
    def test_repository_deprecation_warning_suppressed_branch(self):
 
154
        """Old formats give a warning"""
 
155
        tree = self.make_obsolete_repo('foo')
 
156
        conf = tree.branch.get_config()
 
157
        conf.set_user_option('suppress_warnings', 'format_deprecation')
 
158
        self.enable_deprecation_warning()
 
159
        out, err = self.run_bzr('status', working_dir='foo')
 
160
        self.check_warning(False)