1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# "weren't nothing promised to you. do i look like i got a promise face?"
21
"""Tests for trace library"""
25
from StringIO import StringIO
27
from bzrlib.tests import TestCaseInTempDir, TestCase
28
from bzrlib.trace import mutter, report_exception
29
from bzrlib.errors import NotBranchError
32
def _format_exception():
33
"""Format an exception as it would normally be displayed to the user"""
35
report_exception(sys.exc_info(), buf)
39
class TestTrace(TestCase):
41
def test_format_sys_exception(self):
43
raise NotImplementedError, "time travel"
44
except NotImplementedError:
46
err = _format_exception()
47
self.assertEqualDiff(err.splitlines()[0],
48
'bzr: ERROR: exceptions.NotImplementedError: time travel')
49
self.assertContainsRe(err,
50
r'File.*test_trace.py')
52
def test_format_interrupt_exception(self):
54
raise KeyboardInterrupt()
55
except KeyboardInterrupt:
56
# XXX: Some risk that a *real* keyboard interrupt won't be seen
58
msg = _format_exception()
59
self.assertTrue(len(msg) > 0)
60
self.assertEqualDiff(msg, 'bzr: interrupted\n')
62
def test_format_os_error(self):
64
file('nosuchfile22222')
65
except (OSError, IOError):
67
msg = _format_exception()
68
self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
71
def test_format_exception(self):
72
"""Short formatting of bzr exceptions"""
74
raise NotBranchError, 'wibble'
75
except NotBranchError:
77
msg = _format_exception()
78
self.assertTrue(len(msg) > 0)
79
self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
81
def test_trace_unicode(self):
82
"""Write Unicode to trace log"""
83
self.log(u'the unicode character for benzene is \N{BENZENE RING}')
84
self.assertContainsRe('the unicode character',