26
26
from bzrlib import (
29
from bzrlib.tests import TestCaseInTempDir, TestCase
31
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
30
32
from bzrlib.trace import mutter, report_exception
33
35
def _format_exception():
34
36
"""Format an exception as it would normally be displayed to the user"""
36
report_exception(sys.exc_info(), buf)
38
report = report_exception(sys.exc_info(), buf)
39
return buf.getvalue(), report
40
42
class TestTrace(TestCase):
42
def test_format_sys_exception(self):
44
def test_format_sys_exception_no_apport(self):
44
46
raise NotImplementedError, "time travel"
45
47
except NotImplementedError:
47
err = _format_exception()
49
old_use_apport = trace._use_apport
50
trace._use_apport = False
52
err, report = _format_exception()
54
trace._use_apport = old_use_apport
55
self.assertEqual(None, report)
48
56
self.assertEqualDiff(err.splitlines()[0],
49
57
'bzr: ERROR: exceptions.NotImplementedError: time travel')
50
58
self.assertContainsRe(err,
51
59
r'File.*test_trace.py')
61
def test_format_sys_exception_apport(self):
65
raise TestSkipped('Apport not installed')
67
raise NotImplementedError, "time travel"
68
except NotImplementedError:
71
sys.argv = ['foo', 'bar', 'quux']
73
err, (report, report_filename) = _format_exception()
76
self.assertIsInstance(report, problem_report.ProblemReport)
77
# the error formatting is checked by the blackbox ui command.
78
# here we need to check that the file on disk - the problem report
79
# will contain the right information.
85
# check the report logical data.
86
self.assertEqual('foo bar quux', report['CommandLine'])
87
known_plugins = ' '.join(plugin.all_plugins())
88
self.assertEqual(known_plugins, report['BzrPlugins'])
89
self.assertContainsRe(report['Traceback'], r'Traceback')
90
# Stock apport facilities we just invoke, no need to test their
92
self.assertNotEqual(None, report['Package'])
93
self.assertNotEqual(None, report['Uname'])
94
# check the file 'looks' like a good file, because we dont
95
# want apport changes to break the user interface.
96
report_file = file(report_filename, 'r')
98
report_text = report_file.read()
101
# so we check this by looking across two fields and they should
102
# be just \n separated.
103
self.assertTrue('ProblemType: Crash\n'
104
'BzrPlugins: ' in report_text)
53
106
def test_format_interrupt_exception(self):
55
108
raise KeyboardInterrupt()
56
109
except KeyboardInterrupt:
57
110
# XXX: Some risk that a *real* keyboard interrupt won't be seen
111
# We can probably detect that by checking for the specific line
112
# that we raise from in the test being in the backtrace.
59
msg = _format_exception()
114
msg, report = _format_exception()
60
115
self.assertTrue(len(msg) > 0)
61
116
self.assertEqualDiff(msg, 'bzr: interrupted\n')
73
128
raise errors.BzrCommandError(u'argument foo\xb5 does not exist')
74
129
except errors.BzrCommandError:
76
msg = _format_exception()
131
msg, report = _format_exception()
78
133
def test_format_exception(self):
79
134
"""Short formatting of bzr exceptions"""
81
raise errors.NotBranchError, 'wibble'
136
raise errors.NotBranchError('wibble')
82
137
except errors.NotBranchError:
84
msg = _format_exception()
139
msg, report = _format_exception()
85
140
self.assertTrue(len(msg) > 0)
86
141
self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
88
143
def test_trace_unicode(self):
89
144
"""Write Unicode to trace log"""
90
mutter(u'the unicode character for benzene is \N{BENZENE RING}')
91
self._log_file.flush()
92
self.assertContainsRe(self._get_log(), 'the unicode character',)
145
self.log(u'the unicode character for benzene is \N{BENZENE RING}')
146
self.assertContainsRe(self._get_log(keep_log_file=True),
147
"the unicode character for benzene is")
94
149
def test_trace_argument_unicode(self):
95
150
"""Write a Unicode argument to the trace log"""
96
151
mutter(u'the unicode character for benzene is %s', u'\N{BENZENE RING}')
97
self._log_file.flush()
98
self.assertContainsRe(self._get_log(), 'the unicode character')
152
self.assertContainsRe(self._get_log(keep_log_file=True),
153
'the unicode character')
100
155
def test_trace_argument_utf8(self):
101
156
"""Write a Unicode argument to the trace log"""
102
157
mutter(u'the unicode character for benzene is %s',
103
158
u'\N{BENZENE RING}'.encode('utf-8'))
104
self._log_file.flush()
105
self.assertContainsRe(self._get_log(), 'the unicode character')
159
self.assertContainsRe(self._get_log(keep_log_file=True),
160
'the unicode character')
107
162
def test_report_broken_pipe(self):
109
164
raise IOError(errno.EPIPE, 'broken pipe foofofo')
110
165
except IOError, e:
111
msg = _format_exception()
166
msg, report = _format_exception()
112
167
self.assertEquals(msg, "bzr: broken pipe\n")
114
169
self.fail("expected error not raised")
119
174
mutter(u'Writing a greek mu (\xb5) works in a unicode string')
120
175
mutter('But fails in an ascii string \xb5')
121
176
mutter('and in an ascii argument: %s', '\xb5')
122
# TODO: jam 20051227 mutter() doesn't flush the log file, and
123
# self._get_log() opens the file directly and reads it.
124
# So we need to manually flush the log file
125
self._log_file.flush()
126
log = self._get_log()
177
log = self._get_log(keep_log_file=True)
127
178
self.assertContainsRe(log, 'Writing a greek mu')
128
179
self.assertContainsRe(log, "But fails in an ascii string")
129
180
self.assertContainsRe(log, u"ascii argument: \xb5")