Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 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 | |
| 6 | REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 7 | |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 8 | function print() { echo -e "\x1b[1m> $@\x1b[0m"; } |
| 9 | |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 10 | # The test directory contains the Go and protobuf toolchains used for testing. |
| 11 | # The bin directory contains symlinks to each tool by version. |
| 12 | # It is safe to delete this directory and run the test script from scratch. |
Joe Tsai | f8505dac | 2018-09-06 12:37:19 -0700 | [diff] [blame] | 13 | TEST_DIR=$REPO_ROOT/.cache |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 14 | mkdir -p $TEST_DIR/bin |
| 15 | function register_binary() { |
| 16 | rm -f $TEST_DIR/bin/$1 # best-effort delete |
| 17 | ln -s $TEST_DIR/$2 $TEST_DIR/bin/$1 |
| 18 | } |
| 19 | export PATH=$TEST_DIR/bin:$PATH |
| 20 | cd $TEST_DIR # install toolchains relative to test directory |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 21 | |
| 22 | # Download and build the protobuf toolchain. |
| 23 | # We avoid downloading the pre-compiled binaries since they do not contain |
| 24 | # the conformance test runner. |
| 25 | PROTOBUF_VERSION=3.6.1 |
| 26 | PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION" |
| 27 | if [ ! -d $PROTOBUF_DIR ]; then |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 28 | print "download and build $PROTOBUF_DIR" |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 29 | (curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1 |
| 30 | (cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1 |
| 31 | fi |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 32 | register_binary conformance-test-runner $PROTOBUF_DIR/conformance/conformance-test-runner |
| 33 | register_binary protoc $PROTOBUF_DIR/src/protoc |
Damien Neil | a6c374a | 2018-09-19 11:43:35 -0700 | [diff] [blame] | 34 | # Allow protoc to find google/protobuf/*.proto. |
| 35 | rm -rf $PROTOBUF_DIR/src/include |
| 36 | mkdir -p $PROTOBUF_DIR/src/include |
| 37 | ln -s ../google $PROTOBUF_DIR/src/include/google |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 38 | |
| 39 | # Download each Go toolchain version. |
Herbie Ong | 402382e | 2019-01-02 15:58:39 -0800 | [diff] [blame^] | 40 | GO_LATEST=go1.11.4 |
| 41 | GO_VERSIONS=(go1.9.7 go1.10.7 $GO_LATEST) |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 42 | for GO_VERSION in ${GO_VERSIONS[@]}; do |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 43 | if [ ! -d $GO_VERSION ]; then |
| 44 | print "download $GO_VERSION" |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 45 | GOOS=$(uname | tr '[:upper:]' '[:lower:]') |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 46 | (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 |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 47 | fi |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 48 | register_binary $GO_VERSION $GO_VERSION/bin/go |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 49 | done |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 50 | register_binary go $GO_LATEST/bin/go |
| 51 | register_binary gofmt $GO_LATEST/bin/gofmt |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 52 | |
Joe Tsai | d1e65e6 | 2018-08-07 14:54:07 -0700 | [diff] [blame] | 53 | # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains. |
| 54 | # Explicitly clear GOROOT, so each toolchain uses their default GOROOT. |
| 55 | unset GOROOT |
| 56 | |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 57 | # Setup GOPATH for pre-module support. |
Joe Tsai | b4af2c6 | 2018-09-22 16:00:02 -0700 | [diff] [blame] | 58 | export GOPATH=$TEST_DIR/gopath |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 59 | MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}") |
Joe Tsai | b4af2c6 | 2018-09-22 16:00:02 -0700 | [diff] [blame] | 60 | rm -rf gopath/src # best-effort delete |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 61 | mkdir -p gopath/src/$(dirname $MODULE_PATH) |
| 62 | (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH)) |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 63 | |
Joe Tsai | ed2fbe0 | 2018-08-04 00:17:53 -0700 | [diff] [blame] | 64 | # Download dependencies using modules. |
| 65 | # For pre-module support, dump the dependencies in a vendor directory. |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 66 | (cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1 |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 67 | |
| 68 | # Run tests across every supported version of Go. |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 69 | LABELS=() |
| 70 | PIDS=() |
| 71 | OUTS=() |
| 72 | function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; } |
| 73 | trap cleanup EXIT |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 74 | for GO_VERSION in ${GO_VERSIONS[@]}; do |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 75 | # Run the go command in a background process. |
| 76 | function go() { |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 77 | # Use a per-version Go cache to work around bugs in Go build caching. |
| 78 | # See https://golang.org/issue/26883 |
| 79 | GO_CACHE="$TEST_DIR/cache.$GO_VERSION" |
| 80 | LABELS+=("$(echo "$GO_VERSION $@")") |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 81 | OUT=$(mktemp) |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 82 | (cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) & |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 83 | PIDS+=($!) |
| 84 | OUTS+=($OUT) |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 87 | go build ./... |
| 88 | go test -race ./... |
Joe Tsai | dbc9a12 | 2018-08-03 17:13:23 -0700 | [diff] [blame] | 89 | go test -race -tags purego ./... |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 90 | go test -race -tags proto1_legacy ./... |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 91 | |
Joe Tsai | 42fa50d | 2018-10-15 17:34:43 -0700 | [diff] [blame] | 92 | # Only run golden tests with the latest Go version since |
| 93 | # the test results are sensitive to the exact version used. |
| 94 | if [ $GO_VERSION = $GO_LATEST ]; then |
| 95 | go test -tags golden ./... |
| 96 | fi |
| 97 | |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 98 | unset go # to avoid confusing later invocations of "go" |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 99 | done |
Joe Tsai | ed2fbe0 | 2018-08-04 00:17:53 -0700 | [diff] [blame] | 100 | |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 101 | # Wait for all processes to finish. |
| 102 | RET=0 |
| 103 | for I in ${!PIDS[@]}; do |
| 104 | print "${LABELS[$I]}" |
| 105 | if ! wait ${PIDS[$I]}; then |
| 106 | cat ${OUTS[$I]} # only output upon error |
| 107 | RET=1 |
| 108 | fi |
| 109 | done |
| 110 | |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 111 | # Run commands that produce output when there is a failure. |
| 112 | function check() { |
| 113 | OUT=$(cd $REPO_ROOT && "$@" 2>&1) |
| 114 | if [ ! -z "$OUT" ]; then |
| 115 | print "$@" |
| 116 | echo "$OUT" |
| 117 | RET=1 |
| 118 | fi |
| 119 | } |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 120 | |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 121 | # Check for stale or unformatted source files. |
| 122 | check go run ./internal/cmd/generate-types |
Joe Tsai | f8505dac | 2018-09-06 12:37:19 -0700 | [diff] [blame] | 123 | check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go') |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 124 | |
Joe Tsai | 913ace2 | 2018-08-15 11:53:54 -0700 | [diff] [blame] | 125 | # Check for changed or untracked files. |
| 126 | check git diff --no-prefix HEAD |
| 127 | check git ls-files --others --exclude-standard |
Joe Tsai | ed2fbe0 | 2018-08-04 00:17:53 -0700 | [diff] [blame] | 128 | |
Joe Tsai | ea04a13 | 2018-08-07 16:42:30 -0700 | [diff] [blame] | 129 | # Print termination status. |
| 130 | if [ $RET -eq 0 ]; then |
| 131 | echo -e "\x1b[32;1mPASS\x1b[0m" |
| 132 | else |
| 133 | echo -e "\x1b[31;1mFAIL\x1b[0m" |
| 134 | fi |
| 135 | exit $RET |