From 7ecae507e0c7551209f54395073bebcf0a9e932d Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Thu, 7 Nov 2019 15:28:04 -0600 Subject: [PATCH] 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 --- Makefile | 7 +++++++ tools/gomod_check | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100755 tools/gomod_check diff --git a/Makefile b/Makefile index 7327c188a..a92858ee0 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/tools/gomod_check b/tools/gomod_check new file mode 100755 index 000000000..034dd9a18 --- /dev/null +++ b/tools/gomod_check @@ -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