test.bash 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. # Allow protoc to find google/protobuf/*.proto.
  31. rm -rf $PROTOBUF_DIR/src/include
  32. mkdir -p $PROTOBUF_DIR/src/include
  33. ln -s ../google $PROTOBUF_DIR/src/include/google
  34. # Download each Go toolchain version.
  35. GO_LATEST=go1.11
  36. GO_VERSIONS=(go1.9.7 go1.10.3 $GO_LATEST)
  37. for GO_VERSION in ${GO_VERSIONS[@]}; do
  38. if [ ! -d $GO_VERSION ]; then
  39. print "download $GO_VERSION"
  40. GOOS=$(uname | tr '[:upper:]' '[:lower:]')
  41. (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
  42. fi
  43. register_binary $GO_VERSION $GO_VERSION/bin/go
  44. done
  45. register_binary go $GO_LATEST/bin/go
  46. register_binary gofmt $GO_LATEST/bin/gofmt
  47. # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
  48. # Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
  49. unset GOROOT
  50. # Setup GOPATH for pre-module support.
  51. export GOPATH=$TEST_DIR/gopath
  52. MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
  53. rm -rf gopath/src # best-effort delete
  54. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  55. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  56. # Download dependencies using modules.
  57. # For pre-module support, dump the dependencies in a vendor directory.
  58. (cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
  59. # Run tests across every supported version of Go.
  60. LABELS=()
  61. PIDS=()
  62. OUTS=()
  63. function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
  64. trap cleanup EXIT
  65. for GO_VERSION in ${GO_VERSIONS[@]}; do
  66. # Run the go command in a background process.
  67. function go() {
  68. # Use a per-version Go cache to work around bugs in Go build caching.
  69. # See https://golang.org/issue/26883
  70. GO_CACHE="$TEST_DIR/cache.$GO_VERSION"
  71. LABELS+=("$(echo "$GO_VERSION $@")")
  72. OUT=$(mktemp)
  73. (cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) &
  74. PIDS+=($!)
  75. OUTS+=($OUT)
  76. }
  77. go build ./...
  78. go test -race ./...
  79. go test -race -tags purego ./...
  80. go test -race -tags proto1_legacy ./...
  81. unset go # to avoid confusing later invocations of "go"
  82. done
  83. # Wait for all processes to finish.
  84. RET=0
  85. for I in ${!PIDS[@]}; do
  86. print "${LABELS[$I]}"
  87. if ! wait ${PIDS[$I]}; then
  88. cat ${OUTS[$I]} # only output upon error
  89. RET=1
  90. fi
  91. done
  92. # Run commands that produce output when there is a failure.
  93. function check() {
  94. OUT=$(cd $REPO_ROOT && "$@" 2>&1)
  95. if [ ! -z "$OUT" ]; then
  96. print "$@"
  97. echo "$OUT"
  98. RET=1
  99. fi
  100. }
  101. # Check for stale or unformatted source files.
  102. check go run ./internal/cmd/generate-types
  103. check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go')
  104. # Check for changed or untracked files.
  105. check git diff --no-prefix HEAD
  106. check git ls-files --others --exclude-standard
  107. # Print termination status.
  108. if [ $RET -eq 0 ]; then
  109. echo -e "\x1b[32;1mPASS\x1b[0m"
  110. else
  111. echo -e "\x1b[31;1mFAIL\x1b[0m"
  112. fi
  113. exit $RET