Skip to content

Commit 8660fd5

Browse files
authored
Makefile tooling for linting and testing (#29)
1 parent e7a601d commit 8660fd5

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

.circleci/config.yml

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,21 @@ jobs:
44
docker:
55
- image: circleci/golang:1.12.9
66
working_directory: /go/src/github.com/joshdk/go-junit
7-
environment:
8-
GO111MODULE: "on"
97
steps:
108
- checkout
11-
- run:
12-
name: Install tools
13-
working_directory: /tmp
14-
command: go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
159
- run:
1610
name: Lint Go code
17-
command: golangci-lint run
11+
command: make lint
1812

1913
test:
2014
docker:
2115
- image: circleci/golang:1.12.9
2216
working_directory: /go/src/github.com/joshdk/go-junit
23-
environment:
24-
GO111MODULE: "on"
2517
steps:
2618
- checkout
27-
- run:
28-
name: Install tools
29-
working_directory: /tmp
30-
command: go get -u github.com/jstemmer/go-junit-report
3119
- run:
3220
name: Test Go code
33-
command: |
34-
mkdir -p test-results
35-
go test -v 2>&1 | tee test-results/report.log
36-
- run:
37-
name: Generate test report
38-
command: cat test-results/report.log | go-junit-report -go-version $GOLANG_VERSION > test-results/report.xml
39-
when: always
21+
command: make test
4022
- store_test_results:
4123
path: test-results
4224

Makefile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#### Environment ####
2+
3+
# Enforce usage of the Go modules system.
4+
export GO111MODULE := on
5+
6+
# Determine where `go get` will install binaries to.
7+
GOBIN := $(HOME)/go/bin
8+
ifdef GOPATH
9+
GOBIN := $(GOPATH)/bin
10+
endif
11+
12+
# All target for when make is run on its own.
13+
.PHONY: all
14+
all: test lint
15+
16+
#### Binary Dependencies ####
17+
18+
# Install binary for go-junit-report.
19+
go-junit-report := $(GOBIN)/go-junit-report
20+
$(go-junit-report):
21+
cd /tmp && go get -u github.com/jstemmer/go-junit-report
22+
23+
# Install binary for golangci-lint.
24+
golangci-lint := $(GOBIN)/golangci-lint
25+
$(golangci-lint):
26+
cd /tmp && go get -u github.com/golangci/golangci-lint/cmd/[email protected]
27+
28+
# Install binary for goimports.
29+
goimports := $(GOBIN)/goimports
30+
$(goimports):
31+
cd /tmp && go get -u golang.org/x/tools/cmd/goimports
32+
33+
#### Linting ####
34+
35+
# Run code linters.
36+
.PHONY: lint
37+
lint: $(golangci-lint) style
38+
golangci-lint run
39+
40+
# Run code formatters. Unformatted code will fail in CircleCI.
41+
.PHONY: style
42+
style: $(goimports)
43+
ifdef CI
44+
goimports -l .
45+
else
46+
goimports -l -w .
47+
endif
48+
49+
#### Testing ####
50+
51+
# Run Go tests and generate a JUnit XML style test report for ingestion by CircleCI.
52+
.PHONY: test
53+
test: $(go-junit-report)
54+
@mkdir -p test-results
55+
@go test -race -v 2>&1 | tee test-results/report.log
56+
@cat test-results/report.log | go-junit-report -set-exit-code > test-results/report.xml
57+
58+
# Clean up test reports.
59+
.PHONY: clean
60+
clean:
61+
@rm -rf test-results

0 commit comments

Comments
 (0)