Skip to content

Commit

Permalink
Simplify Makefile (open-telemetry#354)
Browse files Browse the repository at this point in the history
* Simplify Makefile

In particular, this change:
* Ensures that the race checker is always used for all tests
* Removes redundant commands
* Splits out `generate` and `lint` as their own commands
* Renames the `circle-ci` command to the more generic `ci`

* Use GOTEST_WITH_COVERAGE instead of old name

* Fix test-with-coverage command

* Fix test-386 command
  • Loading branch information
iredelmeier authored and rghetia committed Nov 27, 2019
1 parent 77543cd commit e6d7256
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- run:
name: "Precommit and Coverage Report"
command: |
make circle-ci
make ci
mv coverage.html $TEST_RESULTS/
- save_cache:
Expand Down
76 changes: 37 additions & 39 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ ALL_DOCS := $(shell find . -name '*.md' -type f | sort)
# All directories with go.mod files. Used in go mod tidy.
ALL_GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort)

GOTEST=go test
GOTEST_OPT?=-v -timeout 30s
GOTEST_OPT_WITH_RACE = $(GOTEST_OPT) -race
GOTEST_OPT_WITH_COVERAGE = $(GOTEST_OPT) -coverprofile=coverage.txt -covermode=atomic
GOTEST_MIN = go test -v -timeout 30s
GOTEST = $(GOTEST_MIN) -race
GOTEST_WITH_COVERAGE = $(GOTEST) -coverprofile=coverage.txt -covermode=atomic

.DEFAULT_GOAL := precommit

Expand All @@ -27,41 +26,22 @@ $(TOOLS_DIR)/misspell: go.mod go.sum tools.go
$(TOOLS_DIR)/stringer: go.mod go.sum tools.go
go build -o $(TOOLS_DIR)/stringer golang.org/x/tools/cmd/stringer

precommit: $(TOOLS_DIR)/golangci-lint $(TOOLS_DIR)/misspell $(TOOLS_DIR)/stringer
PATH="$(abspath $(TOOLS_DIR)):$${PATH}" go generate ./...
# TODO: Fix this on windows.
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "compiling all packages in $${dir}"; \
(cd "$${dir}" && \
go build ./... && \
go test -run xxxxxMatchNothingxxxxx ./... >/dev/null); \
done
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "golangci-lint in $${dir}"; \
(cd "$${dir}" && \
$(abspath $(TOOLS_DIR))/golangci-lint run --fix); \
done
$(TOOLS_DIR)/misspell -w $(ALL_DOCS)
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go mod tidy in $${dir}"; \
(cd "$${dir}" && \
go mod tidy); \
done
precommit: lint generate build examples test

.PHONY: test-with-coverage
test-with-coverage:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... + coverage in $${dir}"; \
(cd "$${dir}" && \
$(GOTEST) $(GOTEST_OPT_WITH_COVERAGE) ./... && \
$(GOTEST_WITH_COVERAGE) ./... && \
go tool cover -html=coverage.txt -o coverage.html); \
done

.PHONY: circle-ci
circle-ci: precommit test-clean-work-tree test-with-coverage test-race test-386 examples
.PHONY: ci
ci: precommit check-clean-work-tree test-with-coverage test-386

.PHONY: test-clean-work-tree
test-clean-work-tree:
.PHONY: check-clean-work-tree
check-clean-work-tree:
@if ! git diff --quiet; then \
echo; \
echo 'Working tree is not clean, did you forget to run "make precommit"?'; \
Expand All @@ -70,29 +50,30 @@ test-clean-work-tree:
exit 1; \
fi

.PHONY: test
test: examples
.PHONY: build
build:
# TODO: Fix this on windows.
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... + race in $${dir}"; \
echo "compiling all packages in $${dir}"; \
(cd "$${dir}" && \
$(GOTEST) $(GOTEST_OPT) ./... && \
$(GOTEST) $(GOTEST_OPT_WITH_RACE) ./...); \
go build ./... && \
go test -run xxxxxMatchNothingxxxxx ./... >/dev/null); \
done

.PHONY: test-race
test-race:
.PHONY: test
test:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test -race ./... in $${dir}"; \
echo "go test ./... + race in $${dir}"; \
(cd "$${dir}" && \
$(GOTEST) $(GOTEST_OPT_WITH_RACE) ./...); \
$(GOTEST) ./...); \
done

.PHONY: test-386
test-386:
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go test ./... GOARCH 386 in $${dir}"; \
(cd "$${dir}" && \
GOARCH=386 $(GOTEST) -v -timeout 30s ./...); \
GOARCH=386 $(GOTEST_MIN) ./...); \
done

.PHONY: examples
Expand All @@ -102,3 +83,20 @@ examples:
(cd "$${ex}" && \
go build .); \
done

.PHONY: lint
lint: $(TOOLS_DIR)/golangci-lint $(TOOLS_DIR)/misspell $(TOOLS_DIR)/stringer
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "golangci-lint in $${dir}"; \
(cd "$${dir}" && \
$(abspath $(TOOLS_DIR))/golangci-lint run --fix); \
done
$(TOOLS_DIR)/misspell -w $(ALL_DOCS)
set -e; for dir in $(ALL_GO_MOD_DIRS); do \
echo "go mod tidy in $${dir}"; \
(cd "$${dir}" && \
go mod tidy); \
done

generate:
PATH="$(abspath $(TOOLS_DIR)):$${PATH}" go generate ./...

0 comments on commit e6d7256

Please sign in to comment.