~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_trace.py

NEWS section template into a separate file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008, 2009 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
# "weren't nothing promised to you.  do i look like i got a promise face?"
18
18
 
27
27
 
28
28
from bzrlib import (
29
29
    errors,
 
30
    trace,
30
31
    )
31
32
from bzrlib.tests import TestCaseInTempDir, TestCase
32
33
from bzrlib.trace import (
48
49
class TestTrace(TestCase):
49
50
 
50
51
    def test_format_sys_exception(self):
 
52
        # Test handling of an internal/unexpected error that probably
 
53
        # indicates a bug in bzr.  The details of the message may vary
 
54
        # depending on whether apport is available or not.  See test_crash for
 
55
        # more.
51
56
        try:
52
57
            raise NotImplementedError, "time travel"
53
58
        except NotImplementedError:
56
61
        self.assertEqualDiff(err.splitlines()[0],
57
62
                'bzr: ERROR: exceptions.NotImplementedError: time travel')
58
63
        self.assertContainsRe(err,
59
 
                r'File.*test_trace.py')
 
64
            'Bazaar has encountered an internal error.')
60
65
 
61
66
    def test_format_interrupt_exception(self):
62
67
        try:
68
73
        self.assertTrue(len(msg) > 0)
69
74
        self.assertEqualDiff(msg, 'bzr: interrupted\n')
70
75
 
 
76
    def test_format_memory_error(self):
 
77
        try:
 
78
            raise MemoryError()
 
79
        except MemoryError:
 
80
            pass
 
81
        msg = _format_exception()
 
82
        self.assertEquals(msg,
 
83
            "bzr: out of memory\n")
 
84
 
71
85
    def test_format_os_error(self):
72
86
        try:
 
87
            os.rmdir('nosuchfile22222')
 
88
        except OSError:
 
89
            pass
 
90
        msg = _format_exception()
 
91
        self.assertContainsRe(msg,
 
92
            r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile22222')
 
93
 
 
94
    def test_format_io_error(self):
 
95
        try:
73
96
            file('nosuchfile22222')
74
 
        except (OSError, IOError):
 
97
        except IOError:
75
98
            pass
76
99
        msg = _format_exception()
77
100
        self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
113
136
            pass
114
137
        msg = _format_exception()
115
138
        self.assertContainsRe(msg,
116
 
            r"Traceback \(most recent call last\)")
 
139
            r'Bazaar has encountered an internal error')
117
140
 
118
141
    def test_trace_unicode(self):
119
142
        """Write Unicode to trace log"""
120
143
        self.log(u'the unicode character for benzene is \N{BENZENE RING}')
121
144
        self.assertContainsRe(self._get_log(keep_log_file=True),
122
145
                              "the unicode character for benzene is")
123
 
    
 
146
 
124
147
    def test_trace_argument_unicode(self):
125
148
        """Write a Unicode argument to the trace log"""
126
149
        mutter(u'the unicode character for benzene is %s', u'\N{BENZENE RING}')
186
209
    def test_push_log_file(self):
187
210
        """Can push and pop log file, and this catches mutter messages.
188
211
 
189
 
        This is primarily for use in the test framework. 
 
212
        This is primarily for use in the test framework.
190
213
        """
191
214
        tmp1 = tempfile.NamedTemporaryFile()
192
215
        tmp2 = tempfile.NamedTemporaryFile()
216
239
            tmp1.close()
217
240
            tmp2.close()
218
241
 
 
242
    def test__open_bzr_log_uses_stderr_for_failures(self):
 
243
        # If _open_bzr_log cannot open the file, then we should write the
 
244
        # warning to stderr. Since this is normally happening before logging is
 
245
        # set up.
 
246
        self.addCleanup(setattr, sys, 'stderr', sys.stderr)
 
247
        self.addCleanup(setattr, trace, '_bzr_log_filename',
 
248
                        trace._bzr_log_filename)
 
249
        sys.stderr = StringIO()
 
250
        # Set the log file to something that cannot exist
 
251
        os.environ['BZR_LOG'] = os.getcwd() + '/no-dir/bzr.log'
 
252
        logf = trace._open_bzr_log()
 
253
        self.assertIs(None, logf)
 
254
        self.assertContainsRe(sys.stderr.getvalue(),
 
255
                              'failed to open trace file: .*/no-dir/bzr.log')
219
256
 
220
257
class TestVerbosityLevel(TestCase):
221
258