server-sdk/scripts/codecoverage.sh
2019-11-06 13:37:28 +01:00

132 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# @see https://intoli.com/blog/exit-on-errors-in-bash-scripts/
set -e
trap 'LASTRES=$?; LAST=$BASH_COMMAND; if [[ "$LASTRES" -ne 0 ]]; then fail "Command: \"$LAST\" exited with exit code: $LASTRES"; elif [ "$FAIL" == "true" ]; then fail finished with error; exit 1; else pass "finished";fi' EXIT
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
DIRNAME="${SRCDIR##*/}"
SOURCE_DIR=$PWD
PRO_FILE=$(find . -d 1 -type f -name "*.pro")
BUILDPATH=$SOURCE_DIR"/build/testcc"
COVERAGE_DIR=$SOURCE_DIR"/build/testcc-info"
HTML_RESULTS_DIR=$SOURCE_DIR"/build/testcc-html"
COVERAGE_FILTER="*Qt*.framework* *Xcode.app* *.moc *moc_*.cpp */test/* *qrc_* ui_* *qt.headers */build/* *main*.* *c++*"
main() {
NAME=$( basename $PWD )
if [ "$NAME" != "server-sdk" ]; then
COVERAGE_FILTER+=" *server-sdk*"
fi
if [ ! -d src ]; then
fail "'src' dir does not exists. ${BLUE}Expecting 'src' dir with c++ source code${NC}"
else
pass "src dir exits"
fi
if [ ! -f $PRO_FILE ]; then
fail ".pro file does not exists: " $PRO_FILE
else
pass ".pro file: " $PRO_FILE
fi
info "Creating: $BUILDPATH"
mkdir -p $BUILDPATH
cd $BUILDPATH
info "Generate makefile with coverage option"
qmake $SOURCE_DIR/$PRO_FILE "QMAKE_CXXFLAGS += --coverage" "QMAKE_LFLAGS += --coverage"
info "Remove previous *.gcda files"
find $BUILDPATH -name *.gcda -exec rm "{}" \;
info "Build"
make -j4 # build
info "Run unittests"
make check test
echo "--------------------"
echo "Source : $SRCDIR"
echo "Build : $BUILDPATH"
echo "Makeflags : $MAKEFLAGS"
echo "COVERAGE_DIR : $COVERAGE_DIR"
echo "SOURCE_DIR : $SOURCE_DIR"
echo "HTML_RESULTS_DIR : $HTML_RESULTS_DIR"
echo "COVERAGE_FILTER : $COVERAGE_FILTER"
echo "--------------------"
#exit 0
info "Create dummy files to ensure not tested class are acounted"
find $BUILDPATH -name "*.gcno" -exec sh -c 'touch -a "${1%.gcno}.gcda"' _ {} \;
mkdir -p $COVERAGE_DIR
info "Make dir 'codecoverage' clean"
rm -f $COVERAGE_DIR/*.*
rm -rf $HTML_RESULTS_DIR
info "Copy .gc?? file from $BUILDPATH into 'codecoverage' dir"
find $BUILDPATH -name *.gc?? -exec cp "{}" $COVERAGE_DIR \;
##########################
# RUN LCOV
##########################
# ${1} is the directory containing the .gcno files (%{PROJBUILD} in Qt Creator)
LCOV=lcov
GENHTML=genhtml
BROWSER="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
info "Generating initial code coverage info report"
cd $BUILDPATH
${LCOV} -d ${BUILDPATH} -c -o ${COVERAGE_DIR}/coverage.info
info "Filtering code coverage info report"
${LCOV} -r ${COVERAGE_DIR}/coverage.info $COVERAGE_FILTER -o ${COVERAGE_DIR}/coverage-filtered.info
info "Creating html output"
mkdir -p ${HTML_RESULTS_DIR}
cd $BUILDPATH
"${GENHTML}" -o "${HTML_RESULTS_DIR}" "${COVERAGE_DIR}/coverage-filtered.info"
info "Reseting coverage counts"
"${LCOV}" -d "${COVERAGE_DIR}" -z
if [ "$1" == "open" ]; then
open "${HTML_RESULTS_DIR}/index.html" &
fi
}
# ################
# Output funcitons
# ################
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
function info () {
echo -e "[${BLUE} $@ ${NC}]"
}
function pass() {
echo -e "[${GREEN}PASS${NC}] $@"
}
function fail() {
FAIL="true"; echo -e "[${RED}FAIL${NC}] $@"
exit
}
# Run main function
main "$@"