1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
1 |
# Copyright (C) 2005, 2006 by Canonical Ltd
|
1185.33.9
by Martin Pool
Add new selftest module. |
2 |
#
|
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.
|
|
7 |
#
|
|
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.
|
|
12 |
#
|
|
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
|
|
16 |
||
17 |
# "weren't nothing promised to you. do i look like i got a promise face?"
|
|
18 |
||
19 |
"""Tests for trace library"""
|
|
20 |
||
1948.1.5
by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments |
21 |
from cStringIO import StringIO |
1740.5.7
by Martin Pool
Add test for formatting of EPIPE |
22 |
import errno |
1185.33.9
by Martin Pool
Add new selftest module. |
23 |
import os |
24 |
import sys |
|
25 |
||
1948.1.5
by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments |
26 |
from bzrlib import ( |
27 |
errors, |
|
28 |
)
|
|
1185.31.25
by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py |
29 |
from bzrlib.tests import TestCaseInTempDir, TestCase |
1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
30 |
from bzrlib.trace import mutter, report_exception |
1185.33.9
by Martin Pool
Add new selftest module. |
31 |
|
1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
32 |
|
33 |
def _format_exception(): |
|
34 |
"""Format an exception as it would normally be displayed to the user"""
|
|
35 |
buf = StringIO() |
|
36 |
report_exception(sys.exc_info(), buf) |
|
37 |
return buf.getvalue() |
|
38 |
||
39 |
||
1185.33.9
by Martin Pool
Add new selftest module. |
40 |
class TestTrace(TestCase): |
1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
41 |
|
1185.33.9
by Martin Pool
Add new selftest module. |
42 |
def test_format_sys_exception(self): |
43 |
try: |
|
44 |
raise NotImplementedError, "time travel" |
|
45 |
except NotImplementedError: |
|
46 |
pass
|
|
1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
47 |
err = _format_exception() |
48 |
self.assertEqualDiff(err.splitlines()[0], |
|
1740.5.3
by Martin Pool
Cleanup more exception-formatting code |
49 |
'bzr: ERROR: exceptions.NotImplementedError: time travel') |
1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
50 |
self.assertContainsRe(err, |
51 |
r'File.*test_trace.py') |
|
1185.33.9
by Martin Pool
Add new selftest module. |
52 |
|
1740.5.3
by Martin Pool
Cleanup more exception-formatting code |
53 |
def test_format_interrupt_exception(self): |
54 |
try: |
|
55 |
raise KeyboardInterrupt() |
|
1740.5.5
by Martin Pool
Show short form for OSError and IOError too |
56 |
except KeyboardInterrupt: |
1740.5.3
by Martin Pool
Cleanup more exception-formatting code |
57 |
# XXX: Some risk that a *real* keyboard interrupt won't be seen
|
58 |
pass
|
|
59 |
msg = _format_exception() |
|
60 |
self.assertTrue(len(msg) > 0) |
|
61 |
self.assertEqualDiff(msg, 'bzr: interrupted\n') |
|
62 |
||
1740.5.5
by Martin Pool
Show short form for OSError and IOError too |
63 |
def test_format_os_error(self): |
64 |
try: |
|
65 |
file('nosuchfile22222') |
|
66 |
except (OSError, IOError): |
|
67 |
pass
|
|
68 |
msg = _format_exception() |
|
69 |
self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile') |
|
70 |
||
1948.1.5
by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments |
71 |
def test_format_unicode_error(self): |
72 |
try: |
|
73 |
raise errors.BzrCommandError(u'argument foo\xb5 does not exist') |
|
74 |
except errors.BzrCommandError: |
|
75 |
pass
|
|
76 |
msg = _format_exception() |
|
1740.5.5
by Martin Pool
Show short form for OSError and IOError too |
77 |
|
1185.33.9
by Martin Pool
Add new selftest module. |
78 |
def test_format_exception(self): |
1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
79 |
"""Short formatting of bzr exceptions"""
|
1185.33.9
by Martin Pool
Add new selftest module. |
80 |
try: |
1948.1.5
by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments |
81 |
raise errors.NotBranchError, 'wibble' |
82 |
except errors.NotBranchError: |
|
1185.33.9
by Martin Pool
Add new selftest module. |
83 |
pass
|
1740.5.2
by Martin Pool
Improved tests for display of exceptions. |
84 |
msg = _format_exception() |
85 |
self.assertTrue(len(msg) > 0) |
|
86 |
self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n') |
|
1185.33.63
by Martin Pool
Better display of BzrError classes that are not BzrNewErrors. |
87 |
|
1185.33.51
by Martin Pool
Fix trace of non-ascii messages, and add test. |
88 |
def test_trace_unicode(self): |
89 |
"""Write Unicode to trace log"""
|
|
1948.1.2
by John Arbash Meinel
Fix the test_trace functions to actually test that things are written to the 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',) |
|
93 |
||
94 |
def test_trace_argument_unicode(self): |
|
95 |
"""Write a Unicode argument to the trace log"""
|
|
96 |
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') |
|
1185.85.5
by John Arbash Meinel
mutter() should not fail because of unicode errors |
99 |
|
1948.1.3
by John Arbash Meinel
Fix mutter() so even if args are invalid, it still works |
100 |
def test_trace_argument_utf8(self): |
101 |
"""Write a Unicode argument to the trace log"""
|
|
102 |
mutter(u'the unicode character for benzene is %s', |
|
103 |
u'\N{BENZENE RING}'.encode('utf-8')) |
|
104 |
self._log_file.flush() |
|
105 |
self.assertContainsRe(self._get_log(), 'the unicode character') |
|
106 |
||
1740.5.7
by Martin Pool
Add test for formatting of EPIPE |
107 |
def test_report_broken_pipe(self): |
108 |
try: |
|
109 |
raise IOError(errno.EPIPE, 'broken pipe foofofo') |
|
110 |
except IOError, e: |
|
111 |
msg = _format_exception() |
|
112 |
self.assertEquals(msg, "bzr: broken pipe\n") |
|
113 |
else: |
|
114 |
self.fail("expected error not raised") |
|
1740.5.9
by Martin Pool
[merge] bzr.dev |
115 |
|
1185.85.5
by John Arbash Meinel
mutter() should not fail because of unicode errors |
116 |
def test_mutter_never_fails(self): |
117 |
# Even if the decode/encode stage fails, mutter should not
|
|
118 |
# raise an exception
|
|
119 |
mutter(u'Writing a greek mu (\xb5) works in a unicode string') |
|
120 |
mutter('But fails in an ascii string \xb5') |
|
1948.1.4
by John Arbash Meinel
Update test_never_fails, to cover one of the failure points |
121 |
mutter('and in an ascii argument: %s', '\xb5') |
1185.85.5
by John Arbash Meinel
mutter() should not fail because of unicode errors |
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
|
|
1948.1.4
by John Arbash Meinel
Update test_never_fails, to cover one of the failure points |
125 |
self._log_file.flush() |
1185.85.5
by John Arbash Meinel
mutter() should not fail because of unicode errors |
126 |
log = self._get_log() |
127 |
self.assertContainsRe(log, 'Writing a greek mu') |
|
1948.1.9
by John Arbash Meinel
Change mutter() so that it doesn't try so hard to write out perfect utf8, instead, rather than using a utf8 file, it changes unicode to utf8 manually |
128 |
self.assertContainsRe(log, "But fails in an ascii string") |
129 |
self.assertContainsRe(log, u"ascii argument: \xb5") |