Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittest_checker_logging.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) 2014 Google, Inc.
  2. # Copyright (c) 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  3. # Copyright (c) 2015-2018 Claudiu Popa <pcmanticore@gmail.com>
  4. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  5. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  6. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  7. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  8. """Unittest for the logging checker."""
  9. import astroid
  10. from pylint.checkers import logging
  11. from pylint.testutils import CheckerTestCase, Message, set_config
  12. class TestLoggingModuleDetection(CheckerTestCase):
  13. CHECKER_CLASS = logging.LoggingChecker
  14. def test_detects_standard_logging_module(self):
  15. stmts = astroid.extract_node("""
  16. import logging #@
  17. logging.warn('%s' % '%s') #@
  18. """)
  19. self.checker.visit_module(None)
  20. self.checker.visit_import(stmts[0])
  21. with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
  22. self.checker.visit_call(stmts[1])
  23. def test_dont_crash_on_invalid_format_string(self):
  24. node = astroid.parse('''
  25. import logging
  26. logging.error('0} - {1}'.format(1, 2))
  27. ''')
  28. self.walk(node)
  29. def test_detects_renamed_standard_logging_module(self):
  30. stmts = astroid.extract_node("""
  31. import logging as blogging #@
  32. blogging.warn('%s' % '%s') #@
  33. """)
  34. self.checker.visit_module(None)
  35. self.checker.visit_import(stmts[0])
  36. with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
  37. self.checker.visit_call(stmts[1])
  38. @set_config(logging_modules=['logging', 'my.logging'])
  39. def test_nonstandard_logging_module(self):
  40. stmts = astroid.extract_node("""
  41. from my import logging as blogging #@
  42. blogging.warn('%s' % '%s') #@
  43. """)
  44. self.checker.visit_module(None)
  45. self.checker.visit_import(stmts[0])
  46. with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
  47. self.checker.visit_call(stmts[1])