|
| 1 | +# Adapted from https://github.com/vincentbernat/hellogopher |
| 2 | + |
| 3 | +MODULE = $(shell env GO111MODULE=on $(GO) list -m) |
| 4 | +DATE ?= $(shell date +%FT%T%z) |
| 5 | +VERSION ?= $(shell git describe --tags --always --dirty --match=v* | cut -c2- 2> /dev/null || \ |
| 6 | + cat $(CURDIR)/.version 2> /dev/null || echo v0) |
| 7 | +COMMIT ?= $(shell git rev-parse HEAD) |
| 8 | +PKGS = $(or $(PKG),$(shell env GO111MODULE=on $(GO) list ./...)) |
| 9 | +TESTPKGS = $(shell env GO111MODULE=on $(GO) list -f \ |
| 10 | + '{{ if or .TestGoFiles .XTestGoFiles }}{{ .ImportPath }}{{ end }}' \ |
| 11 | + $(PKGS)) |
| 12 | +GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/) |
| 13 | + |
| 14 | +GO = go |
| 15 | +GOLINT = golint |
| 16 | +GOLANGCILINT = golangci-lint |
| 17 | +GOCOV = gocov |
| 18 | +GOCOVXML = gocov-xml |
| 19 | + |
| 20 | +TIMEOUT = 15 |
| 21 | +V = 0 |
| 22 | +Q = $(if $(filter 1,$V),,@) |
| 23 | +# Prompt shown before each item |
| 24 | +M = $(shell printf "\033[34;1m▶\033[0m") |
| 25 | + |
| 26 | +export GO111MODULE=on |
| 27 | + |
| 28 | +.PHONY: all |
| 29 | +all: fmt lint test ## Build, format, and test |
| 30 | + |
| 31 | +# Tests |
| 32 | + |
| 33 | +TEST_TARGETS := test-default test-bench test-short test-verbose test-race |
| 34 | +.PHONY: $(TEST_TARGETS) check test tests |
| 35 | +test-bench: ARGS=-run=__absolutelynothing__ -bench=. ## Run benchmarks |
| 36 | +test-short: ARGS=-short ## Run only short tests |
| 37 | +test-verbose: ARGS=-v ## Run tests in verbose mode with coverage reporting |
| 38 | +test-race: ARGS=-race ## Run tests with race detector |
| 39 | +$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%) |
| 40 | +$(TEST_TARGETS): test |
| 41 | +test: lint ; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests |
| 42 | + $Q $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(TESTPKGS) |
| 43 | + |
| 44 | +COVERAGE_MODE = atomic |
| 45 | +COVERAGE_PROFILE = $(COVERAGE_DIR)/profile.out |
| 46 | +COVERAGE_XML = $(COVERAGE_DIR)/coverage.xml |
| 47 | +COVERAGE_HTML = $(COVERAGE_DIR)/index.html |
| 48 | +.PHONY: test-coverage test-coverage-tools |
| 49 | +test-coverage-tools: | $(GOCOV) $(GOCOVXML) |
| 50 | +test-coverage: COVERAGE_DIR := $(CURDIR)/test/coverage.$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 51 | +test-coverage: lint test-coverage-tools ; $(info $(M) running coverage tests…) @ ## Run coverage tests |
| 52 | + $Q mkdir -p $(COVERAGE_DIR) |
| 53 | + $Q $(GO) test \ |
| 54 | + -coverpkg=$$($(GO) list -f '{{ join .Deps "\n" }}' $(TESTPKGS) | \ |
| 55 | + grep '^$(MODULE)/' | \ |
| 56 | + tr '\n' ',' | sed 's/,$$//') \ |
| 57 | + -covermode=$(COVERAGE_MODE) \ |
| 58 | + -coverprofile="$(COVERAGE_PROFILE)" $(TESTPKGS) |
| 59 | + $Q $(GO) tool cover -html=$(COVERAGE_PROFILE) -o $(COVERAGE_HTML) |
| 60 | + $Q $(GOCOV) convert $(COVERAGE_PROFILE) | $(GOCOVXML) > $(COVERAGE_XML) |
| 61 | + |
| 62 | +.PHONY: lint |
| 63 | +lint: golint golangci-lint ## Run linters |
| 64 | + |
| 65 | +golint: | ; $(info $(M) running golint…) @ ## Run golint |
| 66 | + $Q $(GOLINT) -set_exit_status ./... |
| 67 | + |
| 68 | +.PHONY: golangci-lint |
| 69 | +golangci-lint: | ; $(info $(M) running golangci-lint…) @ ## Run golangci-lint |
| 70 | + $Q $(GOLANGCILINT) run |
| 71 | + |
| 72 | +.PHONY: fmt |
| 73 | +fmt: ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files |
| 74 | + $Q $(GO) fmt $(PKGS) |
| 75 | + |
| 76 | +# Misc |
| 77 | + |
| 78 | +.PHONY: help |
| 79 | +help: |
| 80 | + @grep -hE '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ |
| 81 | + awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-17s\033[0m %s\n", $$1, $$2}' |
| 82 | + |
| 83 | +.PHONY: version |
| 84 | +version: |
| 85 | + @echo $(VERSION) |
0 commit comments