blob: c010f948cd2cf9e17fbad72173fe227fca708764 [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 ./...
Joe Tsaidbc9a122018-08-03 17:13:23 -070080 go test -race -tags purego ./...
Joe Tsaiea04a132018-08-07 16:42:30 -070081 go test -race -tags proto1_legacy ./...
Joe Tsai1bed4542018-08-03 19:13:55 -070082done
Joe Tsaied2fbe02018-08-04 00:17:53 -070083
Joe Tsaiea04a132018-08-07 16:42:30 -070084# Wait for all processes to finish.
85RET=0
86for 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
92done
93
94# TODO: Check for stale generated Go source files.
95
96# Check for unformatted Go source files.
97FMT_DIFF=$(cd $REPO_ROOT && ${GO_LATEST_BIN}fmt -d . 2>&1)
98if [ ! -z "$FMT_DIFF" ]; then
99 print "gofmt -d ."
100 echo "$FMT_DIFF"
101 RET=1
102fi
103
Joe Tsaied2fbe02018-08-04 00:17:53 -0700104# Check for changed files.
105GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
106if [ ! -z "$GIT_DIFF" ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -0700107 print "git diff HEAD"
108 echo "$GIT_DIFF"
109 RET=1
Joe Tsaied2fbe02018-08-04 00:17:53 -0700110fi
111
112# Check for untracked files.
113GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
114if [ ! -z "$GIT_UNTRACKED" ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -0700115 print "git ls-files --others --exclude-standard"
116 echo "$GIT_UNTRACKED"
117 RET=1
Joe Tsaied2fbe02018-08-04 00:17:53 -0700118fi
119
Joe Tsaiea04a132018-08-07 16:42:30 -0700120# Print termination status.
121if [ $RET -eq 0 ]; then
122 echo -e "\x1b[32;1mPASS\x1b[0m"
123else
124 echo -e "\x1b[31;1mFAIL\x1b[0m"
125fi
126exit $RET