diff --git a/Makefile b/Makefile index 20e34a84e7..637f070978 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,6 @@ TOOLS_DIR := tools GOCC ?= go LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint -GOACC_PKG := github.com/ory/go-acc GOIMPORTS_PKG := github.com/rinchsan/gosimports/cmd/gosimports GO_BIN := $(shell go env GOBIN) @@ -16,7 +15,6 @@ endif GOIMPORTS_BIN := $(GO_BIN)/gosimports LINT_BIN := $(GO_BIN)/golangci-lint -GOACC_BIN := $(GO_BIN)/go-acc LINT_VERSION := v1.60.1 GOACC_VERSION := v0.2.8 @@ -36,6 +34,8 @@ CP := cp MAKE := make XARGS := xargs -L 1 +include make/testing_flags.mk + # Linting uses a lot of memory, so keep it under control by limiting the number # of workers if requested. ifneq ($(workers),) @@ -68,10 +68,6 @@ $(LINT_BIN): @$(call print, "Fetching linter") $(GOINSTALL) $(LINT_PKG)@$(LINT_VERSION) -$(GOACC_BIN): - @$(call print, "Fetching go-acc") - $(GOINSTALL) $(GOACC_PKG)@$(GOACC_VERSION) - # ============ # INSTALLATION # ============ @@ -98,17 +94,27 @@ check: unit #? unit: Run unit tests unit: @$(call print, "Running unit tests.") - $(GOLIST) | $(XARGS) env $(GOTEST) -test.timeout=20m + $(UNIT) #? unit-cover: Run unit coverage tests -unit-cover: $(GOACC_BIN) +unit-cover: @$(call print, "Running unit coverage tests.") - $(GOACC_BIN) $(GOLIST_COVER) + $(UNIT_COVER) #? unit-race: Run unit race tests unit-race: @$(call print, "Running unit race tests.") - env CGO_ENABLED=1 GORACE="history_size=7 halt_on_errors=1" $(GOLIST) | $(XARGS) env $(GOTEST) -race -test.timeout=20m + env CGO_ENABLED=1 GORACE="history_size=7 halt_on_errors=1" $(UNIT_RACE) + +#? unit-debug: Run unit tests with verbose debug output enabled +unit-debug: + @$(call print, "Running debug unit tests.") + $(UNIT_DEBUG) + +#? unit-bench: Run benchmark tests +unit-bench: + @$(call print, "Running benchmark tests.") + $(UNIT_BENCH) # ========= # UTILITIES @@ -152,6 +158,8 @@ tidy-module-check: tidy-module unit \ unit-cover \ unit-race \ + unit-debug \ + unit-bench \ fmt \ fmt-check \ lint \ diff --git a/make/testing_flags.mk b/make/testing_flags.mk new file mode 100644 index 0000000000..cb0f6ebfa7 --- /dev/null +++ b/make/testing_flags.mk @@ -0,0 +1,61 @@ +TEST_FLAGS = +COVER_PKG = $$($(GOCC) list -deps ./... | grep '$(PKG)') +COVER_FLAGS = -coverprofile=coverage.txt -covermode=atomic -coverpkg=$(PKG)/... + +# If specific package is being unit tested, construct the full name of the +# subpackage. +ifneq ($(pkg),) +UNITPKG := $(PKG)/$(pkg) +UNIT_TARGETED = yes +COVER_PKG = $(PKG)/$(pkg) +endif + +# If a specific unit test case is being target, construct test.run filter. +ifneq ($(case),) +TEST_FLAGS += -test.run=$(case) +UNIT_TARGETED = yes +endif + +# If a timeout was requested, construct initialize the proper flag for the go +# test command. If not, we set 20m (btcwallet default). +ifneq ($(timeout),) +TEST_FLAGS += -test.timeout=$(timeout) +else +TEST_FLAGS += -test.timeout=20m +endif + +ifneq ($(verbose),) +TEST_FLAGS += -test.v +endif + +ifneq ($(nocache),) +TEST_FLAGS += -test.count=1 +endif + +GOLIST := $(GOCC) list -deps $(PKG)/... | grep '$(PKG)' + +# UNIT_TARGETED is undefined iff a specific package and/or unit test case is +# not being targeted. +UNIT_TARGETED ?= no + +# If a specific package/test case was requested, run the unit test for the +# targeted case. Otherwise, default to running all tests. +ifeq ($(UNIT_TARGETED), yes) +UNIT := $(GOTEST) $(TEST_FLAGS) $(UNITPKG) +UNIT_DEBUG := $(GOTEST) -v $(TEST_FLAGS) $(UNITPKG) +UNIT_RACE := $(GOTEST) $(TEST_FLAGS) -race $(UNITPKG) +# NONE is a special value which selects no other tests but only executes the +# benchmark tests here. +UNIT_BENCH := $(GOTEST) -test.bench=. -test.run=NONE $(UNITPKG) +endif + +ifeq ($(UNIT_TARGETED), no) +UNIT := $(GOLIST) | $(XARGS) env $(GOTEST) $(TEST_FLAGS) +UNIT_DEBUG := $(GOLIST) | $(XARGS) env $(GOTEST) -v $(TEST_FLAGS) +UNIT_RACE := $(UNIT) -race +# NONE is a special value which selects no other tests but only executes the +# benchmark tests here. +UNIT_BENCH := $(GOLIST) | $(XARGS) env $(GOTEST) -test.bench=. -test.run=NONE +endif + +UNIT_COVER := $(GOTEST) $(COVER_FLAGS) $(TEST_FLAGS) $(COVER_PKG)