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"""
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")
70
95
def test_mutter_never_fails(self):
71
96
# Even if the decode/encode stage fails, mutter should not
72
97
# raise an exception