1
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
5
# This program is free software; you can redistribute it and/or modify
5
6
# it under the terms of the GNU General Public License as published by
25
from StringIO import StringIO
25
27
from bzrlib.tests import TestCaseInTempDir, TestCase
26
from bzrlib.trace import format_exception_short, mutter
28
from bzrlib.trace import mutter, report_exception
27
29
from bzrlib.errors import NotBranchError, BzrError, BzrNewError
32
def _format_exception():
33
"""Format an exception as it would normally be displayed to the user"""
35
report_exception(sys.exc_info(), buf)
29
39
class TestTrace(TestCase):
30
41
def test_format_sys_exception(self):
31
"""Short formatting of exceptions"""
33
43
raise NotImplementedError, "time travel"
34
44
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$')
46
err = _format_exception()
47
self.assertEqualDiff(err.splitlines()[0],
48
'bzr: unhandled error: exceptions.NotImplementedError: time travel')
49
self.assertContainsRe(err,
50
r'File.*test_trace.py')
44
52
def test_format_exception(self):
45
"""Short formatting of exceptions"""
53
"""Short formatting of bzr exceptions"""
47
55
raise NotBranchError, 'wibble'
48
56
except NotBranchError:
50
msg = format_exception_short(sys.exc_info())
51
self.assertEqualDiff(msg, 'Not a branch: wibble')
58
msg = _format_exception()
59
self.assertTrue(len(msg) > 0)
60
self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
53
62
def test_format_old_exception(self):
54
63
# format a class that doesn't descend from BzrNewError;
58
67
raise BzrError('some old error')
61
msg = format_exception_short(sys.exc_info())
62
self.assertEqualDiff(msg, 'some old error')
70
msg = _format_exception()
71
self.assertEqualDiff(msg, 'bzr: ERROR: some old error\n')
64
73
def test_trace_unicode(self):
65
74
"""Write Unicode to trace log"""