gopkg.in/frapposelli/wwhrd.v0@v0.2.1/hack/coverage (about) 1 #!/bin/bash 2 # Generate test coverage statistics for Go packages. 3 # 4 # Works around the fact that `go test -coverprofile` does not work 5 # with multiple packages, see https://code.google.com/p/go/issues/detail?id=6909 6 # 7 # Usage: script/coverage [--html] 8 # 9 # --html Create HTML report and open it in browser 10 # 11 12 set -e 13 14 workdir=.cover 15 profile="$workdir/cover.out" 16 mode=atomic 17 dir=$(dirname $0) 18 19 # list any files (or patterns) to explicitly exclude from coverage 20 # you should have a pretty good reason before putting items here 21 exclude_files=( 22 "wwhrd.go" 23 ) 24 25 join() { local IFS="$1"; shift; echo "$*"; } 26 27 excludes=$(join "|" ${exclude_files[@]} | sed -e 's/\./\\./g') 28 29 generate_cover_data() { 30 rm -rf "$workdir" 31 mkdir "$workdir" 32 33 for pkg in "$@"; do 34 f="$workdir/$(echo $pkg | tr / -).cover" 35 go test -race -v -covermode="$mode" -coverprofile="$f" "$pkg" 36 done 37 38 echo "mode: $mode" >"$profile" 39 grep -h -v "^mode:" "$workdir"/*.cover | egrep -v "$excludes" >>"$profile" 40 } 41 42 show_cover_report() { 43 go tool cover -${1}="$profile" 44 } 45 46 generate_cover_data $(go list ./... | grep -v /vendor/) 47 show_cover_report func 48 case "$1" in 49 "") 50 ;; 51 --html) 52 show_cover_report html ;; 53 *) 54 echo >&2 "error: invalid option: $1"; exit 1 ;; 55 esac