Merge pull request #116166 from pohly/test-go-vet

fix "go vet" issues, check as part of golangci-lint
This commit is contained in:
Kubernetes Prow Robot
2023-03-03 14:14:58 -08:00
committed by GitHub
62 changed files with 492 additions and 412 deletions

View File

@@ -22,6 +22,7 @@ linters:
enable: # please keep this alphabetized
- ginkgolinter
- gocritic
- govet
- ineffassign
- logcheck
- staticcheck

View File

@@ -19,26 +19,7 @@ set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
source "${KUBE_ROOT}/hack/lib/init.sh"
cd "${KUBE_ROOT}"
# Filter out arguments that start with "-" and move them to goflags.
targets=()
goflags=()
for arg; do
if [[ "${arg}" == -* ]]; then
goflags+=("${arg}")
else
targets+=("${arg}")
fi
done
if [[ ${#targets[@]} -eq 0 ]]; then
# Do not run on third_party directories or generated client code or build tools.
while IFS='' read -r line; do
targets+=("${line}")
done < <(go list -e ./... | grep -E -v "/(build|third_party|vendor|staging|clientset_generated|hack)/")
fi
go vet "${goflags[@]:+${goflags[@]}}" "${targets[@]}"
# Ignore the usual golangci.yaml config because it would
# enable additional linters, then enable just "go vet".
"${KUBE_ROOT}/hack/verify-golangci-lint.sh" -c none -- --disable-all --enable=govet "$@"

View File

@@ -16,7 +16,16 @@
# This script checks coding style for go language files in each
# Kubernetes package by golint.
# Usage: `hack/verify-golangci-lint.sh`.
usage () {
cat <<EOF >&2
Usage: $0 [-- <golangci-lint run flags>] [packages]"
-c <config|"none">: use the specified configuration or none instead of the default hack/golangci.yaml
[packages]: check specific packages or directories instead of everything
EOF
exit 1
}
set -o errexit
set -o nounset
@@ -41,7 +50,35 @@ invocation=(./hack/verify-golangci-lint.sh "$@")
# _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}")
while getopts "c:" o; do
case "${o}" in
c)
if [ "${OPTARG}" = "none" ]; then
golangci_config=""
else
golangci_config="${OPTARG}"
fi
;;
*)
usage
;;
esac
done
if [ "${golangci_config}" ]; then
golangci+=(--config="${golangci_config}")
fi
# Filter out arguments that start with "-" and move them to the run flags.
shift $((OPTIND-1))
targets=()
for arg; do
if [[ "${arg}" == -* ]]; then
golangci+=("${arg}")
else
targets+=("${arg}")
fi
done
kube::golang::verify_go_version
@@ -52,15 +89,18 @@ export GO111MODULE=on
echo "installing golangci-lint and logcheck plugin from hack/tools into ${GOBIN}"
pushd "${KUBE_ROOT}/hack/tools" >/dev/null
go install github.com/golangci/golangci-lint/cmd/golangci-lint
go build -o "${GOBIN}/logcheck.so" -buildmode=plugin sigs.k8s.io/logtools/logcheck/plugin
if [ "${golangci_config}" ]; then
# This cannot be used without a config.
go build -o "${GOBIN}/logcheck.so" -buildmode=plugin sigs.k8s.io/logtools/logcheck/plugin
fi
popd >/dev/null
cd "${KUBE_ROOT}"
res=0
if [[ "$#" -gt 0 ]]; then
echo "running ${golangci[*]} $*" >&2
"${golangci[@]}" "$@" >&2 || res=$?
if [[ "${#targets[@]}" -gt 0 ]]; then
echo "running ${golangci[*]} ${targets[*]}" >&2
"${golangci[@]}" "${targets[@]}" >&2 || res=$?
else
echo "running ${golangci[*]} ./..." >&2
"${golangci[@]}" ./... >&2 || res=$?

View File

@@ -39,4 +39,10 @@ popd >/dev/null
LEVEE_BIN="$(which levee)"
CONFIG_FILE="${KUBE_ROOT}/hack/testdata/levee/levee-config.yaml"
"${KUBE_ROOT}"/hack/verify-govet.sh -vettool="${LEVEE_BIN}" -config="${CONFIG_FILE}"
# Do not run on third_party directories or generated client code or build tools.
targets=()
while IFS='' read -r line; do
targets+=("${line}")
done < <(go list --find -e ./... | grep -E -v "/(build|third_party|vendor|staging|clientset_generated|hack)/")
go vet -vettool="${LEVEE_BIN}" -config="${CONFIG_FILE}" "${targets[@]}"

View File

@@ -1,41 +0,0 @@
#!/usr/bin/env bash
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script vets all *.go files by running `go vet`. It is equivalent to
# `make vet`.
# Usage: `hack/verify-govet.sh` or `make vet`.
# Note: This script is a vestigial redirection. Please do not add "real" logic.
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
# For help output
ARGHELP=""
if [[ "$#" -gt 0 ]]; then
ARGHELP="WHAT='$*'"
fi
echo "NOTE: $0 has been replaced by 'make vet'"
echo
echo "The equivalent of this invocation is: "
echo " make vet ${ARGHELP}"
echo
echo
make --no-print-directory -C "${KUBE_ROOT}" vet WHAT="$*"