test.bash 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. function print() { echo -e "\x1b[1m> $@\x1b[0m"; }
  7. # Create a test directory.
  8. # The Go and protobuf toolchains used for testing will be cached here.
  9. TEST_DIR=/tmp/golang-protobuf-test
  10. if [ ! -d $TEST_DIR ]; then
  11. print "mkdir $TEST_DIR"
  12. mkdir -p $TEST_DIR
  13. fi
  14. cd $TEST_DIR
  15. # Download and build the protobuf toolchain.
  16. # We avoid downloading the pre-compiled binaries since they do not contain
  17. # the conformance test runner.
  18. PROTOBUF_VERSION=3.6.1
  19. PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
  20. if [ ! -d $PROTOBUF_DIR ]; then
  21. print "download and build $PROTOBUF_DIR"
  22. (curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1
  23. (cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1
  24. fi
  25. export PATH=$TEST_DIR/$PROTOBUF_DIR/src:$TEST_DIR/$PROTOBUF_DIR/conformance:$PATH
  26. # Download each Go toolchain version.
  27. GO_LATEST=1.11beta3
  28. GO_VERSIONS=(1.9.7 1.10.3 $GO_LATEST)
  29. for GO_VERSION in ${GO_VERSIONS[@]}; do
  30. GO_DIR=go$GO_VERSION
  31. if [ ! -d $GO_DIR ]; then
  32. print "download $GO_DIR"
  33. GOOS=$(uname | tr '[:upper:]' '[:lower:]')
  34. (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
  35. fi
  36. done
  37. # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
  38. # Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
  39. unset GOROOT
  40. # Download dependencies using modules.
  41. # For pre-module support, dump the dependencies in a vendor directory.
  42. # TODO: use GOFLAGS="-mod=readonly" when https://golang.org/issue/26850 is fixed.
  43. GO_LATEST_BIN=$TEST_DIR/go$GO_LATEST/bin/go
  44. (cd $REPO_ROOT && $GO_LATEST_BIN mod tidy && $GO_LATEST_BIN mod vendor) || exit 1
  45. # Setup GOPATH for pre-module support.
  46. MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}")
  47. if [ ! -d gopath/src/$MODULE_PATH ]; then
  48. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  49. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  50. fi
  51. export GOPATH=$TEST_DIR/gopath
  52. # Run tests across every supported version of Go.
  53. LABELS=()
  54. PIDS=()
  55. OUTS=()
  56. function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
  57. trap cleanup EXIT
  58. for GO_VERSION in ${GO_VERSIONS[@]}; do
  59. # Run the go command in a background process.
  60. function go() {
  61. GO_BIN=go$GO_VERSION/bin/go
  62. LABELS+=("$(echo "go$GO_VERSION $@")")
  63. OUT=$(mktemp)
  64. (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN $@ &> $OUT) &
  65. PIDS+=($!)
  66. OUTS+=($OUT)
  67. }
  68. go build ./...
  69. go test -race ./...
  70. go test -race -tags proto1_legacy ./...
  71. done
  72. # Wait for all processes to finish.
  73. RET=0
  74. for I in ${!PIDS[@]}; do
  75. print "${LABELS[$I]}"
  76. if ! wait ${PIDS[$I]}; then
  77. cat ${OUTS[$I]} # only output upon error
  78. RET=1
  79. fi
  80. done
  81. # TODO: Check for stale generated Go source files.
  82. # Check for unformatted Go source files.
  83. FMT_DIFF=$(cd $REPO_ROOT && ${GO_LATEST_BIN}fmt -d . 2>&1)
  84. if [ ! -z "$FMT_DIFF" ]; then
  85. print "gofmt -d ."
  86. echo "$FMT_DIFF"
  87. RET=1
  88. fi
  89. # Check for changed files.
  90. GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
  91. if [ ! -z "$GIT_DIFF" ]; then
  92. print "git diff HEAD"
  93. echo "$GIT_DIFF"
  94. RET=1
  95. fi
  96. # Check for untracked files.
  97. GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
  98. if [ ! -z "$GIT_UNTRACKED" ]; then
  99. print "git ls-files --others --exclude-standard"
  100. echo "$GIT_UNTRACKED"
  101. RET=1
  102. fi
  103. # Print termination status.
  104. if [ $RET -eq 0 ]; then
  105. echo -e "\x1b[32;1mPASS\x1b[0m"
  106. else
  107. echo -e "\x1b[31;1mFAIL\x1b[0m"
  108. fi
  109. exit $RET