github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/hooks/coverage.sh (about)

     1  #!/bin/sh
     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: script/coverage [--html|--coveralls]
     8  #
     9  #     --html      Additionally create HTML report and open it in browser
    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          godep go test -test.short -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_cover_report() {
    33      godep go tool cover -${1}="$profile"
    34  }
    35  
    36  push_to_coveralls() {
    37      echo "Pushing coverage statistics to coveralls.io"
    38      goveralls -coverprofile="$profile" -service=travis-ci -repotoken $COVERALLS_TOKEN
    39  }
    40  
    41  generate_cover_data $(godep go list ./...)
    42  show_cover_report func
    43  case "$1" in
    44  "")
    45      ;;
    46  --html)
    47      show_cover_report html ;;
    48  --coveralls)
    49      push_to_coveralls ;;
    50  *)
    51      echo >&2 "error: invalid option: $1"; exit 1 ;;
    52  esac