vet.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # Check to make sure it's safe to modify the user's git repo.
  13. if git status --porcelain | read; then
  14. die "Uncommitted or untracked files found; commit changes first"
  15. fi
  16. if [[ -d "${GOPATH}/src" ]]; then
  17. die "\${GOPATH}/src (${GOPATH}/src) exists; this script will delete it."
  18. fi
  19. # Undo any edits made by this script.
  20. cleanup() {
  21. rm -rf "${GOPATH}/src"
  22. git reset --hard HEAD
  23. }
  24. trap cleanup EXIT
  25. fail_on_output() {
  26. tee /dev/stderr | (! read)
  27. }
  28. PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"
  29. if [[ "$1" = "-install" ]]; then
  30. # Check for module support
  31. if go help mod >& /dev/null; then
  32. go install \
  33. golang.org/x/lint/golint \
  34. golang.org/x/tools/cmd/goimports \
  35. honnef.co/go/tools/cmd/staticcheck \
  36. github.com/client9/misspell/cmd/misspell \
  37. github.com/golang/protobuf/protoc-gen-go
  38. else
  39. # Ye olde `go get` incantation.
  40. # Note: this gets the latest version of all tools (vs. the pinned versions
  41. # with Go modules).
  42. go get -u \
  43. golang.org/x/lint/golint \
  44. golang.org/x/tools/cmd/goimports \
  45. honnef.co/go/tools/cmd/staticcheck \
  46. github.com/client9/misspell/cmd/misspell \
  47. github.com/golang/protobuf/protoc-gen-go
  48. fi
  49. if [[ -z "${VET_SKIP_PROTO}" ]]; then
  50. if [[ "${TRAVIS}" = "true" ]]; then
  51. PROTOBUF_VERSION=3.3.0
  52. PROTOC_FILENAME=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip
  53. pushd /home/travis
  54. wget https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_FILENAME}
  55. unzip ${PROTOC_FILENAME}
  56. bin/protoc --version
  57. popd
  58. elif ! which protoc > /dev/null; then
  59. die "Please install protoc into your path"
  60. fi
  61. fi
  62. exit 0
  63. elif [[ "$#" -ne 0 ]]; then
  64. die "Unknown argument(s): $*"
  65. fi
  66. # - Ensure all source files contain a copyright message.
  67. git ls-files "*.go" | xargs grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" 2>&1 | fail_on_output
  68. # - Do not import math/rand for real library code. Use internal/grpcrand for
  69. # thread safety.
  70. git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand')
  71. # - Ensure all ptypes proto packages are renamed when importing.
  72. git ls-files "*.go" | (! xargs grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/")
  73. # - Check imports that are illegal in appengine (until Go 1.11).
  74. # TODO: Remove when we drop Go 1.10 support
  75. go list -f {{.Dir}} ./... | xargs go run test/go_vet/vet.go
  76. # - gofmt, goimports, golint (with exceptions for generated code), go vet.
  77. gofmt -s -d -l . 2>&1 | fail_on_output
  78. goimports -l . 2>&1 | fail_on_output
  79. golint ./... 2>&1 | (! grep -vE "(_mock|\.pb)\.go:")
  80. go tool vet -all .
  81. # - Check that generated proto files are up to date.
  82. if [[ -z "${VET_SKIP_PROTO}" ]]; then
  83. PATH="/home/travis/bin:${PATH}" make proto && \
  84. git status --porcelain 2>&1 | fail_on_output || \
  85. (git status; git --no-pager diff; exit 1)
  86. fi
  87. # - Check that our module is tidy.
  88. if go help mod >& /dev/null; then
  89. go mod tidy && \
  90. git status --porcelain 2>&1 | fail_on_output || \
  91. (git status; git --no-pager diff; exit 1)
  92. fi
  93. # - Collection of static analysis checks
  94. ### HACK HACK HACK: Remove once staticcheck works with modules.
  95. # Make a symlink in ${GOPATH}/src to its ${GOPATH}/pkg/mod equivalent for every package we use.
  96. for x in $(find "${GOPATH}/pkg/mod" -name '*@*' | grep -v \/mod\/cache\/); do
  97. pkg="$(echo ${x#"${GOPATH}/pkg/mod/"} | cut -f1 -d@)";
  98. # If multiple versions exist, just use the existing one.
  99. if [[ -L "${GOPATH}/src/${pkg}" ]]; then continue; fi
  100. mkdir -p "$(dirname "${GOPATH}/src/${pkg}")";
  101. ln -s $x "${GOPATH}/src/${pkg}";
  102. done
  103. ### END HACK HACK HACK
  104. # TODO(menghanl): fix errors in transport_test.
  105. staticcheck -ignore '
  106. balancer.go:SA1019
  107. balancer_test.go:SA1019
  108. clientconn_test.go:SA1019
  109. balancer/roundrobin/roundrobin_test.go:SA1019
  110. benchmark/benchmain/main.go:SA1019
  111. internal/transport/handler_server.go:SA1019
  112. internal/transport/handler_server_test.go:SA1019
  113. internal/transport/transport_test.go:SA2002
  114. stats/stats_test.go:SA1019
  115. test/channelz_test.go:SA1019
  116. test/end2end_test.go:SA1019
  117. ' ./...
  118. misspell -error .