From 6ed2660676a2bc74fcc36835b34bcbf858fa96f6 Mon Sep 17 00:00:00 2001 From: Brian Haley Date: Tue, 9 Jun 2020 14:46:36 -0400 Subject: [PATCH] Add a hacking check for importing mock library We should only be using unittest.mock since we're python3-only, so add and enable a hacking check that will catch it. Change-Id: I64847f329ee3e1a3377a8c63df90c697d8b74c86 --- HACKING.rst | 1 + ovn_octavia_provider/hacking/checks.py | 19 ++++++++++++++ .../tests/unit/hacking/test_checks.py | 25 +++++++++++++++++++ tox.ini | 1 + 4 files changed, 46 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index 5d718c1c..706b7f28 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -15,3 +15,4 @@ Below you can find a list of checks specific to this repository. - [N343] Production code must not import from ovn_octavia_provider.tests.* - [N344] Python 3: Do not use filter(lambda obj: test(obj), data). Replace it with [obj for obj in data if test(obj)]. +- [N347] Test code must not import mock library diff --git a/ovn_octavia_provider/hacking/checks.py b/ovn_octavia_provider/hacking/checks.py index 3d9e52b0..bb5bb23b 100644 --- a/ovn_octavia_provider/hacking/checks.py +++ b/ovn_octavia_provider/hacking/checks.py @@ -42,6 +42,9 @@ tests_imports_from2 = re.compile( r"\bfrom[\s]+ovn_octavia_provider[\s]+import[\s]+tests\b") no_line_continuation_backslash_re = re.compile(r'.*(\\)\n') +import_mock = re.compile(r"\bimport[\s]+mock\b") +import_from_mock = re.compile(r"\bfrom[\s]+mock[\s]+import\b") + @core.flake8ext def check_assert_called_once_with(logical_line, filename): @@ -162,3 +165,19 @@ def check_python3_no_filter(logical_line): if filter_match.match(logical_line): yield(0, msg) + + +@core.flake8ext +def check_no_import_mock(logical_line, filename, noqa): + """N347 - Test code must not import mock library.""" + msg = ("N347: Test code must not import mock library") + + if noqa: + return + + if 'ovn_octavia_provider/tests/' not in filename: + return + + for regex in import_mock, import_from_mock: + if re.match(regex, logical_line): + yield(0, msg) diff --git a/ovn_octavia_provider/tests/unit/hacking/test_checks.py b/ovn_octavia_provider/tests/unit/hacking/test_checks.py index aecb5d7f..06153330 100644 --- a/ovn_octavia_provider/tests/unit/hacking/test_checks.py +++ b/ovn_octavia_provider/tests/unit/hacking/test_checks.py @@ -221,3 +221,28 @@ class HackingTestCase(base.BaseTestCase): self.assertLinePasses(f, "[obj for obj in data if test(obj)]") self.assertLinePasses(f, "filter(function, range(0,10))") self.assertLinePasses(f, "lambda x, y: x+y") + + def test_check_no_import_mock(self): + pass_line = 'from unittest import mock' + fail_lines = ('import mock', + 'import mock as mock_lib', + 'from mock import patch') + self.assertEqual( + 0, len(list( + checks.check_no_import_mock( + pass_line, + "ovn_octavia_provider/tests/test_fake.py", + None)))) + for fail_line in fail_lines: + self.assertEqual( + 0, len(list( + checks.check_no_import_mock( + fail_line, + "ovn_octavia_provider/common/utils.py", + None)))) + self.assertEqual( + 1, len(list( + checks.check_no_import_mock( + fail_line, + "ovn_octavia_provider/tests/test_fake.py", + None)))) diff --git a/tox.ini b/tox.ini index 41d9e358..5ca8bf6e 100644 --- a/tox.ini +++ b/tox.ini @@ -121,6 +121,7 @@ extension = N332 = checks:check_assertequal_for_httpcode N343 = checks:check_no_imports_from_tests N344 = checks:check_python3_no_filter + N347 = checks:check_no_import_mock paths =./ovn_octavia_provider/hacking [testenv:genconfig]