2014-03-30 20:11:47 +00:00
|
|
|
# Copyright 2014 Google Inc. All Rights Reserved.
|
2014-07-26 15:11:14 +00:00
|
|
|
"""Unittest for the logging checker."""
|
|
|
|
import unittest
|
2014-03-30 20:11:47 +00:00
|
|
|
from astroid import test_utils
|
|
|
|
|
|
|
|
from pylint.checkers import logging
|
|
|
|
|
2014-03-30 21:19:33 +00:00
|
|
|
from pylint.testutils import CheckerTestCase, Message, set_config
|
2014-03-30 20:11:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoggingModuleDetectionTest(CheckerTestCase):
|
|
|
|
CHECKER_CLASS = logging.LoggingChecker
|
|
|
|
|
|
|
|
def test_detects_standard_logging_module(self):
|
|
|
|
stmts = test_utils.extract_node("""
|
|
|
|
import logging #@
|
|
|
|
logging.warn('%s' % '%s') #@
|
|
|
|
""")
|
|
|
|
self.checker.visit_module(None)
|
|
|
|
self.checker.visit_import(stmts[0])
|
2014-04-17 09:04:41 +00:00
|
|
|
with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
|
2014-03-30 20:11:47 +00:00
|
|
|
self.checker.visit_callfunc(stmts[1])
|
|
|
|
|
|
|
|
def test_detects_renamed_standard_logging_module(self):
|
|
|
|
stmts = test_utils.extract_node("""
|
|
|
|
import logging as blogging #@
|
|
|
|
blogging.warn('%s' % '%s') #@
|
|
|
|
""")
|
|
|
|
self.checker.visit_module(None)
|
|
|
|
self.checker.visit_import(stmts[0])
|
2014-04-17 09:04:41 +00:00
|
|
|
with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
|
2014-03-30 20:11:47 +00:00
|
|
|
self.checker.visit_callfunc(stmts[1])
|
|
|
|
|
2014-03-30 21:19:33 +00:00
|
|
|
@set_config(logging_modules=['logging', 'my.logging'])
|
2014-03-30 20:11:47 +00:00
|
|
|
def test_nonstandard_logging_module(self):
|
|
|
|
stmts = test_utils.extract_node("""
|
|
|
|
from my import logging as blogging #@
|
|
|
|
blogging.warn('%s' % '%s') #@
|
|
|
|
""")
|
|
|
|
self.checker.visit_module(None)
|
|
|
|
self.checker.visit_import(stmts[0])
|
2014-04-17 09:04:41 +00:00
|
|
|
with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
|
2014-03-30 20:11:47 +00:00
|
|
|
self.checker.visit_callfunc(stmts[1])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-07-26 15:11:14 +00:00
|
|
|
unittest.main()
|