Add hacking rule for using assertIsNone instead of assertEqual(None,***)

Change-Id: I03d3a45769a1a7a125d9bb1de94d22f9004d8d41
Partial-Bug: #1608173
This commit is contained in:
Namrata 2016-08-03 14:24:13 +05:30 committed by Serg Melikyan
parent ed4aa4730e
commit 71249f19f6
3 changed files with 33 additions and 0 deletions

View File

@ -6,6 +6,8 @@ Read the OpenStack Style Commandments http://docs.openstack.org/developer/hackin
Murano Specific Commandments
---------------------------
- [M318] Change assertEqual(A, None) or assertEqual(None, A) by optimal assert
like assertIsNone(A)
- [M322] Method's default argument shouldn't be mutable.
- [M323] Python 3: do not use dict.iteritems.
- [M324] Python 3: do not use dict.iterkeys.

View File

@ -29,6 +29,23 @@ Guidelines for writing new hacking checks
"""
mutable_default_args = re.compile(r"^\s*def .+\((.+=\{\}|.+=\[\])")
assert_equal_end_with_none_re = re.compile(
r"(.)*assertEqual\((\w|\.|\'|\"|\[|\])+, None\)")
assert_equal_start_with_none_re = re.compile(
r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)")
def assert_equal_none(logical_line):
"""Check for assertEqual(A, None) or assertEqual(None, A) sentences
M318
"""
msg = ("M318: assertEqual(A, None) or assertEqual(None, A) "
"sentences not allowed")
res = (assert_equal_start_with_none_re.match(logical_line) or
assert_equal_end_with_none_re.match(logical_line))
if res:
yield (0, msg)
def no_mutable_default_args(logical_line):
@ -63,6 +80,7 @@ def check_no_basestring(logical_line):
def factory(register):
register(assert_equal_none)
register(no_mutable_default_args)
register(check_python3_no_iteritems)
register(check_python3_no_iterkeys)

View File

@ -74,6 +74,19 @@ class HackingTestCase(base.MuranoTestCase):
def _assert_has_no_errors(self, code, checker, filename=None):
self._assert_has_errors(code, checker, filename=filename)
def test_assert_equal_none(self):
errors = [(1, 0, "M318")]
check = checks.assert_equal_none
code = "self.assertEqual(A, None)"
self._assert_has_errors(code, check, errors)
code = "self.assertEqual(None, A)"
self._assert_has_errors(code, check, errors)
code = "self.assertIsNone()"
self._assert_has_no_errors(code, check)
def test_no_mutable_default_args(self):
self.assertEqual(1, len(list(checks.no_mutable_default_args(
"def get_info_from_bdm(virt_type, bdm, mapping=[])"))))