test.bash 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_VERSIONS=(1.9.7 1.10.3 1.11beta3)
  26. for GO_VERSION in ${GO_VERSIONS[@]}; do
  27. GO_DIR=go$GO_VERSION
  28. if [ ! -d $GO_DIR ]; then
  29. echo "download $GO_DIR"
  30. GOOS=$(uname | tr '[:upper:]' '[:lower:]')
  31. (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
  32. fi
  33. done
  34. # Setup GOPATH for pre-module support.
  35. MODULE_PATH=$(grep '^module ' $REPO_ROOT/go.mod | cut -d " " -f 2)
  36. if [ ! -d gopath/src/$MODULE_PATH ]; then
  37. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  38. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  39. fi
  40. export GOPATH=$TEST_DIR/gopath
  41. # Run tests across every supported version of Go.
  42. FAIL=0
  43. for GO_VERSION in ${GO_VERSIONS[@]}; do
  44. export GOROOT=$TEST_DIR/go$GO_VERSION
  45. GO_BIN=go$GO_VERSION/bin/go
  46. function go_build() {
  47. echo "$GO_BIN build $@"
  48. (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN build $@) || FAIL=1
  49. }
  50. function go_test() {
  51. echo "$GO_BIN test $@"
  52. (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN test $@) || FAIL=1
  53. }
  54. go_build ./...
  55. go_test -race ./...
  56. go_test -race -tags proto1_legacy ./...
  57. done
  58. exit $FAIL