style: adding pre-commit check-style.sh

This commit is contained in:
Henry Schreiner 2020-07-20 15:07:22 -04:00 committed by Henry Schreiner
parent b16347e51d
commit 8b0ccf7657
4 changed files with 22 additions and 42 deletions

View File

@ -24,5 +24,14 @@ repos:
rev: 3.8.2 rev: 3.8.2
hooks: hooks:
- id: flake8 - id: flake8
additional_dependencies: [flake8-bugbear] additional_dependencies: [flake8-bugbear, pep8-naming]
exclude: ^(docs/.*|tools/.*)$ exclude: ^(docs/.*|tools/.*)$
- repo: local
hooks:
- id: check-style
name: Classic check-style
language: system
types:
- c++
entry: ./tools/check-style.sh

View File

@ -9,7 +9,7 @@ matrix:
# also tests the automatic discovery functions in CMake (Python version, C++ standard). # also tests the automatic discovery functions in CMake (Python version, C++ standard).
- os: linux - os: linux
dist: xenial # Necessary to run doxygen 1.8.15 dist: xenial # Necessary to run doxygen 1.8.15
name: Style, docs, and pip name: Docs and pip
cache: false cache: false
before_install: before_install:
- pyenv global $(pyenv whence 2to3) # activate all python versions - pyenv global $(pyenv whence 2to3) # activate all python versions
@ -18,12 +18,10 @@ matrix:
install: install:
# breathe 4.14 doesn't work with bit fields. See https://github.com/michaeljones/breathe/issues/462 # breathe 4.14 doesn't work with bit fields. See https://github.com/michaeljones/breathe/issues/462
# Latest breathe + Sphinx causes warnings and errors out # Latest breathe + Sphinx causes warnings and errors out
- $PY_CMD -m pip install --user --upgrade "sphinx<3" sphinx_rtd_theme breathe==4.13.1 flake8 pep8-naming pytest - $PY_CMD -m pip install --user --upgrade "sphinx<3" sphinx_rtd_theme breathe==4.13.1 pytest
- curl -fsSL https://sourceforge.net/projects/doxygen/files/rel-1.8.15/doxygen-1.8.15.linux.bin.tar.gz/download | tar xz - curl -fsSL https://sourceforge.net/projects/doxygen/files/rel-1.8.15/doxygen-1.8.15.linux.bin.tar.gz/download | tar xz
- export PATH="$PWD/doxygen-1.8.15/bin:$PATH" - export PATH="$PWD/doxygen-1.8.15/bin:$PATH"
script: script:
- tools/check-style.sh
- flake8
- $PY_CMD -m sphinx -W -b html docs docs/.build - $PY_CMD -m sphinx -W -b html docs docs/.build
- | - |
# Make sure setup.py distributes and installs all the headers # Make sure setup.py distributes and installs all the headers

View File

@ -27,11 +27,10 @@ adhere to the following rules to make the process as smooth as possible:
do add value by themselves. do add value by themselves.
* Add tests for any new functionality and run the test suite (``make pytest``) * Add tests for any new functionality and run the test suite (``make pytest``)
to ensure that no existing features break. to ensure that no existing features break.
* Please run [``pre-commit``][pre-commit] and ``tools/check-style.sh`` to check * Please run [``pre-commit``][pre-commit] to check your code matches the
your code matches the project style. (Note that ``check-style.sh`` requires project style. (Note that ``gawk`` is required.) Use `pre-commit run
``gawk``.) Use `pre-commit run --all-files` before committing (or use --all-files` before committing (or use installed-mode, check pre-commit docs)
installed-mode, check pre-commit docs) to verify your code passes before to verify your code passes before pushing to save time.
pushing to save time.
* This project has a strong focus on providing general solutions using a * This project has a strong focus on providing general solutions using a
minimal amount of code, thus small pull requests are greatly preferred. minimal amount of code, thus small pull requests are greatly preferred.

View File

@ -4,45 +4,19 @@
# #
# This script currently checks for # This script currently checks for
# #
# 1. use of tabs instead of spaces # 1. missing space between keyword and parenthesis, e.g.: for(, if(, while(
# 2. MSDOS-style CRLF endings # 2. Missing space between right parenthesis and brace, e.g. 'for (...){'
# 3. trailing spaces # 3. opening brace on its own line. It should always be on the same line as the
# 4. missing space between keyword and parenthesis, e.g.: for(, if(, while(
# 5. Missing space between right parenthesis and brace, e.g. 'for (...){'
# 6. opening brace on its own line. It should always be on the same line as the
# if/while/for/do statement. # if/while/for/do statement.
# #
# Invoke as: tools/check-style.sh # Invoke as: tools/check-style.sh <filenames>
# #
check_style_errors=0 check_style_errors=0
IFS=$'\n' IFS=$'\n'
found="$( GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
if [ -n "$found" ]; then
# The mt=41 sets a red background for matched tabs:
echo -e '\033[31;01mError: found tab characters in the following files:\033[0m'
check_style_errors=1
echo "$found" | sed -e 's/^/ /'
fi
found="$(grep '\<\(if\|for\|while\|catch\)(\|){' $@ -rn --color=always)"
found="$( grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always )"
if [ -n "$found" ]; then
echo -e '\033[31;01mError: found CRLF characters in the following files:\033[0m'
check_style_errors=1
echo "$found" | sed -e 's/^/ /'
fi
found="$(GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
if [ -n "$found" ]; then
# The mt=41 sets a red background for matched trailing spaces
echo -e '\033[31;01mError: found trailing spaces in the following files:\033[0m'
check_style_errors=1
echo "$found" | sed -e 's/^/ /'
fi
found="$(grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,h} -rn --color=always)"
if [ -n "$found" ]; then if [ -n "$found" ]; then
echo -e '\033[31;01mError: found the following coding style problems:\033[0m' echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
check_style_errors=1 check_style_errors=1
@ -60,7 +34,7 @@ last && /^\s*{/ {
last="" last=""
} }
{ last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" } { last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" }
' $(find include -type f) tests/*.{cpp,h} docs/*.rst)" ' $(find include -type f) $@)"
if [ -n "$found" ]; then if [ -n "$found" ]; then
check_style_errors=1 check_style_errors=1
echo -e '\033[31;01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files:\033[0m' echo -e '\033[31;01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files:\033[0m'