blob: 0e737072781ba292adda9a52dcf14807eab13cc9 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001#!/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
16fail_on_output() {
17 tee /dev/stderr | (! read)
18}
19
20# Check to make sure it's safe to modify the user's git repo.
21git status --porcelain | fail_on_output
22
23# Undo any edits made by this script.
24cleanup() {
25 git reset --hard HEAD
26}
27trap cleanup EXIT
28
29PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"
30
31if [[ "$1" = "-install" ]]; then
32 # Check for module support
33 if go help mod >& /dev/null; then
Arjun E K57a7fcb2020-01-30 06:44:45 +000034 # Install the pinned versions as defined in module tools.
35 pushd ./test/tools
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070036 go install \
37 golang.org/x/lint/golint \
38 golang.org/x/tools/cmd/goimports \
39 honnef.co/go/tools/cmd/staticcheck \
40 github.com/client9/misspell/cmd/misspell \
41 github.com/golang/protobuf/protoc-gen-go
Arjun E K57a7fcb2020-01-30 06:44:45 +000042 popd
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070043 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 golang.org/x/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
72# - Ensure all source files contain a copyright message.
Arjun E K57a7fcb2020-01-30 06:44:45 +000073(! git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" -- '*.go')
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070074
75# - Make sure all tests in grpc and grpc/test use leakcheck via Teardown.
76(! grep 'func Test[^(]' *_test.go)
77(! grep 'func Test[^(]' test/*.go)
78
Arjun E K57a7fcb2020-01-30 06:44:45 +000079# - Do not import x/net/context.
80(! git grep -l 'x/net/context' -- "*.go")
81
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070082# - Do not import math/rand for real library code. Use internal/grpcrand for
83# thread safety.
Arjun E K57a7fcb2020-01-30 06:44:45 +000084git grep -l '"math/rand"' -- "*.go" 2>&1 | (! grep -v '^examples\|^stress\|grpcrand\|^benchmark\|wrr_test')
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070085
86# - Ensure all ptypes proto packages are renamed when importing.
Arjun E K57a7fcb2020-01-30 06:44:45 +000087(! git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go")
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070088
89# - Check imports that are illegal in appengine (until Go 1.11).
90# TODO: Remove when we drop Go 1.10 support
91go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go
92
93# - gofmt, goimports, golint (with exceptions for generated code), go vet.
94gofmt -s -d -l . 2>&1 | fail_on_output
Arjun E K57a7fcb2020-01-30 06:44:45 +000095goimports -l . 2>&1 | (! grep -vE "(_mock|\.pb)\.go")
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070096golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:")
97go vet -all .
98
Arjun E K57a7fcb2020-01-30 06:44:45 +000099misspell -error .
100
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700101# - Check that generated proto files are up to date.
102if [[ -z "${VET_SKIP_PROTO}" ]]; then
103 PATH="/home/travis/bin:${PATH}" make proto && \
104 git status --porcelain 2>&1 | fail_on_output || \
105 (git status; git --no-pager diff; exit 1)
106fi
107
108# - Check that our module is tidy.
109if go help mod >& /dev/null; then
110 go mod tidy && \
111 git status --porcelain 2>&1 | fail_on_output || \
112 (git status; git --no-pager diff; exit 1)
113fi
114
115# - Collection of static analysis checks
Arjun E K57a7fcb2020-01-30 06:44:45 +0000116#
117# TODO(dfawley): don't use deprecated functions in examples or first-party
118# plugins.
119SC_OUT="$(mktemp)"
120staticcheck -go 1.9 -checks 'inherit,-ST1015' ./... > "${SC_OUT}" || true
121# Error if anything other than deprecation warnings are printed.
122(! grep -v "is deprecated:.*SA1019" "${SC_OUT}")
123# Only ignore the following deprecated types/fields/functions.
124(! grep -Fv '.HandleResolvedAddrs
125.HandleSubConnStateChange
126.HeaderMap
127.NewAddress
128.NewServiceConfig
129.Metadata is deprecated: use Attributes
130.Type is deprecated: use Attributes
131.UpdateBalancerState
132balancer.Picker
133grpc.CallCustomCodec
134grpc.Code
135grpc.Compressor
136grpc.Decompressor
137grpc.MaxMsgSize
138grpc.MethodConfig
139grpc.NewGZIPCompressor
140grpc.NewGZIPDecompressor
141grpc.RPCCompressor
142grpc.RPCDecompressor
143grpc.RoundRobin
144grpc.ServiceConfig
145grpc.WithBalancer
146grpc.WithBalancerName
147grpc.WithCompressor
148grpc.WithDecompressor
149grpc.WithDialer
150grpc.WithMaxMsgSize
151grpc.WithServiceConfig
152grpc.WithTimeout
153http.CloseNotifier
154naming.Resolver
155naming.Update
156naming.Watcher
157resolver.Backend
158resolver.GRPCLB' "${SC_OUT}"
159)