blob: f44cf48e840f6ae04a0e2135a8b2eb79a264f98a [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 Tsaied2fbe02018-08-04 00:17:53 -070040# Download dependencies using modules.
41# For pre-module support, dump the dependencies in a vendor directory.
42# TODO: use GOFLAGS="-mod=readonly" when https://golang.org/issue/26850 is fixed.
43GO_LATEST_BIN=$TEST_DIR/go$GO_LATEST/bin/go
44(cd $REPO_ROOT && $GO_LATEST_BIN mod tidy && $GO_LATEST_BIN mod vendor) || exit 1
45
Joe Tsai1bed4542018-08-03 19:13:55 -070046# Setup GOPATH for pre-module support.
Joe Tsaied2fbe02018-08-04 00:17:53 -070047MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}")
Joe Tsai1bed4542018-08-03 19:13:55 -070048if [ ! -d gopath/src/$MODULE_PATH ]; then
49 mkdir -p gopath/src/$(dirname $MODULE_PATH)
50 (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
51fi
52export GOPATH=$TEST_DIR/gopath
53
54# Run tests across every supported version of Go.
55FAIL=0
56for GO_VERSION in ${GO_VERSIONS[@]}; do
57 export GOROOT=$TEST_DIR/go$GO_VERSION
58 GO_BIN=go$GO_VERSION/bin/go
59 function go_build() {
60 echo "$GO_BIN build $@"
61 (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN build $@) || FAIL=1
62 }
63 function go_test() {
64 echo "$GO_BIN test $@"
65 (cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN test $@) || FAIL=1
66 }
67
68 go_build ./...
69 go_test -race ./...
70 go_test -race -tags proto1_legacy ./...
71done
Joe Tsaied2fbe02018-08-04 00:17:53 -070072
73# Check for changed files.
74GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
75if [ ! -z "$GIT_DIFF" ]; then
76 echo -e "git diff HEAD\n$GIT_DIFF"
77 FAIL=1
78fi
79
80# Check for untracked files.
81GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
82if [ ! -z "$GIT_UNTRACKED" ]; then
83 echo -e "git ls-files --others --exclude-standard\n$GIT_UNTRACKED"
84 FAIL=1
85fi
86
Joe Tsai1bed4542018-08-03 19:13:55 -070087exit $FAIL