[hacking] Ensure not to use LOG.warn

This patchs ensures not to use LOG.warn because LOG.warn deprecated
in Python 3 [1], use LOG.warning instead.

[1] https://docs.python.org/3/library/logging.html#logging.warning

TrivialFix

Change-Id: I50e44a89092e0ee15f5be294837a87c321427921
This commit is contained in:
Ha Van Tu 2016-09-27 10:14:47 +07:00
parent 845ba22619
commit 8407f72c2c
3 changed files with 28 additions and 0 deletions

View File

@ -27,6 +27,7 @@ Manila Specific Commandments
with a sequence of key-value pairs.
- [M337] Ensure to not use xrange().
- [M354] Use oslo_utils.uuidutils to generate UUID instead of uuid4().
- [M338] Ensure to not use LOG.warn().
LOG Translations
----------------

View File

@ -59,6 +59,7 @@ dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)")
assert_no_xrange_re = re.compile(r"\s*xrange\s*\(")
assert_True = re.compile(r".*assertEqual\(True, .*\)")
assert_None = re.compile(r".*assertEqual\(None, .*\)")
no_log_warn = re.compile(r"\s*LOG.warn\(.*")
class BaseASTChecker(ast.NodeVisitor):
@ -360,6 +361,19 @@ def check_uuid4(logical_line):
yield (0, msg)
def no_log_warn_check(logical_line):
"""Disallow 'LOG.warn'
Deprecated LOG.warn(), instead use LOG.warning
://bugs.launchpad.net/manila/+bug/1508442
M338
"""
msg = ("M338: LOG.warn is deprecated, use LOG.warning.")
if re.match(no_log_warn, logical_line):
yield(0, msg)
def factory(register):
register(validate_log_translations)
register(check_explicit_underscore_import)
@ -373,3 +387,4 @@ def factory(register):
register(validate_assertTrue)
register(validate_assertIsNone)
register(check_uuid4)
register(no_log_warn_check)

View File

@ -329,3 +329,15 @@ class HackingTestCase(test.TestCase):
hex_uuid = uuid.uuid4().hex
"""
self._assert_has_no_errors(code, checks.check_uuid4)
def test_no_log_warn_check(self):
self.assertEqual(0, len(list(checks.no_log_warn_check(
"LOG.warning('This should not trigger LOG.warn "
"hacking check.')"))))
self.assertEqual(1, len(list(checks.no_log_warn_check(
"LOG.warn('We should not use LOG.warn')"))))
foo = """
LOG.warn('Catch me too, please'
)
"""
self.assertEqual(1, len(list(checks.no_log_warn_check(foo))))