blob: 94a50640b5e706f8bc518ab95dbd720486069628 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001#!/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
32fail_on_output() {
33 tee /dev/stderr | (! read)
34}
35
36PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"
37
38if [[ "$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
72elif [[ "$#" -ne 0 ]]; then
73 die "Unknown argument(s): $*"
74fi
75
76# - Ensure all source files contain a copyright message.
77git ls-files "*.go" | xargs grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" 2>&1 | fail_on_output
78
Stephane Barbarie260a5632019-02-26 16:12:49 -050079# - Make sure all tests in grpc and grpc/test use leakcheck via Teardown.
80(! grep 'func Test[^(]' *_test.go)
81(! grep 'func Test[^(]' test/*.go)
82
khenaidooac637102019-01-14 15:44:34 -050083# - Do not import math/rand for real library code. Use internal/grpcrand for
84# thread safety.
85git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand')
86
87# - Ensure all ptypes proto packages are renamed when importing.
88git ls-files "*.go" | (! xargs grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/")
89
90# - Check imports that are illegal in appengine (until Go 1.11).
91# TODO: Remove when we drop Go 1.10 support
92go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go
93
94# - gofmt, goimports, golint (with exceptions for generated code), go vet.
95gofmt -s -d -l . 2>&1 | fail_on_output
96goimports -l . 2>&1 | fail_on_output
97golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:")
98go tool vet -all .
99
100# - Check that generated proto files are up to date.
101if [[ -z "${VET_SKIP_PROTO}" ]]; then
102 PATH="/home/travis/bin:${PATH}" make proto && \
103 git status --porcelain 2>&1 | fail_on_output || \
104 (git status; git --no-pager diff; exit 1)
105fi
106
107# - Check that our module is tidy.
108if go help mod >& /dev/null; then
109 go mod tidy && \
110 git status --porcelain 2>&1 | fail_on_output || \
111 (git status; git --no-pager diff; exit 1)
112fi
113
114# - Collection of static analysis checks
115### HACK HACK HACK: Remove once staticcheck works with modules.
116# Make a symlink in ${GOPATH}/src to its ${GOPATH}/pkg/mod equivalent for every package we use.
117for x in $(find "${GOPATH}/pkg/mod" -name '*@*' | grep -v \/mod\/cache\/); do
118 pkg="$(echo ${x#"${GOPATH}/pkg/mod/"} | cut -f1 -d@)";
119 # If multiple versions exist, just use the existing one.
120 if [[ -L "${GOPATH}/src/${pkg}" ]]; then continue; fi
121 mkdir -p "$(dirname "${GOPATH}/src/${pkg}")";
122 ln -s $x "${GOPATH}/src/${pkg}";
123done
124### END HACK HACK HACK
125
126# TODO(menghanl): fix errors in transport_test.
Stephane Barbarie260a5632019-02-26 16:12:49 -0500127staticcheck -go 1.9 -ignore '
khenaidooac637102019-01-14 15:44:34 -0500128balancer.go:SA1019
129balancer_test.go:SA1019
130clientconn_test.go:SA1019
131balancer/roundrobin/roundrobin_test.go:SA1019
132benchmark/benchmain/main.go:SA1019
133internal/transport/handler_server.go:SA1019
134internal/transport/handler_server_test.go:SA1019
135internal/transport/transport_test.go:SA2002
136stats/stats_test.go:SA1019
137test/channelz_test.go:SA1019
138test/end2end_test.go:SA1019
Stephane Barbarie260a5632019-02-26 16:12:49 -0500139test/healthcheck_test.go:SA1019
khenaidooac637102019-01-14 15:44:34 -0500140' ./...
141misspell -error .