diff --git a/Makefile b/Makefile index 8d8270ea2c4..cf9d51eb842 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ fuzz: verify: verify-gofmt verify-bom verify-lint verify-dep verify-shellcheck verify-goword \ verify-govet verify-license-header verify-receiver-name verify-mod-tidy verify-shellcheck \ - verify-shellws verify-proto-annotations verify-genproto + verify-shellws verify-proto-annotations verify-genproto verify-goimport fix: fix-bom fix-lint ./scripts/fix.sh @@ -109,6 +109,10 @@ verify-proto-annotations: verify-genproto: PASSES="genproto" ./scripts/test.sh +.PHONY: verify-goimport +verify-goimport: + PASSES="goimport" ./scripts/test.sh + # Failpoints GOFAIL_VERSION = $(shell cd tools/mod && go list -m -f {{.Version}} go.etcd.io/gofail) diff --git a/scripts/test.sh b/scripts/test.sh index 4caf3196e0c..1ad67b11613 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -632,6 +632,21 @@ function genproto_pass { "${ETCD_ROOT_DIR}/scripts/verify_genproto.sh" } +function goimport_for_module { + GOFILES=$(run go list --f "{{with \$d:=.}}{{range .GoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) + TESTGOFILES=$(run go list --f "{{with \$d:=.}}{{range .TestGoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) + cd "${ETCD_ROOT_DIR}/tools/mod" + FILESNEEDSFIX=$(echo "${GOFILES}" "${TESTGOFILES}" | grep -v '.gw.go' | grep -v '.pb.go' | xargs -n 100 go run golang.org/x/tools/cmd/goimports -l -local go.etcd.io) + if [ -n "$FILESNEEDSFIX" ]; then + log_error -e "the following files are not sync with 'goimports'. run 'make fix'\\n$FILESNEEDSFIX" + return 255 + fi +} + +function goimport_pass { + run_for_modules goimport_for_module +} + ########### MAIN ############################################################### function run_pass {