Adding pre-commit

Introduced changes:
- pre-commit config and rules
- Add pre-commit to pep8 gate, Flake8 is covered in the pre-commit hooks.
- Applying fixes for pre-commit compliance in all code.

Also commit hash will be used instead of version tags in pre-commit to
prevend arbitrary code from running in developer's machines.

pre-commit will be used to:
- trailing whitespace;
- Replaces or checks mixed line ending (mixed-line-ending);
- Forbid files which have a UTF-8 byte-order marker (check-byte-order-marker);
- Checks that non-binary executables have a proper
  shebang (check-executables-have-shebangs);
- Check for files that contain merge conflict strings (check-merge-conflict);
- Check for debugger imports and py37+ breakpoint()
  calls in python source (debug-statements);
- Attempts to load all yaml files to verify syntax (check-yaml);
- Run flake8 checks (flake8) (local)

For further details about tests please refer to:
https://github.com/pre-commit/pre-commit-hooks

Change-Id: Ibd0c3d64fdc5c293d9d676d33eab828d9fde971f
Co-authored-by: Moisés Guimarães de Medeiros <moguimar@redhat.com>
This commit is contained in:
Hervé Beraud 2020-08-21 15:12:08 +02:00
parent 4bae96d7f7
commit a997f09e5b
12 changed files with 71 additions and 18 deletions

35
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,35 @@
# We from the Oslo project decided to pin repos based on the
# commit hash instead of the version tag to prevend arbitrary
# code from running in developer's machines. To update to a
# newer version, run `pre-commit autoupdate` and then replace
# the newer versions with their commit hash.
default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: ebc15addedad713c86ef18ae9632c88e187dd0af # v3.1.0
hooks:
- id: trailing-whitespace
# Replaces or checks mixed line ending
- id: mixed-line-ending
args: ['--fix', 'lf']
exclude: '.*\.(svg)$'
# Forbid files which have a UTF-8 byte-order marker
- id: check-byte-order-marker
# Checks that non-binary executables have a proper shebang
- id: check-executables-have-shebangs
# Check for files that contain merge conflict strings.
- id: check-merge-conflict
# Check for debugger imports and py37+ breakpoint()
# calls in python source
- id: debug-statements
- id: check-yaml
files: .*\.(yaml|yml)$
- repo: https://gitlab.com/pycqa/flake8
rev: 181bb46098dddf7e2d45319ea654b4b4d58c2840 # 3.8.3
hooks:
- id: flake8
additional_dependencies:
- hacking>=3.0.1,<3.1.0

View File

@ -1,4 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Configuration file for the Sphinx documentation builder.
#

View File

@ -16,4 +16,4 @@ Reference
notification_driver
notification_listener
serializer
exceptions
exceptions

View File

@ -1208,8 +1208,8 @@ class TestLinkRecovery(_AmqpBrokerTestCase):
else:
# unblock all link when RPC call is made
link.add_capacity(10)
for l in self._blocked_links:
l.add_capacity(10)
for li in self._blocked_links:
li.add_capacity(10)
self._broker.on_receiver_active = _on_active
self._broker.on_credit_exhausted = lambda link: None
@ -1293,15 +1293,15 @@ class TestAddressing(test_utils.BaseTestCase):
{"msg": topic}, 2.0)
expected.append(topic)
for l in rl:
l.join(timeout=30)
for li in rl:
li.join(timeout=30)
# anycast will not evenly distribute an odd number of msgs
predicate = lambda: len(expected) == (nl[0].messages.qsize() +
nl[1].messages.qsize())
_wait_until(predicate, 30)
for l in nl:
l.kill(timeout=30)
for li in nl:
li.kill(timeout=30)
s1_payload = [m.message.get('msg') for m in rl[0].get_messages()]
s2_payload = [m.message.get('msg') for m in rl[1].get_messages()]

View File

@ -280,13 +280,13 @@ class IsValidDistributionOf(object):
def match(self, actual):
errors = InvalidDistribution(self.original, actual)
received = [[i for i in l] for l in actual]
received = [[idx for idx in act] for act in actual]
def _remove(obj, lists):
for l in lists:
if obj in l:
front = l[0]
l.remove(obj)
for li in lists:
if obj in li:
front = li[0]
li.remove(obj)
return front
return None
@ -296,8 +296,8 @@ class IsValidDistributionOf(object):
errors.missing += item
elif item != o:
errors.wrong_order.append([item, o])
for l in received:
errors.extra += l
for li in received:
errors.extra += li
return errors or None

1
oslo_messaging/tests/notify/test_notifier.py Executable file → Normal file
View File

@ -1,4 +1,3 @@
# Copyright 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may

0
oslo_messaging/tests/rpc/test_client.py Executable file → Normal file
View File

0
oslo_messaging/tests/rpc/test_dispatcher.py Executable file → Normal file
View File

0
oslo_messaging/tests/test_transport.py Executable file → Normal file
View File

View File

@ -7,6 +7,7 @@ hacking>=3.0.1,<3.1.0 # Apache-2.0
fixtures>=3.0.0 # Apache-2.0/BSD
stestr>=2.0.0 # Apache-2.0
pre-commit>=2.6.0 # MIT
testscenarios>=0.4 # Apache-2.0/BSD
testtools>=2.2.0 # MIT
oslotest>=3.2.0 # Apache-2.0

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
@ -10,8 +11,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import eventlet # noqa
eventlet.monkey_patch() # noqa
try:
# Avoid https://github.com/PyCQA/pycodestyle/issues/472
import eventlet
eventlet.monkey_patch()
except ImportError:
raise
import argparse
import bisect

View File

@ -19,7 +19,7 @@ commands = stestr run --slowest {posargs}
[testenv:pep8]
commands =
flake8
pre-commit run -a
# run security linter
bandit -r oslo_messaging -x tests -n5