vet.sh 4.5 KB

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