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 | |
| 8 | # Create a test directory. |
| 9 | # The Go and protobuf toolchains used for testing will be cached here. |
| 10 | TEST_DIR=/tmp/golang-protobuf-test |
| 11 | if [ ! -d $TEST_DIR ]; then |
| 12 | echo "mkdir $TEST_DIR" |
| 13 | mkdir -p $TEST_DIR |
| 14 | fi |
| 15 | cd $TEST_DIR |
| 16 | |
| 17 | # Download and build the protobuf toolchain. |
| 18 | # We avoid downloading the pre-compiled binaries since they do not contain |
| 19 | # the conformance test runner. |
| 20 | PROTOBUF_VERSION=3.6.1 |
| 21 | PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION" |
| 22 | if [ ! -d $PROTOBUF_DIR ]; then |
| 23 | echo "download and build $PROTOBUF_DIR" |
| 24 | (curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1 |
| 25 | (cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1 |
| 26 | fi |
| 27 | |
| 28 | # Download each Go toolchain version. |
Joe Tsai | ed2fbe0 | 2018-08-04 00:17:53 -0700 | [diff] [blame] | 29 | GO_LATEST=1.11beta3 |
| 30 | GO_VERSIONS=(1.9.7 1.10.3 $GO_LATEST) |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 31 | for GO_VERSION in ${GO_VERSIONS[@]}; do |
| 32 | GO_DIR=go$GO_VERSION |
| 33 | if [ ! -d $GO_DIR ]; then |
| 34 | echo "download $GO_DIR" |
| 35 | GOOS=$(uname | tr '[:upper:]' '[:lower:]') |
| 36 | (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 |
| 37 | fi |
| 38 | done |
| 39 | |
Joe Tsai | d1e65e6 | 2018-08-07 14:54:07 -0700 | [diff] [blame] | 40 | # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains. |
| 41 | # Explicitly clear GOROOT, so each toolchain uses their default GOROOT. |
| 42 | unset GOROOT |
| 43 | |
Joe Tsai | ed2fbe0 | 2018-08-04 00:17:53 -0700 | [diff] [blame] | 44 | # Download dependencies using modules. |
| 45 | # For pre-module support, dump the dependencies in a vendor directory. |
| 46 | # TODO: use GOFLAGS="-mod=readonly" when https://golang.org/issue/26850 is fixed. |
| 47 | GO_LATEST_BIN=$TEST_DIR/go$GO_LATEST/bin/go |
| 48 | (cd $REPO_ROOT && $GO_LATEST_BIN mod tidy && $GO_LATEST_BIN mod vendor) || exit 1 |
| 49 | |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 50 | # Setup GOPATH for pre-module support. |
Joe Tsai | ed2fbe0 | 2018-08-04 00:17:53 -0700 | [diff] [blame] | 51 | MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}") |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 52 | if [ ! -d gopath/src/$MODULE_PATH ]; then |
| 53 | mkdir -p gopath/src/$(dirname $MODULE_PATH) |
| 54 | (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH)) |
| 55 | fi |
| 56 | export GOPATH=$TEST_DIR/gopath |
| 57 | |
| 58 | # Run tests across every supported version of Go. |
| 59 | FAIL=0 |
| 60 | for GO_VERSION in ${GO_VERSIONS[@]}; do |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 61 | GO_BIN=go$GO_VERSION/bin/go |
| 62 | function go_build() { |
| 63 | echo "$GO_BIN build $@" |
| 64 | (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN build $@) || FAIL=1 |
| 65 | } |
| 66 | function go_test() { |
| 67 | echo "$GO_BIN test $@" |
| 68 | (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN test $@) || FAIL=1 |
| 69 | } |
| 70 | |
| 71 | go_build ./... |
| 72 | go_test -race ./... |
| 73 | go_test -race -tags proto1_legacy ./... |
| 74 | done |
Joe Tsai | ed2fbe0 | 2018-08-04 00:17:53 -0700 | [diff] [blame] | 75 | |
| 76 | # Check for changed files. |
| 77 | GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1) |
| 78 | if [ ! -z "$GIT_DIFF" ]; then |
| 79 | echo -e "git diff HEAD\n$GIT_DIFF" |
| 80 | FAIL=1 |
| 81 | fi |
| 82 | |
| 83 | # Check for untracked files. |
| 84 | GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1) |
| 85 | if [ ! -z "$GIT_UNTRACKED" ]; then |
| 86 | echo -e "git ls-files --others --exclude-standard\n$GIT_UNTRACKED" |
| 87 | FAIL=1 |
| 88 | fi |
| 89 | |
Joe Tsai | 1bed454 | 2018-08-03 19:13:55 -0700 | [diff] [blame] | 90 | exit $FAIL |