cov_report.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env sh
  2. # >> This script is not POSIX-compliant. <<
  3. # This script will use your default ${GOROOT}
  4. # and ${GOPATH}, so ensure these locations exist,
  5. # and "${GOPATH}/bin" is in your default ${PATH}.
  6. # NOTE: This script is known to work on bash, zsh,
  7. # and ksh (ksh93 and ksh2000+), but it is not
  8. # POSIX compliant, due to the using the ksh-derived
  9. # "type" utility. If you are not able to use this
  10. # script, the tools are easily accessible and manual
  11. # runs should be straight-forward on most platforms.
  12. # Abort and inform in the case of csh or tcsh as sh.
  13. # shellcheck disable=SC2046,SC2006,SC2116,SC2065
  14. test _$(echo asdf 2> /dev/null) != _asdf > /dev/null &&
  15. printf '%s\n' "Error: csh as sh is unsupported." &&
  16. exit 1
  17. cleanUp()
  18. {
  19. printf '%s\n' "Running cleanup tasks." >&2 ||
  20. true :
  21. set +u > /dev/null 2>&1 ||
  22. true :
  23. set +e > /dev/null 2>&1 ||
  24. true :
  25. rm -f ./gocov_report_gonuma* > /dev/null 2>&1 ||
  26. true :
  27. printf '%s\n' "All cleanup tasks completed." >&2 ||
  28. true :
  29. }
  30. global_trap()
  31. {
  32. err=${?}
  33. trap - EXIT
  34. trap '' EXIT INT TERM ABRT ALRM HUP
  35. cleanUp
  36. }
  37. trap 'global_trap $?' EXIT
  38. trap 'err=$?; global_trap; exit $?' ABRT ALRM HUP TERM
  39. trap 'err=$?; trap - EXIT; global_trap $err; exit $err' QUIT
  40. trap 'global_trap; trap - INT; kill -INT $$; sleep 1; trap - TERM; kill -TERM $$' INT
  41. trap '' EMT IO LOST SYS URG > /dev/null 2>&1 ||
  42. true :
  43. if [ ! -f "./.gonuma_root" ]; then
  44. printf '\n%s\n' "You must run this tool from the root directory" >&2
  45. printf '%s\n' "of your local gonuma source tree or checkout." >&2
  46. exit 1 ||
  47. :
  48. fi
  49. export CGO_ENABLED="1"
  50. export TEST1_TAGS='-tags=osnetgo,osusergo,leaktest,purego'
  51. export TEST2_TAGS='-tags=osnetgo,osusergo,leaktest,amd64'
  52. export TEST_FLAGS="-count=1 -covermode=atomic -cpu=1 -parallel=1 -trimpath"
  53. # shellcheck disable=SC2155
  54. export GOC_TARGETS="$(go list ./... |
  55. grep -v test |
  56. sort |
  57. uniq)"
  58. type gocov 1> /dev/null 2>&1
  59. # shellcheck disable=SC2181
  60. if [ "${?}" -ne 0 ]; then
  61. printf '\n%s\n' "This script requires the gocov tool." >&2
  62. printf '%s\n' "You may obtain it with the following command:" >&2
  63. printf '%s\n\n' '"go get github.com/axw/gocov/gocov"' >&2
  64. exit 1 ||
  65. :
  66. fi
  67. cleanUp ||
  68. true &&
  69. unset="Error: Testing flags are unset, aborting." &&
  70. export unset
  71. (
  72. printf '%s\n' "Testing coverage with ${TEST1_TAGS:?${unset:?}}"
  73. # shellcheck disable=SC2086,SC2248
  74. go test -v ${TEST1_TAGS:?${unset:?}} ${TEST_FLAGS:?${unset:?}} -bench=. ${GOC_TARGETS} -coverprofile gocov_report_gonuma1.profile |
  75. grep --line-buffered -v '^coverage: ' &&
  76. printf '%s\n' "Testing coverage with ${TEST2_TAGS:?${unset:?}}"
  77. # shellcheck disable=SC2086,SC2248
  78. go test -v ${TEST2_TAGS:?${unset:?}} ${TEST_FLAGS:?${unset:?}} -bench=. ${GOC_TARGETS} -coverprofile gocov_report_gonuma2.profile |
  79. grep --line-buffered -v '^coverage: ' &&
  80. gocovmerge gocov_report_gonuma1.profile gocov_report_gonuma2.profile > gocov_report_gonuma.profile &&
  81. /bin/rm -f -- ./gocov_report_gonuma1.profile ./gocov_report_gonuma2.profile &&
  82. gocov convert gocov_report_gonuma.profile > gocov_report_gonuma.json &&
  83. gocov report < gocov_report_gonuma.json > gocov_report_gonuma.txt
  84. ) ||
  85. {
  86. printf '\n%s\n' "gocov failed complete successfully." >&2
  87. exit 1 ||
  88. :
  89. }
  90. type gocov-html 1> /dev/null 2>&1
  91. # shellcheck disable=SC2181
  92. if [ "${?}" -ne 0 ]; then
  93. printf '%\n%s\n' "This script optionally utilizes gocov-html." >&2
  94. printf '%s\n' "You may obtain it with the following command:" >&2
  95. printf '%s\n\n' '"go get https://github.com/matm/gocov-html"' >&2
  96. exit 1 ||
  97. :
  98. fi
  99. (gocov-html < gocov_report_gonuma.json > gocov_report_gonuma.html) ||
  100. {
  101. printf '\n%s\n' "gocov-html failed to complete successfully." >&2
  102. exit 1 ||
  103. :
  104. }
  105. if [ -x "${HOME}/.gonuma.cov.local" ]; then
  106. printf '%s\n' "Local script started"
  107. ("${HOME}/.gonuma.cov.local")
  108. printf '%s\n' "Local script ended"
  109. fi
  110. mkdir -p ./cov &&
  111. mv -f ./gocov_report_gonuma* ./cov &&
  112. printf '%s\n' "Done - output is located at ./cov"