Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [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 | go install \ |
| 35 | golang.org/x/lint/golint \ |
| 36 | golang.org/x/tools/cmd/goimports \ |
| 37 | honnef.co/go/tools/cmd/staticcheck \ |
| 38 | github.com/client9/misspell/cmd/misspell \ |
| 39 | github.com/golang/protobuf/protoc-gen-go |
| 40 | else |
| 41 | # Ye olde `go get` incantation. |
| 42 | # Note: this gets the latest version of all tools (vs. the pinned versions |
| 43 | # with Go modules). |
| 44 | go get -u \ |
| 45 | golang.org/x/lint/golint \ |
| 46 | golang.org/x/tools/cmd/goimports \ |
| 47 | honnef.co/go/tools/cmd/staticcheck \ |
| 48 | github.com/client9/misspell/cmd/misspell \ |
| 49 | github.com/golang/protobuf/protoc-gen-go |
| 50 | fi |
| 51 | if [[ -z "${VET_SKIP_PROTO}" ]]; then |
| 52 | if [[ "${TRAVIS}" = "true" ]]; then |
| 53 | PROTOBUF_VERSION=3.3.0 |
| 54 | PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip |
| 55 | pushd /home/travis |
| 56 | wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME} |
| 57 | unzip ${PROTOC_FILENAME} |
| 58 | bin/protoc --version |
| 59 | popd |
| 60 | elif ! which protoc > /dev/null; then |
| 61 | die "Please install protoc into your path" |
| 62 | fi |
| 63 | fi |
| 64 | exit 0 |
| 65 | elif [[ "$#" -ne 0 ]]; then |
| 66 | die "Unknown argument(s): $*" |
| 67 | fi |
| 68 | |
| 69 | # - Ensure all source files contain a copyright message. |
| 70 | git ls-files "*.go" | xargs grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" 2>&1 | fail_on_output |
| 71 | |
| 72 | # - Make sure all tests in grpc and grpc/test use leakcheck via Teardown. |
| 73 | (! grep 'func Test[^(]' *_test.go) |
| 74 | (! grep 'func Test[^(]' test/*.go) |
| 75 | |
| 76 | # - Do not import math/rand for real library code. Use internal/grpcrand for |
| 77 | # thread safety. |
| 78 | git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand') |
| 79 | |
| 80 | # - Ensure all ptypes proto packages are renamed when importing. |
| 81 | git ls-files "*.go" | (! xargs grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/") |
| 82 | |
| 83 | # - Check imports that are illegal in appengine (until Go 1.11). |
| 84 | # TODO: Remove when we drop Go 1.10 support |
| 85 | go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go |
| 86 | |
| 87 | # - gofmt, goimports, golint (with exceptions for generated code), go vet. |
| 88 | gofmt -s -d -l . 2>&1 | fail_on_output |
| 89 | goimports -l . 2>&1 | fail_on_output |
| 90 | golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:") |
| 91 | go tool vet -all . |
| 92 | |
| 93 | # - Check that generated proto files are up to date. |
| 94 | if [[ -z "${VET_SKIP_PROTO}" ]]; then |
| 95 | PATH="/home/travis/bin:${PATH}" make proto && \ |
| 96 | git status --porcelain 2>&1 | fail_on_output || \ |
| 97 | (git status; git --no-pager diff; exit 1) |
| 98 | fi |
| 99 | |
| 100 | # - Check that our module is tidy. |
| 101 | if go help mod >& /dev/null; then |
| 102 | go mod tidy && \ |
| 103 | git status --porcelain 2>&1 | fail_on_output || \ |
| 104 | (git status; git --no-pager diff; exit 1) |
| 105 | fi |
| 106 | |
| 107 | # - Collection of static analysis checks |
| 108 | # TODO(menghanl): fix errors in transport_test. |
| 109 | staticcheck -go 1.9 -checks 'inherit,-ST1015' -ignore ' |
| 110 | google.golang.org/grpc/balancer.go:SA1019 |
| 111 | google.golang.org/grpc/balancer_test.go:SA1019 |
| 112 | google.golang.org/grpc/clientconn_test.go:SA1019 |
| 113 | google.golang.org/grpc/balancer/roundrobin/roundrobin_test.go:SA1019 |
| 114 | google.golang.org/grpc/benchmark/benchmain/main.go:SA1019 |
| 115 | google.golang.org/grpc/benchmark/worker/benchmark_client.go:SA1019 |
| 116 | google.golang.org/grpc/internal/transport/handler_server.go:SA1019 |
| 117 | google.golang.org/grpc/internal/transport/handler_server_test.go:SA1019 |
| 118 | google.golang.org/grpc/stats/stats_test.go:SA1019 |
| 119 | google.golang.org/grpc/test/channelz_test.go:SA1019 |
| 120 | google.golang.org/grpc/test/end2end_test.go:SA1019 |
| 121 | google.golang.org/grpc/test/healthcheck_test.go:SA1019 |
| 122 | ' ./... |
| 123 | misspell -error . |