20
19
"""Tests for trace library"""
24
from StringIO import StringIO
25
26
from bzrlib.tests import TestCaseInTempDir, TestCase
26
from bzrlib.trace import format_exception_short, mutter
27
from bzrlib.errors import NotBranchError, BzrError, BzrNewError
27
from bzrlib.trace import mutter, report_exception
28
from bzrlib.errors import NotBranchError
31
def _format_exception():
32
"""Format an exception as it would normally be displayed to the user"""
34
report_exception(sys.exc_info(), buf)
29
38
class TestTrace(TestCase):
30
40
def test_format_sys_exception(self):
31
"""Short formatting of exceptions"""
33
42
raise NotImplementedError, "time travel"
34
43
except NotImplementedError:
36
error_lines = format_exception_short(sys.exc_info()).splitlines()
37
self.assertEqualDiff(error_lines[0],
38
'exceptions.NotImplementedError: time travel')
39
self.assertContainsRe(error_lines[1],
40
r'^ at .*trace\.py line \d+$')
41
self.assertContainsRe(error_lines[2],
42
r'^ in test_format_sys_exception$')
45
err = _format_exception()
46
self.assertEqualDiff(err.splitlines()[0],
47
'bzr: ERROR: exceptions.NotImplementedError: time travel')
48
self.assertContainsRe(err,
49
r'File.*test_trace.py')
51
def test_format_interrupt_exception(self):
53
raise KeyboardInterrupt()
54
except KeyboardInterrupt:
55
# XXX: Some risk that a *real* keyboard interrupt won't be seen
57
msg = _format_exception()
58
self.assertTrue(len(msg) > 0)
59
self.assertEqualDiff(msg, 'bzr: interrupted\n')
61
def test_format_os_error(self):
63
file('nosuchfile22222')
64
except (OSError, IOError):
66
msg = _format_exception()
67
self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
44
70
def test_format_exception(self):
45
"""Short formatting of exceptions"""
71
"""Short formatting of bzr exceptions"""
47
73
raise NotBranchError, 'wibble'
48
74
except NotBranchError:
50
msg = format_exception_short(sys.exc_info())
51
self.assertEqualDiff(msg, 'Not a branch: wibble')
53
def test_format_old_exception(self):
54
# format a class that doesn't descend from BzrNewError;
55
# remove this test when everything is unified there
56
self.assertFalse(issubclass(BzrError, BzrNewError))
58
raise BzrError('some old error')
61
msg = format_exception_short(sys.exc_info())
62
self.assertEqualDiff(msg, 'some old error')
76
msg = _format_exception()
77
self.assertTrue(len(msg) > 0)
78
self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
64
80
def test_trace_unicode(self):
65
81
"""Write Unicode to trace log"""
66
82
self.log(u'the unicode character for benzene is \N{BENZENE RING}')
67
83
self.assertContainsRe('the unicode character',
86
def test_report_broken_pipe(self):
88
raise IOError(errno.EPIPE, 'broken pipe foofofo')
90
msg = _format_exception()
91
self.assertEquals(msg, "bzr: broken pipe\n")
93
self.fail("expected error not raised")
95
def test_mutter_never_fails(self):
96
# Even if the decode/encode stage fails, mutter should not
98
mutter(u'Writing a greek mu (\xb5) works in a unicode string')
99
mutter('But fails in an ascii string \xb5')
100
# TODO: jam 20051227 mutter() doesn't flush the log file, and
101
# self._get_log() opens the file directly and reads it.
102
# So we need to manually flush the log file
104
bzrlib.trace._trace_file.flush()
105
log = self._get_log()
106
self.assertContainsRe(log, 'Writing a greek mu')
107
self.assertContainsRe(log, 'UnicodeError')
108
self.assertContainsRe(log, "'But fails in an ascii string")