cover.test.bash 762 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env bash
  2. #
  3. # Generate coverage HTML for a package
  4. # e.g. PKG=./unit ./tests/cover.test.bash
  5. #
  6. set -e
  7. if ! [[ "$0" =~ "tests/cover.test.bash" ]]; then
  8. echo "must be run from repository root"
  9. exit 255
  10. fi
  11. if [ -z "$PKG" ]; then
  12. echo "cover only works with a single package, sorry"
  13. exit 255
  14. fi
  15. COVEROUT="coverage"
  16. if ! [ -d "$COVEROUT" ]; then
  17. mkdir "$COVEROUT"
  18. fi
  19. # strip leading dot/slash and trailing slash and sanitize other slashes
  20. # e.g. ./etcdserver/etcdhttp/ ==> etcdserver_etcdhttp
  21. COVERPKG=${PKG/#./}
  22. COVERPKG=${COVERPKG/#\//}
  23. COVERPKG=${COVERPKG/%\//}
  24. COVERPKG=${COVERPKG//\//_}
  25. # generate arg for "go test"
  26. export COVER="-coverprofile ${COVEROUT}/${COVERPKG}.out"
  27. source ./test
  28. go tool cover -html=${COVEROUT}/${COVERPKG}.out