@@ -6,9 +6,14 @@ REPO_PATH=$(ORG_PATH)/$(PACKAGE)
66GOPATH =$(CURDIR ) /.gopath
77GOBIN =$(CURDIR ) /bin
88BUILDDIR =$(CURDIR ) /build
9+ COVERAGE_DIR := $(BUILDDIR ) /coverage
10+ PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
11+ BIN_DIR := $(PROJECT_DIR ) /bin
912PLUGINSSOURCEDIR =$(CURDIR ) /pkg/sm/plugins
1013PLUGINSBUILDDIR =$(BUILDDIR ) /plugins
1114GOFILES =$(shell find . -name * .go | grep -vE "(\/vendor\/) |(_test.go)")
15+ PKGS := $(shell cd $(PROJECT_DIR ) && go list ./... | grep -v mocks)
16+ TESTPKGS := $(shell go list -f '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' $(PKGS ) )
1217
1318export GOPATH
1419export GOBIN
@@ -43,6 +48,10 @@ GOLANGCI_LINT = $(GOBIN)/golangci-lint
4348TIMEOUT = 15
4449Q = $(if $(filter 1,$V) ,,@)
4550
51+ ENVTEST := $(BIN_DIR ) /setup-envtest
52+ GOCOVMERGE := $(BIN_DIR ) /gocovmerge
53+ GCOV2LCOV := $(BIN_DIR ) /gcov2lcov
54+
4655.PHONY : all
4756all : build plugins
4857
@@ -83,7 +92,7 @@ plugins: noop-plugin ufm-plugin ; $(info Building plugins...) ## Build plugins
8392
8493% -plugin : $(PLUGINSBUILDDIR )
8594 @echo Building $* plugin
86- $Q $(GO ) build $(GOFLAGS ) -ldflags " -X $( REPO_PATH) /version=1.0" -o $(PLUGINSBUILDDIR ) /$* .so -buildmode=plugin -i $(REPO_PATH ) /pkg/sm/plugins/$*
95+ $Q $(GO ) build $(GOFLAGS ) -ldflags " -X $( REPO_PATH) /version=1.0" -o $(PLUGINSBUILDDIR ) /$* .so -buildmode=plugin $(REPO_PATH ) /pkg/sm/plugins/$*
8796 @echo Done building $* plugin
8897
8998TEST_TARGETS := test-bench test-short test-verbose test-race
@@ -100,9 +109,27 @@ test: | plugins; $(info running $(NAME:%=% )tests...) @ ## Run tests
100109
101110COVERAGE_MODE = count
102111.PHONY : test-coverage test-coverage-tools
103- test-coverage-tools : | $(GOVERALLS )
104- test-coverage : test-coverage-tools | plugins; $(info running coverage tests...) @ # # Run coverage tests
105- $Q $(GO ) test -covermode=$(COVERAGE_MODE ) -coverprofile=coverage.out ./...
112+ test-coverage : | envtest gocovmerge gcov2lcov # # Run coverage tests
113+ mkdir -p $(BUILDDIR ) /coverage/pkgs
114+ for pkg in $( TESTPKGS) ; do \
115+ KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) -p path) " go test \
116+ -covermode=atomic \
117+ -coverprofile=" $( COVERAGE_DIR) /pkgs/` echo $$ pkg | tr " /" " -" ` .cover" $$ pkg ; \
118+ done
119+ $(GOCOVMERGE ) $(COVERAGE_DIR ) /pkgs/* .cover > $(COVERAGE_DIR ) /profile.out
120+ $(GCOV2LCOV ) -infile $(COVERAGE_DIR ) /profile.out -outfile $(COVERAGE_DIR ) /lcov.info
121+
122+ .PHONY : envtest
123+ envtest : # # Download envtest if necessary
124+ $(call go-install-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
125+
126+ .PHONY : gocovmerge
127+ gocovmerge : # # Download gocovmerge if necessary
128+ $(call go-install-tool,$(GOCOVMERGE ) ,github.com/wadey/gocovmerge@latest)
129+
130+ .PHONY : gcov2lcov
131+ gcov2lcov : # # Download gcov2lcov if necessary
132+ $(call go-install-tool,$(GCOV2LCOV ) ,github.com/jandelgado/[email protected] ) 106133
107134# Container image
108135.PHONY : image
@@ -123,3 +150,12 @@ clean: ; $(info Cleaning...) ## Cleanup everything
123150help : # # Show this message
124151 @grep -E ' ^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST ) | \
125152 awk ' BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
153+
154+ # go-get-tool will 'go get' any package $2 and install it to $1.
155+ define go-install-tool
156+ @[ -f $(1 ) ] || { \
157+ set -e ;\
158+ echo "Downloading $(2 ) " ;\
159+ GOBIN=$(PROJECT_DIR ) /bin go install $(2 ) ;\
160+ }
161+ endef
0 commit comments