Skip to content

Commit

Permalink
Add hacking check for 3rd party mock
Browse files Browse the repository at this point in the history
Change-Id: I25715d04791e9c8c681a63954719d48f7b390dcd
Signed-off-by: Sean McGinnis <[email protected]>
  • Loading branch information
stmcginnis committed Apr 9, 2020
1 parent c3c7a63 commit eb915e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cinder/tests/hacking/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,18 @@ def validate_assertTrue(logical_line, filename):
msg = ("C313: Unit tests should use assertTrue(value) instead"
" of using assertEqual(True, value).")
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)
10 changes: 10 additions & 0 deletions cinder/tests/unit/test_hacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,13 @@ def test_validate_assertTrue(self):
def test_no_test_log(self, first, second, third, fourth):
self.assertEqual(first, len(list(checks.no_test_log(
"%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))))
2 changes: 1 addition & 1 deletion cinder/tests/unit/volume/drivers/test_prophetstor_dpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import errno
import re
from unittest import mock

import mock
from oslo_utils import units
from six.moves import http_client

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ extension =
C312 = checks:no_translate_logs
C313 = checks:validate_assertTrue
C336 = checks:dict_constructor_with_list_copy
C337 = checks:no_third_party_mock
paths = ./cinder/tests/hacking

[doc8]
Expand Down

0 comments on commit eb915e2

Please sign in to comment.