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: I65a40bbff17d92b959764846801d5e0ddd3b97ce
Signed-off-by: Moisés Guimarães de Medeiros <moguimar@redhat.com>
This commit is contained in:
Hervé Beraud 2020-07-21 13:29:40 +02:00
parent f97f99a0c4
commit 5e1011a630
5 changed files with 59 additions and 9 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.
import os
import sys

View File

@ -405,16 +405,16 @@ class GeneratorTestCase(base.BaseTestCase):
#
''' # noqa
'# Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod '
'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, '
'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo '
'consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse '
'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat '
'non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. '
'(string value)'
'# Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod ' # noqa
'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, ' # noqa
'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo ' # noqa
'consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse ' # noqa
'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat ' # noqa
'non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ' # noqa
'(string value)' # noqa
'''
#long_help = <None>
''')),
''')), # noqa
('long_help_with_preformatting',
dict(opts=[('test', [(None, [opts['long_help_pre']])])],
wrap_width=70,

View File

@ -25,3 +25,5 @@ requests_mock>=1.5.0 # Apache-2.0
# Bandit security code scanner
bandit>=1.6.0,<1.7.0 # Apache-2.0
pre-commit>=2.6.0 # MIT

View File

@ -31,7 +31,7 @@ deps =
[testenv:pep8]
commands =
flake8
pre-commit run -a
# TODO(stephenfin): Add mypy here once it's passing
{[testenv:bandit]commands}