1# TODO: Fix this on windows.
2ALL_SRC := $(shell find . -name '*.go' \
3								-not -path '*/internal/testpb/*' \
4								-not -name 'tools.go' \
5								-type f | sort)
6ALL_PKGS := $(shell go list $(sort $(dir $(ALL_SRC))))
7
8GOTEST_OPT?=-v -race -timeout 30s
9GOTEST_OPT_WITH_COVERAGE = $(GOTEST_OPT) -coverprofile=coverage.txt -covermode=atomic
10GOTEST=go test
11GOFMT=gofmt
12GOLINT=golint
13GOIMPORTS=goimports
14GOVET=go vet
15EMBEDMD=embedmd
16STATICCHECK=staticcheck
17# TODO decide if we need to change these names.
18README_FILES := $(shell find . -name '*README.md' | sort | tr '\n' ' ')
19
20.DEFAULT_GOAL := defaul-goal
21
22.PHONY: defaul-goal
23defaul-goal: fmt lint vet embedmd goimports staticcheck test
24
25# TODO: enable test-with-cover when find out why "scripts/check-test-files.sh: 4: set: Illegal option -o pipefail"
26.PHONY: travis-ci
27travis-ci: fmt lint vet embedmd goimports staticcheck test test-386 test-with-coverage
28
29all-pkgs:
30	@echo $(ALL_PKGS) | tr ' ' '\n' | sort
31
32all-srcs:
33	@echo $(ALL_SRC) | tr ' ' '\n' | sort
34
35.PHONY: test
36test:
37	$(GOTEST) $(GOTEST_OPT) $(ALL_PKGS)
38
39.PHONY: test-386
40test-386:
41	GOARCH=386 $(GOTEST) -v -timeout 30s $(ALL_PKGS)
42
43.PHONY: test-with-coverage
44test-with-coverage:
45	@echo pre-compiling tests
46	@time go test -i $(ALL_PKGS)
47	$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) $(ALL_PKGS)
48	go tool cover -html=coverage.txt -o coverage.html
49
50.PHONY: test-with-cover
51test-with-cover:
52	@echo Verifying that all packages have test files to count in coverage
53	@scripts/check-test-files.sh $(subst contrib.go.opencensus.io/exporter/stackdriver,./,$(ALL_PKGS))
54	@echo pre-compiling tests
55	@time go test -i $(ALL_PKGS)
56	$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) $(ALL_PKGS)
57	go tool cover -html=coverage.txt -o coverage.html
58
59.PHONY: fmt
60fmt:
61	@FMTOUT=`$(GOFMT) -s -l $(ALL_SRC) 2>&1`; \
62	if [ "$$FMTOUT" ]; then \
63		echo "$(GOFMT) FAILED => gofmt the following files:\n"; \
64		echo "$$FMTOUT\n"; \
65		exit 1; \
66	else \
67	    echo "Fmt finished successfully"; \
68	fi
69
70.PHONY: lint
71lint:
72	@LINTOUT=`$(GOLINT) $(ALL_PKGS) 2>&1`; \
73	if [ "$$LINTOUT" ]; then \
74		echo "$(GOLINT) FAILED => clean the following lint errors:\n"; \
75		echo "$$LINTOUT\n"; \
76		exit 1; \
77	else \
78	    echo "Lint finished successfully"; \
79	fi
80
81.PHONY: vet
82vet:
83    # TODO: Understand why go vet downloads "github.com/google/go-cmp v0.2.0"
84	@VETOUT=`$(GOVET) ./... | grep -v "go: downloading" 2>&1`; \
85	if [ "$$VETOUT" ]; then \
86		echo "$(GOVET) FAILED => go vet the following files:\n"; \
87		echo "$$VETOUT\n"; \
88		exit 1; \
89	else \
90	    echo "Vet finished successfully"; \
91	fi
92
93.PHONY: embedmd
94embedmd:
95	@EMBEDMDOUT=`$(EMBEDMD) -d $(README_FILES) 2>&1`; \
96	if [ "$$EMBEDMDOUT" ]; then \
97		echo "$(EMBEDMD) FAILED => embedmd the following files:\n"; \
98		echo "$$EMBEDMDOUT\n"; \
99		exit 1; \
100	else \
101	    echo "Embedmd finished successfully"; \
102	fi
103
104.PHONY: goimports
105goimports:
106	@IMPORTSOUT=`$(GOIMPORTS) -d . 2>&1`; \
107	if [ "$$IMPORTSOUT" ]; then \
108		echo "$(GOIMPORTS) FAILED => fix the following goimports errors:\n"; \
109		echo "$$IMPORTSOUT\n"; \
110		exit 1; \
111	else \
112	    echo "Goimports finished successfully"; \
113	fi
114
115.PHONY: staticcheck
116staticcheck:
117	$(STATICCHECK) ./...
118
119.PHONY: install-tools
120install-tools:
121	GO111MODULE=on go install \
122		golang.org/x/lint/golint \
123		golang.org/x/tools/cmd/goimports \
124		github.com/rakyll/embedmd \
125		honnef.co/go/tools/cmd/staticcheck
126