blob: 99c8a7fc321742df22b56b554a644eb72e66979b [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
Joe Tsaiea04a132018-08-07 16:42:30 -07008function print() { echo -e "\x1b[1m> $@\x1b[0m"; }
9
Joe Tsai1bed4542018-08-03 19:13:55 -070010# Create a test directory.
11# The Go and protobuf toolchains used for testing will be cached here.
12TEST_DIR=/tmp/golang-protobuf-test
13if [ ! -d $TEST_DIR ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -070014 print "mkdir $TEST_DIR"
Joe Tsai1bed4542018-08-03 19:13:55 -070015 mkdir -p $TEST_DIR
16fi
17cd $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.
22PROTOBUF_VERSION=3.6.1
23PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
24if [ ! -d $PROTOBUF_DIR ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -070025 print "download and build $PROTOBUF_DIR"
Joe Tsai1bed4542018-08-03 19:13:55 -070026 (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
28fi
Joe Tsaiea04a132018-08-07 16:42:30 -070029export PATH=$TEST_DIR/$PROTOBUF_DIR/src:$TEST_DIR/$PROTOBUF_DIR/conformance:$PATH
Joe Tsai1bed4542018-08-03 19:13:55 -070030
31# Download each Go toolchain version.
Joe Tsaied2fbe02018-08-04 00:17:53 -070032GO_LATEST=1.11beta3
33GO_VERSIONS=(1.9.7 1.10.3 $GO_LATEST)
Joe Tsai1bed4542018-08-03 19:13:55 -070034for GO_VERSION in ${GO_VERSIONS[@]}; do
35 GO_DIR=go$GO_VERSION
36 if [ ! -d $GO_DIR ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -070037 print "download $GO_DIR"
Joe Tsai1bed4542018-08-03 19:13:55 -070038 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
41done
42
Joe Tsaid1e65e62018-08-07 14:54:07 -070043# Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
44# Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
45unset GOROOT
46
Joe Tsaied2fbe02018-08-04 00:17:53 -070047# 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.
50GO_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 Tsai1bed4542018-08-03 19:13:55 -070053# Setup GOPATH for pre-module support.
Joe Tsaied2fbe02018-08-04 00:17:53 -070054MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}")
Joe Tsai1bed4542018-08-03 19:13:55 -070055if [ ! -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))
58fi
59export GOPATH=$TEST_DIR/gopath
60
61# Run tests across every supported version of Go.
Joe Tsaiea04a132018-08-07 16:42:30 -070062LABELS=()
63PIDS=()
64OUTS=()
65function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
66trap cleanup EXIT
Joe Tsai1bed4542018-08-03 19:13:55 -070067for GO_VERSION in ${GO_VERSIONS[@]}; do
Joe Tsaiea04a132018-08-07 16:42:30 -070068 # 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 Tsai1bed4542018-08-03 19:13:55 -070076 }
77
Joe Tsaiea04a132018-08-07 16:42:30 -070078 go build ./...
79 go test -race ./...
80 go test -race -tags proto1_legacy ./...
Joe Tsai1bed4542018-08-03 19:13:55 -070081done
Joe Tsaied2fbe02018-08-04 00:17:53 -070082
Joe Tsaiea04a132018-08-07 16:42:30 -070083# Wait for all processes to finish.
84RET=0
85for I in ${!PIDS[@]}; do
86 print "${LABELS[$I]}"
87 if ! wait ${PIDS[$I]}; then
88 cat ${OUTS[$I]} # only output upon error
89 RET=1
90 fi
91done
92
93# TODO: Check for stale generated Go source files.
94
95# Check for unformatted Go source files.
96FMT_DIFF=$(cd $REPO_ROOT && ${GO_LATEST_BIN}fmt -d . 2>&1)
97if [ ! -z "$FMT_DIFF" ]; then
98 print "gofmt -d ."
99 echo "$FMT_DIFF"
100 RET=1
101fi
102
Joe Tsaied2fbe02018-08-04 00:17:53 -0700103# Check for changed files.
104GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
105if [ ! -z "$GIT_DIFF" ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -0700106 print "git diff HEAD"
107 echo "$GIT_DIFF"
108 RET=1
Joe Tsaied2fbe02018-08-04 00:17:53 -0700109fi
110
111# Check for untracked files.
112GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
113if [ ! -z "$GIT_UNTRACKED" ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -0700114 print "git ls-files --others --exclude-standard"
115 echo "$GIT_UNTRACKED"
116 RET=1
Joe Tsaied2fbe02018-08-04 00:17:53 -0700117fi
118
Joe Tsaiea04a132018-08-07 16:42:30 -0700119# Print termination status.
120if [ $RET -eq 0 ]; then
121 echo -e "\x1b[32;1mPASS\x1b[0m"
122else
123 echo -e "\x1b[31;1mFAIL\x1b[0m"
124fi
125exit $RET