...

Text file src/google.golang.org/grpc/scripts/vet.sh

Documentation: google.golang.org/grpc/scripts

     1#!/bin/bash
     2
     3set -ex         # Exit on error; debugging enabled.
     4set -o pipefail # Fail a pipe if any sub-command fails.
     5
     6source "$(dirname $0)/vet-common.sh"
     7
     8# Check to make sure it's safe to modify the user's git repo.
     9git status --porcelain | fail_on_output
    10
    11# noret_grep will return 0 if zero or more lines were selected, and >1 if an
    12# error occurred. Suppresses grep's return code of 1 when there are no matches
    13# (for eg, empty file).
    14noret_grep() {
    15  grep "$@" || [[ $? == 1 ]]
    16}
    17
    18# Undo any edits made by this script.
    19cleanup() {
    20  git reset --hard HEAD
    21}
    22trap cleanup EXIT
    23
    24PATH="${HOME}/go/bin:${GOROOT}/bin:${PATH}"
    25go version
    26
    27if [[ "$1" = "-install" ]]; then
    28  # Install the pinned versions as defined in module tools.
    29  pushd ./test/tools
    30  go install \
    31    golang.org/x/tools/cmd/goimports \
    32    honnef.co/go/tools/cmd/staticcheck \
    33    github.com/client9/misspell/cmd/misspell
    34  popd
    35  exit 0
    36elif [[ "$#" -ne 0 ]]; then
    37  die "Unknown argument(s): $*"
    38fi
    39
    40# - Ensure all source files contain a copyright message.
    41# (Done in two parts because Darwin "git grep" has broken support for compound
    42# exclusion matches.)
    43(grep -L "DO NOT EDIT" $(git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)" -- '*.go') || true) | fail_on_output
    44
    45# - Make sure all tests in grpc and grpc/test use leakcheck via Teardown.
    46not grep 'func Test[^(]' *_test.go
    47not grep 'func Test[^(]' test/*.go
    48
    49# - Check for typos in test function names
    50git grep --quiet 'func (s) ' -- "*_test.go" | not grep -v 'func (s) Test'
    51git grep --quiet 'func [A-Z]' -- "*_test.go" | not grep -v 'func Test\|Benchmark\|Example'
    52
    53# - Do not use time.After except in tests.  It has the potential to leak the
    54#   timer since there is no way to stop it early.
    55git grep --quiet -l 'time.After(' -- "*.go" | not grep -v '_test.go\|test_utils\|testutils'
    56
    57# - Do not import math/rand for real library code.  Use internal/grpcrand for
    58#   thread safety.
    59git grep --quiet -l '"math/rand"' -- "*.go" 2>&1 | not grep -v '^examples\|^interop/stress\|grpcrand\|^benchmark\|wrr_test'
    60
    61# - Do not use "interface{}"; use "any" instead.
    62git grep --quiet -l 'interface{}' -- "*.go" 2>&1 | not grep -v '\.pb\.go\|protoc-gen-go-grpc\|grpc_testing_not_regenerate'
    63
    64# - Do not call grpclog directly. Use grpclog.Component instead.
    65git grep --quiet -l -e 'grpclog.I' --or -e 'grpclog.W' --or -e 'grpclog.E' --or -e 'grpclog.F' --or -e 'grpclog.V' -- "*.go" | not grep -v '^grpclog/component.go\|^internal/grpctest/tlogger_test.go'
    66
    67# - Ensure that the deprecated protobuf dependency is not used.
    68not git grep --quiet "\"github.com/golang/protobuf/*" -- "*.go" ':(exclude)reflection/test/grpc_testing_not_regenerate/*'
    69
    70# - Ensure all usages of grpc_testing package are renamed when importing.
    71not git grep --quiet "\(import \|^\s*\)\"google.golang.org/grpc/interop/grpc_testing" -- "*.go"
    72
    73# - Ensure all xds proto imports are renamed to *pb or *grpc.
    74git grep --quiet '"github.com/envoyproxy/go-control-plane/envoy' -- '*.go' ':(exclude)*.pb.go' | not grep -v 'pb "\|grpc "'
    75
    76misspell -error .
    77
    78# - gofmt, goimports, go vet, go mod tidy.
    79# Perform these checks on each module inside gRPC.
    80for MOD_FILE in $(find . -name 'go.mod'); do
    81  MOD_DIR=$(dirname ${MOD_FILE})
    82  pushd ${MOD_DIR}
    83  go vet -all ./... | fail_on_output
    84  gofmt -s -d -l . 2>&1 | fail_on_output
    85  goimports -l . 2>&1 | not grep -vE "\.pb\.go"
    86
    87  go mod tidy -compat=1.19
    88  git status --porcelain 2>&1 | fail_on_output || \
    89    (git status; git --no-pager diff; exit 1)
    90  
    91  # - Collection of static analysis checks
    92  SC_OUT="$(mktemp)"
    93  staticcheck -go 1.19 -checks 'all' ./... >"${SC_OUT}" || true
    94
    95  # Error for anything other than checks that need exclusions.
    96  noret_grep -v "(ST1000)" "${SC_OUT}" | noret_grep -v "(SA1019)" | noret_grep -v "(ST1003)" | noret_grep -v "(ST1019)\|\(other import of\)" | not grep -v "(SA4000)"
    97
    98  # Exclude underscore checks for generated code.
    99  noret_grep "(ST1003)" "${SC_OUT}" | not grep -v '\(.pb.go:\)\|\(code_string_test.go:\)\|\(grpc_testing_not_regenerate\)'
   100
   101  # Error for duplicate imports not including grpc protos.
   102  noret_grep "(ST1019)\|\(other import of\)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused
   103channelz/grpc_channelz_v1"
   104go-control-plane/envoy
   105grpclb/grpc_lb_v1"
   106health/grpc_health_v1"
   107interop/grpc_testing"
   108orca/v3"
   109proto/grpc_gcp"
   110proto/grpc_lookup_v1"
   111examples/features/proto/echo"
   112reflection/grpc_reflection_v1"
   113reflection/grpc_reflection_v1alpha"
   114XXXXX PleaseIgnoreUnused'
   115
   116  # Error for any package comments not in generated code.
   117  noret_grep "(ST1000)" "${SC_OUT}" | not grep -v "\.pb\.go:"
   118
   119  # Ignore a false positive when operands have side affectes.
   120  # TODO(https://github.com/dominikh/go-tools/issues/54): Remove this once the issue is fixed in staticcheck.
   121  noret_grep "(SA4000)" "${SC_OUT}" | not grep -ev "crl.go:\d*:\d*: identical expressions on the left and right side of the '||' operator (SA4000)"
   122
   123  # Only ignore the following deprecated types/fields/functions and exclude
   124  # generated code.
   125  noret_grep "(SA1019)" "${SC_OUT}" | not grep -Fv 'XXXXX PleaseIgnoreUnused
   126XXXXX Protobuf related deprecation errors:
   127"github.com/golang/protobuf
   128.pb.go:
   129grpc_testing_not_regenerate
   130: ptypes.
   131proto.RegisterType
   132XXXXX gRPC internal usage deprecation errors:
   133"google.golang.org/grpc
   134: grpc.
   135: v1alpha.
   136: v1alphareflectionpb.
   137BalancerAttributes is deprecated:
   138CredsBundle is deprecated:
   139Metadata is deprecated: use Attributes instead.
   140NewSubConn is deprecated:
   141OverrideServerName is deprecated:
   142RemoveSubConn is deprecated:
   143SecurityVersion is deprecated:
   144Target is deprecated: Use the Target field in the BuildOptions instead.
   145UpdateAddresses is deprecated:
   146UpdateSubConnState is deprecated:
   147balancer.ErrTransientFailure is deprecated:
   148grpc/reflection/v1alpha/reflection.proto
   149SwitchTo is deprecated:
   150XXXXX xDS deprecated fields we support
   151.ExactMatch
   152.PrefixMatch
   153.SafeRegexMatch
   154.SuffixMatch
   155GetContainsMatch
   156GetExactMatch
   157GetMatchSubjectAltNames
   158GetPrefixMatch
   159GetSafeRegexMatch
   160GetSuffixMatch
   161GetTlsCertificateCertificateProviderInstance
   162GetValidationContextCertificateProviderInstance
   163XXXXX PleaseIgnoreUnused'
   164  popd
   165done
   166
   167echo SUCCESS

View as plain text