Fix update/verify-mocks.sh

There appears to be a bug in `go generate` for workspaces which will be
fixed in the 1.22.1 release.
This commit is contained in:
Tim Hockin
2022-02-25 08:07:59 -08:00
parent 58ab5eea89
commit b725fd20c2
4 changed files with 43 additions and 52 deletions

View File

@@ -23,28 +23,24 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
# Explicitly opt into go modules
export GO111MODULE=on
kube::golang::verify_go_version
kube::golang::old::setup_env
kube::golang::new::setup_env
echo 'installing mockgen'
pushd "${KUBE_ROOT}/hack/tools" >/dev/null
go install github.com/golang/mock/mockgen
popd >/dev/null
function git_find() {
# Similar to find but faster and easier to understand. We want to include
# modified and untracked files because this might be running against code
# which is not tracked by git yet.
git ls-files -cmo --exclude-standard \
function git_grep() {
git grep --untracked --exclude-standard \
"$@" \
':!:vendor/*' `# catches vendor/...` \
':!:*/vendor/*' `# catches any subdir/vendor/...` \
':!:third_party/*' `# catches third_party/...` \
':!:*/third_party/*' `# catches third_party/...` \
':!:*/testdata/*' \
"$@"
':!:*/testdata/*' `# catches any testdata` \
':(glob)**/*.go'
}
cd "${KUBE_ROOT}"
@@ -55,19 +51,12 @@ GENERATED_MOCK_FILE_REGEX="^// Code generated by MockGen. DO NOT EDIT.$"
tmp=$(mktemp)
kube::util::trap_add "rm -f ${tmp:?}" EXIT
# We use this pattern here rather than `git grep` because we don't really want
# to encode the pathspec list in multiple places and anything more complicated
# just isn't worth the effort.
git_find -z ':(glob)**/*.go' \
| { xargs -0 grep -l --null "${GENERATED_MOCK_FILE_REGEX}" || true; } \
| xargs -0 rm -f
git_grep -l -z "${GENERATED_MOCK_FILE_REGEX}" | xargs -0 rm -f
echo 'executing go generate command on below files'
git_find -z ':(glob)**/*.go' | while read -r -d $'\0' file; do
test -f "$file" || continue
grep -q "//go:generate mockgen" "$file" || continue
git_grep -l -z "//go:generate mockgen" | while read -r -d $'\0' file; do
echo "- ${file}"
temp_file_name="$(kube::realpath "$(mktemp -t "$(basename "$0").XXXXXX")")"
# search for build tag used in file
@@ -82,7 +71,9 @@ git_find -z ':(glob)**/*.go' | while read -r -d $'\0' file; do
BUILD_TAG_FILE=$temp_file_name go generate -v "$file"
else
# if no +build tag is defined in interface file
go generate -v "$file"
# NOTE: This works around a bug in `go generate` with workspaces, which
# should be fixed in Go 1.22.1.
go -C "$(dirname "$file")" generate "$(basename "$file")"
fi
done