diff --git a/octavia/hacking/checks.py b/octavia/hacking/checks.py index ceea4de309..52070bb719 100644 --- a/octavia/hacking/checks.py +++ b/octavia/hacking/checks.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import re - """ Guidelines for writing new hacking checks @@ -30,6 +28,11 @@ Guidelines for writing new hacking checks """ +import re + +from hacking import core + + _all_log_levels = {'critical', 'error', 'exception', 'info', 'warning'} _all_hints = {'_LC', '_LE', '_LI', '_', '_LW'} @@ -70,6 +73,7 @@ def _translation_checks_not_enforced(filename): return any(pat in filename for pat in ["/tests/", "rally-jobs/plugins/"]) +@core.flake8ext def assert_true_instance(logical_line): """Check for assertTrue(isinstance(a, b)) sentences @@ -80,6 +84,7 @@ def assert_true_instance(logical_line): "Use assertIsInstance instead.") +@core.flake8ext def assert_equal_or_not_none(logical_line): """Check for assertEqual(A, None) or assertEqual(None, A) sentences, @@ -97,6 +102,7 @@ def assert_equal_or_not_none(logical_line): yield (0, msg) +@core.flake8ext def assert_equal_true_or_false(logical_line): """Check for assertEqual(True, A) or assertEqual(False, A) sentences @@ -109,12 +115,14 @@ def assert_equal_true_or_false(logical_line): "sentences not allowed") +@core.flake8ext def no_mutable_default_args(logical_line): msg = "O324: Method's default argument shouldn't be mutable!" if mutable_default_args.match(logical_line): yield (0, msg) +@core.flake8ext def assert_equal_in(logical_line): """Check for assertEqual(A in B, True), assertEqual(True, A in B), @@ -130,6 +138,7 @@ def assert_equal_in(logical_line): "contents.") +@core.flake8ext def no_log_warn(logical_line): """Disallow 'LOG.warn(' @@ -139,6 +148,7 @@ def no_log_warn(logical_line): yield(0, "O339:Use LOG.warning() rather than LOG.warn()") +@core.flake8ext def no_translate_logs(logical_line, filename): """O341 - Don't translate logs. @@ -164,6 +174,7 @@ def no_translate_logs(logical_line, filename): yield (logical_line.index(match.group()), msg) +@core.flake8ext def check_raised_localized_exceptions(logical_line, filename): """O342 - Untranslated exception message. @@ -185,6 +196,7 @@ def check_raised_localized_exceptions(logical_line, filename): yield (logical_line.index(exception_msg), msg) +@core.flake8ext def check_no_eventlet_imports(logical_line): """O345 - Usage of Python eventlet module not allowed. @@ -198,6 +210,7 @@ def check_no_eventlet_imports(logical_line): yield logical_line.index('eventlet'), msg +@core.flake8ext def check_line_continuation_no_backslash(logical_line, tokens): """O346 - Don't use backslashes for line continuation. @@ -219,6 +232,7 @@ def check_line_continuation_no_backslash(logical_line, tokens): yield backslash, msg +@core.flake8ext def revert_must_have_kwargs(logical_line): """O347 - Taskflow revert methods must have \\*\\*kwargs. @@ -232,6 +246,7 @@ def revert_must_have_kwargs(logical_line): yield 0, msg +@core.flake8ext def check_no_logging_imports(logical_line): """O348 - Usage of Python logging module not allowed. @@ -243,18 +258,3 @@ def check_no_logging_imports(logical_line): if no_logging_re.match(logical_line): msg = 'O348 Usage of Python logging module not allowed, use oslo_log' yield logical_line.index('logging'), msg - - -def factory(register): - register(assert_true_instance) - register(assert_equal_or_not_none) - register(no_translate_logs) - register(assert_equal_true_or_false) - register(no_mutable_default_args) - register(assert_equal_in) - register(no_log_warn) - register(check_raised_localized_exceptions) - register(check_no_eventlet_imports) - register(check_line_continuation_no_backslash) - register(revert_must_have_kwargs) - register(check_no_logging_imports) diff --git a/octavia/tests/unit/test_hacking.py b/octavia/tests/unit/test_hacking.py index 189215cda4..9456cd62db 100644 --- a/octavia/tests/unit/test_hacking.py +++ b/octavia/tests/unit/test_hacking.py @@ -59,20 +59,6 @@ class HackingTestCase(base.BaseTestCase): def assertLineFails(self, func, *args): self.assertIsInstance(next(func(*args)), tuple) - def _get_factory_checks(self, factory): - check_fns = [] - - def _reg(check_fn): - self.assertTrue(hasattr(check_fn, '__call__')) - self.assertFalse(check_fn in check_fns) - check_fns.append(check_fn) - - factory(_reg) - return check_fns - - def test_factory(self): - self.assertGreater(len(self._get_factory_checks(checks.factory)), 0) - def test_assert_true_instance(self): self.assertEqual(1, len(list(checks.assert_true_instance( "self.assertTrue(isinstance(e, " diff --git a/test-requirements.txt b/test-requirements.txt index 61c791c684..7d8277649e 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,7 +1,7 @@ # The order of packages is significant, because pip processes them in the order # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -hacking>=1.1.0 # Apache-2.0 +hacking>=3.0 # Apache-2.0 requests-mock>=1.2.0 # Apache-2.0 coverage!=4.4,>=4.0 # Apache-2.0 fixtures>=3.0.0 # Apache-2.0/BSD diff --git a/tox.ini b/tox.ini index 3f09605831..b6cc30c77c 100644 --- a/tox.ini +++ b/tox.ini @@ -85,6 +85,7 @@ whitelist_externals = deps = -c{env:UPPER_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master} -r{toxinidir}/requirements.txt + -r{toxinidir}/test-requirements.txt -r{toxinidir}/doc/requirements.txt whitelist_externals = rm commands = @@ -166,7 +167,23 @@ commands = bash -c "find {toxinidir} \ [hacking] import_exceptions = octavia.i18n -local-check-factory = octavia.hacking.checks.factory + +[flake8:local-plugins] +extension = + O316 = checks:assert_true_instance + O318 = checks:assert_equal_or_not_none + O323 = checks:assert_equal_true_or_false + O324 = checks:no_mutable_default_args + O338 = checks:assert_equal_in + O339 = checks:no_log_warn + O341 = checks:no_translate_logs + O342 = checks:check_raised_localized_exceptions + O345 = checks:check_no_eventlet_imports + O346 = checks:check_line_continuation_no_backslash + O347 = checks:revert_must_have_kwargs + O348 = checks:check_no_logging_imports +paths = + ./octavia/hacking [doc8] max-line-length = 79