21
21
from cStringIO import StringIO
28
29
from bzrlib import (
32
from bzrlib.tests import TestCaseInTempDir, TestCase
34
from bzrlib.tests import features, TestCaseInTempDir, TestCase
33
35
from bzrlib.trace import (
34
36
mutter, mutter_callsite, report_exception,
35
37
set_verbosity_level, get_verbosity_level, is_quiet, is_verbose, be_quiet,
38
40
_rollover_trace_maybe,
79
82
except MemoryError:
81
84
msg = _format_exception()
82
self.assertEquals(msg,
83
"bzr: out of memory\n")
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 ")
85
99
def test_format_os_error(self):
100
114
msg = _format_exception()
101
115
# Even though Windows and Linux differ for 'os.rmdir', they both give
102
116
# 'No such file' for open()
103
self.assertContainsRe(msg,
104
r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
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):")
106
146
def test_format_unicode_error(self):
216
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')
219
279
def test_push_log_file(self):
220
280
"""Can push and pop log file, and this catches mutter messages.
256
316
self.overrideAttr(sys, 'stderr', StringIO())
257
317
# Set the log file to something that cannot exist
258
# FIXME: A bit dangerous: we are not in an isolated dir here -- vilajam
260
os.environ['BZR_LOG'] = os.getcwd() + '/no-dir/bzr.log'
318
self.overrideEnv('BZR_LOG', os.getcwd() + '/no-dir/bzr.log')
261
319
self.overrideAttr(trace, '_bzr_log_filename')
262
320
logf = trace._open_bzr_log()
263
321
self.assertIs(None, logf)
289
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")
292
416
class TestBzrLog(TestCaseInTempDir):
294
418
def test_log_rollover(self):
299
423
_rollover_trace_maybe(temp_log_name)
300
424
# should have been rolled over
301
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)