1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
# -*- coding: utf-8 -*-
# Copyright 2010 Jaap Karssenberg <jaap.karssenberg@gmail.com>
from __future__ import with_statement
import tests
import logging
import zim.errors
from zim.gui.widgets import ErrorDialog
class StubError(zim.errors.Error):
description = '''\
Some description
here
'''
def __init__(self, i):
self.msg = 'Error %i' % i
class TestErrors(tests.TestCase):
def runTest(self):
'''Check base class for errors'''
wanted = '''\
Error 6
Some description
here
'''
self.assertEqual(str(StubError(6)), wanted)
self.assertEqual(unicode(StubError(6)), wanted)
self.assertEqual(repr(StubError(6)), '<StubError: Error 6>')
class CatchAllLogging(tests.LoggingFilter):
def __init__(self):
tests.LoggingFilter.__init__(self)
self.records = []
def filter(self, record):
self.records.append(record)
return False
class TestExceptionHandler(tests.TestCase):
def setUp(self):
logger = logging.getLogger('zim')
self.oldlevel = logger.getEffectiveLevel()
logger.setLevel(logging.DEBUG)
def tearDown(self):
logger = logging.getLogger('zim')
logger.setLevel(self.oldlevel)
def testExceptionHandlerWithGtk(self):
def error_dialog_with_trace(dialog):
self.assertIsInstance(dialog, ErrorDialog)
self.assertTrue(dialog.showing_trace)
def error_dialog_without_trace(dialog):
self.assertIsInstance(dialog, ErrorDialog)
self.assertFalse(dialog.showing_trace)
zim.errors.set_use_gtk(True)
try:
self.assertTrue(zim.errors.use_gtk_errordialog)
with tests.DialogContext(
error_dialog_with_trace,
error_dialog_with_trace,
error_dialog_without_trace,
error_dialog_without_trace,
):
with tests.LoggingFilter(
logger='zim.gui',
message='Running ErrorDialog'
):
self.testExceptionHandler()
except:
zim.errors.set_use_gtk(False)
raise
else:
zim.errors.set_use_gtk(False)
self.assertFalse(zim.errors.use_gtk_errordialog)
def testExceptionHandler(self):
## Handle unexpected error or bug
try:
raise AssertionError, 'My AssertionError'
except:
myfilter = CatchAllLogging()
with myfilter:
zim.errors.exception_handler('Test Error')
records = myfilter.records
# Should log one error message with traceback
self.assertEqual(len(records), 1)
self.assertEqual(records[0].getMessage(), 'Test Error')
self.assertEqual(records[0].levelno, logging.ERROR)
self.assertIsNotNone(records[0].exc_info)
else:
assert False
## Show caught bug
try:
raise AssertionError, 'My AssertionError'
except Exception, error:
myfilter = CatchAllLogging()
with myfilter:
zim.errors.show_error(error)
records = myfilter.records
# Should log one error message with traceback
self.assertEqual(len(records), 1)
self.assertEqual(records[0].getMessage(), 'Looks like you found a bug')
self.assertEqual(records[0].levelno, logging.ERROR)
self.assertIsNotNone(records[0].exc_info)
else:
assert False
## Handle normal application error
try:
raise zim.errors.Error('My normal Error')
except:
myfilter = CatchAllLogging()
with myfilter:
zim.errors.exception_handler('Test Error')
records = myfilter.records
# Should log a debug message with traceback
# and user error message without traceback
self.assertEqual(len(records), 2)
self.assertEqual(records[0].getMessage(), 'Test Error')
self.assertEqual(records[0].levelno, logging.DEBUG)
self.assertIsNotNone(records[0].exc_info)
self.assertEqual(records[1].getMessage(), 'My normal Error')
self.assertEqual(records[1].levelno, logging.ERROR)
self.assertIsNone(records[1].exc_info)
else:
assert False
## Handle normal IOError
try:
open('/some/non/existing/file/').read()
except:
myfilter = CatchAllLogging()
with myfilter:
zim.errors.exception_handler('Test IOError')
records = myfilter.records
# Should log a debug message with traceback
# and user error message without traceback
self.assertEqual(len(records), 2)
self.assertEqual(records[0].getMessage(), 'Test IOError')
self.assertEqual(records[0].levelno, logging.DEBUG)
self.assertIsNotNone(records[0].exc_info)
self.assertIn('/some/non/existing/file/', records[1].getMessage())
# do not test exact message - could be localized
self.assertEqual(records[1].levelno, logging.ERROR)
self.assertIsNone(records[1].exc_info)
else:
assert False
|