14
13
# You should have received a copy of the GNU General Public License
15
14
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
17
# "weren't nothing promised to you. do i look like i got a promise face?"
20
19
"""Tests for trace library"""
21
from cStringIO import StringIO
25
from bzrlib.tests import TestCaseInTempDir, TestCase
26
from bzrlib.trace import format_exception_short, mutter
27
from bzrlib.errors import NotBranchError, BzrError, BzrNewError
34
from bzrlib.tests import features, TestCaseInTempDir, TestCase
35
from bzrlib.trace import (
36
mutter, mutter_callsite, report_exception,
37
set_verbosity_level, get_verbosity_level, is_quiet, is_verbose, be_quiet,
40
_rollover_trace_maybe,
45
def _format_exception():
46
"""Format an exception as it would normally be displayed to the user"""
48
report_exception(sys.exc_info(), buf)
29
52
class TestTrace(TestCase):
30
54
def test_format_sys_exception(self):
31
"""Short formatting of exceptions"""
55
# Test handling of an internal/unexpected error that probably
56
# indicates a bug in bzr. The details of the message may vary
57
# depending on whether apport is available or not. See test_crash for
33
60
raise NotImplementedError, "time travel"
34
61
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$')
63
err = _format_exception()
64
self.assertEqualDiff(err.splitlines()[0],
65
'bzr: ERROR: exceptions.NotImplementedError: time travel')
66
self.assertContainsRe(err,
67
'Bazaar has encountered an internal error.')
69
def test_format_interrupt_exception(self):
71
raise KeyboardInterrupt()
72
except KeyboardInterrupt:
73
# XXX: Some risk that a *real* keyboard interrupt won't be seen
75
msg = _format_exception()
76
self.assertTrue(len(msg) > 0)
77
self.assertEqualDiff(msg, 'bzr: interrupted\n')
79
def test_format_memory_error(self):
84
msg = _format_exception()
85
self.assertEquals(msg,
86
"bzr: out of memory\nUse -Dmem_dump to dump memory to a file.\n")
88
def test_format_mem_dump(self):
89
self.requireFeature(features.meliae)
90
debug.debug_flags.add('mem_dump')
95
msg = _format_exception()
96
self.assertStartsWith(msg,
97
"bzr: out of memory\nMemory dumped to ")
99
def test_format_os_error(self):
101
os.rmdir('nosuchfile22222')
104
msg = _format_exception()
105
# Linux seems to give "No such file" but Windows gives "The system
106
# cannot find the file specified".
107
self.assertEqual('bzr: ERROR: %s\n' % (e_str,), msg)
109
def test_format_io_error(self):
111
file('nosuchfile22222')
114
msg = _format_exception()
115
# Even though Windows and Linux differ for 'os.rmdir', they both give
116
# 'No such file' for open()
117
# However it now gets translated so we can not test for a specific message
118
self.assertContainsRe(msg,
119
r'^bzr: ERROR: \[Errno .*\] .*nosuchfile')
121
def test_format_pywintypes_error(self):
122
self.requireFeature(features.pywintypes)
123
import pywintypes, win32file
125
win32file.RemoveDirectory('nosuchfile22222')
126
except pywintypes.error:
128
msg = _format_exception()
129
# GZ 2010-05-03: Formatting for pywintypes.error is basic, a 3-tuple
130
# with errno, function name, and locale error message
131
self.assertContainsRe(msg,
132
r"^bzr: ERROR: \(2, 'RemoveDirectory[AW]?', .*\)")
134
def test_format_sockets_error(self):
137
sock = socket.socket()
138
sock.send("This should fail.")
141
msg = _format_exception()
143
self.assertNotContainsRe(msg,
144
r"Traceback (most recent call last):")
146
def test_format_unicode_error(self):
148
raise errors.BzrCommandError(u'argument foo\xb5 does not exist')
149
except errors.BzrCommandError:
151
msg = _format_exception()
44
153
def test_format_exception(self):
45
"""Short formatting of exceptions"""
47
raise NotBranchError, 'wibble'
48
except NotBranchError:
50
msg = format_exception_short(sys.exc_info())
51
self.assertEqualDiff(msg, 'Not a branch: wibble')
53
def test_format_old_exception(self):
54
# format a class that doesn't descend from BzrNewError;
55
# remove this test when everything is unified there
56
self.assertFalse(issubclass(BzrError, BzrNewError))
58
raise BzrError('some old error')
61
msg = format_exception_short(sys.exc_info())
62
self.assertEqualDiff(msg, 'some old error')
154
"""Short formatting of bzr exceptions"""
156
raise errors.NotBranchError('wibble')
157
except errors.NotBranchError:
159
msg = _format_exception()
160
self.assertTrue(len(msg) > 0)
161
self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: \"wibble\".\n')
163
def test_report_external_import_error(self):
164
"""Short friendly message for missing system modules."""
166
import ImaginaryModule
167
except ImportError, e:
170
self.fail("somehow succeeded in importing %r" % ImaginaryModule)
171
msg = _format_exception()
172
self.assertEqual(msg,
173
'bzr: ERROR: No module named ImaginaryModule\n'
174
'You may need to install this Python library separately.\n')
176
def test_report_import_syntax_error(self):
178
raise ImportError("syntax error")
179
except ImportError, e:
181
msg = _format_exception()
182
self.assertContainsRe(msg,
183
r'Bazaar has encountered an internal error')
64
185
def test_trace_unicode(self):
65
186
"""Write Unicode to trace log"""
66
187
self.log(u'the unicode character for benzene is \N{BENZENE RING}')
67
self.assertContainsRe('the unicode character',
189
self.assertContainsRe(log, "the unicode character for benzene is")
191
def test_trace_argument_unicode(self):
192
"""Write a Unicode argument to the trace log"""
193
mutter(u'the unicode character for benzene is %s', u'\N{BENZENE RING}')
195
self.assertContainsRe(log, 'the unicode character')
197
def test_trace_argument_utf8(self):
198
"""Write a Unicode argument to the trace log"""
199
mutter(u'the unicode character for benzene is %s',
200
u'\N{BENZENE RING}'.encode('utf-8'))
202
self.assertContainsRe(log, 'the unicode character')
204
def test_report_broken_pipe(self):
206
raise IOError(errno.EPIPE, 'broken pipe foofofo')
208
msg = _format_exception()
209
self.assertEquals(msg, "bzr: broken pipe\n")
211
self.fail("expected error not raised")
213
def assertLogStartsWith(self, log, string):
214
"""Like assertStartsWith, but skips the log timestamp."""
215
self.assertContainsRe(log,
216
'^\\d+\\.\\d+ ' + re.escape(string))
218
def test_mutter_callsite_1(self):
219
"""mutter_callsite can capture 1 level of stack frame."""
220
mutter_callsite(1, "foo %s", "a string")
222
# begin with the message
223
self.assertLogStartsWith(log, 'foo a string\nCalled from:\n')
224
# should show two frame: this frame and the one above
225
self.assertContainsRe(log,
226
'test_trace\\.py", line \\d+, in test_mutter_callsite_1\n')
227
# this frame should be the final one
228
self.assertEndsWith(log, ' "a string")\n')
230
def test_mutter_callsite_2(self):
231
"""mutter_callsite can capture 2 levels of stack frame."""
232
mutter_callsite(2, "foo %s", "a string")
234
# begin with the message
235
self.assertLogStartsWith(log, 'foo a string\nCalled from:\n')
236
# should show two frame: this frame and the one above
237
self.assertContainsRe(log,
238
'test_trace.py", line \d+, in test_mutter_callsite_2\n')
239
# this frame should be the final one
240
self.assertEndsWith(log, ' "a string")\n')
242
def test_mutter_never_fails(self):
243
# Even if the decode/encode stage fails, mutter should not
245
# This test checks that mutter doesn't fail; the current behaviour
246
# is that it doesn't fail *and writes non-utf8*.
247
mutter(u'Writing a greek mu (\xb5) works in a unicode string')
248
mutter('But fails in an ascii string \xb5')
249
mutter('and in an ascii argument: %s', '\xb5')
251
self.assertContainsRe(log, 'Writing a greek mu')
252
self.assertContainsRe(log, "But fails in an ascii string")
253
# However, the log content object does unicode replacement on reading
254
# to let it get unicode back where good data has been written. So we
255
# have to do a replaceent here as well.
256
self.assertContainsRe(log, "ascii argument: \xb5".decode('utf8',
259
def test_show_error(self):
261
show_error(u'error2 \xb5 blah')
262
show_error('arg: %s', 'blah')
263
show_error('arg2: %(key)s', {'key':'stuff'})
265
raise Exception("oops")
267
show_error('kwarg', exc_info=True)
269
self.assertContainsRe(log, 'error1')
270
self.assertContainsRe(log, u'error2 \xb5 blah')
271
self.assertContainsRe(log, 'arg: blah')
272
self.assertContainsRe(log, 'arg2: stuff')
273
self.assertContainsRe(log, 'kwarg')
274
self.assertContainsRe(log, 'Traceback \\(most recent call last\\):')
275
self.assertContainsRe(log, 'File ".*test_trace.py", line .*, in test_show_error')
276
self.assertContainsRe(log, 'raise Exception\\("oops"\\)')
277
self.assertContainsRe(log, 'Exception: oops')
279
def test_push_log_file(self):
280
"""Can push and pop log file, and this catches mutter messages.
282
This is primarily for use in the test framework.
284
tmp1 = tempfile.NamedTemporaryFile()
285
tmp2 = tempfile.NamedTemporaryFile()
287
memento1 = push_log_file(tmp1)
288
mutter("comment to file1")
290
memento2 = push_log_file(tmp2)
292
mutter("comment to file2")
294
pop_log_file(memento2)
295
mutter("again to file1")
297
pop_log_file(memento1)
298
# the files were opened in binary mode, so should have exactly
299
# these bytes. and removing the file as the log target should
300
# have caused them to be flushed out. need to match using regexps
301
# as there's a timestamp at the front.
303
self.assertContainsRe(tmp1.read(),
304
r"\d+\.\d+ comment to file1\n\d+\.\d+ again to file1\n")
306
self.assertContainsRe(tmp2.read(),
307
r"\d+\.\d+ comment to file2\n")
312
def test__open_bzr_log_uses_stderr_for_failures(self):
313
# If _open_bzr_log cannot open the file, then we should write the
314
# warning to stderr. Since this is normally happening before logging is
316
self.overrideAttr(sys, 'stderr', StringIO())
317
# Set the log file to something that cannot exist
318
self.overrideEnv('BZR_LOG', os.getcwd() + '/no-dir/bzr.log')
319
self.overrideAttr(trace, '_bzr_log_filename')
320
logf = trace._open_bzr_log()
321
self.assertIs(None, logf)
322
self.assertContainsRe(sys.stderr.getvalue(),
323
'failed to open trace file: .*/no-dir/bzr.log')
326
class TestVerbosityLevel(TestCase):
328
def test_verbosity_level(self):
329
set_verbosity_level(1)
330
self.assertEqual(1, get_verbosity_level())
331
self.assertTrue(is_verbose())
332
self.assertFalse(is_quiet())
333
set_verbosity_level(-1)
334
self.assertEqual(-1, get_verbosity_level())
335
self.assertFalse(is_verbose())
336
self.assertTrue(is_quiet())
337
set_verbosity_level(0)
338
self.assertEqual(0, get_verbosity_level())
339
self.assertFalse(is_verbose())
340
self.assertFalse(is_quiet())
342
def test_be_quiet(self):
343
# Confirm the old API still works
345
self.assertEqual(-1, get_verbosity_level())
347
self.assertEqual(0, get_verbosity_level())
350
class TestLogging(TestCase):
351
"""Check logging functionality robustly records information"""
355
self.assertEqual(" INFO Noted\n", self.get_log())
357
def test_warning(self):
358
trace.warning("Warned")
359
self.assertEqual(" WARNING Warned\n", self.get_log())
362
logging.getLogger("bzr").error("Errored")
363
self.assertEqual(" ERROR Errored\n", self.get_log())
365
def test_log_sub(self):
366
logging.getLogger("bzr.test_log_sub").debug("Whispered")
367
self.assertEqual(" DEBUG Whispered\n", self.get_log())
369
def test_log_unicode_msg(self):
370
logging.getLogger("bzr").debug(u"\xa7")
371
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
373
def test_log_unicode_arg(self):
374
logging.getLogger("bzr").debug("%s", u"\xa7")
375
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
377
def test_log_utf8_msg(self):
378
logging.getLogger("bzr").debug("\xc2\xa7")
379
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
381
def test_log_utf8_arg(self):
382
logging.getLogger("bzr").debug("%s", "\xc2\xa7")
383
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
385
def test_log_bytes_msg(self):
386
logging.getLogger("bzr").debug("\xa7")
388
self.assertContainsString(log, "UnicodeDecodeError: ")
389
self.assertContainsString(log,
390
"Logging record unformattable: '\\xa7' % ()\n")
392
def test_log_bytes_arg(self):
393
logging.getLogger("bzr").debug("%s", "\xa7")
395
self.assertContainsString(log, "UnicodeDecodeError: ")
396
self.assertContainsString(log,
397
"Logging record unformattable: '%s' % ('\\xa7',)\n")
399
def test_log_mixed_strings(self):
400
logging.getLogger("bzr").debug(u"%s", "\xa7")
402
self.assertContainsString(log, "UnicodeDecodeError: ")
403
self.assertContainsString(log,
404
"Logging record unformattable: u'%s' % ('\\xa7',)\n")
406
def test_log_repr_broken(self):
407
class BadRepr(object):
409
raise ValueError("Broken object")
410
logging.getLogger("bzr").debug("%s", BadRepr())
412
self.assertContainsRe(log, "ValueError: Broken object\n")
413
self.assertContainsRe(log, "Logging record unformattable: '%s' % .*\n")
416
class TestBzrLog(TestCaseInTempDir):
418
def test_log_rollover(self):
419
temp_log_name = 'test-log'
420
trace_file = open(temp_log_name, 'at')
421
trace_file.writelines(['test_log_rollover padding\n'] * 200000)
423
_rollover_trace_maybe(temp_log_name)
424
# should have been rolled over
425
self.assertFalse(os.access(temp_log_name, os.R_OK))
428
class TestTraceConfiguration(TestCaseInTempDir):
430
def test_default_config(self):
431
config = trace.DefaultConfig()
432
self.overrideAttr(trace, "_bzr_log_filename", None)
433
trace._bzr_log_filename = None
434
expected_filename = trace._get_bzr_log_filename()
435
self.assertEqual(None, trace._bzr_log_filename)
438
# Should have entered and setup a default filename.
439
self.assertEqual(expected_filename, trace._bzr_log_filename)
441
config.__exit__(None, None, None)
442
# Should have exited and cleaned up.
443
self.assertEqual(None, trace._bzr_log_filename)