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