From 8b0ccf7657b855e1d4156e39d45b9bb42d433094 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 20 Jul 2020 15:07:22 -0400 Subject: [PATCH] style: adding pre-commit check-style.sh --- .pre-commit-config.yaml | 11 ++++++++++- .travis.yml | 6 ++---- CONTRIBUTING.md | 9 ++++----- tools/check-style.sh | 38 ++++++-------------------------------- 4 files changed, 22 insertions(+), 42 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 63e4c6f7d..bc478a2b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,5 +24,14 @@ repos: rev: 3.8.2 hooks: - id: flake8 - additional_dependencies: [flake8-bugbear] + additional_dependencies: [flake8-bugbear, pep8-naming] exclude: ^(docs/.*|tools/.*)$ + +- repo: local + hooks: + - id: check-style + name: Classic check-style + language: system + types: + - c++ + entry: ./tools/check-style.sh diff --git a/.travis.yml b/.travis.yml index 12067bca8..30a9465a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ matrix: # also tests the automatic discovery functions in CMake (Python version, C++ standard). - os: linux dist: xenial # Necessary to run doxygen 1.8.15 - name: Style, docs, and pip + name: Docs and pip cache: false before_install: - pyenv global $(pyenv whence 2to3) # activate all python versions @@ -18,12 +18,10 @@ matrix: install: # 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 - - $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 - export PATH="$PWD/doxygen-1.8.15/bin:$PATH" script: - - tools/check-style.sh - - flake8 - $PY_CMD -m sphinx -W -b html docs docs/.build - | # Make sure setup.py distributes and installs all the headers diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 099791812..9dd3bd6f6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -27,11 +27,10 @@ adhere to the following rules to make the process as smooth as possible: do add value by themselves. * Add tests for any new functionality and run the test suite (``make pytest``) to ensure that no existing features break. -* Please run [``pre-commit``][pre-commit] and ``tools/check-style.sh`` to check - your code matches the project style. (Note that ``check-style.sh`` requires - ``gawk``.) Use `pre-commit run --all-files` before committing (or use - installed-mode, check pre-commit docs) to verify your code passes before - pushing to save time. +* Please run [``pre-commit``][pre-commit] to check your code matches the + project style. (Note that ``gawk`` is required.) Use `pre-commit run + --all-files` before committing (or use installed-mode, check pre-commit docs) + to verify your code passes before pushing to save time. * This project has a strong focus on providing general solutions using a minimal amount of code, thus small pull requests are greatly preferred. diff --git a/tools/check-style.sh b/tools/check-style.sh index 0a9f7d24f..f7af2a416 100755 --- a/tools/check-style.sh +++ b/tools/check-style.sh @@ -4,45 +4,19 @@ # # This script currently checks for # -# 1. use of tabs instead of spaces -# 2. MSDOS-style CRLF endings -# 3. trailing spaces -# 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 +# 1. missing space between keyword and parenthesis, e.g.: for(, if(, while( +# 2. Missing space between right parenthesis and brace, e.g. 'for (...){' +# 3. opening brace on its own line. It should always be on the same line as the # if/while/for/do statement. # -# Invoke as: tools/check-style.sh +# Invoke as: tools/check-style.sh # check_style_errors=0 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 -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)" +found="$(grep '\<\(if\|for\|while\|catch\)(\|){' $@ -rn --color=always)" if [ -n "$found" ]; then echo -e '\033[31;01mError: found the following coding style problems:\033[0m' check_style_errors=1 @@ -60,7 +34,7 @@ last && /^\s*{/ { last="" } { 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 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'