blob: e12024fb86383dceb9d2fbac7be742b8405ffea6 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001#!/bin/bash
2
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07003set -ex # Exit on error; debugging enabled.
4set -o pipefail # Fail a pipe if any sub-command fails.
5
amit.ghosh258d14c2020-10-02 15:13:38 +02006# 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
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070013die() {
14 echo "$@" >&2
15 exit 1
16}
17
18fail_on_output() {
amit.ghosh258d14c2020-10-02 15:13:38 +020019 tee /dev/stderr | not read
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070020}
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
Arjun E K57a7fcb2020-01-30 06:44:45 +000036 # Install the pinned versions as defined in module tools.
37 pushd ./test/tools
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070038 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
Arjun E K57a7fcb2020-01-30 06:44:45 +000044 popd
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070045 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
amit.ghosh258d14c2020-10-02 15:13:38 +020065 elif not which protoc > /dev/null; then
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070066 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.
amit.ghosh258d14c2020-10-02 15:13:38 +020075not git grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" -- '*.go'
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070076
77# - Make sure all tests in grpc and grpc/test use leakcheck via Teardown.
amit.ghosh258d14c2020-10-02 15:13:38 +020078not grep 'func Test[^(]' *_test.go
79not grep 'func Test[^(]' test/*.go
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070080
Arjun E K57a7fcb2020-01-30 06:44:45 +000081# - Do not import x/net/context.
amit.ghosh258d14c2020-10-02 15:13:38 +020082not git grep -l 'x/net/context' -- "*.go"
Arjun E K57a7fcb2020-01-30 06:44:45 +000083
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070084# - Do not import math/rand for real library code. Use internal/grpcrand for
85# thread safety.
amit.ghosh258d14c2020-10-02 15:13:38 +020086git grep -l '"math/rand"' -- "*.go" 2>&1 | not grep -v '^examples\|^stress\|grpcrand\|^benchmark\|wrr_test'
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070087
88# - Ensure all ptypes proto packages are renamed when importing.
amit.ghosh258d14c2020-10-02 15:13:38 +020089not git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070090
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
amit.ghosh258d14c2020-10-02 15:13:38 +020097goimports -l . 2>&1 | not grep -vE "(_mock|\.pb)\.go"
98golint ./... 2>&1 | not grep -vE "(_mock|\.pb)\.go:"
99go vet -all ./...
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700100
Arjun E K57a7fcb2020-01-30 06:44:45 +0000101misspell -error .
102
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700103# - 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
Arjun E K57a7fcb2020-01-30 06:44:45 +0000118#
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.
amit.ghosh258d14c2020-10-02 15:13:38 +0200124not grep -v "is deprecated:.*SA1019" "${SC_OUT}"
Arjun E K57a7fcb2020-01-30 06:44:45 +0000125# Only ignore the following deprecated types/fields/functions.
amit.ghosh258d14c2020-10-02 15:13:38 +0200126not grep -Fv '.HandleResolvedAddrs
Arjun E K57a7fcb2020-01-30 06:44:45 +0000127.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
amit.ghosh258d14c2020-10-02 15:13:38 +0200156info.SecurityVersion
Arjun E K57a7fcb2020-01-30 06:44:45 +0000157naming.Resolver
158naming.Update
159naming.Watcher
160resolver.Backend
161resolver.GRPCLB' "${SC_OUT}"
amit.ghosh258d14c2020-10-02 15:13:38 +0200162
163echo SUCCESS