blob: f077fe775adbf30c5de2f541f745ffcabb41ef60 [file] [log] [blame]
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +00001#!/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
34 # Install the pinned versions as defined in module tools.
35 pushd ./test/tools
36 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
42 popd
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 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.
73(! git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" -- '*.go')
74
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
79# - Do not import x/net/context.
80(! git grep -l 'x/net/context' -- "*.go")
81
82# - Do not import math/rand for real library code. Use internal/grpcrand for
83# thread safety.
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000084git grep -l '"math/rand"' -- "*.go" 2>&1 | (! grep -v '^examples\|^stress\|grpcrand\|^benchmark\|wrr_test')
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000085
86# - Ensure all ptypes proto packages are renamed when importing.
87(! git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go")
88
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
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000095goimports -l . 2>&1 | (! grep -vE "(_mock|\.pb)\.go")
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000096golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:")
Scott Baker105df152020-04-13 15:55:14 -070097go vet -all ./...
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000098
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000099misspell -error .
100
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000101# - 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
Dinesh Belwalkar396b6522020-02-06 22:11:53 +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
Scott Baker105df152020-04-13 15:55:14 -0700154info.SecurityVersion
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000155naming.Resolver
156naming.Update
157naming.Watcher
158resolver.Backend
159resolver.GRPCLB' "${SC_OUT}"
160)