Closes issue #11557: removal of non-idiomatic code in test_logging.

This commit is contained in:
Vinay Sajip 2011-06-05 09:31:34 +01:00
parent d64b2bae9b
commit 5056c8c718
1 changed files with 25 additions and 31 deletions

View File

@ -158,8 +158,7 @@ class BaseTest(unittest.TestCase):
except AttributeError: except AttributeError:
# StringIO.StringIO lacks a reset() method. # StringIO.StringIO lacks a reset() method.
actual_lines = stream.getvalue().splitlines() actual_lines = stream.getvalue().splitlines()
self.assertEqual(len(actual_lines), len(expected_values), self.assertEqual(len(actual_lines), len(expected_values))
'%s vs. %s' % (actual_lines, expected_values))
for actual, expected in zip(actual_lines, expected_values): for actual, expected in zip(actual_lines, expected_values):
match = pat.search(actual) match = pat.search(actual)
if not match: if not match:
@ -599,8 +598,8 @@ class StreamHandlerTest(BaseTest):
h = logging.StreamHandler(BadStream()) h = logging.StreamHandler(BadStream())
sys.stderr = sio = io.StringIO() sys.stderr = sio = io.StringIO()
h.handle(r) h.handle(r)
self.assertTrue('\nRuntimeError: ' self.assertIn('\nRuntimeError: deliberate mistake\n',
'deliberate mistake\n' in sio.getvalue()) sio.getvalue())
logging.raiseExceptions = False logging.raiseExceptions = False
sys.stderr = sio = io.StringIO() sys.stderr = sio = io.StringIO()
h.handle(r) h.handle(r)
@ -940,7 +939,7 @@ class SMTPHandlerTest(BaseTest):
peer, mailfrom, rcpttos, data = self.messages[0] peer, mailfrom, rcpttos, data = self.messages[0]
self.assertEqual(mailfrom, 'me') self.assertEqual(mailfrom, 'me')
self.assertEqual(rcpttos, ['you']) self.assertEqual(rcpttos, ['you'])
self.assertTrue('\nSubject: Log\n' in data) self.assertIn('\nSubject: Log\n', data)
self.assertTrue(data.endswith('\n\nHello')) self.assertTrue(data.endswith('\n\nHello'))
h.close() h.close()
@ -1692,7 +1691,7 @@ class WarningsTest(BaseTest):
def test_warnings(self): def test_warnings(self):
with warnings.catch_warnings(): with warnings.catch_warnings():
logging.captureWarnings(True) logging.captureWarnings(True)
self.addCleanup(lambda: logging.captureWarnings(False)) self.addCleanup(logging.captureWarnings, False)
warnings.filterwarnings("always", category=UserWarning) warnings.filterwarnings("always", category=UserWarning)
stream = io.StringIO() stream = io.StringIO()
h = logging.StreamHandler(stream) h = logging.StreamHandler(stream)
@ -1716,14 +1715,14 @@ class WarningsTest(BaseTest):
def test_warnings_no_handlers(self): def test_warnings_no_handlers(self):
with warnings.catch_warnings(): with warnings.catch_warnings():
logging.captureWarnings(True) logging.captureWarnings(True)
self.addCleanup(lambda: logging.captureWarnings(False)) self.addCleanup(logging.captureWarnings, False)
# confirm our assumption: no loggers are set # confirm our assumption: no loggers are set
logger = logging.getLogger("py.warnings") logger = logging.getLogger("py.warnings")
assert logger.handlers == [] self.assertEqual(logger.handlers, [])
warnings.showwarning("Explicit", UserWarning, "dummy.py", 42) warnings.showwarning("Explicit", UserWarning, "dummy.py", 42)
self.assertTrue(len(logger.handlers) == 1) self.assertEqual(len(logger.handlers), 1)
self.assertIsInstance(logger.handlers[0], logging.NullHandler) self.assertIsInstance(logger.handlers[0], logging.NullHandler)
@ -2971,7 +2970,7 @@ class ShutdownTest(BaseTest):
self.called = [] self.called = []
raise_exceptions = logging.raiseExceptions raise_exceptions = logging.raiseExceptions
self.addCleanup(lambda: setattr(logging, 'raiseExceptions', raise_exceptions)) self.addCleanup(setattr, logging, 'raiseExceptions', raise_exceptions)
def raise_error(self, error): def raise_error(self, error):
def inner(): def inner():
@ -3056,8 +3055,8 @@ class ModuleLevelMiscTest(BaseTest):
def test_disable(self): def test_disable(self):
old_disable = logging.root.manager.disable old_disable = logging.root.manager.disable
# confirm our assumptions are correct # confirm our assumptions are correct
assert old_disable == 0 self.assertEqual(old_disable, 0)
self.addCleanup(lambda: logging.disable(old_disable)) self.addCleanup(logging.disable, old_disable)
logging.disable(83) logging.disable(83)
self.assertEqual(logging.root.manager.disable, 83) self.assertEqual(logging.root.manager.disable, 83)
@ -3266,7 +3265,7 @@ class BasicConfigTest(unittest.TestCase):
def test_level(self): def test_level(self):
old_level = logging.root.level old_level = logging.root.level
self.addCleanup(lambda: logging.root.setLevel(old_level)) self.addCleanup(logging.root.setLevel, old_level)
logging.basicConfig(level=57) logging.basicConfig(level=57)
self.assertEqual(logging.root.level, 57) self.assertEqual(logging.root.level, 57)
@ -3311,7 +3310,7 @@ class BasicConfigTest(unittest.TestCase):
old_basic_config() old_basic_config()
old_level = logging.root.level old_level = logging.root.level
logging.root.setLevel(100) # avoid having messages in stderr logging.root.setLevel(100) # avoid having messages in stderr
self.addCleanup(lambda: logging.root.setLevel(old_level)) self.addCleanup(logging.root.setLevel, old_level)
called.append((a, kw)) called.append((a, kw))
patch(self, logging, 'basicConfig', my_basic_config) patch(self, logging, 'basicConfig', my_basic_config)
@ -3353,7 +3352,7 @@ class LoggerAdapterTest(unittest.TestCase):
self.recording = RecordingHandler() self.recording = RecordingHandler()
self.logger = logging.root self.logger = logging.root
self.logger.addHandler(self.recording) self.logger.addHandler(self.recording)
self.addCleanup(lambda: self.logger.removeHandler(self.recording)) self.addCleanup(self.logger.removeHandler, self.recording)
self.addCleanup(self.recording.close) self.addCleanup(self.recording.close)
def cleanup(): def cleanup():
@ -3367,8 +3366,8 @@ class LoggerAdapterTest(unittest.TestCase):
msg = 'testing exception: %r' msg = 'testing exception: %r'
exc = None exc = None
try: try:
assert False 1 / 0
except AssertionError as e: except ZeroDivisionError as e:
exc = e exc = e
self.adapter.exception(msg, self.recording) self.adapter.exception(msg, self.recording)
@ -3393,8 +3392,8 @@ class LoggerAdapterTest(unittest.TestCase):
def test_is_enabled_for(self): def test_is_enabled_for(self):
old_disable = self.adapter.logger.manager.disable old_disable = self.adapter.logger.manager.disable
self.adapter.logger.manager.disable = 33 self.adapter.logger.manager.disable = 33
self.addCleanup(lambda: setattr(self.adapter.logger.manager, self.addCleanup(setattr, self.adapter.logger.manager, 'disable',
'disable', old_disable)) old_disable)
self.assertFalse(self.adapter.isEnabledFor(32)) self.assertFalse(self.adapter.isEnabledFor(32))
def test_has_handlers(self): def test_has_handlers(self):
@ -3402,8 +3401,8 @@ class LoggerAdapterTest(unittest.TestCase):
for handler in self.logger.handlers: for handler in self.logger.handlers:
self.logger.removeHandler(handler) self.logger.removeHandler(handler)
assert not self.logger.hasHandlers()
self.assertFalse(self.logger.hasHandlers())
self.assertFalse(self.adapter.hasHandlers()) self.assertFalse(self.adapter.hasHandlers())
@ -3414,7 +3413,7 @@ class LoggerTest(BaseTest):
self.recording = RecordingHandler() self.recording = RecordingHandler()
self.logger = logging.Logger(name='blah') self.logger = logging.Logger(name='blah')
self.logger.addHandler(self.recording) self.logger.addHandler(self.recording)
self.addCleanup(lambda: self.logger.removeHandler(self.recording)) self.addCleanup(self.logger.removeHandler, self.recording)
self.addCleanup(self.recording.close) self.addCleanup(self.recording.close)
self.addCleanup(logging.shutdown) self.addCleanup(logging.shutdown)
@ -3425,8 +3424,8 @@ class LoggerTest(BaseTest):
msg = 'testing exception: %r' msg = 'testing exception: %r'
exc = None exc = None
try: try:
assert False 1 / 0
except AssertionError as e: except ZeroDivisionError as e:
exc = e exc = e
self.logger.exception(msg, self.recording) self.logger.exception(msg, self.recording)
@ -3440,14 +3439,14 @@ class LoggerTest(BaseTest):
def test_log_invalid_level_with_raise(self): def test_log_invalid_level_with_raise(self):
old_raise = logging.raiseExceptions old_raise = logging.raiseExceptions
self.addCleanup(lambda: setattr(logging, 'raiseExecptions', old_raise)) self.addCleanup(setattr, logging, 'raiseExecptions', old_raise)
logging.raiseExceptions = True logging.raiseExceptions = True
self.assertRaises(TypeError, self.logger.log, '10', 'test message') self.assertRaises(TypeError, self.logger.log, '10', 'test message')
def test_log_invalid_level_no_raise(self): def test_log_invalid_level_no_raise(self):
old_raise = logging.raiseExceptions old_raise = logging.raiseExceptions
self.addCleanup(lambda: setattr(logging, 'raiseExecptions', old_raise)) self.addCleanup(setattr, logging, 'raiseExecptions', old_raise)
logging.raiseExceptions = False logging.raiseExceptions = False
self.logger.log('10', 'test message') # no exception happens self.logger.log('10', 'test message') # no exception happens
@ -3489,22 +3488,17 @@ class LoggerTest(BaseTest):
for handler in self.logger.handlers: for handler in self.logger.handlers:
self.logger.removeHandler(handler) self.logger.removeHandler(handler)
assert not self.logger.hasHandlers()
self.assertFalse(self.logger.hasHandlers()) self.assertFalse(self.logger.hasHandlers())
def test_has_handlers_no_propagate(self): def test_has_handlers_no_propagate(self):
child_logger = logging.getLogger('blah.child') child_logger = logging.getLogger('blah.child')
child_logger.propagate = False child_logger.propagate = False
assert child_logger.handlers == []
self.assertFalse(child_logger.hasHandlers()) self.assertFalse(child_logger.hasHandlers())
def test_is_enabled_for(self): def test_is_enabled_for(self):
old_disable = self.logger.manager.disable old_disable = self.logger.manager.disable
self.logger.manager.disable = 23 self.logger.manager.disable = 23
self.addCleanup(lambda: setattr(self.logger.manager, self.addCleanup(setattr, self.logger.manager, 'disable', old_disable)
'disable', old_disable))
self.assertFalse(self.logger.isEnabledFor(22)) self.assertFalse(self.logger.isEnabledFor(22))