Add `make tidy` to check for up-to-date go.mod

This commit adds the `tidy` target. This target will fail if the go.mod
or go.sum files are not up to date with the code. It will also be a
prerequisite to the `lint` command, implying that it will also be caught
by the Zuul gates.

Change-Id: Ie33de2d793beca100435670346eb0e2ea7b3b0dd
This commit is contained in:
Ian Howell 2019-11-07 15:28:04 -06:00
parent e48b436680
commit 7ecae507e0
2 changed files with 29 additions and 0 deletions

View File

@ -53,11 +53,18 @@ cover: unit-tests
@./tools/coverage_check $(COVER_PROFILE)
.PHONY: lint
lint: tidy
lint: $(LINTER)
@echo "Performing linting step..."
@./$(LINTER) run --config $(LINTER_CONFIG)
@echo "Linting completed successfully"
.PHONY: tidy
tidy:
@echo "Checking that go.mod is up to date..."
@./tools/gomod_check
@echo "go.mod is up to date"
.PHONY: docker-image
docker-image:
@docker build . --build-arg MAKE_TARGET=$(DOCKER_MAKE_TARGET) --tag $(DOCKER_IMAGE) --target $(DOCKER_TARGET_STAGE)

22
tools/gomod_check Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
set -e
backup_dir=$(mktemp -d)
revert() {
cp "$backup_dir/go.mod" "go.mod"
cp "$backup_dir/go.sum" "go.sum"
}
cp go.mod go.sum "$backup_dir"
if [[ $(go mod tidy 2>&1) ]]; then
printf "FAIL: error in go.mod. Please run 'go mod tidy' and fix any issues\n"
revert
exit 1
fi
if [[ $(diff "go.mod" "$backup_dir/go.mod") ]] || [[ $(diff "go.sum" "$backup_dir/go.sum") ]]; then
printf "FAIL: go.mod/go.sum are not up to date. Please run 'go mod tidy'\n"
revert
exit 1
fi