Skip to content

Commit e226251

Browse files
committed
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.
1 parent af74a10 commit e226251

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

.travis.yml

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ language: bash
33
os: linux
44
dist: trusty
55

6+
cache:
7+
directories:
8+
- $HOME/astyle
9+
610
matrix:
711
include:
812
- env:
@@ -15,9 +19,23 @@ matrix:
1519
- BUILD_TYPE=package
1620
- env:
1721
- BUILD_TYPE=host_tests
22+
- env:
23+
- BUILD_TYPE=style_check
1824

1925
install:
20-
- pip install --user -r doc/requirements.txt
26+
- >
27+
[ "$BUILD_TYPE" = "docs" ] && {
28+
pip install --user -r doc/requirements.txt;
29+
} || true
30+
- >
31+
[ "$BUILD_TYPE" = "style_check" ] && {
32+
[ -f $HOME/astyle/build/gcc/bin/astyle ] || {
33+
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;
34+
tar -xf astyle_3.1_linux.tar.gz -C $HOME;
35+
make -C $HOME/astyle/build/gcc;
36+
}
37+
make -C $HOME/astyle/build/gcc prefix=$HOME install;
38+
} || true
2139
2240
script:
2341
- $TRAVIS_BUILD_DIR/tests/common.sh

tests/common.sh

+28
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,30 @@ function build_sketches_with_arduino()
213213
echo -e "travis_fold:end:size_report"
214214
}
215215

216+
function check_examples_style()
217+
{
218+
echo -e "travis_fold:start:check_examples_style"
219+
220+
find $TRAVIS_BUILD_DIR/libraries -name '*.ino' -exec \
221+
astyle \
222+
--suffix=none \
223+
--options=$TRAVIS_BUILD_DIR/tests/examples_style.conf {} \;
224+
225+
git diff --exit-code -- $TRAVIS_BUILD_DIR/libraries
226+
227+
echo -e "travis_fold:end:check_examples_style"
228+
}
229+
216230
set -e
217231

232+
if [ -z "$TRAVIS_BUILD_DIR" ]; then
233+
echo "TRAVIS_BUILD_DIR is not set, trying to guess:"
234+
pushd $(dirname $0)/../ > /dev/null
235+
TRAVIS_BUILD_DIR=$PWD
236+
popd > /dev/null
237+
echo "TRAVIS_BUILD_DIR=$TRAVIS_BUILD_DIR"
238+
fi
239+
218240
if [ "$BUILD_TYPE" = "build" ]; then
219241
install_arduino
220242
build_sketches_with_arduino
@@ -236,5 +258,11 @@ elif [ "$BUILD_TYPE" = "host_tests" ]; then
236258
# Run host side tests
237259
cd $TRAVIS_BUILD_DIR/tests
238260
run_host_tests
261+
elif [ "$BUILD_TYPE" = "style_check" ]; then
262+
# Check code style
263+
check_examples_style
264+
else
265+
echo "BUILD_TYPE not set"
266+
exit 1
239267
fi
240268

tests/examples_style.conf

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Code formatting rules for Arduino examples, taken from:
2+
#
3+
# https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf
4+
#
5+
6+
mode=c
7+
lineend=linux
8+
9+
# 2 spaces indentation
10+
indent=spaces=2
11+
12+
# also indent macros
13+
indent-preprocessor
14+
15+
# indent classes, switches (and cases), comments starting at column 1
16+
indent-classes
17+
indent-switches
18+
indent-cases
19+
indent-col1-comments
20+
21+
# put a space around operators
22+
pad-oper
23+
24+
# put a space after if/for/while
25+
pad-header
26+
27+
# if you like one-liners, keep them
28+
keep-one-line-statements
29+
add-braces
30+
31+
style=java
32+
attach-namespaces
33+
attach-classes
34+
attach-inlines
35+
attach-extern-c
36+
indent-modifiers
37+
indent-namespaces
38+
indent-labels
39+
indent-preproc-block
40+
indent-preproc-define
41+
indent-preproc-cond
42+
unpad-paren
43+
add-braces
44+
remove-comment-prefix

0 commit comments

Comments
 (0)