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