~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_trace.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    errors,
30
30
    trace,
31
31
    )
32
 
from bzrlib.tests import features, TestCaseInTempDir, TestCase
 
32
from bzrlib.tests import TestCaseInTempDir, TestCase
33
33
from bzrlib.trace import (
34
34
    mutter, mutter_callsite, report_exception,
35
35
    set_verbosity_level, get_verbosity_level, is_quiet, is_verbose, be_quiet,
36
36
    pop_log_file,
37
37
    push_log_file,
38
38
    _rollover_trace_maybe,
39
 
    show_error,
40
39
    )
41
40
 
42
41
 
104
103
        self.assertContainsRe(msg,
105
104
            r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
106
105
 
107
 
    def test_format_pywintypes_error(self):
108
 
        self.requireFeature(features.pywintypes)
109
 
        import pywintypes, win32file
110
 
        try:
111
 
            win32file.RemoveDirectory('nosuchfile22222')
112
 
        except pywintypes.error:
113
 
            pass
114
 
        msg = _format_exception()
115
 
        # GZ 2010-05-03: Formatting for pywintypes.error is basic, a 3-tuple
116
 
        #                with errno, function name, and locale error message
117
 
        self.assertContainsRe(msg,
118
 
            r"^bzr: ERROR: \(2, 'RemoveDirectory[AW]?', .*\)")
119
 
 
120
106
    def test_format_unicode_error(self):
121
107
        try:
122
108
            raise errors.BzrCommandError(u'argument foo\xb5 does not exist')
229
215
        # have to do a replaceent here as well.
230
216
        self.assertContainsRe(log, "ascii argument: \xb5".decode('utf8',
231
217
            'replace'))
232
 
        
233
 
    def test_show_error(self):
234
 
        show_error('error1')
235
 
        show_error(u'error2 \xb5 blah')
236
 
        show_error('arg: %s', 'blah')
237
 
        show_error('arg2: %(key)s', {'key':'stuff'})
238
 
        try:
239
 
            raise Exception("oops")
240
 
        except:
241
 
            show_error('kwarg', exc_info=True)
242
 
        log = self.get_log()
243
 
        self.assertContainsRe(log, 'error1')
244
 
        self.assertContainsRe(log, u'error2 \xb5 blah')
245
 
        self.assertContainsRe(log, 'arg: blah')
246
 
        self.assertContainsRe(log, 'arg2: stuff')
247
 
        self.assertContainsRe(log, 'kwarg')
248
 
        self.assertContainsRe(log, 'Traceback \\(most recent call last\\):')
249
 
        self.assertContainsRe(log, 'File ".*test_trace.py", line .*, in test_show_error')
250
 
        self.assertContainsRe(log, 'raise Exception\\("oops"\\)')
251
 
        self.assertContainsRe(log, 'Exception: oops')
252
218
 
253
219
    def test_push_log_file(self):
254
220
        """Can push and pop log file, and this catches mutter messages.
287
253
        # If _open_bzr_log cannot open the file, then we should write the
288
254
        # warning to stderr. Since this is normally happening before logging is
289
255
        # set up.
290
 
        self.overrideAttr(sys, 'stderr', StringIO())
 
256
        self.addCleanup(setattr, sys, 'stderr', sys.stderr)
 
257
        self.addCleanup(setattr, trace, '_bzr_log_filename',
 
258
                        trace._bzr_log_filename)
 
259
        sys.stderr = StringIO()
291
260
        # Set the log file to something that cannot exist
292
 
        # FIXME: A bit dangerous: we are not in an isolated dir here -- vilajam
293
 
        # 20100125
294
261
        os.environ['BZR_LOG'] = os.getcwd() + '/no-dir/bzr.log'
295
 
        self.overrideAttr(trace, '_bzr_log_filename')
296
262
        logf = trace._open_bzr_log()
297
263
        self.assertIs(None, logf)
298
264
        self.assertContainsRe(sys.stderr.getvalue(),
299
265
                              'failed to open trace file: .*/no-dir/bzr.log')
300
266
 
301
 
 
302
267
class TestVerbosityLevel(TestCase):
303
268
 
304
269
    def test_verbosity_level(self):
333
298
        _rollover_trace_maybe(temp_log_name)
334
299
        # should have been rolled over
335
300
        self.assertFalse(os.access(temp_log_name, os.R_OK))
336
 
 
337
 
 
338
 
class TestTraceConfiguration(TestCaseInTempDir):
339
 
 
340
 
    def test_default_config(self):
341
 
        config = trace.DefaultConfig()
342
 
        self.overrideAttr(trace, "_bzr_log_filename", None)
343
 
        trace._bzr_log_filename = None
344
 
        expected_filename = trace._get_bzr_log_filename()
345
 
        self.assertEqual(None, trace._bzr_log_filename)
346
 
        config.__enter__()
347
 
        try:
348
 
            # Should have entered and setup a default filename.
349
 
            self.assertEqual(expected_filename, trace._bzr_log_filename)
350
 
        finally:
351
 
            config.__exit__(None, None, None)
352
 
            # Should have exited and cleaned up.
353
 
            self.assertEqual(None, trace._bzr_log_filename)