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 <ying.li@docker.com>
This commit is contained in:
Ying Li 2015-10-24 02:08:10 -07:00
parent 0eb76f4057
commit 408f1efee7
3 changed files with 30 additions and 13 deletions

View File

@ -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

View File

@ -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

10
coverpkg.sh Executable file
View File

@ -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 ' ' ','