Use assertIsNone instead of assertEqual(None, *)

One should replace assertEqual(None, *) with assertIsNone in tests
that validate that a variable is None.

This is based on commit 33a459c2c309670b0fcb2fcac5d128e802be7a69

Change-Id: I7794934f5c2267125bafb9d9953352daa68c39a7
This commit is contained in:
Boden R 2016-09-29 13:19:11 -07:00 committed by garyk
parent 9b3ea8f29f
commit 90008a6f4f
4 changed files with 46 additions and 0 deletions

View File

@ -21,3 +21,4 @@ Neutron Library Specific Commandments
- [N533] Validate that debug level logs are not translated
- [N534] Exception messages should be translated
- [N535] Usage of Python eventlet module not allowed
- [N536] Use assertIsNone/assertIsNotNone rather than assertEqual/assertIs to check None values.

View File

@ -37,6 +37,11 @@ namespace_imports_from_dot = re.compile(r"from[\s]+([\w]+)[.]")
namespace_imports_from_root = re.compile(r"from[\s]+([\w]+)[\s]+import[\s]+")
contextlib_nested = re.compile(r"^\s*with (contextlib\.)?nested\(")
assert_equal_none_re = re.compile(
r"assertEqual\(.*?,\s+None\)|assertEqual\(None,")
assert_is_none_re = re.compile(
r"assertIs(Not)?\(.*,\s+None\)|assertIs(Not)?\(None,")
def use_jsonutils(logical_line, filename):
"""N521 - jsonutils must be used instead of json.
@ -223,6 +228,19 @@ def check_no_eventlet_imports(logical_line):
yield logical_line.index('eventlet'), msg
def assert_equal_none(logical_line):
"""N536 - Use assertIsNone."""
if assert_equal_none_re.search(logical_line):
msg = ("N536: Use assertIsNone rather than assertEqual "
"to check for None values")
yield logical_line.index('assert'), msg
if assert_is_none_re.search(logical_line):
msg = ("N536: Use assertIsNone or assertIsNotNone rather than "
"assertIs or assertIsNone to check for None values.")
yield logical_line.index('assert'), msg
def factory(register):
"""Hacking check factory for neutron-lib adopter compliant checks.
@ -255,6 +273,7 @@ def incubating_factory(register):
:param register: The function to register the check functions with.
:returns: None.
"""
register(assert_equal_none)
def _neutron_lib_factory(register):

View File

@ -225,3 +225,25 @@ class HackingTestCase(base.BaseTestCase):
self.assertLinePasses(f, 'print("eventlet not here")')
self.assertLinePasses(f, 'print("eventlet.timeout")')
self.assertLinePasses(f, "from mymod.timeout import (eventlet, X)")
def test_assert_equal_none(self):
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(A, None) # Comment"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(None, A)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"self.assertEqual(None, A) # Comment"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(A, None)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(A, None) # Comment"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(None, A)"))), 1)
self.assertEqual(len(list(checks.assert_equal_none(
"assertIsNot(None, A) # Comment"))), 1)
self.assertEqual(
len(list(checks.assert_equal_none("self.assertIsNone(A)"))), 0)
self.assertEqual(
len(list(checks.assert_equal_none("self.assertIsNotNone(A)"))), 0)

View File

@ -0,0 +1,4 @@
---
features:
- Added hacking check ``N536``. This hacking check is added to the
incubating checks.