Skip to content

Commit 6d8c7a2

Browse files
Merge pull request #36 from Clever/INFRANG-6623-update-golang-makefile
fix(INFRANG-6623): Update golang.mk
2 parents 58f2bd6 + 6a2cd18 commit 6d8c7a2

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

.circleci/config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
version: 2
22
jobs:
33
build:
4-
working_directory: /go/src/github.com/Clever/gitbot
4+
working_directory: ~/go/src/github.com/Clever/gitbot
55
docker:
6-
- image: circleci/golang:1.13-stretch
6+
- image: cimg/go:1.21
77
environment:
88
GOPRIVATE: github.com/Clever/*
99
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*~
22
version.go
33
gitbot
4+
bin
5+
vendor

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module github.com/Clever/gitbot
22

3-
go 1.13
3+
go 1.21
44

55
require (
66
github.com/stretchr/testify v0.0.0-20150110193518-2160c81a963b
7-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
87
gopkg.in/yaml.v2 v2.0.0-20150224225758-49c95bdc2184
98
)
9+
10+
require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect

golang.mk

+61-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This is the default Clever Golang Makefile.
22
# It is stored in the dev-handbook repo, github.com/Clever/dev-handbook
33
# Please do not alter this file directly.
4-
GOLANG_MK_VERSION := 1.0.0
4+
GOLANG_MK_VERSION := 1.3.1
55

66
SHELL := /bin/bash
77
SYSTEM := $(shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]')
@@ -11,7 +11,7 @@ SYSTEM := $(shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]')
1111
export TZ=UTC
1212

1313
# go build flags for use across all commands which accept them
14-
GO_BUILD_FLAGS := "-mod=vendor"
14+
export GOFLAGS := -mod=vendor $(GOFLAGS)
1515

1616
# if the gopath includes several directories, use only the first
1717
GOPATH=$(shell echo $$GOPATH | cut -d: -f1)
@@ -39,17 +39,19 @@ endef
3939
# so we're defended against it breaking or changing in the future.
4040
FGT := $(GOPATH)/bin/fgt
4141
$(FGT):
42-
go get github.com/GeertJohan/fgt@262f7b11eec07dc7b147c44641236f3212fee89d
42+
go install -mod=readonly github.com/GeertJohan/fgt@262f7b11eec07dc7b147c44641236f3212fee89d
4343

4444
golang-ensure-curl-installed:
4545
@command -v curl >/dev/null 2>&1 || { echo >&2 "curl not installed. Please install curl."; exit 1; }
4646

4747
# Golint is a tool for linting Golang code for common errors.
4848
# We pin its version because an update could add a new lint check which would make
4949
# previously passing tests start failing without changing our code.
50+
# this package is deprecated and frozen
51+
# Infra recommendation is to eventually move to https://github.com/golangci/golangci-lint so don't fail on linting error for now
5052
GOLINT := $(GOPATH)/bin/golint
5153
$(GOLINT):
52-
go get golang.org/x/lint/golint@738671d3881b9731cc63024d5d88cf28db875626
54+
go install -mod=readonly golang.org/x/lint/golint@738671d3881b9731cc63024d5d88cf28db875626
5355

5456
# golang-fmt-deps requires the FGT tool for checking output
5557
golang-fmt-deps: $(FGT)
@@ -74,22 +76,14 @@ endef
7476
# golang-lint-deps-strict requires the golint tool for golang linting.
7577
golang-lint-deps-strict: $(GOLINT) $(FGT)
7678

77-
# golang-lint-strict calls golint on all golang files in the pkg and fails if any lint
78-
# errors are found.
79-
# arg1: pkg path
80-
define golang-lint-strict
81-
@echo "LINTING $(1)..."
82-
@PKG_PATH=$$(go list -f '{{.Dir}}' $(1)); find $${PKG_PATH}/*.go -type f | grep -v gen_ | xargs $(FGT) $(GOLINT)
83-
endef
84-
8579
# golang-test-deps is here for consistency
8680
golang-test-deps:
8781

8882
# golang-test uses the Go toolchain to run all tests in the pkg.
8983
# arg1: pkg path
9084
define golang-test
9185
@echo "TESTING $(1)..."
92-
@go test $(GO_BUILD_FLAGS) -v $(1)
86+
@go test -v $(1)
9387
endef
9488

9589
# golang-test-strict-deps is here for consistency
@@ -99,7 +93,22 @@ golang-test-strict-deps:
9993
# arg1: pkg path
10094
define golang-test-strict
10195
@echo "TESTING $(1)..."
102-
@go test -v $(GO_BUILD_FLAGS) -race $(1)
96+
@go test -v -race $(1)
97+
endef
98+
99+
# golang-test-strict-cover-deps is here for consistency
100+
golang-test-strict-cover-deps:
101+
102+
# golang-test-strict-cover uses the Go toolchain to run all tests in the pkg with the race and cover flag.
103+
# appends coverage results to coverage.txt
104+
# arg1: pkg path
105+
define golang-test-strict-cover
106+
@echo "TESTING $(1)..."
107+
@go test -v -race -cover -coverprofile=profile.tmp -covermode=atomic $(1)
108+
@if [ -f profile.tmp ]; then \
109+
cat profile.tmp | tail -n +2 >> coverage.txt; \
110+
rm profile.tmp; \
111+
fi;
103112
endef
104113

105114
# golang-vet-deps is here for consistency
@@ -109,7 +118,7 @@ golang-vet-deps:
109118
# arg1: pkg path
110119
define golang-vet
111120
@echo "VETTING $(1)..."
112-
@go vet $(GO_BUILD_FLAGS) $(1)
121+
@go vet $(1)
113122
endef
114123

115124
# golang-test-all-deps installs all dependencies needed for different test cases.
@@ -132,24 +141,52 @@ golang-test-all-strict-deps: golang-fmt-deps golang-lint-deps-strict golang-test
132141
# arg1: pkg path
133142
define golang-test-all-strict
134143
$(call golang-fmt,$(1))
135-
$(call golang-lint-strict,$(1))
144+
$(call golang-lint,$(1))
136145
$(call golang-vet,$(1))
137146
$(call golang-test-strict,$(1))
138147
endef
139148

140-
# golang-build: builds a golang binary. ensures CGO build is done during CI. This is needed to make a binary that works with a Docker alpine image.
149+
# golang-test-all-strict-cover-deps: installs all dependencies needed for different test cases.
150+
golang-test-all-strict-cover-deps: golang-fmt-deps golang-lint-deps-strict golang-test-strict-cover-deps golang-vet-deps
151+
152+
# golang-test-all-strict-cover calls fmt, lint, vet and test on the specified pkg with strict and cover
153+
# requirements that no errors are thrown while linting.
154+
# arg1: pkg path
155+
define golang-test-all-strict-cover
156+
$(call golang-fmt,$(1))
157+
$(call golang-lint,$(1))
158+
$(call golang-vet,$(1))
159+
$(call golang-test-strict-cover,$(1))
160+
endef
161+
162+
# golang-build: builds a golang binary
141163
# arg1: pkg path
142164
# arg2: executable name
143165
define golang-build
144-
@echo "BUILDING..."
145-
@if [ -z "$$CI" ]; then \
146-
go build $(GO_BUILD_FLAGS) -o bin/$(2) $(1); \
147-
else \
148-
echo "-> Building CGO binary"; \
149-
CGO_ENABLED=0 go build $(GO_BUILD_FLAGS) -installsuffix cgo -o bin/$(2) $(1); \
150-
fi;
166+
@echo "BUILDING $(2)..."
167+
@CGO_ENABLED=0 go build -o bin/$(2) $(1);
168+
endef
169+
170+
# golang-debug-build: builds a golang binary with debugging capabilities
171+
# arg1: pkg path
172+
# arg2: executable name
173+
define golang-debug-build
174+
@echo "BUILDING $(2) FOR DEBUG..."
175+
@CGO_ENABLED=0 go build -gcflags="all=-N -l" -o bin/$(2) $(1);
151176
endef
152177

178+
# golang-cgo-build: builds a golang binary with CGO
179+
# arg1: pkg path
180+
# arg2: executable name
181+
define golang-cgo-build
182+
@echo "BUILDING $(2) WITH CGO ..."
183+
@CGO_ENABLED=1 go build -installsuffix cgo -o bin/$(2) $(1);
184+
endef
185+
186+
# golang-setup-coverage: set up the coverage file
187+
golang-setup-coverage:
188+
@echo "mode: atomic" > coverage.txt
189+
153190
# golang-update-makefile downloads latest version of golang.mk
154191
golang-update-makefile:
155192
@wget https://raw.githubusercontent.com/Clever/dev-handbook/master/make/golang-v1.mk -O /tmp/golang.mk 2>/dev/null

0 commit comments

Comments
 (0)