coverage.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. set -e
  13. workdir=.cover
  14. profile="$workdir/cover.out"
  15. mode=count
  16. generate_cover_data() {
  17. rm -rf "$workdir"
  18. mkdir "$workdir"
  19. for pkg in "$@"; do
  20. f="$workdir/$(echo $pkg | tr / -).cover"
  21. go test -covermode="$mode" -coverprofile="$f" "$pkg"
  22. done
  23. echo "mode: $mode" >"$profile"
  24. grep -h -v "^mode:" "$workdir"/*.cover >>"$profile"
  25. }
  26. show_html_report() {
  27. go tool cover -html="$profile" -o="$workdir"/coverage.html
  28. }
  29. show_csv_report() {
  30. go tool cover -func="$profile" -o="$workdir"/coverage.csv
  31. }
  32. push_to_coveralls() {
  33. echo "Pushing coverage statistics to coveralls.io"
  34. # ignore failure to push - it happens
  35. $GOPATH/bin/goveralls -coverprofile="$profile" \
  36. -service=github \
  37. -ignore="nsqadmin/bindata.go" || true
  38. }
  39. generate_cover_data $(go list ./... | grep -v /vendor/)
  40. show_csv_report
  41. case "$1" in
  42. "")
  43. ;;
  44. --html)
  45. show_html_report ;;
  46. --coveralls)
  47. push_to_coveralls ;;
  48. *)
  49. echo >&2 "error: invalid option: $1"; exit 1 ;;
  50. esac