~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_trace.py

(gz) Making logging non-ascii strings more robust with new
 EncodedStreamHandler class (Martin Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
from cStringIO import StringIO
22
22
import errno
 
23
import logging
23
24
import os
24
25
import re
25
26
import sys
345
346
        self.assertEqual(0, get_verbosity_level())
346
347
 
347
348
 
 
349
class TestLogging(TestCase):
 
350
    """Check logging functionality robustly records information"""
 
351
 
 
352
    def test_note(self):
 
353
        trace.note("Noted")
 
354
        self.assertEqual("    INFO  Noted\n", self.get_log())
 
355
 
 
356
    def test_warning(self):
 
357
        trace.warning("Warned")
 
358
        self.assertEqual(" WARNING  Warned\n", self.get_log())
 
359
 
 
360
    def test_log(self):
 
361
        logging.getLogger("bzr").error("Errored")
 
362
        self.assertEqual("   ERROR  Errored\n", self.get_log())
 
363
 
 
364
    def test_log_sub(self):
 
365
        logging.getLogger("bzr.test_log_sub").debug("Whispered")
 
366
        self.assertEqual("   DEBUG  Whispered\n", self.get_log())
 
367
 
 
368
    def test_log_unicode_msg(self):
 
369
        logging.getLogger("bzr").debug(u"\xa7")
 
370
        self.assertEqual(u"   DEBUG  \xa7\n", self.get_log())
 
371
 
 
372
    def test_log_unicode_arg(self):
 
373
        logging.getLogger("bzr").debug("%s", u"\xa7")
 
374
        self.assertEqual(u"   DEBUG  \xa7\n", self.get_log())
 
375
 
 
376
    def test_log_utf8_msg(self):
 
377
        logging.getLogger("bzr").debug("\xc2\xa7")
 
378
        self.assertEqual(u"   DEBUG  \xa7\n", self.get_log())
 
379
 
 
380
    def test_log_utf8_arg(self):
 
381
        logging.getLogger("bzr").debug("%s", "\xc2\xa7")
 
382
        self.assertEqual(u"   DEBUG  \xa7\n", self.get_log())
 
383
 
 
384
    def test_log_bytes_msg(self):
 
385
        logging.getLogger("bzr").debug("\xa7")
 
386
        log = self.get_log()
 
387
        self.assertContainsString(log, "UnicodeDecodeError: ")
 
388
        self.assertContainsString(log,
 
389
            "Logging record unformattable: '\\xa7' % ()\n")
 
390
 
 
391
    def test_log_bytes_arg(self):
 
392
        logging.getLogger("bzr").debug("%s", "\xa7")
 
393
        log = self.get_log()
 
394
        self.assertContainsString(log, "UnicodeDecodeError: ")
 
395
        self.assertContainsString(log,
 
396
            "Logging record unformattable: '%s' % ('\\xa7',)\n")
 
397
 
 
398
    def test_log_mixed_strings(self):
 
399
        logging.getLogger("bzr").debug(u"%s", "\xa7")
 
400
        log = self.get_log()
 
401
        self.assertContainsString(log, "UnicodeDecodeError: ")
 
402
        self.assertContainsString(log,
 
403
            "Logging record unformattable: u'%s' % ('\\xa7',)\n")
 
404
 
 
405
    def test_log_repr_broken(self):
 
406
        class BadRepr(object):
 
407
            def __repr__(self):
 
408
                raise ValueError("Broken object")
 
409
        logging.getLogger("bzr").debug("%s", BadRepr())
 
410
        log = self.get_log()
 
411
        self.assertContainsRe(log, "ValueError: Broken object\n")
 
412
        self.assertContainsRe(log, "Logging record unformattable: '%s' % .*\n")
 
413
 
 
414
 
348
415
class TestBzrLog(TestCaseInTempDir):
349
416
 
350
417
    def test_log_rollover(self):