|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -# based on http://stackoverflow.com/questions/21126011/is-it-possible-to-post-coverage-for-multiple-packages-to-coveralls |
4 |
| -# with script found at https://github.com/gopns/gopns/blob/master/test-coverage.sh |
| 3 | +PROFILE_OUT=`pwd`/profile.out |
| 4 | +ACC_OUT=`pwd`/acc.out |
5 | 5 |
|
6 |
| -echo "mode: set" > acc.out |
7 |
| -returnval=`go test -v -coverprofile=profile.out` |
8 |
| -echo ${returnval} |
9 |
| -if [[ ${returnval} != *FAIL* ]] |
10 |
| -then |
11 |
| - if [ -f profile.out ] |
12 |
| - then |
13 |
| - cat profile.out | grep -v "mode: set" >> acc.out |
14 |
| - fi |
15 |
| -else |
16 |
| - exit 1 |
17 |
| -fi |
| 6 | +testCover() { |
| 7 | + # set the return value to 0 (succesful) |
| 8 | + retval=0 |
| 9 | + # get the directory to check from the parameter. Default to '.' |
| 10 | + d=${1:-.} |
| 11 | + # skip if there are no Go files here |
| 12 | + ls $d/*.go &> /dev/null || return $retval |
| 13 | + # switch to the directory to check |
| 14 | + pushd $d > /dev/null |
| 15 | + # create the coverage profile |
| 16 | + coverageresult=`go test -v -coverprofile=$PROFILE_OUT` |
| 17 | + # output the result so we can check the shell output |
| 18 | + echo ${coverageresult} |
| 19 | + # append the results to acc.out if coverage didn't fail, else set the retval to 1 (failed) |
| 20 | + ( [[ ${coverageresult} == *FAIL* ]] && retval=1 ) || ( [ -f $PROFILE_OUT ] && grep -v "mode: set" $PROFILE_OUT >> $ACC_OUT ) |
| 21 | + # return to our working dir |
| 22 | + popd > /dev/null |
| 23 | + # return our return value |
| 24 | + return $retval |
| 25 | +} |
18 | 26 |
|
19 |
| -for Dir in $(find ./* -maxdepth 10 -type d ); |
20 |
| -do |
21 |
| - if ls $Dir/*.go &> /dev/null; |
22 |
| - then |
23 |
| - echo $Dir |
24 |
| - returnval=`go test -v -coverprofile=profile.out $Dir` |
25 |
| - echo ${returnval} |
26 |
| - if [[ ${returnval} != *FAIL* ]] |
27 |
| - then |
28 |
| - if [ -f profile.out ] |
29 |
| - then |
30 |
| - cat profile.out | grep -v "mode: set" >> acc.out |
31 |
| - fi |
32 |
| - else |
33 |
| - exit 1 |
34 |
| - fi |
35 |
| - fi |
36 |
| -done |
37 |
| -if [ -n "$COVERALLS_TOKEN" ] |
38 |
| -then |
39 |
| - $HOME/gopath/bin/goveralls -coverprofile=acc.out -service=travis-ci -repotoken $COVERALLS_TOKEN |
40 |
| -fi |
| 27 | +# Init acc.out |
| 28 | +echo "mode: set" > $ACC_OUT |
| 29 | + |
| 30 | +# Run test coverage on all directories containing go files |
| 31 | +find . -maxdepth 10 -type d | while read d; do testCover $d || exit; done |
| 32 | + |
| 33 | +# Upload the coverage profile to coveralls.io |
| 34 | +[ -n "$COVERALLS_TOKEN" ] && goveralls -coverprofile=$ACC_OUT -service=travis-ci -repotoken $COVERALLS_TOKEN |
41 | 35 |
|
42 |
| -rm -rf ./profile.out |
43 |
| -rm -rf ./acc.out |
0 commit comments