vet.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"
  26. if [[ "$1" = "-install" ]]; then
  27. # Check for module support
  28. if go help mod >& /dev/null; then
  29. go install \
  30. github.com/golang/lint/golint \
  31. golang.org/x/tools/cmd/goimports \
  32. honnef.co/go/tools/cmd/staticcheck \
  33. github.com/client9/misspell/cmd/misspell \
  34. github.com/golang/protobuf/protoc-gen-go
  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. github.com/golang/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. git ls-files "*.go" | xargs grep -L "\(Copyright [0-9]\{4,\} gRPC authors\)\|DO NOT EDIT" 2>&1 | tee /dev/stderr | (! read)
  64. git ls-files "*.go" | xargs grep -l '"math/rand"' 2>&1 | (! grep -v '^examples\|^stress\|grpcrand') | tee /dev/stderr | (! read)
  65. git ls-files | xargs dirname | sort | uniq | xargs go run test/go_vet/vet.go | tee /dev/stderr | (! read)
  66. gofmt -s -d -l . 2>&1 | tee /dev/stderr | (! read)
  67. goimports -l . 2>&1 | tee /dev/stderr | (! read)
  68. golint ./... 2>&1 | (grep -vE "(_mock|\.pb)\.go:" || true) | tee /dev/stderr | (! read)
  69. # Rewrite golang.org/x/net/context -> context imports (see grpc/grpc-go#1484).
  70. # TODO: Remove this mangling once "context" is imported directly (grpc/grpc-go#711).
  71. git ls-files "*.go" | xargs sed -i 's:"golang.org/x/net/context":"context":'
  72. set +o pipefail # vet exits with non-zero error if issues are found
  73. # TODO(deklerk) remove when we drop Go 1.6 support
  74. go tool vet -all . 2>&1 | \
  75. grep -vE 'clientconn.go:.*cancel (function|var)' | \
  76. grep -vE '.*transport_test.go:.*cancel' | \
  77. tee /dev/stderr | \
  78. (! read)
  79. set -o pipefail
  80. git reset --hard HEAD
  81. if [[ -z "${VET_SKIP_PROTO}" ]]; then
  82. PATH="/home/travis/bin:${PATH}" make proto && \
  83. git status --porcelain 2>&1 | (! read) || \
  84. (git status; git --no-pager diff; exit 1)
  85. fi
  86. if go help mod >& /dev/null; then
  87. go mod tidy && \
  88. git status --porcelain 2>&1 | (! read) || \
  89. (git status; git --no-pager diff; exit 1)
  90. fi
  91. ### HACK HACK HACK: Remove once staticcheck works with modules.
  92. # Make a symlink in ${GOPATH}/src to its ${GOPATH}/pkg/mod equivalent for every package we use.
  93. for x in $(find "${GOPATH}/pkg/mod" -name '*@*' | grep -v \/mod\/cache\/); do
  94. pkg="$(echo ${x#"${GOPATH}/pkg/mod/"} | cut -f1 -d@)";
  95. # If multiple versions exist, just use the existing one.
  96. if [[ -L "${GOPATH}/src/${pkg}" ]]; then continue; fi
  97. mkdir -p "$(dirname "${GOPATH}/src/${pkg}")";
  98. ln -s $x "${GOPATH}/src/${pkg}";
  99. done
  100. ### END HACK HACK HACK
  101. # TODO(menghanl): fix errors in transport_test.
  102. staticcheck -ignore '
  103. internal/transport/transport_test.go:SA2002
  104. benchmark/benchmain/main.go:SA1019
  105. stats/stats_test.go:SA1019
  106. test/end2end_test.go:SA1019
  107. balancer_test.go:SA1019
  108. balancer.go:SA1019
  109. clientconn_test.go:SA1019
  110. internal/transport/handler_server_test.go:SA1019
  111. internal/transport/handler_server.go:SA1019
  112. ' ./...
  113. misspell -error .