1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2005-2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
28
28
from bzrlib import (
32
from bzrlib.tests import TestCaseInTempDir, TestCase
33
from bzrlib.tests import features, TestCaseInTempDir, TestCase
33
34
from bzrlib.trace import (
34
35
mutter, mutter_callsite, report_exception,
35
36
set_verbosity_level, get_verbosity_level, is_quiet, is_verbose, be_quiet,
82
83
msg = _format_exception()
83
84
self.assertEquals(msg,
84
"bzr: out of memory\n")
85
"bzr: out of memory\nUse -Dmem_dump to dump memory to a file.\n")
87
def test_format_mem_dump(self):
88
self.requireFeature(features.meliae)
89
debug.debug_flags.add('mem_dump')
94
msg = _format_exception()
95
self.assertStartsWith(msg,
96
"bzr: out of memory\nMemory dumped to ")
86
98
def test_format_os_error(self):
101
113
msg = _format_exception()
102
114
# Even though Windows and Linux differ for 'os.rmdir', they both give
103
115
# 'No such file' for open()
104
self.assertContainsRe(msg,
105
r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
116
# However it now gets translated so we can not test for a specific message
117
self.assertContainsRe(msg,
118
r'^bzr: ERROR: \[Errno .*\] .*nosuchfile')
120
def test_format_pywintypes_error(self):
121
self.requireFeature(features.pywintypes)
122
import pywintypes, win32file
124
win32file.RemoveDirectory('nosuchfile22222')
125
except pywintypes.error:
127
msg = _format_exception()
128
# GZ 2010-05-03: Formatting for pywintypes.error is basic, a 3-tuple
129
# with errno, function name, and locale error message
130
self.assertContainsRe(msg,
131
r"^bzr: ERROR: \(2, 'RemoveDirectory[AW]?', .*\)")
133
def test_format_sockets_error(self):
136
sock = socket.socket()
137
sock.send("This should fail.")
140
msg = _format_exception()
142
self.assertNotContainsRe(msg,
143
r"Traceback (most recent call last):")
107
145
def test_format_unicode_error(self):
277
315
self.overrideAttr(sys, 'stderr', StringIO())
278
316
# Set the log file to something that cannot exist
279
# FIXME: A bit dangerous: we are not in an isolated dir here -- vilajam
281
os.environ['BZR_LOG'] = os.getcwd() + '/no-dir/bzr.log'
317
self.overrideEnv('BZR_LOG', os.getcwd() + '/no-dir/bzr.log')
282
318
self.overrideAttr(trace, '_bzr_log_filename')
283
319
logf = trace._open_bzr_log()
284
320
self.assertIs(None, logf)
320
356
_rollover_trace_maybe(temp_log_name)
321
357
# should have been rolled over
322
358
self.assertFalse(os.access(temp_log_name, os.R_OK))
361
class TestTraceConfiguration(TestCaseInTempDir):
363
def test_default_config(self):
364
config = trace.DefaultConfig()
365
self.overrideAttr(trace, "_bzr_log_filename", None)
366
trace._bzr_log_filename = None
367
expected_filename = trace._get_bzr_log_filename()
368
self.assertEqual(None, trace._bzr_log_filename)
371
# Should have entered and setup a default filename.
372
self.assertEqual(expected_filename, trace._bzr_log_filename)
374
config.__exit__(None, None, None)
375
# Should have exited and cleaned up.
376
self.assertEqual(None, trace._bzr_log_filename)