Add a hacking rule for deprecated assertion methods

Add a hacking rule for the following deprecated methods(*)
in Python 3.

* assertRegexpMatches
* assertNotRegexpMatches

[N361] assertRegex/assertNotRegex must be used instead of
       assertRegexpMatches/assertNotRegexpMatches.

*: https://docs.python.org/3.6/library/unittest.html#deprecated-aliases

Change-Id: Icfbaf26a7db6986820e264d1888982b985d613a1
This commit is contained in:
Takashi NATSUME 2018-10-25 11:24:09 +09:00
parent 7a76d0c71d
commit 249174943e
3 changed files with 30 additions and 0 deletions

View File

@ -68,6 +68,8 @@ Nova Specific Commandments
- [N358] Return must always be followed by a space when returning a value.
- [N359] Check for redundant import aliases.
- [N360] Yield must always be followed by a space when yielding a value.
- [N361] Check for usage of deprecated assertRegexpMatches and
assertNotRegexpMatches
Creating Unit Tests
-------------------

View File

@ -100,6 +100,8 @@ return_not_followed_by_space = re.compile(r"^\s*return(?:\(|{|\"|'|#).*$")
uuid4_re = re.compile(r"uuid4\(\)($|[^\.]|\.hex)")
redundant_import_alias_re = re.compile(r"import (?:.*\.)?(.+) as \1$")
yield_not_followed_by_space = re.compile(r"^\s*yield(?:\(|{|\[|\"|').*$")
asse_regexpmatches = re.compile(
r"(assertRegexpMatches|assertNotRegexpMatches)\(")
class BaseASTChecker(ast.NodeVisitor):
@ -838,6 +840,17 @@ def yield_followed_by_space(logical_line):
"N360: Yield keyword should be followed by a space.")
def assert_regexpmatches(logical_line):
"""Check for usage of deprecated assertRegexpMatches/assertNotRegexpMatches
N361
"""
res = asse_regexpmatches.search(logical_line)
if res:
yield (0, "N361: assertRegex/assertNotRegex must be used instead "
"of assertRegexpMatches/assertNotRegexpMatches.")
def factory(register):
register(import_no_db_in_virt)
register(no_db_session_in_public_api)
@ -881,3 +894,4 @@ def factory(register):
register(return_followed_by_space)
register(no_redundant_import_alias)
register(yield_followed_by_space)
register(assert_regexpmatches)

View File

@ -839,3 +839,17 @@ class HackingTestCase(test.NoDBTestCase):
yieldx_func(a, b)
"""
self._assert_has_no_errors(code, checks.yield_followed_by_space)
def test_assert_regexpmatches(self):
code = """
self.assertRegexpMatches("Test", output)
self.assertNotRegexpMatches("Notmatch", output)
"""
errors = [(x + 1, 0, 'N361') for x in range(2)]
self._assert_has_errors(code, checks.assert_regexpmatches,
expected_errors=errors)
code = """
self.assertRegexpMatchesfoo("Test", output)
self.assertNotRegexpMatchesbar("Notmatch", output)
"""
self._assert_has_no_errors(code, checks.assert_regexpmatches)