Takahiro Suzuki | 241c10e | 2020-12-17 20:17:57 +0900 | [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 |
| 34 | # Install the pinned versions as defined in module tools. |
| 35 | pushd ./test/tools |
| 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 |
| 42 | popd |
| 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. |
| 73 | (! git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" -- '*.go') |
| 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 | |
| 79 | # - Do not import x/net/context. |
| 80 | (! git grep -l 'x/net/context' -- "*.go") |
| 81 | |
| 82 | # - Do not import math/rand for real library code. Use internal/grpcrand for |
| 83 | # thread safety. |
| 84 | git grep -l '"math/rand"' -- "*.go" 2>&1 | (! grep -v '^examples\|^stress\|grpcrand\|wrr_test') |
| 85 | |
| 86 | # - Ensure all ptypes proto packages are renamed when importing. |
| 87 | (! git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go") |
| 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 |
| 95 | goimports -l . 2>&1 | (! grep -vE "(_mock|\.pb)\.go") | fail_on_output |
| 96 | golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:") |
| 97 | go vet -all . |
| 98 | |
| 99 | # - Check that generated proto files are up to date. |
| 100 | if [[ -z "${VET_SKIP_PROTO}" ]]; then |
| 101 | PATH="/home/travis/bin:${PATH}" make proto && \ |
| 102 | git status --porcelain 2>&1 | fail_on_output || \ |
| 103 | (git status; git --no-pager diff; exit 1) |
| 104 | fi |
| 105 | |
| 106 | # - Check that our module is tidy. |
| 107 | if go help mod >& /dev/null; then |
| 108 | go mod tidy && \ |
| 109 | git status --porcelain 2>&1 | fail_on_output || \ |
| 110 | (git status; git --no-pager diff; exit 1) |
| 111 | fi |
| 112 | |
| 113 | # - Collection of static analysis checks |
| 114 | # TODO(dfawley): don't use deprecated functions in examples. |
| 115 | staticcheck -go 1.9 -checks 'inherit,-ST1015' -ignore ' |
| 116 | google.golang.org/grpc/balancer.go:SA1019 |
| 117 | google.golang.org/grpc/balancer/grpclb/grpclb_remote_balancer.go:SA1019 |
| 118 | google.golang.org/grpc/balancer/grpclb/grpclb_test.go:SA1019 |
| 119 | google.golang.org/grpc/balancer/roundrobin/roundrobin_test.go:SA1019 |
| 120 | google.golang.org/grpc/xds/internal/balancer/edsbalancer/balancergroup.go:SA1019 |
| 121 | google.golang.org/grpc/xds/internal/resolver/xds_resolver.go:SA1019 |
| 122 | google.golang.org/grpc/xds/internal/balancer/xds.go:SA1019 |
| 123 | google.golang.org/grpc/xds/internal/balancer/xds_client.go:SA1019 |
| 124 | google.golang.org/grpc/balancer_conn_wrappers.go:SA1019 |
| 125 | google.golang.org/grpc/balancer_test.go:SA1019 |
| 126 | google.golang.org/grpc/benchmark/benchmain/main.go:SA1019 |
| 127 | google.golang.org/grpc/benchmark/worker/benchmark_client.go:SA1019 |
| 128 | google.golang.org/grpc/clientconn.go:S1024 |
| 129 | google.golang.org/grpc/clientconn_state_transition_test.go:SA1019 |
| 130 | google.golang.org/grpc/clientconn_test.go:SA1019 |
| 131 | google.golang.org/grpc/examples/features/debugging/client/main.go:SA1019 |
| 132 | google.golang.org/grpc/examples/features/load_balancing/client/main.go:SA1019 |
| 133 | google.golang.org/grpc/internal/transport/handler_server.go:SA1019 |
| 134 | google.golang.org/grpc/internal/transport/handler_server_test.go:SA1019 |
| 135 | google.golang.org/grpc/internal/resolver/dns/dns_resolver.go:SA1019 |
| 136 | google.golang.org/grpc/stats/stats_test.go:SA1019 |
| 137 | google.golang.org/grpc/test/balancer_test.go:SA1019 |
| 138 | google.golang.org/grpc/test/channelz_test.go:SA1019 |
| 139 | google.golang.org/grpc/test/end2end_test.go:SA1019 |
| 140 | google.golang.org/grpc/test/healthcheck_test.go:SA1019 |
| 141 | ' ./... |
| 142 | misspell -error . |