Skip to content

Commit 1b6df0b

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add new hacking rules"
2 parents 232a0ab + eb7c4c6 commit 1b6df0b

File tree

5 files changed

+127
-10
lines changed

5 files changed

+127
-10
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ repos:
3232
- id: mypy
3333
additional_dependencies:
3434
- types-requests
35-
# keep this in-sync with '[mypy] exclude' in 'setup.cfg'
35+
# keep this in-sync with '[tool.mypy] exclude' in 'pyproject.toml'
3636
exclude: |
3737
(?x)(
3838
doc/.*
3939
| examples/.*
40+
| hacking/.*
4041
| releasenotes/.*
4142
)

hacking/checks.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import os
14+
import re
15+
16+
from hacking import core
17+
18+
"""
19+
Guidelines for writing new hacking checks
20+
21+
- Use only for python-openstackclient specific tests. OpenStack general tests
22+
should be submitted to the common 'hacking' module.
23+
- Pick numbers in the range O4xx. Find the current test with the highest
24+
allocated number and then pick the next value.
25+
"""
26+
27+
28+
@core.flake8ext
29+
def assert_no_oslo(logical_line):
30+
"""Check for use of oslo libraries.
31+
32+
O400
33+
"""
34+
if re.match(r'(from|import) oslo_.*', logical_line):
35+
yield (0, "0400: oslo libraries should not be used in SDK projects")
36+
37+
38+
@core.flake8ext
39+
def assert_no_duplicated_setup(logical_line, filename):
40+
"""Check for use of various unnecessary test duplications.
41+
42+
O401
43+
"""
44+
if os.path.join('openstackclient', 'tests', 'unit') not in filename:
45+
return
46+
47+
if re.match(r'self.app = .*\(self.app, self.namespace\)', logical_line):
48+
yield (
49+
0,
50+
'O401: It is not necessary to create dummy Namespace objects',
51+
)
52+
53+
if os.path.basename(filename) != 'fakes.py':
54+
if re.match(
55+
r'self.[a-z_]+_client = self.app.client_manager.*', logical_line
56+
):
57+
yield (
58+
0,
59+
"O401: Aliases for mocks of the service client are already "
60+
"provided by the respective service's FakeClientMixin class",
61+
)
62+
63+
if match := re.match(
64+
r'self.app.client_manager.([a-z_]+) = mock.Mock', logical_line
65+
):
66+
service = match.group(1)
67+
if service == 'auth_ref':
68+
return
69+
yield (
70+
0,
71+
f"O401: client_manager.{service} mocks are already provided by "
72+
f"the {service} service's FakeClientMixin class",
73+
)
74+
75+
76+
@core.flake8ext
77+
def assert_use_of_client_aliases(logical_line, filename):
78+
"""Ensure we use $service_client instead of $sdk_connection.service.
79+
80+
O402
81+
"""
82+
# we should expand the list of services as we drop legacy clients
83+
if match := re.match(
84+
r'self\.app\.client_manager\.sdk_connnection\.(compute|network|image)',
85+
logical_line,
86+
):
87+
service = match.group(1)
88+
yield (0, f"0402: prefer {service}_client to sdk_connection.{service}")
89+
90+
if match := re.match(
91+
r'(self\.app\.client_manager\.(compute|network|image)+\.[a-z_]+) = mock.Mock',
92+
logical_line,
93+
):
94+
yield (
95+
0,
96+
f"O402: {match.group(1)} is already a mock: there's no need to "
97+
f"assign a new mock.Mock instance.",
98+
)
99+
100+
if match := re.match(
101+
r'(self\.(compute|network|image)_client\.[a-z_]+) = mock.Mock',
102+
logical_line,
103+
):
104+
yield (
105+
0,
106+
f"O402: {match.group(1)} is already a mock: there's no need to "
107+
f"assign a new mock.Mock instance.",
108+
)

openstackclient/tests/unit/network/test_common.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ def setUp(self):
126126
# Create client mocks. Note that we intentionally do not use specced
127127
# mocks since we want to test fake methods.
128128

129-
self.app.client_manager.network = mock.Mock()
130-
self.network_client = self.app.client_manager.network
129+
self.app.client_manager.network = mock.Mock() # noqa: O401
130+
self.network_client = self.app.client_manager.network # noqa: O401
131131
self.network_client.network_action.return_value = 'take_action_network'
132132

133-
self.app.client_manager.compute = mock.Mock()
134-
self.compute_client = self.app.client_manager.compute
133+
self.app.client_manager.compute = mock.Mock() # noqa: O401
134+
self.compute_client = self.app.client_manager.compute # noqa: O401
135135
self.compute_client.compute_action.return_value = 'take_action_compute'
136136

137137
self.cmd = FakeNetworkAndComputeCommand(self.app, None)
@@ -201,9 +201,9 @@ def setUp(self):
201201
# Create client mocks. Note that we intentionally do not use specced
202202
# mocks since we want to test fake methods.
203203

204-
self.app.client_manager.network = mock.Mock()
205-
self.network_client = self.app.client_manager.network
206-
self.network_client.test_create_action = mock.Mock()
204+
self.app.client_manager.network = mock.Mock() # noqa: O401
205+
self.network_client = self.app.client_manager.network # noqa: O401
206+
self.network_client.test_create_action = mock.Mock() # noqa: O402
207207

208208
# Subclasses can override the command object to test.
209209
self.cmd = FakeCreateNeutronCommandWithExtraArgs(self.app, None)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ exclude = '''
732732
(?x)(
733733
doc
734734
| examples
735+
| hacking
735736
| releasenotes
736737
)
737738
'''

tox.ini

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,16 @@ commands =
113113
[flake8]
114114
show-source = true
115115
exclude = .venv,.git,.tox,dist,doc,*lib/python*,*egg,build,tools,releasenotes
116-
# We only enable the hacking (H) checks
117-
select = H
116+
# We only enable the hacking (H) and openstackclient (O) checks
117+
select = H,O
118118
# H301 Black will put commas after imports that can't fit on one line
119119
ignore = H301
120120
import-order-style = pep8
121121
application_import_names = openstackclient
122+
123+
[flake8:local-plugins]
124+
extension =
125+
O400 = checks:assert_no_oslo
126+
O401 = checks:assert_no_duplicated_setup
127+
O402 = checks:assert_use_of_client_aliases
128+
paths = ./hacking

0 commit comments

Comments
 (0)