...
1#!/bin/bash
2
3MODE=set
4PROFILE_OUT="${PWD}/profile.out"
5ACC_OUT="${PWD}/coverage.txt"
6
7testCover() {
8 # set the return value to 0 (successful)
9 retval=0
10 # get the directory to check from the parameter. Default to '.'
11 d=${1:-.}
12 # skip if there are no Go files here
13 ls $d/*.go &> /dev/null || return $retval
14 # switch to the directory to check
15 pushd $d > /dev/null
16 # create the coverage profile
17 coverageresult=$(go test $TAGS -coverprofile="${PROFILE_OUT}" -covermode=${MODE})
18 # output the result so we can check the shell output
19 echo ${coverageresult}
20 # append the results to acc.out if coverage didn't fail, else set the retval to 1 (failed)
21 ( [[ ${coverageresult} == *FAIL* ]] && retval=1 ) || ( [ -f "${PROFILE_OUT}" ] && grep -v "mode: ${MODE}" "${PROFILE_OUT}" >> "${ACC_OUT}" )
22 # return to our working dir
23 popd > /dev/null
24 # return our return value
25 return $retval
26}
27
28# Init coverage.txt
29echo "mode: ${MODE}" > $ACC_OUT
30
31# Run test coverage on all directories containing go files.
32find . -type d | while read d; do testCover $d || exit; done
View as plain text