Add hacking rule for assertEqual(None, *)
Several bugs have been raised and fixed to convert assertEqual(None, *) with assertIsNone in tests. Install a hacking rule to prevent new occurrences of this problem. Change-Id: Ib157f911e2f6a90d21c3886b3139f9b109962052
This commit is contained in:
parent
f5ce8edea2
commit
2b7a755af4
@ -8,6 +8,8 @@ Manila Style Commandments
|
||||
|
||||
Manila Specific Commandments
|
||||
----------------------------
|
||||
|
||||
- [M312] Use assertIsNone(...) instead of assertEqual(None, ...).
|
||||
- [M313] Use assertTrue(...) rather than assertEqual(True, ...).
|
||||
- [M319] Validate that debug level logs are not translated.
|
||||
- [M323] Ensure that the _() function is explicitly imported to ensure proper translations.
|
||||
|
@ -56,8 +56,8 @@ custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
|
||||
oslo_namespace_imports = re.compile(r"from[\s]*oslo[.](.*)")
|
||||
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_True = re.compile(r".*assertEqual\(True, .*\)")
|
||||
assert_None = re.compile(r".*assertEqual\(None, .*\)")
|
||||
|
||||
|
||||
class BaseASTChecker(ast.NodeVisitor):
|
||||
@ -258,6 +258,13 @@ def validate_assertTrue(logical_line):
|
||||
yield(0, msg)
|
||||
|
||||
|
||||
def validate_assertIsNone(logical_line):
|
||||
if re.match(assert_None, logical_line):
|
||||
msg = ("M312: Unit tests should use assertIsNone(value) instead"
|
||||
" of using assertEqual(None, value).")
|
||||
yield(0, msg)
|
||||
|
||||
|
||||
def factory(register):
|
||||
register(validate_log_translations)
|
||||
register(check_explicit_underscore_import)
|
||||
@ -268,3 +275,4 @@ def factory(register):
|
||||
register(dict_constructor_with_list_copy)
|
||||
register(no_xrange)
|
||||
register(validate_assertTrue)
|
||||
register(validate_assertIsNone)
|
||||
|
@ -250,3 +250,10 @@ class HackingTestCase(test.TestCase):
|
||||
"assertTrue(True)"))))
|
||||
self.assertEqual(1, len(list(checks.validate_assertTrue(
|
||||
"assertEqual(True, %s)" % test_value))))
|
||||
|
||||
def test_validate_assertIsNone(self):
|
||||
test_value = None
|
||||
self.assertEqual(0, len(list(checks.validate_assertIsNone(
|
||||
"assertIsNone(None)"))))
|
||||
self.assertEqual(1, len(list(checks.validate_assertIsNone(
|
||||
"assertEqual(None, %s)" % test_value))))
|
||||
|
Loading…
Reference in New Issue
Block a user