From 3f2c6a3147527de63c5fe1e04db5541b94422b6e Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 20 Jul 2016 14:13:30 +0800 Subject: [PATCH 1/6] Support coverage report. Signed-off-by: Bo-Yi Wu --- .gitignore | 1 + Makefile | 33 ++++++++++++++++++++++++++++++--- docker/testing.sh | 4 +++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index bed8b36..140c705 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,4 @@ vendor lint.txt vet.txt Dockerfile.tmp +junit.txt diff --git a/Makefile b/Makefile index 79ce6a8..c20eea6 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ build: clean sh script/build.sh $(VERSION) coverage: - sh go.test.sh atomic + @sh go.test.sh atomic | tee junit.txt test: redis_test boltdb_test memory_test config_test go test -v -cover -covermode=count -coverprofile=coverage.txt ./gorush/... @@ -68,6 +68,10 @@ endif install: @which glide || (curl https://glide.sh/get | sh) + @which go-junit-report || go get -u github.com/jstemmer/go-junit-report + @which gocov || go get -u github.com/axw/gocov/gocov + @which gocov-xml || go get -u github.com/AlekSi/gocov-xml + @which golint || go get -u github.com/golang/lint/golint @glide install update: @@ -77,7 +81,30 @@ fmt: @echo $(TARGETS_NOVENDOR) | xargs go fmt lint: - @golint -set_exit_status=1 gorush/ + @golint -set_exit_status=1 ./... + +vet: + @go vet -n -x ./... + +junit_report: + cat junit.txt | go-junit-report > report.xml + +coverage_report: + gocov convert coverage.txt | gocov-xml > coverage.xml + +lint_report: + golint ./... > lint.txt + +vet_report: + go vet -n -x ./... > vet.txt + +report: junit_report coverage_report lint_report vet_report clean: - -rm -rf build.tar.gz gorush.tar.gz bin/* coverage.txt gorush.tar.gz gorush/gorush.db storage/boltdb/gorush.db + -rm -rf build.tar.gz \ + gorush.tar.gz bin/* \ + gorush.tar.gz \ + gorush/gorush.db \ + storage/boltdb/gorush.db \ + *.txt \ + *.xml diff --git a/docker/testing.sh b/docker/testing.sh index 52951ff..a71acd7 100755 --- a/docker/testing.sh +++ b/docker/testing.sh @@ -9,4 +9,6 @@ sed -i"" -e "s/localhost/redis/g" config/config.go sed -i"" -e "s/localhost/redis/g" config/config_test.go sed -i"" -e "s/localhost/redis/g" gorush/status_test.go sed -i"" -e "s/localhost/redis/g" storage/redis/redis_test.go -make install && make test +make install +make coverage +make report From 96eb62f47aed6b15ee99720cfd5ea7a3fe79f5b3 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 20 Jul 2016 15:50:28 +0800 Subject: [PATCH 2/6] add coverage script. Signed-off-by: Bo-Yi Wu --- .gitignore | 7 +--- .travis.yml | 2 +- Makefile | 25 ++++++------ go.test.sh | 26 ------------- script/coverage.sh | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 46 deletions(-) delete mode 100755 go.test.sh create mode 100755 script/coverage.sh diff --git a/.gitignore b/.gitignore index 140c705..2c4ffdb 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.travis.yml b/.travis.yml index ea802cb..d411d82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Makefile b/Makefile index c20eea6..8e2ccec 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/go.test.sh b/go.test.sh deleted file mode 100755 index eae93e0..0000000 --- a/go.test.sh +++ /dev/null @@ -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 diff --git a/script/coverage.sh b/script/coverage.sh new file mode 100755 index 0000000..2c1dbd8 --- /dev/null +++ b/script/coverage.sh @@ -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 From ac71f41f45d9d48612d9e036fa0c29dbc81c7843 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 20 Jul 2016 17:06:26 +0800 Subject: [PATCH 3/6] fix testing report. Signed-off-by: Bo-Yi Wu --- .travis.yml | 2 +- gorush/version.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d411d82..71bc958 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,7 @@ install: script: - make coverage - - $(go env GOPATH | awk 'BEGIN{FS=":"} {print $1}')/bin/goveralls -coverprofile=coverage.txt + - $(go env GOPATH | awk 'BEGIN{FS=":"} {print $1}')/bin/goveralls -coverprofile=.cover/coverage.txt -service=travis-ci -repotoken=$COVERALLS_TOKEN after_success: diff --git a/gorush/version.go b/gorush/version.go index faa0280..45dd995 100644 --- a/gorush/version.go +++ b/gorush/version.go @@ -24,6 +24,7 @@ func PrintGoRushVersion() { version, runtime.Compiler, runtime.Version()) + fmt.Println() } // VersionMiddleware : add version on header. From 8a0772849a2f669b3f28a1f62e90baf97246d5bb Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 20 Jul 2016 21:10:08 +0800 Subject: [PATCH 4/6] update default mode. Signed-off-by: Bo-Yi Wu --- script/coverage.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/script/coverage.sh b/script/coverage.sh index 2c1dbd8..476142f 100755 --- a/script/coverage.sh +++ b/script/coverage.sh @@ -41,6 +41,7 @@ EOF testing() { test -f ${junit_report} && rm -f ${junit_report} coverage_mode=$@ + output "Running ${coverage_mode} mode for coverage." for pkg in $packages; do f="$workdir/$(echo $pkg | tr / -).cover" output "Testing coverage report for ${pkg}" @@ -78,8 +79,8 @@ case "$1" in "") show_help ;; testing) - mode=$2 - test -z $mode && mode="set" + mode="set" + test -z $2 || mode=$2 testing $mode ;; coverage) generate_cover_report ;; From 511d8b87003e242bc45de8c3ed072965c2fdf594 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 20 Jul 2016 22:49:59 +0800 Subject: [PATCH 5/6] support Count Lines of Code report. Signed-off-by: Bo-Yi Wu --- Makefile | 5 ++++- script/coverage.sh | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8e2ccec..5f9d38f 100644 --- a/Makefile +++ b/Makefile @@ -98,7 +98,10 @@ lint_report: vet_report: sh ./script/coverage.sh vet -report: junit_report coverage_report lint_report vet_report +cloc_report: + sh ./script/coverage.sh cloc + +report: junit_report coverage_report lint_report vet_report cloc_report clean: -rm -rf build.tar.gz \ diff --git a/script/coverage.sh b/script/coverage.sh index 476142f..a7e6f99 100755 --- a/script/coverage.sh +++ b/script/coverage.sh @@ -22,6 +22,7 @@ junit_report="$workdir/junit.txt" junit_xml_report="$workdir/report.xml" lint_report="$workdir/lint.txt" vet_report="$workdir/vet.txt" +cloc_report="$workdir/cloc.xml" packages=$(go list ./... | grep -v vendor) test -d $workdir || mkdir -p $workdir @@ -35,6 +36,7 @@ Generate test coverage statistics for Go packages. junit Generate coverage xml report for junit plugin lint Generate Lint report for all packages vet Generate Vet report for all packages + cloc Generate Count Lines of Code report for all files EOF } @@ -75,6 +77,19 @@ generate_vet_report() { done } +generate_cloc_report() { + case "$OSTYPE" in + darwin*) + which cloc || brew install cloc ;; + linux*) + which cloc || apt-get install cloc ;; + *) + output "unknown: $OSTYPE" 1 ;; + esac + + cloc --by-file --xml --out=${cloc_report} --exclude-dir=vendor,Godeps . +} + case "$1" in "") show_help ;; @@ -90,6 +105,8 @@ case "$1" in generate_lint_report ;; vet) generate_vet_report ;; + cloc) + generate_cloc_report ;; *) show_help ;; esac From f4dd294d675340284f398ba9f5af4d88e0274012 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 20 Jul 2016 23:48:59 +0800 Subject: [PATCH 6/6] support install cloc on alpine. Signed-off-by: Bo-Yi Wu --- docker/golang/Dockerfile | 2 +- script/coverage.sh | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docker/golang/Dockerfile b/docker/golang/Dockerfile index ead91ee..4d16eb6 100644 --- a/docker/golang/Dockerfile +++ b/docker/golang/Dockerfile @@ -3,7 +3,7 @@ FROM golang:1.6.2-alpine MAINTAINER Bo-Yi Wu RUN apk update \ - && apk add git make curl && rm -rf /var/cache/apk/* + && apk add git make curl perl && rm -rf /var/cache/apk/* # install glide package management. RUN curl https://glide.sh/get | sh diff --git a/script/coverage.sh b/script/coverage.sh index a7e6f99..f6b5946 100755 --- a/script/coverage.sh +++ b/script/coverage.sh @@ -84,10 +84,11 @@ generate_cloc_report() { linux*) which cloc || apt-get install cloc ;; *) - output "unknown: $OSTYPE" 1 ;; + curl https://raw.githubusercontent.com/AlDanial/cloc/master/cloc -o /usr/bin/cloc + chmod 755 /usr/bin/cloc esac - cloc --by-file --xml --out=${cloc_report} --exclude-dir=vendor,Godeps . + cloc --by-file --xml --out=${cloc_report} --exclude-dir=vendor,Godeps,.cover . } case "$1" in