From e226251b2776503caa4396fe07bc974a474bdd48 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 19 Feb 2018 18:29:34 +0300 Subject: [PATCH] ci: check examples code style Use code style defined in Arduino project to check code style of the examples. The check is done by formatting all files with astyle and checking whether any changes have been introduced. --- .travis.yml | 20 +++++++++++++++++- tests/common.sh | 28 +++++++++++++++++++++++++ tests/examples_style.conf | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/examples_style.conf diff --git a/.travis.yml b/.travis.yml index feb90d184..b3c39d2ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,10 @@ language: bash os: linux dist: trusty +cache: + directories: + - $HOME/astyle + matrix: include: - env: @@ -15,9 +19,23 @@ matrix: - BUILD_TYPE=package - env: - BUILD_TYPE=host_tests + - env: + - BUILD_TYPE=style_check install: - - pip install --user -r doc/requirements.txt + - > + [ "$BUILD_TYPE" = "docs" ] && { + pip install --user -r doc/requirements.txt; + } || true + - > + [ "$BUILD_TYPE" = "style_check" ] && { + [ -f $HOME/astyle/build/gcc/bin/astyle ] || { + wget -O astyle_3.1_linux.tar.gz https://sourceforge.net/projects/astyle/files/astyle/astyle%203.1/astyle_3.1_linux.tar.gz/download; + tar -xf astyle_3.1_linux.tar.gz -C $HOME; + make -C $HOME/astyle/build/gcc; + } + make -C $HOME/astyle/build/gcc prefix=$HOME install; + } || true script: - $TRAVIS_BUILD_DIR/tests/common.sh diff --git a/tests/common.sh b/tests/common.sh index 81f142e07..e76f24300 100755 --- a/tests/common.sh +++ b/tests/common.sh @@ -213,8 +213,30 @@ function build_sketches_with_arduino() echo -e "travis_fold:end:size_report" } +function check_examples_style() +{ + echo -e "travis_fold:start:check_examples_style" + + find $TRAVIS_BUILD_DIR/libraries -name '*.ino' -exec \ + astyle \ + --suffix=none \ + --options=$TRAVIS_BUILD_DIR/tests/examples_style.conf {} \; + + git diff --exit-code -- $TRAVIS_BUILD_DIR/libraries + + echo -e "travis_fold:end:check_examples_style" +} + set -e +if [ -z "$TRAVIS_BUILD_DIR" ]; then + echo "TRAVIS_BUILD_DIR is not set, trying to guess:" + pushd $(dirname $0)/../ > /dev/null + TRAVIS_BUILD_DIR=$PWD + popd > /dev/null + echo "TRAVIS_BUILD_DIR=$TRAVIS_BUILD_DIR" +fi + if [ "$BUILD_TYPE" = "build" ]; then install_arduino build_sketches_with_arduino @@ -236,5 +258,11 @@ elif [ "$BUILD_TYPE" = "host_tests" ]; then # Run host side tests cd $TRAVIS_BUILD_DIR/tests run_host_tests +elif [ "$BUILD_TYPE" = "style_check" ]; then + # Check code style + check_examples_style +else + echo "BUILD_TYPE not set" + exit 1 fi diff --git a/tests/examples_style.conf b/tests/examples_style.conf new file mode 100644 index 000000000..0ca991bf8 --- /dev/null +++ b/tests/examples_style.conf @@ -0,0 +1,44 @@ +# Code formatting rules for Arduino examples, taken from: +# +# https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf +# + +mode=c +lineend=linux + +# 2 spaces indentation +indent=spaces=2 + +# also indent macros +indent-preprocessor + +# indent classes, switches (and cases), comments starting at column 1 +indent-classes +indent-switches +indent-cases +indent-col1-comments + +# put a space around operators +pad-oper + +# put a space after if/for/while +pad-header + +# if you like one-liners, keep them +keep-one-line-statements +add-braces + +style=java +attach-namespaces +attach-classes +attach-inlines +attach-extern-c +indent-modifiers +indent-namespaces +indent-labels +indent-preproc-block +indent-preproc-define +indent-preproc-cond +unpad-paren +add-braces +remove-comment-prefix \ No newline at end of file