Skip to content

Commit

Permalink
Fixes unnaturally high test coverage by excluding test utilities (#406)
Browse files Browse the repository at this point in the history
Before, our test coverage was accidentally 0.5% higher than it should be
due to inclusion of packages not in the binary (notably test utilities).

This fixes it in native make, which helps us maintain what is main or
not in one place. We use make-derived source lists in order to taint
targets properly.

Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Nov 2, 2021
1 parent c4bdaf3 commit 69d8caf
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
# Please see GNU make's documentation if unfamiliar: https://www.gnu.org/software/make/manual/html_node/
.PHONY: test build e2e dist clean format lint check site

# Make functions strip spaces and use commas to separate parameters. The below variables escape these characters.
comma := ,
space :=
space +=

# Include versions of tools we build on-demand
include Tools.mk

Expand Down Expand Up @@ -77,9 +82,14 @@ test: ## Run all unit tests
@$(go) test . ./internal/...
@printf "$(ansi_format_bright)" test "ok"

# coverage_packages is a lazy inclusion of source packages that comprise the binary.
# To do this in make, we collect the unique main source directories (sort will dedupe).
# Paths need to all start with ./, so we do that manually vs foreach which strips it.
main_packages = $(sort $(foreach f,$(dir $(main_sources)),$(if $(findstring ./,$(f)),./,./$(f))))
coverpkg = $(subst $(space),$(comma),$(main_packages))
coverage: ## Generate test coverage
@printf "$(ansi_format_dark)" coverage "running unit tests with coverage"
@$(go) test -coverprofile=coverage.txt -covermode=atomic --coverpkg=.,./internal/... . ./internal/...
@$(go) test -coverprofile=coverage.txt -covermode=atomic --coverpkg=$(coverpkg) $(main_packages)
@$(go) tool cover -func coverage.txt
@printf "$(ansi_format_bright)" coverage "ok"

Expand All @@ -99,6 +109,7 @@ windows_platforms := windows_amd64
# Make 3.81 doesn't support '**' globbing: Set explicitly instead of recursion.
all_patterns := *.go */*.go */*/*.go */*/*/*.go */*/*/*.go */*/*/*/*.go
all_sources := $(wildcard $(all_patterns))
# main_sources compose the binary, so exclude tests, test utilities and linters
main_sources := $(wildcard $(subst *,*[!_test],$(all_patterns)))

build/func-e_%/func-e: $(main_sources)
Expand Down

0 comments on commit 69d8caf

Please sign in to comment.