coverage.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env bash
  2. # Copyright 2019 Yunion
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o pipefail
  17. function push_to_codecov() {
  18. if [ -z "$CODECOV_TOKEN" ]; then
  19. echo "You must set CODECOV_TOKEN"
  20. exit 1
  21. fi
  22. echo "Push $profile to codecov.io"
  23. curl -s https://codecov.io/bash | bash -s -- -c -F aFlag -f "$profile"
  24. }
  25. covermode=${COVERMODE:-atomic}
  26. coverdir=$(mktemp -d /tmp/coverage.XXXXXXXXXX)
  27. profile="${coverdir}/profile.out"
  28. cwd="$PWD"
  29. if [ -s "$cwd/go.mod" -a -d "$cwd/vendor" ]; then
  30. mod_args=(-mod vendor)
  31. fi
  32. if [ -z "$pkgs" ]; then
  33. pkgs="$(go list "${mod_args[@]}" -test ./... | grep '\.test$' | sed -e 's/\.test$//')"
  34. pkgs="$(echo "$pkgs" | grep -vE 'host-image|hostimage')"
  35. fi
  36. if type circleci &>/dev/null; then
  37. pkgs="$(echo "$pkgs" | circleci tests split)"
  38. fi
  39. echo "mode: $covermode" >"$profile"
  40. echo "$pkgs" | xargs -n 8 --no-run-if-empty echo \
  41. | while read batch; do \
  42. go test -v \
  43. -coverprofile="$profile.tmp" \
  44. -covermode="$covermode" \
  45. "${mod_args[@]}" \
  46. -ldflags '-w' \
  47. $batch; \
  48. tail -n +2 "$profile.tmp" >>"$profile"; \
  49. rm -f "$profile.tmp"; \
  50. done
  51. case "${1-}" in
  52. --html)
  53. go tool cover -html "$profile"
  54. ;;
  55. --codecov)
  56. if ! push_to_codecov; then
  57. echo "ignored: push to codecov failed" >&2
  58. fi
  59. ;;
  60. *)
  61. go tool cover -func "$profile"
  62. ;;
  63. esac