blob: 94d3d54e2fbb427a30c9d3e6714c876179036b8e [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
79# - Do not import math/rand for real library code. Use internal/grpcrand for
80# thread safety.
81git 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.
84git 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
88go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go
89
90# - gofmt, goimports, golint (with exceptions for generated code), go vet.
91gofmt -s -d -l . 2>&1 | fail_on_output
92goimports -l . 2>&1 | fail_on_output
93golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:")
94go tool vet -all .
95
96# - Check that generated proto files are up to date.
97if [[ -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)
101fi
102
103# - Check that our module is tidy.
104if 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)
108fi
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.
113for 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}";
119done
120### END HACK HACK HACK
121
122# TODO(menghanl): fix errors in transport_test.
123staticcheck -ignore '
124balancer.go:SA1019
125balancer_test.go:SA1019
126clientconn_test.go:SA1019
127balancer/roundrobin/roundrobin_test.go:SA1019
128benchmark/benchmain/main.go:SA1019
129internal/transport/handler_server.go:SA1019
130internal/transport/handler_server_test.go:SA1019
131internal/transport/transport_test.go:SA2002
132stats/stats_test.go:SA1019
133test/channelz_test.go:SA1019
134test/end2end_test.go:SA1019
135' ./...
136misspell -error .