Skip to content

Instantly share code, notes, and snippets.

@mozillazg
Last active May 1, 2016
Embed
What would you like to do?
Generate test coverage statistics for Go packages. https://mlafeldt.github.io/blog/test-coverage-in-go/
!/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
@mozillazg
Copy link
Author

mozillazg commented May 1, 2016

$ go_coverage
ok      github.com/mozillazg/go-pinyin  0.018s  coverage: 100.0% of statements
?       github.com/mozillazg/go-pinyin/pinyin   [no test files]
?       github.com/mozillazg/go-pinyin/tools    [no test files]
github.com/mozillazg/go-pinyin/pinyin.go:96:    NewArgs     100.0%
github.com/mozillazg/go-pinyin/pinyin.go:101:   initial     100.0%
github.com/mozillazg/go-pinyin/pinyin.go:113:   final       100.0%
github.com/mozillazg/go-pinyin/pinyin.go:132:   handleYW    100.0%
github.com/mozillazg/go-pinyin/pinyin.go:148:   toFixed     100.0%
github.com/mozillazg/go-pinyin/pinyin.go:190:   applyStyle  100.0%
github.com/mozillazg/go-pinyin/pinyin.go:199:   SinglePinyin    100.0%
github.com/mozillazg/go-pinyin/pinyin.go:221:   Pinyin      100.0%
github.com/mozillazg/go-pinyin/pinyin.go:235:   LazyPinyin  100.0%
github.com/mozillazg/go-pinyin/pinyin.go:245:   Slug        100.0%
total:                      (statements)    100.0%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment