github.com/thediveo/gons@v0.9.9/scripts/cov.sh (about) 1 #!/bin/bash 2 set -e 3 4 if ! command -v gobadge &>/dev/null; then 5 export PATH="$(go env GOPATH)/bin:$PATH" 6 if ! command -v gobadge &>/dev/null; then 7 go install github.com/AlexBeauchemin/gobadge@latest 8 fi 9 fi 10 11 # As of Go 1.20 (and later) we now use the new coverage profiling support that 12 # also *cough* covers integration tests (even if this particular project might 13 # not use the latter). The benefit of this slightly more involved approach is 14 # that we don't need external coverage profile file processing tools anymore, 15 # but can achieve our goal with just using the standard Go toolchain. 16 17 # First, we set up a temporary directory to receive the coverage (binary) 18 # files... 19 GOCOVERTMPDIR="$(mktemp -d)" 20 trap 'rm -rf -- "$GOCOVERTMPDIR"' EXIT 21 # Now run the (unit) tests with coverage, but don't use the existing textual 22 # format and instead tell "go test" to produce the new binary coverage data file 23 # format. This way we can easily run multiple coverage (integration) tests, as 24 # needed, without worrying about how to aggregate the coverage data later. The 25 # new Go toolchain already does this for us. 26 go test -cover -v -p=1 -count=1 -race ./... -args -test.gocoverdir="$GOCOVERTMPDIR" 27 # Finally transform the coverage information collected in potentially multiple 28 # runs into the well-proven textual format so we can process it as we have come 29 # to learn and love. 30 go tool covdata textfmt -i="$GOCOVERTMPDIR" -o=coverage.out 31 go tool cover -html=coverage.out -o=coverage.html 32 go tool cover -func=coverage.out -o=coverage.out 33 gobadge -filename=coverage.out -green=80 -yellow=50