Skip to content

Commit

Permalink
Merge pull request #15485 from HanningLin/verify-goimport
Browse files Browse the repository at this point in the history
Add verify-goimport to verify all import items are well sorted
  • Loading branch information
serathius authored Mar 16, 2023
2 parents 3717448 + 8bc6b28 commit f3587be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f3587be

Please sign in to comment.