blob: e12024fb86383dceb9d2fbac7be742b8405ffea6 [file] [log] [blame]
Scott Baker105df152020-04-13 15:55:14 -07001#!/bin/bash
2
3set -ex # Exit on error; debugging enabled.
4set -o pipefail # Fail a pipe if any sub-command fails.
5
6# not makes sure the command passed to it does not exit with a return code of 0.
7not() {
8 # This is required instead of the earlier (! $COMMAND) because subshells and
9 # pipefail don't work the same on Darwin as in Linux.
10 ! "$@"
11}
12
13die() {
14 echo "$@" >&2
15 exit 1
16}
17
18fail_on_output() {
19 tee /dev/stderr | not read
20}
21
22# Check to make sure it's safe to modify the user's git repo.
23git status --porcelain | fail_on_output
24
25# Undo any edits made by this script.
26cleanup() {
27 git reset --hard HEAD
28}
29trap cleanup EXIT
30
31PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"
32
33if [[ "$1" = "-install" ]]; then
34 # Check for module support
35 if go help mod >& /dev/null; then
36 # Install the pinned versions as defined in module tools.
37 pushd ./test/tools
38 go install \
39 golang.org/x/lint/golint \
40 golang.org/x/tools/cmd/goimports \
41 honnef.co/go/tools/cmd/staticcheck \
42 github.com/client9/misspell/cmd/misspell \
43 github.com/golang/protobuf/protoc-gen-go
44 popd
45 else
46 # Ye olde `go get` incantation.
47 # Note: this gets the latest version of all tools (vs. the pinned versions
48 # with Go modules).
49 go get -u \
50 golang.org/x/lint/golint \
51 golang.org/x/tools/cmd/goimports \
52 honnef.co/go/tools/cmd/staticcheck \
53 github.com/client9/misspell/cmd/misspell \
54 github.com/golang/protobuf/protoc-gen-go
55 fi
56 if [[ -z "${VET_SKIP_PROTO}" ]]; then
57 if [[ "${TRAVIS}" = "true" ]]; then
58 PROTOBUF_VERSION=3.3.0
59 PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip
60 pushd /home/travis
61 wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME}
62 unzip ${PROTOC_FILENAME}
63 bin/protoc --version
64 popd
65 elif not which protoc > /dev/null; then
66 die "Please install protoc into your path"
67 fi
68 fi
69 exit 0
70elif [[ "$#" -ne 0 ]]; then
71 die "Unknown argument(s): $*"
72fi
73
74# - Ensure all source files contain a copyright message.
75not git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" -- '*.go'
76
77# - Make sure all tests in grpc and grpc/test use leakcheck via Teardown.
78not grep 'func Test[^(]' *_test.go
79not grep 'func Test[^(]' test/*.go
80
81# - Do not import x/net/context.
82not git grep -l 'x/net/context' -- "*.go"
83
84# - Do not import math/rand for real library code. Use internal/grpcrand for
85# thread safety.
86git grep -l '"math/rand"' -- "*.go" 2>&1 | not grep -v '^examples\|^stress\|grpcrand\|^benchmark\|wrr_test'
87
88# - Ensure all ptypes proto packages are renamed when importing.
89not git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go"
90
91# - Check imports that are illegal in appengine (until Go 1.11).
92# TODO: Remove when we drop Go 1.10 support
93go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go
94
95# - gofmt, goimports, golint (with exceptions for generated code), go vet.
96gofmt -s -d -l . 2>&1 | fail_on_output
97goimports -l . 2>&1 | not grep -vE "(_mock|\.pb)\.go"
98golint ./... 2>&1 | not grep -vE "(_mock|\.pb)\.go:"
99go vet -all ./...
100
101misspell -error .
102
103# - Check that generated proto files are up to date.
104if [[ -z "${VET_SKIP_PROTO}" ]]; then
105 PATH="/home/travis/bin:${PATH}" make proto && \
106 git status --porcelain 2>&1 | fail_on_output || \
107 (git status; git --no-pager diff; exit 1)
108fi
109
110# - Check that our module is tidy.
111if go help mod >& /dev/null; then
112 go mod tidy && \
113 git status --porcelain 2>&1 | fail_on_output || \
114 (git status; git --no-pager diff; exit 1)
115fi
116
117# - Collection of static analysis checks
118#
119# TODO(dfawley): don't use deprecated functions in examples or first-party
120# plugins.
121SC_OUT="$(mktemp)"
122staticcheck -go 1.9 -checks 'inherit,-ST1015' ./... > "${SC_OUT}" || true
123# Error if anything other than deprecation warnings are printed.
124not grep -v "is deprecated:.*SA1019" "${SC_OUT}"
125# Only ignore the following deprecated types/fields/functions.
126not grep -Fv '.HandleResolvedAddrs
127.HandleSubConnStateChange
128.HeaderMap
129.NewAddress
130.NewServiceConfig
131.Metadata is deprecated: use Attributes
132.Type is deprecated: use Attributes
133.UpdateBalancerState
134balancer.Picker
135grpc.CallCustomCodec
136grpc.Code
137grpc.Compressor
138grpc.Decompressor
139grpc.MaxMsgSize
140grpc.MethodConfig
141grpc.NewGZIPCompressor
142grpc.NewGZIPDecompressor
143grpc.RPCCompressor
144grpc.RPCDecompressor
145grpc.RoundRobin
146grpc.ServiceConfig
147grpc.WithBalancer
148grpc.WithBalancerName
149grpc.WithCompressor
150grpc.WithDecompressor
151grpc.WithDialer
152grpc.WithMaxMsgSize
153grpc.WithServiceConfig
154grpc.WithTimeout
155http.CloseNotifier
156info.SecurityVersion
157naming.Resolver
158naming.Update
159naming.Watcher
160resolver.Backend
161resolver.GRPCLB' "${SC_OUT}"
162
163echo SUCCESS