test.bash 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
  36. # Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
  37. unset GOROOT
  38. # Download dependencies using modules.
  39. # For pre-module support, dump the dependencies in a vendor directory.
  40. # TODO: use GOFLAGS="-mod=readonly" when https://golang.org/issue/26850 is fixed.
  41. GO_LATEST_BIN=$TEST_DIR/go$GO_LATEST/bin/go
  42. (cd $REPO_ROOT && $GO_LATEST_BIN mod tidy && $GO_LATEST_BIN mod vendor) || exit 1
  43. # Setup GOPATH for pre-module support.
  44. MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}")
  45. if [ ! -d gopath/src/$MODULE_PATH ]; then
  46. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  47. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  48. fi
  49. export GOPATH=$TEST_DIR/gopath
  50. # Run tests across every supported version of Go.
  51. FAIL=0
  52. for GO_VERSION in ${GO_VERSIONS[@]}; do
  53. GO_BIN=go$GO_VERSION/bin/go
  54. function go_build() {
  55. echo "$GO_BIN build $@"
  56. (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN build $@) || FAIL=1
  57. }
  58. function go_test() {
  59. echo "$GO_BIN test $@"
  60. (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN test $@) || FAIL=1
  61. }
  62. go_build ./...
  63. go_test -race ./...
  64. go_test -race -tags proto1_legacy ./...
  65. done
  66. # Check for changed files.
  67. GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
  68. if [ ! -z "$GIT_DIFF" ]; then
  69. echo -e "git diff HEAD\n$GIT_DIFF"
  70. FAIL=1
  71. fi
  72. # Check for untracked files.
  73. GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
  74. if [ ! -z "$GIT_UNTRACKED" ]; then
  75. echo -e "git ls-files --others --exclude-standard\n$GIT_UNTRACKED"
  76. FAIL=1
  77. fi
  78. exit $FAIL