2016-07-20 07:50:28 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Generate test coverage statistics for Go packages.
|
|
|
|
#
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
output() {
|
|
|
|
printf "\033[32m"
|
|
|
|
echo $1
|
|
|
|
printf "\033[0m"
|
|
|
|
|
|
|
|
if [ "$2" = 1 ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
workdir=".cover"
|
|
|
|
coverage_report="$workdir/coverage.txt"
|
|
|
|
coverage_xml_report="$workdir/coverage.xml"
|
|
|
|
junit_report="$workdir/junit.txt"
|
|
|
|
junit_xml_report="$workdir/report.xml"
|
|
|
|
lint_report="$workdir/lint.txt"
|
|
|
|
vet_report="$workdir/vet.txt"
|
2016-07-20 14:49:59 +00:00
|
|
|
cloc_report="$workdir/cloc.xml"
|
2016-07-20 07:50:28 +00:00
|
|
|
packages=$(go list ./... | grep -v vendor)
|
|
|
|
|
|
|
|
test -d $workdir || mkdir -p $workdir
|
|
|
|
|
|
|
|
show_help() {
|
|
|
|
cat << EOF
|
|
|
|
Generate test coverage statistics for Go packages.
|
|
|
|
|
|
|
|
testing [set|count|atomic] Run go testing for all packages
|
|
|
|
coverage Generate coverage report for all packages
|
|
|
|
junit Generate coverage xml report for junit plugin
|
|
|
|
lint Generate Lint report for all packages
|
|
|
|
vet Generate Vet report for all packages
|
2016-07-20 14:49:59 +00:00
|
|
|
cloc Generate Count Lines of Code report for all files
|
2016-07-20 07:50:28 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
testing() {
|
|
|
|
test -f ${junit_report} && rm -f ${junit_report}
|
|
|
|
coverage_mode=$@
|
2016-07-20 13:10:08 +00:00
|
|
|
output "Running ${coverage_mode} mode for coverage."
|
2016-07-20 07:50:28 +00:00
|
|
|
for pkg in $packages; do
|
|
|
|
f="$workdir/$(echo $pkg | tr / -).cover"
|
|
|
|
output "Testing coverage report for ${pkg}"
|
|
|
|
go test -v -cover -coverprofile=${f} -covermode=${coverage_mode} $pkg | tee -a ${junit_report}
|
|
|
|
done
|
|
|
|
|
|
|
|
output "Convert all packages coverage report to $coverage_report"
|
|
|
|
echo "mode: $coverage_mode" > "$coverage_report"
|
|
|
|
grep -h -v "^mode:" "$workdir"/*.cover >> "$coverage_report"
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_cover_report() {
|
|
|
|
gocov convert ${coverage_report} | gocov-xml > ${coverage_xml_report}
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_junit_report() {
|
|
|
|
cat $junit_report | go-junit-report > ${junit_xml_report}
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_lint_report() {
|
|
|
|
for pkg in $packages; do
|
|
|
|
output "Go Lint report for ${pkg}"
|
|
|
|
golint ${pkg} | tee -a ${lint_report}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_vet_report() {
|
|
|
|
for pkg in $packages; do
|
|
|
|
output "Go Vet report for ${pkg}"
|
|
|
|
go vet -n -x ${pkg} | tee -a ${vet_report}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-07-20 14:49:59 +00:00
|
|
|
generate_cloc_report() {
|
|
|
|
case "$OSTYPE" in
|
|
|
|
darwin*)
|
|
|
|
which cloc || brew install cloc ;;
|
|
|
|
linux*)
|
|
|
|
which cloc || apt-get install cloc ;;
|
|
|
|
*)
|
2016-07-20 15:48:59 +00:00
|
|
|
curl https://raw.githubusercontent.com/AlDanial/cloc/master/cloc -o /usr/bin/cloc
|
|
|
|
chmod 755 /usr/bin/cloc
|
2016-07-20 14:49:59 +00:00
|
|
|
esac
|
|
|
|
|
2016-07-20 15:48:59 +00:00
|
|
|
cloc --by-file --xml --out=${cloc_report} --exclude-dir=vendor,Godeps,.cover .
|
2016-07-20 14:49:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 07:50:28 +00:00
|
|
|
case "$1" in
|
|
|
|
"")
|
|
|
|
show_help ;;
|
|
|
|
testing)
|
2016-07-20 13:10:08 +00:00
|
|
|
mode="set"
|
|
|
|
test -z $2 || mode=$2
|
2016-07-20 07:50:28 +00:00
|
|
|
testing $mode ;;
|
|
|
|
coverage)
|
|
|
|
generate_cover_report ;;
|
|
|
|
junit)
|
|
|
|
generate_junit_report ;;
|
|
|
|
lint)
|
|
|
|
generate_lint_report ;;
|
|
|
|
vet)
|
|
|
|
generate_vet_report ;;
|
2016-07-20 14:49:59 +00:00
|
|
|
cloc)
|
|
|
|
generate_cloc_report ;;
|
2016-07-20 07:50:28 +00:00
|
|
|
*)
|
|
|
|
show_help ;;
|
|
|
|
esac
|