This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
multipub/.pyenv/lib/python3.7/site-packages/pylint/test/unittest_checker_logging.py

78 lines
2.6 KiB
Python
Raw Normal View History

2019-02-09 02:45:25 -06:00
# Copyright (c) 2014 Google, Inc.
# Copyright (c) 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
# Copyright (c) 2015-2018 Claudiu Popa <pcmanticore@gmail.com>
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
"""Unittest for the logging checker."""
import astroid
from pylint.checkers import logging
from pylint.testutils import CheckerTestCase, Message, set_config
class TestLoggingModuleDetection(CheckerTestCase):
CHECKER_CLASS = logging.LoggingChecker
def test_detects_standard_logging_module(self):
stmts = astroid.extract_node(
"""
import logging #@
logging.warn('%s' % '%s') #@
"""
)
self.checker.visit_module(None)
self.checker.visit_import(stmts[0])
with self.assertAddsMessages(Message("logging-not-lazy", node=stmts[1])):
self.checker.visit_call(stmts[1])
def test_dont_crash_on_invalid_format_string(self):
node = astroid.parse(
"""
import logging
logging.error('0} - {1}'.format(1, 2))
"""
)
self.walk(node)
def test_detects_renamed_standard_logging_module(self):
stmts = astroid.extract_node(
"""
import logging as blogging #@
blogging.warn('%s' % '%s') #@
"""
)
self.checker.visit_module(None)
self.checker.visit_import(stmts[0])
with self.assertAddsMessages(Message("logging-not-lazy", node=stmts[1])):
self.checker.visit_call(stmts[1])
@set_config(logging_modules=["logging", "my.logging"])
def test_nonstandard_logging_module(self):
stmts = astroid.extract_node(
"""
from my import logging as blogging #@
blogging.warn('%s' % '%s') #@
"""
)
self.checker.visit_module(None)
self.checker.visit_import(stmts[0])
with self.assertAddsMessages(Message("logging-not-lazy", node=stmts[1])):
self.checker.visit_call(stmts[1])
@set_config(logging_format_style="new")
def test_brace_format_style(self):
stmts = astroid.extract_node(
"""
import logging #@
logging.error('{}', 1) #@
"""
)
self.checker.visit_module(None)
self.checker.visit_import(stmts[0])
with self.assertNoMessages():
self.checker.visit_call(stmts[1])