Last active
May 1, 2016 03:57
-
-
Save mozillazg/9aa5bcdbb4d6602f761e541ba335a80d to your computer and use it in GitHub Desktop.
Generate test coverage statistics for Go packages. https://mlafeldt.github.io/blog/test-coverage-in-go/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/bin/sh | |
# Generate test coverage statistics for Go packages. | |
# | |
# Works around the fact that `go test -coverprofile` currently does not work | |
# with multiple packages, see https://code.google.com/p/go/issues/detail?id=6909 | |
# | |
# Usage: go_coverage [ [--html | html] | --coveralls | --help ] | |
# | |
# --html Additionally create HTML report and open it in browser | |
# --coveralls Push coverage statistics to coveralls.io | |
# | |
set -e | |
workdir=.cover | |
profile="$workdir/cover.out" | |
mode=count | |
generate_cover_data() { | |
rm -rf "$workdir" | |
mkdir "$workdir" | |
for pkg in "$@"; do | |
f="$workdir/$(echo $pkg | tr / -).cover" | |
go test -covermode="$mode" -coverprofile="$f" "$pkg" | |
done | |
echo "mode: $mode" >"$profile" | |
grep -h -v "^mode:" "$workdir"/*.cover >>"$profile" | |
} | |
show_cover_report() { | |
go tool cover -${1}="$profile" | |
} | |
push_to_coveralls() { | |
echo "Pushing coverage statistics to coveralls.io" | |
goveralls -coverprofile="$profile" | |
} | |
job() { | |
generate_cover_data $(go list ./...) | |
show_cover_report func | |
} | |
case "$1" in | |
--help) | |
echo "go_coverage [ [--html | html] | --coveralls | --help ]" ;; | |
"") | |
job ;; | |
--html | html) | |
job | |
show_cover_report html ;; | |
--coveralls) | |
job | |
push_to_coveralls ;; | |
*) | |
echo >&2 "error: invalid option: $1"; exit 1 ;; | |
esac |
Author
mozillazg
commented
May 1, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment