Skip to content

Commit

Permalink
ci: check examples code style
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
igrr committed Mar 8, 2018
1 parent af74a10 commit e226251
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
20 changes: 19 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ language: bash
os: linux
dist: trusty

cache:
directories:
- $HOME/astyle

matrix:
include:
- env:
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

44 changes: 44 additions & 0 deletions tests/examples_style.conf
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e226251

Please sign in to comment.