Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang test all packages and combine cover profiles
#!/bin/bash
# https://gist.github.com/cdennison/cba009cf297aa2a92759278b5c578ae4
# This script tests multiple packages and creates a consolidated cover profile that's almost identical to coveralls/goveralls
# https://github.com/mattn/goveralls
#
# Inspired by
# https://github.com/getlantern/flashlight-build/blob/devel/testandcover.bash
function die() {
echo $*
exit 1
}
export GOPATH=`pwd`:$GOPATH
# Initialize profile.cov
echo "mode: count" > profile.cov
# Initialize error tracking
ERROR=""
# Test each package and append coverage profile info to profile.cov
for pkg in $(go list ./...)
do
echo "TESTING $pkg"
#$HOME/gopath/bin/
go test -v -covermode=count -coverprofile=profile_tmp.cov -coverpkg=./... $pkg || ERROR="Error testing $pkg"
tail -n +2 profile_tmp.cov >> profile.cov || die "Unable to append coverage for $pkg"
done
if [ ! -z "$ERROR" ]
then
die "Encountered error, last error was: $ERROR"
else
go tool cover -func=profile.cov
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment