blob: 87b1fe049db532e5e2c67b447bff85ca33eb050e [file] [log] [blame]
Joe Tsai1bed4542018-08-03 19:13:55 -07001#!/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
6REPO_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.
10TEST_DIR=/tmp/golang-protobuf-test
11if [ ! -d $TEST_DIR ]; then
12 echo "mkdir $TEST_DIR"
13 mkdir -p $TEST_DIR
14fi
15cd $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.
20PROTOBUF_VERSION=3.6.1
21PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
22if [ ! -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
26fi
27
28# Download each Go toolchain version.
Joe Tsaied2fbe02018-08-04 00:17:53 -070029GO_LATEST=1.11beta3
30GO_VERSIONS=(1.9.7 1.10.3 $GO_LATEST)
Joe Tsai1bed4542018-08-03 19:13:55 -070031for 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
38done
39
Joe Tsaid1e65e62018-08-07 14:54:07 -070040# Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
41# Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
42unset GOROOT
43
Joe Tsaied2fbe02018-08-04 00:17:53 -070044# 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.
47GO_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 Tsai1bed4542018-08-03 19:13:55 -070050# Setup GOPATH for pre-module support.
Joe Tsaied2fbe02018-08-04 00:17:53 -070051MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}")
Joe Tsai1bed4542018-08-03 19:13:55 -070052if [ ! -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))
55fi
56export GOPATH=$TEST_DIR/gopath
57
58# Run tests across every supported version of Go.
59FAIL=0
60for GO_VERSION in ${GO_VERSIONS[@]}; do
Joe Tsai1bed4542018-08-03 19:13:55 -070061 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 ./...
74done
Joe Tsaied2fbe02018-08-04 00:17:53 -070075
76# Check for changed files.
77GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
78if [ ! -z "$GIT_DIFF" ]; then
79 echo -e "git diff HEAD\n$GIT_DIFF"
80 FAIL=1
81fi
82
83# Check for untracked files.
84GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
85if [ ! -z "$GIT_UNTRACKED" ]; then
86 echo -e "git ls-files --others --exclude-standard\n$GIT_UNTRACKED"
87 FAIL=1
88fi
89
Joe Tsai1bed4542018-08-03 19:13:55 -070090exit $FAIL