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')