blob: eb3287036782dc22abf919c152e8a45f255ad65a [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001#!/bin/bash
2
3if [[ `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
6fi
7
8set -ex # Exit on error; debugging enabled.
9set -o pipefail # Fail a pipe if any sub-command fails.
10
11die() {
12 echo "$@" >&2
13 exit 1
14}
15
16# Check to make sure it's safe to modify the user's git repo.
17if git status --porcelain | read; then
18 die "Uncommitted or untracked files found; commit changes first"
19fi
20
21if [[ -d "${GOPATH}/src" ]]; then
22 die "\${GOPATH}/src (${GOPATH}/src) exists; this script will delete it."
23fi
24
25# Undo any edits made by this script.
26cleanup() {
27 rm -rf "${GOPATH}/src"
28 git reset --hard HEAD
29}
30trap cleanup EXIT
31
32PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"
33
34if [[ "$1" = "-install" ]]; then
35 # Check for module support
36 if go help mod >& /dev/null; then
37 go install \
38 github.com/golang/lint/golint \
39 golang.org/x/tools/cmd/goimports \
40 honnef.co/go/tools/cmd/staticcheck \
41 github.com/client9/misspell/cmd/misspell \
42 github.com/golang/protobuf/protoc-gen-go
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 github.com/golang/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
68elif [[ "$#" -ne 0 ]]; then
69 die "Unknown argument(s): $*"
70fi
71
72git ls-files "*.go" | xargs grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" 2>&1 | tee /dev/stderr | (! read)
73git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand') | tee /dev/stderr | (! read)
74git ls-files | xargs dirname | sort | uniq | xargs go run test/go_vet/vet.go | tee /dev/stderr | (! read)
75gofmt -s -d -l . 2>&1 | tee /dev/stderr | (! read)
76goimports -l . 2>&1 | tee /dev/stderr | (! read)
77golint ./... 2>&1 | (grep -vE "(_mock|\.pb)\.go:" || true) | tee /dev/stderr | (! read)
78
79# Rewrite golang.org/x/net/context -> context imports (see grpc/grpc-go#1484).
80# TODO: Remove this mangling once "context" is imported directly (grpc/grpc-go#711).
81git ls-files "*.go" | xargs sed -i 's:"golang.org/x/net/context":"context":'
82set +o pipefail # vet exits with non-zero error if issues are found
83
84# TODO(deklerk) remove when we drop Go 1.6 support
85go tool vet -all . 2>&1 | \
86 grep -vE 'clientconn.go:.*cancel (function|var)' | \
87 grep -vE '.*transport_test.go:.*cancel' | \
88 tee /dev/stderr | \
89 (! read)
90
91set -o pipefail
92git reset --hard HEAD
93
94if [[ -z "${VET_SKIP_PROTO}" ]]; then
95 PATH="/home/travis/bin:${PATH}" make proto && \
96 git status --porcelain 2>&1 | (! read) || \
97 (git status; git --no-pager diff; exit 1)
98fi
99
100if go help mod >& /dev/null; then
101 go mod tidy && \
102 git status --porcelain 2>&1 | (! read) || \
103 (git status; git --no-pager diff; exit 1)
104fi
105
106### HACK HACK HACK: Remove once staticcheck works with modules.
107# Make a symlink in ${GOPATH}/src to its ${GOPATH}/pkg/mod equivalent for every package we use.
108for x in $(find "${GOPATH}/pkg/mod" -name '*@*' | grep -v \/mod\/cache\/); do
109 pkg="$(echo ${x#"${GOPATH}/pkg/mod/"} | cut -f1 -d@)";
110 # If multiple versions exist, just use the existing one.
111 if [[ -L "${GOPATH}/src/${pkg}" ]]; then continue; fi
112 mkdir -p "$(dirname "${GOPATH}/src/${pkg}")";
113 ln -s $x "${GOPATH}/src/${pkg}";
114done
115### END HACK HACK HACK
116
117# TODO(menghanl): fix errors in transport_test.
118staticcheck -ignore '
119internal/transport/transport_test.go:SA2002
120benchmark/benchmain/main.go:SA1019
121stats/stats_test.go:SA1019
122test/end2end_test.go:SA1019
123balancer_test.go:SA1019
124balancer.go:SA1019
125clientconn_test.go:SA1019
126internal/transport/handler_server_test.go:SA1019
127internal/transport/handler_server.go:SA1019
128' ./...
129misspell -error .