#!/usr/bin/env 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 == 1 ]; then fail finished with error; exit 1; else pass "finished all";fi' EXIT SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" DIRNAME="${SRCDIR##*/}" BUILDPATH=$SRCDIR"/../build/testcc" COVERAGE_DIR=$SRCDIR"/../build/testcc-info" SOURCE_CODE=$SRCDIR"/.." HTML_RESULTS_DIR=$SRCDIR"/../build/testcc-html" COVERAGE_FILTER="*Qt*.framework* *Xcode.app* *.moc *moc_*.cpp */test/* *qrc_* ui_* */invsdk/* *qt.headers */build/* *main*.* *c++*" main() { setup testcc } function setup() { mkdir -p $BUILDPATH GIT=`git -C "$SRCDIR" describe --tags --always` GITSTATUS=`git status --porcelain` GITMOD="" if [ ! -z "$GITSTATUS" ]; then GITMOD="-modified" fi } function testcc() { info "Creating: $BUILDPATH" mkdir -p $BUILDPATH cd $BUILDPATH info "Generate makefile with coverage option" qmake $SOURCE_CODE/core-sdk.pro "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_CODE : $SOURCE_CODE" 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 open "${HTML_RESULTS_DIR}/index.html" & } # ################ # 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=1; echo -e "[${RED}FAIL${NC}] $@" } # Run main function main "$@"