1
# Copyright (C) 2005, 2006 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# "weren't nothing promised to you. do i look like i got a promise face?"
19
"""Tests for trace library"""
24
from StringIO import StringIO
26
from bzrlib.tests import TestCaseInTempDir, TestCase
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)
38
class TestTrace(TestCase):
40
def test_format_sys_exception(self):
42
raise NotImplementedError, "time travel"
43
except NotImplementedError:
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')
70
def test_format_exception(self):
71
"""Short formatting of bzr exceptions"""
73
raise NotBranchError, 'wibble'
74
except NotBranchError:
76
msg = _format_exception()
77
self.assertTrue(len(msg) > 0)
78
self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
80
def test_trace_unicode(self):
81
"""Write Unicode to trace log"""
82
self.log(u'the unicode character for benzene is \N{BENZENE RING}')
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")
95
def test_mutter_never_fails(self):
96
# Even if the decode/encode stage fails, mutter should not
98
mutter(u'Writing a greek mu (\xb5) works in a unicode string')
99
mutter('But fails in an ascii string \xb5')
100
# TODO: jam 20051227 mutter() doesn't flush the log file, and
101
# self._get_log() opens the file directly and reads it.
102
# So we need to manually flush the log file
104
bzrlib.trace._trace_file.flush()
105
log = self._get_log()
106
self.assertContainsRe(log, 'Writing a greek mu')
107
self.assertContainsRe(log, 'UnicodeError')
108
self.assertContainsRe(log, "'But fails in an ascii string")