test.bash 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 and update the v2 golang/protobuf code.
  31. if [ ! -d go-protobuf-v2 ]; then
  32. print "download v2 golang/protobuf"
  33. git clone https://go.googlesource.com/protobuf go-protobuf-v2
  34. fi
  35. print "update v2 golang/protobuf"
  36. (cd go-protobuf-v2 && git fetch && git pull)
  37. # Download the Go toolchain.
  38. GO_VERSION=go1.11
  39. if [ ! -d $GO_VERSION ]; then
  40. print "download $GO_VERSION"
  41. GOOS=$(uname | tr '[:upper:]' '[:lower:]')
  42. (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
  43. fi
  44. register_binary go $GO_VERSION/bin/go
  45. register_binary gofmt $GO_VERSION/bin/gofmt
  46. # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
  47. # Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
  48. unset GOROOT
  49. # Setup GOPATH for pre-module support.
  50. MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
  51. rm -f gopath/src/$MODULE_PATH # best-effort delete
  52. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  53. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  54. export GOPATH=$TEST_DIR/gopath
  55. # Download dependencies using modules.
  56. # For pre-module support, dump the dependencies in a vendor directory.
  57. (cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
  58. # Build and run all targets
  59. RET=0
  60. function go() {
  61. print "$GO_VERSION $@"
  62. # Use a per-version Go cache to work around bugs in Go build caching.
  63. # See https://golang.org/issue/26883
  64. GO_CACHE="$TEST_DIR/cache.$GO_VERSION"
  65. (cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@") || RET=1
  66. }
  67. go build ./...
  68. go test ./...
  69. # Run commands that produce output when there is a failure.
  70. function check() {
  71. OUT=$(cd $REPO_ROOT && "$@" 2>&1)
  72. if [ ! -z "$OUT" ]; then
  73. print "$@"
  74. echo "$OUT"
  75. RET=1
  76. fi
  77. }
  78. # Check for stale or unformatted source files.
  79. check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go')
  80. # Check for changed or untracked files.
  81. check git diff --no-prefix HEAD
  82. check git ls-files --others --exclude-standard
  83. # Print termination status.
  84. if [ $RET -eq 0 ]; then
  85. echo -e "\x1b[32;1mPASS\x1b[0m"
  86. else
  87. echo -e "\x1b[31;1mFAIL\x1b[0m"
  88. fi
  89. exit $RET