From 408f1efee7e3c8aaebb223f1356205e8b376aa3c Mon Sep 17 00:00:00 2001 From: Ying Li Date: Sat, 24 Oct 2015 02:08:10 -0700 Subject: [PATCH] Use -coverpkg to build multiple coverage outputs and use a tool to merge them, to get more complete coverage information (so a package can be tested by code outside the package). Signed-off-by: Ying Li --- Makefile | 26 ++++++++++++++++---------- circle.yml | 7 ++++--- coverpkg.sh | 10 ++++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100755 coverpkg.sh diff --git a/Makefile b/Makefile index 95b63fe50b..70a46f7001 100644 --- a/Makefile +++ b/Makefile @@ -67,8 +67,8 @@ build: test: OPTS = test: - @echo "+ $@ ${OPTS}" - go test ${OPTS} ./... + @echo "+ $@ $(OPTS)" + go test $(OPTS) ./... test-full: vet lint @echo "+ $@" @@ -78,28 +78,34 @@ protos: @protoc --go_out=plugins=grpc:. proto/*.proto +# This allows coverage for a package to come from tests in different package. +# Requires that the following: +# go get github.com/wadey/gocovmerge; go install github.com/wadey/gocovmerge +# +# be run first define gocover -$(GO_EXC) test ${OPTS} -covermode="$(COVERMODE)" -coverprofile="$(COVERDIR)/$(subst /,-,$(1)).cover" "$(1)" || exit 1; +$(GO_EXC) test $(OPTS) -covermode="$(COVERMODE)" -coverprofile="$(COVERDIR)/$(subst /,-,$(1)).cover" "$(1)" || exit 1; endef gen-cover: @rm -rf "$(COVERDIR)" @mkdir -p "$(COVERDIR)" $(foreach PKG,$(PKGS),$(call gocover,$(PKG))) - @echo "mode: $(COVERMODE)" > "$(COVERPROFILE)" - @grep -h -v "^mode:" "$(COVERDIR)"/*.cover >> "$(COVERPROFILE)" -cover: GO_EXC = go +cover: GO_EXC := go + OPTS = -coverpkg "$(shell ./coverpkg.sh $(1) $(NOTARY_PKG))" cover: gen-cover + @gocovmerge $(shell ls -1 $(COVERDIR)/* | tr "\n" " ") > $(COVERPROFILE) @go tool cover -func="$(COVERPROFILE)" @go tool cover -html="$(COVERPROFILE)" - -ci: OPTS = -race - GO_EXC = godep go - COVERPROFILE = coverage.out +# Codecov knows how to merge multiple coverage files +ci: OPTS = -race -coverpkg "$(shell ./coverpkg.sh $(1) $(NOTARY_PKG))" + GO_EXC := godep go ci: gen-cover + @gocovmerge $(shell ls -1 $(COVERDIR)/* | tr "\n" " ") > $(COVERPROFILE) + @go tool cover -func="$(COVERPROFILE)" clean-protos: @rm proto/*.pb.go diff --git a/circle.yml b/circle.yml index 9dd61d5ba7..fb89862fed 100644 --- a/circle.yml +++ b/circle.yml @@ -38,7 +38,8 @@ dependencies: # For the stable go version, additionally install linting tools - > gvm use stable && - go get github.com/axw/gocov/gocov github.com/golang/lint/golint + go get github.com/golang/lint/golint github.com/wadey/gocovmerge && + go install github.com/wadey/gocovmerge test: pre: # Output the go versions we are going to test @@ -64,5 +65,5 @@ test: post: # Report to codecov.io - - bash <(curl -s https://codecov.io/bash): - pwd: $BASE_STABLE + # - bash <(curl -s https://codecov.io/bash): + # pwd: $BASE_STABLE diff --git a/coverpkg.sh b/coverpkg.sh new file mode 100755 index 0000000000..eccb28ab7e --- /dev/null +++ b/coverpkg.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +# Given a subpackage and the containing package, figures out which packages +# need to be passed to `go test -coverpkg`: this includes all of the +# subpackage's dependencies within the containing package, as well as the +# subpackage itself. + +DEPENDENCIES="$(go list -f $'{{range $f := .Deps}}{{$f}}\n{{end}}' ${1} | grep ${2})" + +echo "${1} ${DEPENDENCIES}" | xargs echo -n | tr ' ' ','