Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 3 | set -ex # Exit on error; debugging enabled. |
| 4 | set -o pipefail # Fail a pipe if any sub-command fails. |
| 5 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 6 | # not makes sure the command passed to it does not exit with a return code of 0. |
| 7 | not() { |
| 8 | # This is required instead of the earlier (! $COMMAND) because subshells and |
| 9 | # pipefail don't work the same on Darwin as in Linux. |
| 10 | ! "$@" |
| 11 | } |
| 12 | |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 13 | die() { |
| 14 | echo "$@" >&2 |
| 15 | exit 1 |
| 16 | } |
| 17 | |
| 18 | fail_on_output() { |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 19 | tee /dev/stderr | not read |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | # Check to make sure it's safe to modify the user's git repo. |
| 23 | git status --porcelain | fail_on_output |
| 24 | |
| 25 | # Undo any edits made by this script. |
| 26 | cleanup() { |
| 27 | git reset --hard HEAD |
| 28 | } |
| 29 | trap cleanup EXIT |
| 30 | |
| 31 | PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}" |
| 32 | |
| 33 | if [[ "$1" = "-install" ]]; then |
| 34 | # Check for module support |
| 35 | if go help mod >& /dev/null; then |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 36 | # Install the pinned versions as defined in module tools. |
| 37 | pushd ./test/tools |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 38 | go install \ |
| 39 | golang.org/x/lint/golint \ |
| 40 | golang.org/x/tools/cmd/goimports \ |
| 41 | honnef.co/go/tools/cmd/staticcheck \ |
| 42 | github.com/client9/misspell/cmd/misspell \ |
| 43 | github.com/golang/protobuf/protoc-gen-go |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 44 | popd |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 45 | else |
| 46 | # Ye olde `go get` incantation. |
| 47 | # Note: this gets the latest version of all tools (vs. the pinned versions |
| 48 | # with Go modules). |
| 49 | go get -u \ |
| 50 | golang.org/x/lint/golint \ |
| 51 | golang.org/x/tools/cmd/goimports \ |
| 52 | honnef.co/go/tools/cmd/staticcheck \ |
| 53 | github.com/client9/misspell/cmd/misspell \ |
| 54 | github.com/golang/protobuf/protoc-gen-go |
| 55 | fi |
| 56 | if [[ -z "${VET_SKIP_PROTO}" ]]; then |
| 57 | if [[ "${TRAVIS}" = "true" ]]; then |
| 58 | PROTOBUF_VERSION=3.3.0 |
| 59 | PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip |
| 60 | pushd /home/travis |
| 61 | wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME} |
| 62 | unzip ${PROTOC_FILENAME} |
| 63 | bin/protoc --version |
| 64 | popd |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 65 | elif not which protoc > /dev/null; then |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 66 | die "Please install protoc into your path" |
| 67 | fi |
| 68 | fi |
| 69 | exit 0 |
| 70 | elif [[ "$#" -ne 0 ]]; then |
| 71 | die "Unknown argument(s): $*" |
| 72 | fi |
| 73 | |
| 74 | # - Ensure all source files contain a copyright message. |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 75 | not git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" -- '*.go' |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 76 | |
| 77 | # - Make sure all tests in grpc and grpc/test use leakcheck via Teardown. |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 78 | not grep 'func Test[^(]' *_test.go |
| 79 | not grep 'func Test[^(]' test/*.go |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 80 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 81 | # - Do not import x/net/context. |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 82 | not git grep -l 'x/net/context' -- "*.go" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 83 | |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 84 | # - Do not import math/rand for real library code. Use internal/grpcrand for |
| 85 | # thread safety. |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 86 | git grep -l '"math/rand"' -- "*.go" 2>&1 | not grep -v '^examples\|^stress\|grpcrand\|^benchmark\|wrr_test' |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 87 | |
| 88 | # - Ensure all ptypes proto packages are renamed when importing. |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 89 | not git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go" |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 90 | |
| 91 | # - Check imports that are illegal in appengine (until Go 1.11). |
| 92 | # TODO: Remove when we drop Go 1.10 support |
| 93 | go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go |
| 94 | |
| 95 | # - gofmt, goimports, golint (with exceptions for generated code), go vet. |
| 96 | gofmt -s -d -l . 2>&1 | fail_on_output |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 97 | goimports -l . 2>&1 | not grep -vE "(_mock|\.pb)\.go" |
| 98 | golint ./... 2>&1 | not grep -vE "(_mock|\.pb)\.go:" |
| 99 | go vet -all ./... |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 100 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 101 | misspell -error . |
| 102 | |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 103 | # - Check that generated proto files are up to date. |
| 104 | if [[ -z "${VET_SKIP_PROTO}" ]]; then |
| 105 | PATH="/home/travis/bin:${PATH}" make proto && \ |
| 106 | git status --porcelain 2>&1 | fail_on_output || \ |
| 107 | (git status; git --no-pager diff; exit 1) |
| 108 | fi |
| 109 | |
| 110 | # - Check that our module is tidy. |
| 111 | if go help mod >& /dev/null; then |
| 112 | go mod tidy && \ |
| 113 | git status --porcelain 2>&1 | fail_on_output || \ |
| 114 | (git status; git --no-pager diff; exit 1) |
| 115 | fi |
| 116 | |
| 117 | # - Collection of static analysis checks |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 118 | # |
| 119 | # TODO(dfawley): don't use deprecated functions in examples or first-party |
| 120 | # plugins. |
| 121 | SC_OUT="$(mktemp)" |
| 122 | staticcheck -go 1.9 -checks 'inherit,-ST1015' ./... > "${SC_OUT}" || true |
| 123 | # Error if anything other than deprecation warnings are printed. |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 124 | not grep -v "is deprecated:.*SA1019" "${SC_OUT}" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 125 | # Only ignore the following deprecated types/fields/functions. |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 126 | not grep -Fv '.HandleResolvedAddrs |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 127 | .HandleSubConnStateChange |
| 128 | .HeaderMap |
| 129 | .NewAddress |
| 130 | .NewServiceConfig |
| 131 | .Metadata is deprecated: use Attributes |
| 132 | .Type is deprecated: use Attributes |
| 133 | .UpdateBalancerState |
| 134 | balancer.Picker |
| 135 | grpc.CallCustomCodec |
| 136 | grpc.Code |
| 137 | grpc.Compressor |
| 138 | grpc.Decompressor |
| 139 | grpc.MaxMsgSize |
| 140 | grpc.MethodConfig |
| 141 | grpc.NewGZIPCompressor |
| 142 | grpc.NewGZIPDecompressor |
| 143 | grpc.RPCCompressor |
| 144 | grpc.RPCDecompressor |
| 145 | grpc.RoundRobin |
| 146 | grpc.ServiceConfig |
| 147 | grpc.WithBalancer |
| 148 | grpc.WithBalancerName |
| 149 | grpc.WithCompressor |
| 150 | grpc.WithDecompressor |
| 151 | grpc.WithDialer |
| 152 | grpc.WithMaxMsgSize |
| 153 | grpc.WithServiceConfig |
| 154 | grpc.WithTimeout |
| 155 | http.CloseNotifier |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 156 | info.SecurityVersion |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 157 | naming.Resolver |
| 158 | naming.Update |
| 159 | naming.Watcher |
| 160 | resolver.Backend |
| 161 | resolver.GRPCLB' "${SC_OUT}" |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 162 | |
| 163 | echo SUCCESS |