diff --git a/HACKING.rst b/HACKING.rst index 148097561af6..33bd80aae712 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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 ------------------- diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index e6bbab2d273b..65f81ac34d7b 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -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) diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 4efe5a56198a..6b3fec2ed259 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -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)