airshipctl/tools/coverage_check
Ian Howell 1c999e2095 Add coverage checks
This commit adds a makefile target for generating a report for unit test
coverage. It also adds the coverage_check tool to assert that the actual
test coverage meets the specified requirement.

This also improves various aspects of the testing utilities:
* The "test" package has been renamed to "testutil"
* The "Objs" member has been removed from the CmdTest object
* The "Cmd" member has been added to the CmdTest object. This allows
  testing of multiple variants of a root airshipctl command

Finally, this commit includes additional tests for root.go. These are
required in order to meet the required coverage threshold.

Change-Id: Id48343166c0488c543a405ec3143e4a75355ba43
2019-08-15 11:11:51 -05:00

24 lines
636 B
Bash
Executable File

#!/bin/bash
set -ex
if [[ $# -ne 1 ]]; then
printf "Usage: %s <coverfile>\n" "$0"
exit 0
fi
cover_file=$1
min_coverage=80
coverage_report=$(go tool cover -func="$cover_file")
printf "%s\n" "$coverage_report"
coverage_float=$(awk "/^total:/ { print \$3 }" <<< "$coverage_report")
coverage_int=${coverage_float%.*}
if (( "$coverage_int" < "$min_coverage" )) ; then
printf "FAIL: Test coverage is at %s, which does not meet the required coverage (%s%%)\n" "$coverage_float" "$min_coverage"
exit 1
else
printf "SUCCESS: Test coverage is at %s, which meets the required coverage (%s%%)\n" "$coverage_float" "$min_coverage"
fi