aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: b955ad626ed058cf23bed76e87c07af712875c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
help:
	@echo 'Available targets:'
	@echo '    make generate'
	@echo '    make test'

GO_BUILDFLAGS =
GO_LDFLAGS =
GO_TESTENV =

################################################################################

generate: generated.go

%: %.in | util/render_template.go
	@echo ./util/render_template.go < $< > $@
	@./util/render_template.go < $< > $@.new && mv $@.new $@ || (rm $@.new; false)

################################################################################

test: static-tests cover.html
	@printf "\e[1;32m>> All tests successful.\e[0m\n"

# which packages to test with static checkers
GO_ALLPKGS := $(shell go list ./... | grep -v '/util')
# which files to test with static checkers (this contains a list of globs)
GO_ALLFILES := $(addsuffix /*.go,$(patsubst $(shell go list .),.,$(GO_ALLPKGS)))
# which packages to test with "go test"
GO_TESTPKGS := $(shell go list -f '{{if .TestGoFiles}}{{.ImportPath}}{{end}}' ./... | grep -v '/util')
# which packages to measure coverage for
GO_COVERPKGS := $(shell go list ./... | grep -Ev '/util')
# to get around weird Makefile syntax restrictions, we need variables containing a space and comma
space := $(null) $(null)
comma := ,

static-tests: FORCE
	@if ! hash golint 2>/dev/null; then printf "\e[1;36m>> Installing golint...\e[0m\n"; GO111MODULE=off go get -u golang.org/x/lint/golint; fi
	@printf "\e[1;36m>> gofmt\e[0m\n"
	@if s="$$(gofmt -s -d $(GO_ALLFILES) 2>/dev/null)" && test -n "$$s"; then echo "$$s"; false; fi
	@printf "\e[1;36m>> golint\e[0m\n"
	@if s="$$(golint $(GO_ALLPKGS) 2>/dev/null)" && test -n "$$s"; then echo "$$s"; false; fi
	@printf "\e[1;36m>> go vet\e[0m\n"
	@go vet $(GO_BUILDFLAGS) $(GO_ALLPKGS)

cover.out: FORCE
	@printf "\e[1;36m>> go test\e[0m\n"
	@env $(GO_TESTENV) go test $(GO_BUILDFLAGS) -ldflags '-s -w $(GO_LDFLAGS)' -p 1 -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)

cover.html: cover.out
	@printf "\e[1;36m>> go tool cover > $@\e[0m\n"
	@go tool cover -html=$< -o $@

################################################################################

.PHONY: FORCE