Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 5ae1c52

Browse files
authored
Golang v1.11 module support for build-tools (#60)
* Bump to golang v1.11.4 * Convert to go modules * Add the with_vendor directory to the test * Update tests for go 1.11 behavior * Use -mod=vendor if vendor directory exists
1 parent f417856 commit 5ae1c52

File tree

23 files changed

+691
-230
lines changed

23 files changed

+691
-230
lines changed

.buildkite/test.sh

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@
22

33
# This script is used to build drud/build-tools using buildkite
44

5-
# Manufacture a $GOPATH environment that can mount on docker (when buildkite build)
6-
export GOPATH=~/tmp/buildkite-fake-gopath/$BUILDKITE_JOB_ID
7-
DRUDSRC=$GOPATH/src/github.com/drud
8-
mkdir -p $DRUDSRC
9-
ln -s $PWD $DRUDSRC/build-tools
10-
cd $DRUDSRC/build-tools
11-
BUILD_OS=$(go env GOOS)
12-
echo "--- buildkite building $BUILDKITE_JOB_ID at $(date) on $HOSTNAME for OS=$(go env GOOS) in $PWD GOPATH=$GOPATH"
135

146
set -o errexit
157
set -o pipefail
168
set -o nounset
179
set -x
1810

11+
BUILD_OS=$(go env GOOS)
12+
echo "--- buildkite building $BUILDKITE_JOB_ID at $(date) on $HOSTNAME for OS=$(go env GOOS) in $PWD"
13+
1914
# Our testbot should now be sane, run the testbot checker to make sure.
2015
echo "--- Checking for sane testbot"
2116
./.buildkite/sanetestbot.sh

.circleci/config.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ stages:
33
build:
44
machine:
55
image: circleci/classic:201711-01
6-
working_directory: /home/circleci/go/src/github.com/drud/build-tools
76

87
environment:
98
GOPATH: /home/circleci/go
109

1110
steps:
1211
- run: sudo rm -rf /usr/local/go &&
13-
wget -q -O /tmp/golang.tgz https://dl.google.com/go/go1.11.linux-amd64.tar.gz &&
12+
wget -q -O /tmp/golang.tgz https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz &&
1413
sudo tar -C /usr/local -xzf /tmp/golang.tgz
1514

16-
- run: mkdir -p ~/go/src/github.com/drud/build-tools && mkdir -p ~/go/lib && mkdir -p ~/go/bin
17-
1815
- checkout
1916

2017
- run: cd tests && make test

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ make linux
4040
make darwin
4141
make gofmt
4242
make govet
43-
make govendor
4443
make golint
4544
make codecoroner
46-
make static (gofmt, govet, golint, govendor)
45+
make static (gofmt, govet, golint)
4746
make test
4847
make container
4948
make push

makefile_components/base_build_go.mak

Lines changed: 37 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44
##### contents into ../Makefile and commenting out the include and adding a
55
##### comment about what you did and why.
66

7-
8-
.PHONY: all build test push clean container-clean bin-clean version static govendor gofmt govet golint golangci-lint container
7+
DOCKERBUILDCMD=docker run -t --rm -u $(shell id -u):$(shell id -g) \
8+
-v "$(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG)" \
9+
-v "$(S)$$PWD:/workdir$(DOCKERMOUNTFLAG)" \
10+
-e CGO_ENABLED=0 \
11+
-e GOOS=$@ \
12+
-w $(S)/workdir \
13+
$(BUILD_IMAGE)
14+
15+
DOCKERTESTCMD=docker run -t --rm -u $(shell id -u):$(shell id -g) \
16+
-v "$(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG)" \
17+
-v "$(S)$$PWD:/workdir$(DOCKERMOUNTFLAG)" \
18+
-w $(S)/workdir \
19+
$(BUILD_IMAGE)
20+
21+
.PHONY: all build test push clean container-clean bin-clean version static gofmt govet golint golangci-lint container
922
GOTMP=.gotmp
1023

1124
SHELL = /bin/bash
@@ -14,7 +27,7 @@ GOFILES = $(shell find $(SRC_DIRS) -name "*.go")
1427

1528
BUILD_OS = $(shell go env GOHOSTOS)
1629

17-
BUILD_IMAGE ?= drud/golang-build-container:v1.11
30+
BUILD_IMAGE ?= drud/golang-build-container:v1.11.4.2
1831

1932
BUILD_BASE_DIR ?= $$PWD
2033

@@ -35,6 +48,11 @@ VERSION_LDFLAGS := $(foreach v,$(VERSION_VARIABLES),-X "$(PKG)/pkg/version.$(v)=
3548
LDFLAGS := -extldflags -static $(VERSION_LDFLAGS)
3649
DOCKERMOUNTFLAG := :delegated
3750

51+
# In go 1.11 -mod=vendor is not autodetected; it probably will be in 1.12
52+
# See https://github.com/golang/go/issues/27227
53+
USEMODVENDOR := $(shell if [ -d vendor ]; then echo "-mod=vendor"; fi)
54+
55+
3856
PWD=$(shell pwd)
3957
S =
4058
ifeq ($(BUILD_OS),windows)
@@ -47,143 +65,70 @@ build: $(BUILD_OS)
4765

4866
linux darwin windows: $(GOFILES)
4967
@echo "building $@ from $(SRC_AND_UNDER)"
50-
@$(shell rm -f VERSION.txt)
51-
@$(shell mkdir -p bin/$@ $(GOTMP)/{std/$@,bin,src/$(PKG)})
52-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
53-
-v "$(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG)" \
54-
-v "$(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG)" \
55-
-v "$(S)$$PWD/bin/$@:/go/bin$(DOCKERMOUNTFLAG)" \
56-
-v "$(S)$$PWD/bin/$@:/go/bin/$@$(DOCKERMOUNTFLAG)" \
57-
-v "$(S)$$PWD/$(GOTMP)/std/$@:/usr/local/go/pkg/$@_amd64_static$(DOCKERMOUNTFLAG)" \
58-
-e CGO_ENABLED=0 \
59-
-e GOOS=$@ \
60-
-w $(S)/go/src/$(PKG) \
61-
$(BUILD_IMAGE) \
62-
go install -installsuffix static -ldflags ' $(LDFLAGS) ' $(SRC_AND_UNDER)
68+
@mkdir -p $(GOTMP)/{.cache,pkg,src,bin}
69+
@$(DOCKERBUILDCMD) \
70+
go install $(USEMODVENDOR) -installsuffix static -ldflags ' $(LDFLAGS) ' $(SRC_AND_UNDER)
6371
@$(shell touch $@)
72+
$( shell if [ -d $(GOTMP) ]; then chmod -R u+w $(GOTMP); fi )
6473
@echo $(VERSION) >VERSION.txt
6574

66-
govendor:
67-
@echo -n "Using govendor to check for missing dependencies and unused dependencies: "
68-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
69-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
70-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
71-
-w $(S)/go/src/$(PKG) \
72-
$(BUILD_IMAGE) \
73-
bash -c 'OUT=$$(govendor list +missing +unused); if [ -n "$$OUT" ]; then echo "$$OUT"; exit 1; fi'
74-
7575
gofmt:
7676
@echo "Checking gofmt: "
77-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
78-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
79-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
80-
-w $(S)/go/src/$(PKG) \
81-
$(BUILD_IMAGE) \
77+
@$(DOCKERTESTCMD) \
8278
bash -c 'export OUT=$$(gofmt -l $(SRC_DIRS)) && if [ -n "$$OUT" ]; then echo "These files need gofmt -w: $$OUT"; exit 1; fi'
8379

8480
govet:
8581
@echo "Checking go vet: "
86-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
87-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
88-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
89-
-w $S/go/src/$(PKG) \
90-
$(BUILD_IMAGE) \
82+
@$(DOCKERTESTCMD) \
9183
bash -c 'go vet $(SRC_AND_UNDER)'
9284

9385
golint:
9486
@echo "Checking golint: "
95-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
96-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
97-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
98-
-w $(S)/go/src/$(PKG) \
99-
$(BUILD_IMAGE) \
87+
@$(DOCKERTESTCMD) \
10088
bash -c 'export OUT=$$(golint $(SRC_AND_UNDER)) && if [ -n "$$OUT" ]; then echo "Golint problems discovered: $$OUT"; exit 1; fi'
10189

10290
errcheck:
10391
@echo "Checking errcheck: "
104-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
105-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
106-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
107-
-w $(S)/go/src/$(PKG) \
108-
$(BUILD_IMAGE) \
92+
@$(DOCKERTESTCMD) \
10993
errcheck $(SRC_AND_UNDER)
11094

11195
staticcheck:
11296
@echo "Checking staticcheck: "
113-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
114-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
115-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
116-
-w $(S)/go/src/$(PKG) \
117-
$(BUILD_IMAGE) \
97+
@$(DOCKERTESTCMD) \
11898
staticcheck $(SRC_AND_UNDER)
11999

120-
unused:
121-
@echo "Checking unused variables and functions: "
122-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
123-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
124-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
125-
-w $(S)/go/src/$(PKG) \
126-
$(BUILD_IMAGE) \
127-
unused $(SRC_AND_UNDER)
128-
129-
codecoroner:
130-
@echo "Checking codecoroner for unused functions: "
131-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
132-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
133-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
134-
-w $(S)/go/src/$(PKG) \
135-
$(BUILD_IMAGE) \
136-
bash -c 'OUT=$$(codecoroner -tests -ignore vendor funcs $(SRC_AND_UNDER)); if [ -n "$$OUT" ]; then echo "$$OUT"; exit 1; fi' \
137-
138-
139100
varcheck:
140101
@echo "Checking unused globals and struct members: "
141-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
142-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
143-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
144-
-w $(S)/go/src/$(PKG) \
145-
$(BUILD_IMAGE) \
146-
varcheck $(SRC_AND_UNDER) && structcheck $(SRC_AND_UNDER)
102+
@$(DOCKERTESTCMD) \
103+
bash -c "varcheck $(SRC_AND_UNDER) && structcheck $(SRC_AND_UNDER)"
147104

148105
misspell:
149106
@echo "Checking for misspellings: "
150-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
151-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
152-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
153-
-w $(S)/go/src/$(PKG) \
154-
$(BUILD_IMAGE) \
107+
@$(DOCKERTESTCMD) \
155108
misspell $(SRC_DIRS)
156109

157110
gometalinter:
158111
@echo "gometalinter: "
159-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
160-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
161-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
162-
-w $(S)/go/src/$(PKG) \
163-
$(BUILD_IMAGE) \
112+
@$(DOCKERTESTCMD) \
164113
time gometalinter $(GOMETALINTER_ARGS) $(SRC_AND_UNDER)
165114

166115
golangci-lint:
167116
@echo "golangci-lint: "
168-
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
169-
-v $(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
170-
-v $(S)$$PWD:/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
171-
-w $(S)/go/src/$(PKG) \
172-
$(BUILD_IMAGE) \
117+
@$(DOCKERTESTCMD) \
173118
time bash -c "golangci-lint run $(GOLANGCI_LINT_ARGS) $(SRC_AND_UNDER)"
174119

175120
version:
176121
@echo VERSION:$(VERSION)
177122

178123
clean: container-clean bin-clean
179-
@go clean -cache || echo "You're not running latest golang locally" # Make sure the local go cache is clean for testing
180124

181125
container-clean:
182126
@if docker image inspect $(DOCKER_REPO):$(VERSION) >/dev/null 2>&1; then docker rmi -f $(DOCKER_REPO):$(VERSION); fi
183127
@rm -rf .container-* .dockerfile* .push-* linux darwin windows container VERSION.txt .docker_image
184128

185129
bin-clean:
186-
$(shell rm -rf $(GOTMP) bin .tmp)
130+
@rm -rf bin
131+
$(shell if [ -d $(GOTMP) ]; then chmod -R u+w $(GOTMP) && rm -rf $(GOTMP); fi )
187132

188133
# print-ANYVAR prints the expanded variable
189134
print-%: ; @echo $* = $($*)

makefile_components/base_test_go.mak

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
TESTOS = $(BUILD_OS)
88

99
test: build
10-
@mkdir -p bin/linux
11-
@mkdir -p $(GOTMP)/{src/$(PKG),pkg,bin,std/linux}
1210
@echo "Testing $(SRC_AND_UNDER) with TESTARGS=$(TESTARGS)"
11+
@mkdir -p $(GOTMP)/{.cache,pkg,src,bin}
1312
@docker run -t --rm -u $(shell id -u):$(shell id -g) \
1413
-v $(PWD)/$(GOTMP):/go$(DOCKERMOUNTFLAG) \
1514
-v $(PWD):/go/src/$(PKG)$(DOCKERMOUNTFLAG) \
@@ -18,7 +17,8 @@ test: build
1817
-e CGO_ENABLED=0 \
1918
-w /go/src/$(PKG) \
2019
$(BUILD_IMAGE) \
21-
go test -v -installsuffix static -ldflags '$(LDFLAGS)' $(SRC_AND_UNDER) $(TESTARGS)
20+
go test $(USEMODVENDOR) -v -installsuffix static -ldflags '$(LDFLAGS)' $(SRC_AND_UNDER) $(TESTARGS)
21+
@$(shell chmod -R u+w $(GOTMP))
2222

2323
# test_precompile allows a full compilation of _test.go files, without execution of the tests.
2424
# Setup and teardown in TestMain is still executed though, so this can cost some time.

tests/Makefile

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ UPSTREAM_REPO ?= golang:1.7.5-alpine3.5
1313

1414
export WORKING_DIR = $(shell pwd)
1515

16+
TESTDOCKERCMD=docker run -t --rm -u $(shell id -u):$(shell id -g) \
17+
-v "$(S)$$PWD/$(GOTMP):/go$(DOCKERMOUNTFLAG)" \
18+
-v "$(S)$$PWD:/workdir$(DOCKERMOUNTFLAG)" \
19+
-v "$(S)$$PWD/$(GOTMP)/std/$@:/usr/local/go/pkg/$@_amd64_static$(DOCKERMOUNTFLAG)" \
20+
-w $(S)/workdir \
21+
$(BUILD_IMAGE)
22+
23+
1624
# Top-level directories to build - Only build the explicit complex fail stuff by default. Unit test will be overridden.
1725
SRC_DIRS := cmd pkg
1826

@@ -54,10 +62,9 @@ include ../makefile_components/base_push.mak
5462
# We can't use the standard 'test' target because this one actually uses 'make' and counts on resources unavailable in
5563
# golang compiler container.
5664
test: build
57-
@mkdir -p bin/linux
58-
@mkdir -p $(GOTMP)/{src/$(PKG),pkg,bin,std/linux}
59-
go test -v -installsuffix "static" -ldflags '$(LDFLAGS)' $(SRC_AND_UNDER) $(TESTARGS)
60-
$(MAKE) -C standard_target $@
65+
@go test $(USEMODVENDOR) -v -installsuffix "static" -ldflags '$(LDFLAGS)' $(SRC_AND_UNDER) $(TESTARGS)
66+
@$(MAKE) -C standard_target $@
67+
6168

6269
# test_precompile allows a full compilation of _test.go files, without execution of the tests.
6370
# Setup and teardown in TestMain is still executed though, so this can cost some time.
@@ -66,15 +73,9 @@ test_precompile: test
6673

6774
# Simple way to execute a random command in the container for tests - used only for testing
6875
# Example: make COMMAND="govendor fetch golang.org/x/net/context"
69-
COMMAND := govendor list
76+
# COMMAND := govendor list
7077
container_cmd:
71-
@docker run \
72-
-t --rm -u $(shell id -u):$(shell id -g) \
73-
-v $(PWD)/$(GOTMP):/go \
74-
-v $(PWD):/go/src/$(PKG) \
75-
-v $(PWD)/bin/linux:/go/bin \
76-
-v $(PWD)/$(GOTMP)/std/linux:/usr/local/go/pkg/linux_amd64_static \
77-
-w /go/src/$(PKG) \
78-
$(BUILD_IMAGE) \
78+
#@$(shell if [ -f ./go.mod ] ; then GOPATH=$$PWD/$(GOTMP) go get ./...; fi; )
79+
@$(TESTDOCKERCMD) \
7980
$(COMMAND)
8081

tests/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/drud/build-tools/tests
2+
3+
require (
4+
github.com/BurntSushi/toml v0.3.1 // indirect
5+
github.com/davecgh/go-spew v1.1.0 // indirect
6+
github.com/dominikh/go-tools v0.0.0-20190102075043-fe93b0e3b36b // indirect
7+
github.com/lextoumbourou/goodhosts v2.1.0+incompatible
8+
github.com/pmezard/go-difflib v1.0.0 // indirect
9+
github.com/stretchr/testify v0.0.0-20170130113145-4d4bfba8f1d1
10+
golang.org/x/tools v0.0.0-20190102183724-79186431cf29 // indirect
11+
honnef.co/go/tools v0.0.0-20190102075043-fe93b0e3b36b // indirect
12+
)

tests/go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/dominikh/go-tools v0.0.0-20190102075043-fe93b0e3b36b h1:cjKkva93RDqb43YJD5xikcP47FR4r9U9O2Sd40tiBTU=
6+
github.com/dominikh/go-tools v0.0.0-20190102075043-fe93b0e3b36b/go.mod h1:qBNLV2EQXN9hMDGPA+nlUNDKcPCtmEvCC8iVgA6uBic=
7+
github.com/lextoumbourou/goodhosts v2.1.0+incompatible h1:1U1p5Z1wrXl23/fW/GY4zdTbQ8UJbyvrkPbqAZ6tzbw=
8+
github.com/lextoumbourou/goodhosts v2.1.0+incompatible/go.mod h1:89s48k108X3gKDWn8AHk3gUzUGTcMZCCAOsE4QU1bbo=
9+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/stretchr/testify v0.0.0-20170130113145-4d4bfba8f1d1 h1:Zx8Rp9ozC4FPFxfEKRSUu8+Ay3sZxEUZ7JrCWMbGgvE=
12+
github.com/stretchr/testify v0.0.0-20170130113145-4d4bfba8f1d1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
13+
golang.org/x/tools v0.0.0-20190102183724-79186431cf29 h1:zIIZ0Uf4ZEDJmZrJJKpW8g8N2+p3o0v0a9L44AJxAVY=
14+
golang.org/x/tools v0.0.0-20190102183724-79186431cf29/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
15+
honnef.co/go/tools v0.0.0-20190102075043-fe93b0e3b36b h1:81sBftiEKRto8JWPj1APoVShwNrPxLja6VpjobUNXxs=
16+
honnef.co/go/tools v0.0.0-20190102075043-fe93b0e3b36b/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

0 commit comments

Comments
 (0)