khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [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 | # Check to make sure it's safe to modify the user's git repo. |
| 17 | if git status --porcelain | read; then |
| 18 | die "Uncommitted or untracked files found; commit changes first" |
| 19 | fi |
| 20 | |
| 21 | if [[ -d "${GOPATH}/src" ]]; then |
| 22 | die "\${GOPATH}/src (${GOPATH}/src) exists; this script will delete it." |
| 23 | fi |
| 24 | |
| 25 | # Undo any edits made by this script. |
| 26 | cleanup() { |
| 27 | rm -rf "${GOPATH}/src" |
| 28 | git reset --hard HEAD |
| 29 | } |
| 30 | trap cleanup EXIT |
| 31 | |
| 32 | fail_on_output() { |
| 33 | tee /dev/stderr | (! read) |
| 34 | } |
| 35 | |
| 36 | PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}" |
| 37 | |
| 38 | if [[ "$1" = "-install" ]]; then |
| 39 | # Check for module support |
| 40 | if go help mod >& /dev/null; then |
| 41 | go install \ |
| 42 | golang.org/x/lint/golint \ |
| 43 | golang.org/x/tools/cmd/goimports \ |
| 44 | honnef.co/go/tools/cmd/staticcheck \ |
| 45 | github.com/client9/misspell/cmd/misspell \ |
| 46 | github.com/golang/protobuf/protoc-gen-go |
| 47 | else |
| 48 | # Ye olde `go get` incantation. |
| 49 | # Note: this gets the latest version of all tools (vs. the pinned versions |
| 50 | # with Go modules). |
| 51 | go get -u \ |
| 52 | golang.org/x/lint/golint \ |
| 53 | golang.org/x/tools/cmd/goimports \ |
| 54 | honnef.co/go/tools/cmd/staticcheck \ |
| 55 | github.com/client9/misspell/cmd/misspell \ |
| 56 | github.com/golang/protobuf/protoc-gen-go |
| 57 | fi |
| 58 | if [[ -z "${VET_SKIP_PROTO}" ]]; then |
| 59 | if [[ "${TRAVIS}" = "true" ]]; then |
| 60 | PROTOBUF_VERSION=3.3.0 |
| 61 | PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip |
| 62 | pushd /home/travis |
| 63 | wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME} |
| 64 | unzip ${PROTOC_FILENAME} |
| 65 | bin/protoc --version |
| 66 | popd |
| 67 | elif ! which protoc > /dev/null; then |
| 68 | die "Please install protoc into your path" |
| 69 | fi |
| 70 | fi |
| 71 | exit 0 |
| 72 | elif [[ "$#" -ne 0 ]]; then |
| 73 | die "Unknown argument(s): $*" |
| 74 | fi |
| 75 | |
| 76 | # - Ensure all source files contain a copyright message. |
| 77 | git ls-files "*.go" | xargs grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" 2>&1 | fail_on_output |
| 78 | |
| 79 | # - Do not import math/rand for real library code. Use internal/grpcrand for |
| 80 | # thread safety. |
| 81 | git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand') |
| 82 | |
| 83 | # - Ensure all ptypes proto packages are renamed when importing. |
| 84 | git ls-files "*.go" | (! xargs grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/") |
| 85 | |
| 86 | # - Check imports that are illegal in appengine (until Go 1.11). |
| 87 | # TODO: Remove when we drop Go 1.10 support |
| 88 | go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go |
| 89 | |
| 90 | # - gofmt, goimports, golint (with exceptions for generated code), go vet. |
| 91 | gofmt -s -d -l . 2>&1 | fail_on_output |
| 92 | goimports -l . 2>&1 | fail_on_output |
| 93 | golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:") |
| 94 | go tool vet -all . |
| 95 | |
| 96 | # - Check that generated proto files are up to date. |
| 97 | if [[ -z "${VET_SKIP_PROTO}" ]]; then |
| 98 | PATH="/home/travis/bin:${PATH}" make proto && \ |
| 99 | git status --porcelain 2>&1 | fail_on_output || \ |
| 100 | (git status; git --no-pager diff; exit 1) |
| 101 | fi |
| 102 | |
| 103 | # - Check that our module is tidy. |
| 104 | if go help mod >& /dev/null; then |
| 105 | go mod tidy && \ |
| 106 | git status --porcelain 2>&1 | fail_on_output || \ |
| 107 | (git status; git --no-pager diff; exit 1) |
| 108 | fi |
| 109 | |
| 110 | # - Collection of static analysis checks |
| 111 | ### HACK HACK HACK: Remove once staticcheck works with modules. |
| 112 | # Make a symlink in ${GOPATH}/src to its ${GOPATH}/pkg/mod equivalent for every package we use. |
| 113 | for x in $(find "${GOPATH}/pkg/mod" -name '*@*' | grep -v \/mod\/cache\/); do |
| 114 | pkg="$(echo ${x#"${GOPATH}/pkg/mod/"} | cut -f1 -d@)"; |
| 115 | # If multiple versions exist, just use the existing one. |
| 116 | if [[ -L "${GOPATH}/src/${pkg}" ]]; then continue; fi |
| 117 | mkdir -p "$(dirname "${GOPATH}/src/${pkg}")"; |
| 118 | ln -s $x "${GOPATH}/src/${pkg}"; |
| 119 | done |
| 120 | ### END HACK HACK HACK |
| 121 | |
| 122 | # TODO(menghanl): fix errors in transport_test. |
| 123 | staticcheck -ignore ' |
| 124 | balancer.go:SA1019 |
| 125 | balancer_test.go:SA1019 |
| 126 | clientconn_test.go:SA1019 |
| 127 | balancer/roundrobin/roundrobin_test.go:SA1019 |
| 128 | benchmark/benchmain/main.go:SA1019 |
| 129 | internal/transport/handler_server.go:SA1019 |
| 130 | internal/transport/handler_server_test.go:SA1019 |
| 131 | internal/transport/transport_test.go:SA2002 |
| 132 | stats/stats_test.go:SA1019 |
| 133 | test/channelz_test.go:SA1019 |
| 134 | test/end2end_test.go:SA1019 |
| 135 | ' ./... |
| 136 | misspell -error . |