Merge pull request #116130 from pohly/test-golangci-lint-improvements

golangci-lint improvements
This commit is contained in:
Kubernetes Prow Robot 2023-02-28 14:54:07 -08:00 committed by GitHub
commit de995809d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 22 deletions

View File

@ -32,9 +32,9 @@ linters-settings: # please keep this alphabetized
custom: custom:
logcheck: logcheck:
# Installed there by hack/verify-golangci-lint.sh. # Installed there by hack/verify-golangci-lint.sh.
path: _output/local/bin/logcheck.so path: ../_output/local/bin/logcheck.so
description: structured logging checker description: structured logging checker
original-url: k8s.io/klog/hack/tools original-url: k8s.io/logtools/logcheck
gocritic: gocritic:
enabled-checks: enabled-checks:
- equalFold - equalFold

View File

@ -26,12 +26,25 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh" source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/util.sh" source "${KUBE_ROOT}/hack/lib/util.sh"
kube::golang::verify_go_version
# Ensure that we find the binaries we build before anything else. # Ensure that we find the binaries we build before anything else.
export GOBIN="${KUBE_OUTPUT_BINPATH}" export GOBIN="${KUBE_OUTPUT_BINPATH}"
PATH="${GOBIN}:${PATH}" PATH="${GOBIN}:${PATH}"
invocation=(./hack/verify-golangci-lint.sh "$@")
# The logcheck plugin currently has to be configured via env variables
# (https://github.com/golangci/golangci-lint/issues/1512).
#
# Remember to clean the golangci-lint cache when changing
# the configuration and running this script multiple times,
# otherwise golangci-lint will report stale results:
# _output/local/bin/golangci-lint cache clean
golangci=(env LOGCHECK_CONFIG="${KUBE_ROOT}/hack/logcheck.conf" "${GOBIN}/golangci-lint" run)
golangci_config="${KUBE_ROOT}/hack/golangci.yaml"
golangci+=(--config="${golangci_config}")
kube::golang::verify_go_version
# Explicitly opt into go modules, even though we're inside a GOPATH directory # Explicitly opt into go modules, even though we're inside a GOPATH directory
export GO111MODULE=on export GO111MODULE=on
@ -44,29 +57,18 @@ popd >/dev/null
cd "${KUBE_ROOT}" cd "${KUBE_ROOT}"
## The config is in ${KUBE_ROOT}/.golangci.yaml where it will be found
## even when golangci-lint is invoked in a sub-directory.
##
## The logcheck plugin currently has to be configured via env variables
## (https://github.com/golangci/golangci-lint/issues/1512).
##
## Remember to clean the golangci-lint cache when changing
## the configuration and running this script multiple times,
## otherwise golangci-lint will report stale results:
## _output/local/bin/golangci-lint cache clean
export LOGCHECK_CONFIG="${KUBE_ROOT}/hack/logcheck.conf"
echo 'running golangci-lint ' >&2
res=0 res=0
if [[ "$#" -gt 0 ]]; then if [[ "$#" -gt 0 ]]; then
golangci-lint run "$@" >&2 || res=$? echo "running ${golangci[*]} $*" >&2
"${golangci[@]}" "$@" >&2 || res=$?
else else
golangci-lint run ./... >&2 || res=$? echo "running ${golangci[*]} ./..." >&2
"${golangci[@]}" ./... >&2 || res=$?
for d in staging/src/k8s.io/*; do for d in staging/src/k8s.io/*; do
MODPATH="staging/src/k8s.io/$(basename "${d}")" MODPATH="staging/src/k8s.io/$(basename "${d}")"
echo "running golangci-lint for ${KUBE_ROOT}/${MODPATH}" echo "running ( cd ${KUBE_ROOT}/${MODPATH}; ${golangci[*]} --path-prefix ${MODPATH} ./... )"
pushd "${KUBE_ROOT}/${MODPATH}" >/dev/null pushd "${KUBE_ROOT}/${MODPATH}" >/dev/null
golangci-lint --path-prefix "${MODPATH}" run ./... >&2 || res=$? "${golangci[@]}" --path-prefix "${MODPATH}" ./... >&2 || res=$?
popd >/dev/null popd >/dev/null
done done
fi fi
@ -77,7 +79,7 @@ if [ "$res" -eq 0 ]; then
else else
{ {
echo echo
echo 'Please review the above warnings. You can test via "./hack/verify-golangci-lint.sh"' echo "Please review the above warnings. You can test via \"${invocation[*]}\""
echo 'If the above warnings do not make sense, you can exempt this warning with a comment' echo 'If the above warnings do not make sense, you can exempt this warning with a comment'
echo ' (if your reviewer is okay with it).' echo ' (if your reviewer is okay with it).'
echo 'In general please prefer to fix the error, we have already disabled specific lints' echo 'In general please prefer to fix the error, we have already disabled specific lints'