Skip to content

Commit 071ecee

Browse files
committed
Merge pull request #1716 from esp8266/host_tests
Initial batch of host testing mocks and some filesystem tests - CATCH unit testing framework - some mocks - filesystem tests - test coverage calculation (gcov) - codecov.io integration - add tools/build.py — a wrapper around arduino-builder - use arduino-builder instead of full IDE to build sketches (reduces build time from 23 minutes to 16 minutes)
2 parents 1c9f9c3 + ec99268 commit 071ecee

13 files changed

+10409
-7
lines changed

.travis.yml

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ language: bash
33
os:
44
- linux
55

6+
addons:
7+
apt:
8+
sources:
9+
- ubuntu-toolchain-r-test
10+
packages:
11+
- g++-4.8
12+
613
script:
14+
- set -e
15+
- export CXX="g++-4.8" CC="gcc-4.8" GCOV="gcov-4.8"
16+
- pushd $TRAVIS_BUILD_DIR/tests/host
17+
- make
18+
- make clean-objects
19+
- popd
720
- wget -O arduino.tar.xz https://www.arduino.cc/download.php?f=/arduino-nightly-linux64.tar.xz
821
- tar xf arduino.tar.xz
922
- mv arduino-nightly $HOME/arduino_ide
@@ -20,10 +33,12 @@ script:
2033
- which arduino
2134
- cd $TRAVIS_BUILD_DIR
2235
- source tests/common.sh
23-
- arduino --board esp8266com:esp8266:generic --save-prefs
24-
- arduino --get-pref sketchbook.path
2536
- install_libraries
26-
- build_sketches arduino $TRAVIS_BUILD_DIR
37+
- build_sketches $HOME/arduino_ide $TRAVIS_BUILD_DIR "python tools/build.py -l $HOME/Arduino/libraries -b generic -v"
38+
39+
after_success:
40+
- pushd $TRAVIS_BUILD_DIR/tests/host
41+
- bash <(curl -s https://codecov.io/bash) -X gcov
2742

2843
notifications:
2944
email:

tests/common.sh

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,42 @@ function build_sketches()
44
{
55
local arduino=$1
66
local srcpath=$2
7+
local build_cmd=$3
8+
echo $build_cmd
79
local sketches=$(find $srcpath -name *.ino)
10+
export ARDUINO_IDE_PATH=$arduino
811
for sketch in $sketches; do
912
local sketchdir=$(dirname $sketch)
13+
local sketchdirname=$(basename $sketchdir)
14+
local sketchname=$(basename $sketch)
15+
if [[ "${sketchdirname}.ino" != "$sketchname" ]]; then
16+
echo "Skipping $sketch, beacause it is not the main sketch file";
17+
continue
18+
fi;
1019
if [[ -f "$sketchdir/.test.skip" ]]; then
11-
echo -e "\n\n ------------ Skipping $sketch ------------ \n\n";
20+
echo -e "\n ------------ Skipping $sketch ------------ \n";
1221
continue
1322
fi
14-
echo -e "\n\n ------------ Building $sketch ------------ \n\n";
15-
$arduino --verify $sketch;
23+
echo -e "\n ------------ Building $sketch ------------ \n";
24+
# $arduino --verify $sketch;
25+
echo "$build_cmd $sketch"
26+
time ($build_cmd $sketch >build.log)
1627
local result=$?
1728
if [ $result -ne 0 ]; then
1829
echo "Build failed ($1)"
30+
echo "Build log:"
31+
cat build.log
1932
return $result
2033
fi
34+
rm build.log
2135
done
2236
}
2337

2438
function install_libraries()
2539
{
2640
mkdir -p $HOME/Arduino/libraries
2741
pushd $HOME/Arduino/libraries
28-
42+
2943
# install ArduinoJson library
3044
wget https://github.com/bblanchon/ArduinoJson/releases/download/v4.6.1/ArduinoJson-v4.6.1.zip && unzip ArduinoJson-v4.6.1.zip
3145

tests/host/Makefile

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
OBJECT_DIRECTORY := obj
2+
BINARY_DIRECTORY := bin
3+
OUTPUT_BINARY := $(BINARY_DIRECTORY)/host_tests
4+
CORE_PATH := ../../cores/esp8266
5+
6+
# I wasn't able to build with clang when -coverage flag is enabled, forcing GCC on OS X
7+
ifeq ($(shell uname -s),Darwin)
8+
CC := gcc
9+
CXX := g++
10+
endif
11+
GCOV ?= gcov
12+
13+
CORE_CPP_FILES := $(addprefix $(CORE_PATH)/,\
14+
StreamString.cpp \
15+
Stream.cpp \
16+
WString.cpp \
17+
Print.cpp \
18+
FS.cpp \
19+
spiffs_api.cpp \
20+
)
21+
22+
CORE_C_FILES := $(addprefix $(CORE_PATH)/,\
23+
core_esp8266_noniso.c \
24+
spiffs/spiffs_cache.c \
25+
spiffs/spiffs_check.c \
26+
spiffs/spiffs_gc.c \
27+
spiffs/spiffs_hydrogen.c \
28+
spiffs/spiffs_nucleus.c \
29+
)
30+
31+
MOCK_CPP_FILES := $(addprefix common/,\
32+
Arduino.cpp \
33+
spiffs_mock.cpp \
34+
WMath.cpp \
35+
)
36+
37+
INC_PATHS += $(addprefix -I, \
38+
common \
39+
$(CORE_PATH) \
40+
)
41+
42+
TEST_CPP_FILES := \
43+
fs/test_fs.cpp \
44+
45+
CXXFLAGS += -std=c++11 -Wall -coverage -O0
46+
CFLAGS += -std=c99 -Wall -coverage -O0
47+
LDFLAGS += -coverage -O0
48+
49+
remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))
50+
51+
C_SOURCE_FILES = $(CORE_C_FILES)
52+
CPP_SOURCE_FILES = $(MOCK_CPP_FILES) $(CORE_CPP_FILES) $(TEST_CPP_FILES)
53+
C_OBJECTS = $(C_SOURCE_FILES:.c=.c.o)
54+
55+
CPP_OBJECTS = $(CPP_SOURCE_FILES:.cpp=.cpp.o)
56+
57+
OBJECTS = $(C_OBJECTS) $(CPP_OBJECTS)
58+
COVERAGE_FILES = $(OBJECTS:.o=.gc*)
59+
60+
all: build-info $(OUTPUT_BINARY) test gcov
61+
62+
test: $(OUTPUT_BINARY)
63+
$(OUTPUT_BINARY)
64+
65+
clean: clean-objects clean-coverage
66+
rm -rf $(BINARY_DIRECTORY)
67+
68+
clean-objects:
69+
rm -rf $(OBJECTS)
70+
71+
clean-coverage:
72+
rm -rf $(COVERAGE_FILES) *.gcov
73+
74+
gcov: test
75+
find $(CORE_PATH) -name "*.gcno" -exec $(GCOV) -r -pb {} +
76+
77+
build-info:
78+
echo "-------- build tools info --------"
79+
echo "CC: " $(CC)
80+
$(CC) -v
81+
echo "CXX: " $(CXX)
82+
$(CXX) -v
83+
echo "GCOV: " $(GCOV)
84+
$(GCOV) -v
85+
echo "----------------------------------"
86+
87+
$(BINARY_DIRECTORY):
88+
mkdir -p $@
89+
90+
$(C_OBJECTS): %.c.o: %.c
91+
$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $<
92+
93+
$(CPP_OBJECTS): %.cpp.o: %.cpp
94+
$(CXX) $(CXXFLAGS) $(INC_PATHS) -c -o $@ $<
95+
96+
$(OUTPUT_BINARY): $(BINARY_DIRECTORY) $(OBJECTS)
97+
$(CXX) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY)

tests/host/common/Arduino.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Arduino.cpp - Mocks for common Arduino APIs
3+
Copyright © 2016 Ivan Grokhotkov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
*/
15+
16+
#define CATCH_CONFIG_MAIN
17+
#include <catch.hpp>
18+
#include <sys/time.h>
19+
#include "Arduino.h"
20+
21+
22+
extern "C" unsigned long millis()
23+
{
24+
timeval time;
25+
gettimeofday(&time, NULL);
26+
return (time.tv_sec * 1000) + (time.tv_usec / 1000);
27+
}
28+
29+
30+
extern "C" void yield()
31+
{
32+
}
33+
34+
35+
extern "C" void __panic_func(const char* file, int line, const char* func) {
36+
abort();
37+
}

0 commit comments

Comments
 (0)