Add hacking check for 3rd party mock

Change-Id: I25715d04791e9c8c681a63954719d48f7b390dcd
Signed-off-by: Sean McGinnis <sean.mcginnis@gmail.com>
This commit is contained in:
Sean McGinnis 2020-03-31 13:15:52 -05:00
parent c3c7a6328e
commit eb915e2dbb
No known key found for this signature in database
GPG Key ID: CE7EE4BFAF8D70C8
4 changed files with 27 additions and 1 deletions

View File

@ -372,3 +372,18 @@ def validate_assertTrue(logical_line, filename):
msg = ("C313: Unit tests should use assertTrue(value) instead" msg = ("C313: Unit tests should use assertTrue(value) instead"
" of using assertEqual(True, value).") " of using assertEqual(True, value).")
yield(0, msg) yield(0, msg)
third_party_mock = re.compile("^import.mock")
from_third_party_mock = re.compile("^from.mock.import")
@core.flake8ext
def no_third_party_mock(logical_line):
# We should only use unittest.mock, not the third party mock library that
# was needed for py2 support.
if (re.match(third_party_mock, logical_line) or
re.match(from_third_party_mock, logical_line)):
msg = ('C337: Unit tests should use the standard library "mock" '
'module, not the third party mock lib.')
yield(0, msg)

View File

@ -293,3 +293,13 @@ class HackingTestCase(test.TestCase):
def test_no_test_log(self, first, second, third, fourth): def test_no_test_log(self, first, second, third, fourth):
self.assertEqual(first, len(list(checks.no_test_log( self.assertEqual(first, len(list(checks.no_test_log(
"%s('arg')" % second, third, fourth)))) "%s('arg')" % second, third, fourth))))
@ddt.unpack
@ddt.data(
(1, 'import mock'),
(0, 'from unittest import mock'),
(1, 'from mock import patch'),
(0, 'from unittest.mock import patch'))
def test_no_third_party_mock(self, err_count, line):
self.assertEqual(err_count, len(list(checks.no_third_party_mock(
line))))

View File

@ -14,8 +14,8 @@
import errno import errno
import re import re
from unittest import mock
import mock
from oslo_utils import units from oslo_utils import units
from six.moves import http_client from six.moves import http_client

View File

@ -213,6 +213,7 @@ extension =
C312 = checks:no_translate_logs C312 = checks:no_translate_logs
C313 = checks:validate_assertTrue C313 = checks:validate_assertTrue
C336 = checks:dict_constructor_with_list_copy C336 = checks:dict_constructor_with_list_copy
C337 = checks:no_third_party_mock
paths = ./cinder/tests/hacking paths = ./cinder/tests/hacking
[doc8] [doc8]