github.com/nsqio/nsq@v1.3.0/coverage.sh (about) 1 #!/bin/bash 2 # Generate test coverage statistics for Go packages. 3 # 4 # Works around the fact that `go test -coverprofile` currently does not work 5 # with multiple packages, see https://code.google.com/p/go/issues/detail?id=6909 6 # 7 # Usage: coverage.sh [--html|--coveralls] 8 # 9 # --html Additionally create HTML report 10 # --coveralls Push coverage statistics to coveralls.io 11 # 12 13 set -e 14 15 workdir=.cover 16 profile="$workdir/cover.out" 17 mode=count 18 19 generate_cover_data() { 20 rm -rf "$workdir" 21 mkdir "$workdir" 22 23 for pkg in "$@"; do 24 f="$workdir/$(echo $pkg | tr / -).cover" 25 go test -covermode="$mode" -coverprofile="$f" "$pkg" 26 done 27 28 echo "mode: $mode" >"$profile" 29 grep -h -v "^mode:" "$workdir"/*.cover >>"$profile" 30 } 31 32 show_html_report() { 33 go tool cover -html="$profile" -o="$workdir"/coverage.html 34 } 35 36 show_csv_report() { 37 go tool cover -func="$profile" -o="$workdir"/coverage.csv 38 } 39 40 push_to_coveralls() { 41 echo "Pushing coverage statistics to coveralls.io" 42 # ignore failure to push - it happens 43 $GOPATH/bin/goveralls -coverprofile="$profile" \ 44 -service=github \ 45 -ignore="nsqadmin/bindata.go" || true 46 } 47 48 generate_cover_data $(go list ./... | grep -v /vendor/) 49 show_csv_report 50 51 case "$1" in 52 "") 53 ;; 54 --html) 55 show_html_report ;; 56 --coveralls) 57 push_to_coveralls ;; 58 *) 59 echo >&2 "error: invalid option: $1"; exit 1 ;; 60 esac