|
| 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 | + ) |
0 commit comments