add coverage script.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-07-20 15:50:28 +08:00
parent 3f2c6a3147
commit 96eb62f47a
5 changed files with 108 additions and 46 deletions

7
.gitignore vendored
View File

@ -28,15 +28,10 @@ key.pem
config.yml
bin/*
.DS_Store
coverage.txt
coverage.xml
report.xml
gorush/log/*.log
build.tar.gz
gorush.tar.gz
gorush.db
vendor
lint.txt
vet.txt
Dockerfile.tmp
junit.txt
.cover

View File

@ -41,4 +41,4 @@ after_success:
&& test "$TRAVIS_PULL_REQUEST" == "false" && cd $TRAVIS_BUILD_DIR && make docker_build
- test "$TRAVIS_BRANCH" == "master" && test "$TRAVIS_GO_VERSION" == "1.6.2"
&& test "$TRAVIS_PULL_REQUEST" == "false" && cd $TRAVIS_BUILD_DIR && make deploy tag=latest
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash) -f .cover/coverage.txt

View File

@ -25,25 +25,25 @@ build: clean
sh script/build.sh $(VERSION)
coverage:
@sh go.test.sh atomic | tee junit.txt
sh ./script/coverage.sh testing atomic
test: redis_test boltdb_test memory_test config_test
go test -v -cover -covermode=count -coverprofile=coverage.txt ./gorush/...
go test -v -cover ./gorush/...
redis_test: init
go test -v -cover -covermode=count -coverprofile=coverage.txt ./storage/redis/...
go test -v -cover ./storage/redis/...
boltdb_test: init
go test -v -cover -covermode=count -coverprofile=coverage.txt ./storage/boltdb/...
go test -v -cover ./storage/boltdb/...
memory_test: init
go test -v -cover -covermode=count -coverprofile=coverage.txt ./storage/memory/...
go test -v -cover ./storage/memory/...
config_test: init
go test -v -cover -covermode=count -coverprofile=coverage.txt ./config/...
go test -v -cover ./config/...
html:
go tool cover -html=coverage.txt
go tool cover -html=.cover/coverage.txt
docker_build: clean
tar -zcvf build.tar.gz gorush.go gorush config storage Makefile glide.lock glide.yaml
@ -87,16 +87,16 @@ vet:
@go vet -n -x ./...
junit_report:
cat junit.txt | go-junit-report > report.xml
sh ./script/coverage.sh junit
coverage_report:
gocov convert coverage.txt | gocov-xml > coverage.xml
sh ./script/coverage.sh coverage
lint_report:
golint ./... > lint.txt
sh ./script/coverage.sh lint
vet_report:
go vet -n -x ./... > vet.txt
sh ./script/coverage.sh vet
report: junit_report coverage_report lint_report vet_report
@ -106,5 +106,4 @@ clean:
gorush.tar.gz \
gorush/gorush.db \
storage/boltdb/gorush.db \
*.txt \
*.xml
.cover

View File

@ -1,26 +0,0 @@
#!/usr/bin/env bash
set -e
output() {
printf "\033[32m"
echo $1
printf "\033[0m"
exit 1
}
coverage_mode=$1
test -z $coverage_mode && output "Usage: $0 coverage_mode"
test -z $(which glide) && output "glide command not found"
test -f coverage.txt && rm -rf coverage.txt
echo "mode: ${coverage_mode}" > coverage.txt
for d in $(go list ./... | grep -v vendor); do
go test -v -cover -coverprofile=profile.out -covermode=${coverage_mode} $d
if [ -f profile.out ]; then
sed '1d' profile.out >> coverage.txt
rm profile.out
fi
done

94
script/coverage.sh Executable file
View File

@ -0,0 +1,94 @@
#!/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"
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
EOF
}
testing() {
test -f ${junit_report} && rm -f ${junit_report}
coverage_mode=$@
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
}
case "$1" in
"")
show_help ;;
testing)
mode=$2
test -z $mode && mode="set"
testing $mode ;;
coverage)
generate_cover_report ;;
junit)
generate_junit_report ;;
lint)
generate_lint_report ;;
vet)
generate_vet_report ;;
*)
show_help ;;
esac