test.bash 4.0 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. # The test directory contains the Go and protobuf toolchains used for testing.
  8. # The bin directory contains symlinks to each tool by version.
  9. # It is safe to delete this directory and run the test script from scratch.
  10. TEST_DIR=$REPO_ROOT/.cache
  11. mkdir -p $TEST_DIR/bin
  12. function register_binary() {
  13. rm -f $TEST_DIR/bin/$1 # best-effort delete
  14. ln -s $TEST_DIR/$2 $TEST_DIR/bin/$1
  15. }
  16. export PATH=$TEST_DIR/bin:$PATH
  17. cd $TEST_DIR # install toolchains relative to test directory
  18. # Download and build the protobuf toolchain.
  19. # We avoid downloading the pre-compiled binaries since they do not contain
  20. # the conformance test runner.
  21. PROTOBUF_VERSION=3.6.1
  22. PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
  23. if [ ! -d $PROTOBUF_DIR ]; then
  24. print "download and build $PROTOBUF_DIR"
  25. (curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1
  26. (cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1
  27. fi
  28. register_binary conformance-test-runner $PROTOBUF_DIR/conformance/conformance-test-runner
  29. register_binary protoc $PROTOBUF_DIR/src/protoc
  30. # Download each Go toolchain version.
  31. GO_LATEST=go1.11
  32. GO_VERSIONS=(go1.10.3 $GO_LATEST)
  33. for GO_VERSION in ${GO_VERSIONS[@]}; do
  34. if [ ! -d $GO_VERSION ]; then
  35. print "download $GO_VERSION"
  36. GOOS=$(uname | tr '[:upper:]' '[:lower:]')
  37. (mkdir $GO_VERSION && curl -s -L https://dl.google.com/go/$GO_VERSION.$GOOS-amd64.tar.gz | tar -zxf - -C $GO_VERSION --strip-components 1) || exit 1
  38. fi
  39. register_binary $GO_VERSION $GO_VERSION/bin/go
  40. done
  41. register_binary go $GO_LATEST/bin/go
  42. register_binary gofmt $GO_LATEST/bin/gofmt
  43. # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
  44. # Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
  45. unset GOROOT
  46. # Setup GOPATH for pre-module support.
  47. MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
  48. rm -f gopath/src/$MODULE_PATH # best-effort delete
  49. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  50. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  51. export GOPATH=$TEST_DIR/gopath
  52. # Download dependencies using modules.
  53. # For pre-module support, dump the dependencies in a vendor directory.
  54. (cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
  55. # Run tests across every supported version of Go.
  56. LABELS=()
  57. PIDS=()
  58. OUTS=()
  59. function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
  60. trap cleanup EXIT
  61. for GO_VERSION in ${GO_VERSIONS[@]}; do
  62. # Run the go command in a background process.
  63. function go() {
  64. # Use a per-version Go cache to work around bugs in Go build caching.
  65. # See https://golang.org/issue/26883
  66. GO_CACHE="$TEST_DIR/cache.$GO_VERSION"
  67. LABELS+=("$(echo "$GO_VERSION $@")")
  68. OUT=$(mktemp)
  69. (cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) &
  70. PIDS+=($!)
  71. OUTS+=($OUT)
  72. }
  73. go build ./...
  74. go test -race ./...
  75. go test -race -tags purego ./...
  76. go test -race -tags proto1_legacy ./...
  77. unset go # to avoid confusing later invocations of "go"
  78. done
  79. # Wait for all processes to finish.
  80. RET=0
  81. for I in ${!PIDS[@]}; do
  82. print "${LABELS[$I]}"
  83. if ! wait ${PIDS[$I]}; then
  84. cat ${OUTS[$I]} # only output upon error
  85. RET=1
  86. fi
  87. done
  88. # Run commands that produce output when there is a failure.
  89. function check() {
  90. OUT=$(cd $REPO_ROOT && "$@" 2>&1)
  91. if [ ! -z "$OUT" ]; then
  92. print "$@"
  93. echo "$OUT"
  94. RET=1
  95. fi
  96. }
  97. # Check for stale or unformatted source files.
  98. check go run ./internal/cmd/generate-types
  99. check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go')
  100. # Check for changed or untracked files.
  101. check git diff --no-prefix HEAD
  102. check git ls-files --others --exclude-standard
  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