test.bash 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # Copyright 2018 The Go Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style
  4. # license that can be found in the LICENSE file.
  5. REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. # Create a test directory.
  7. # The Go and protobuf toolchains used for testing will be cached here.
  8. TEST_DIR=/tmp/golang-protobuf-test
  9. if [ ! -d $TEST_DIR ]; then
  10. echo "mkdir $TEST_DIR"
  11. mkdir -p $TEST_DIR
  12. fi
  13. cd $TEST_DIR
  14. # Download and build the protobuf toolchain.
  15. # We avoid downloading the pre-compiled binaries since they do not contain
  16. # the conformance test runner.
  17. PROTOBUF_VERSION=3.6.1
  18. PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
  19. if [ ! -d $PROTOBUF_DIR ]; then
  20. echo "download and build $PROTOBUF_DIR"
  21. (curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1
  22. (cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1
  23. fi
  24. # Download each Go toolchain version.
  25. GO_LATEST=1.11beta3
  26. GO_VERSIONS=(1.9.7 1.10.3 $GO_LATEST)
  27. for GO_VERSION in ${GO_VERSIONS[@]}; do
  28. GO_DIR=go$GO_VERSION
  29. if [ ! -d $GO_DIR ]; then
  30. echo "download $GO_DIR"
  31. GOOS=$(uname | tr '[:upper:]' '[:lower:]')
  32. (mkdir $GO_DIR && curl -s -L https://dl.google.com/go/$GO_DIR.$GOOS-amd64.tar.gz | tar -zxf - -C $GO_DIR --strip-components 1) || exit 1
  33. fi
  34. done
  35. # Download dependencies using modules.
  36. # For pre-module support, dump the dependencies in a vendor directory.
  37. # TODO: use GOFLAGS="-mod=readonly" when https://golang.org/issue/26850 is fixed.
  38. GO_LATEST_BIN=$TEST_DIR/go$GO_LATEST/bin/go
  39. (cd $REPO_ROOT && $GO_LATEST_BIN mod tidy && $GO_LATEST_BIN mod vendor) || exit 1
  40. # Setup GOPATH for pre-module support.
  41. MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}")
  42. if [ ! -d gopath/src/$MODULE_PATH ]; then
  43. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  44. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  45. fi
  46. export GOPATH=$TEST_DIR/gopath
  47. # Run tests across every supported version of Go.
  48. FAIL=0
  49. for GO_VERSION in ${GO_VERSIONS[@]}; do
  50. export GOROOT=$TEST_DIR/go$GO_VERSION
  51. GO_BIN=go$GO_VERSION/bin/go
  52. function go_build() {
  53. echo "$GO_BIN build $@"
  54. (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN build $@) || FAIL=1
  55. }
  56. function go_test() {
  57. echo "$GO_BIN test $@"
  58. (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN test $@) || FAIL=1
  59. }
  60. go_build ./...
  61. go_test -race ./...
  62. go_test -race -tags proto1_legacy ./...
  63. done
  64. # Check for changed files.
  65. GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
  66. if [ ! -z "$GIT_DIFF" ]; then
  67. echo -e "git diff HEAD\n$GIT_DIFF"
  68. FAIL=1
  69. fi
  70. # Check for untracked files.
  71. GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
  72. if [ ! -z "$GIT_UNTRACKED" ]; then
  73. echo -e "git ls-files --others --exclude-standard\n$GIT_UNTRACKED"
  74. FAIL=1
  75. fi
  76. exit $FAIL