2018-08-04 02:13:55 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style
|
|
|
|
# license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
|
2018-08-07 23:42:30 +00:00
|
|
|
function print() { echo -e "\x1b[1m> $@\x1b[0m"; }
|
|
|
|
|
2018-08-15 18:53:54 +00:00
|
|
|
# The test directory contains the Go and protobuf toolchains used for testing.
|
|
|
|
# The bin directory contains symlinks to each tool by version.
|
|
|
|
# It is safe to delete this directory and run the test script from scratch.
|
2018-09-06 19:37:19 +00:00
|
|
|
TEST_DIR=$REPO_ROOT/.cache
|
2018-08-15 18:53:54 +00:00
|
|
|
mkdir -p $TEST_DIR/bin
|
|
|
|
function register_binary() {
|
|
|
|
rm -f $TEST_DIR/bin/$1 # best-effort delete
|
|
|
|
ln -s $TEST_DIR/$2 $TEST_DIR/bin/$1
|
|
|
|
}
|
|
|
|
export PATH=$TEST_DIR/bin:$PATH
|
|
|
|
cd $TEST_DIR # install toolchains relative to test directory
|
2018-08-04 02:13:55 +00:00
|
|
|
|
|
|
|
# Download and build the protobuf toolchain.
|
|
|
|
# We avoid downloading the pre-compiled binaries since they do not contain
|
|
|
|
# the conformance test runner.
|
|
|
|
PROTOBUF_VERSION=3.6.1
|
|
|
|
PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
|
|
|
|
if [ ! -d $PROTOBUF_DIR ]; then
|
2018-08-07 23:42:30 +00:00
|
|
|
print "download and build $PROTOBUF_DIR"
|
2018-08-04 02:13:55 +00:00
|
|
|
(curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1
|
|
|
|
(cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1
|
|
|
|
fi
|
2018-08-15 18:53:54 +00:00
|
|
|
register_binary conformance-test-runner $PROTOBUF_DIR/conformance/conformance-test-runner
|
|
|
|
register_binary protoc $PROTOBUF_DIR/src/protoc
|
2018-09-19 18:43:35 +00:00
|
|
|
# Allow protoc to find google/protobuf/*.proto.
|
|
|
|
rm -rf $PROTOBUF_DIR/src/include
|
|
|
|
mkdir -p $PROTOBUF_DIR/src/include
|
|
|
|
ln -s ../google $PROTOBUF_DIR/src/include/google
|
2018-08-04 02:13:55 +00:00
|
|
|
|
|
|
|
# Download each Go toolchain version.
|
2018-10-16 00:24:39 +00:00
|
|
|
GO_LATEST=go1.11.1
|
|
|
|
GO_VERSIONS=(go1.9.7 go1.10.4 $GO_LATEST)
|
2018-08-04 02:13:55 +00:00
|
|
|
for GO_VERSION in ${GO_VERSIONS[@]}; do
|
2018-08-15 18:53:54 +00:00
|
|
|
if [ ! -d $GO_VERSION ]; then
|
|
|
|
print "download $GO_VERSION"
|
2018-08-04 02:13:55 +00:00
|
|
|
GOOS=$(uname | tr '[:upper:]' '[:lower:]')
|
2018-08-15 18:53:54 +00:00
|
|
|
(mkdir $GO_VERSION && curl -s -L https://dl.google.com/go/$GO_VERSION.$GOOS-amd64.tar.gz | tar -zxf - -C $GO_VERSION --strip-components 1) || exit 1
|
2018-08-04 02:13:55 +00:00
|
|
|
fi
|
2018-08-15 18:53:54 +00:00
|
|
|
register_binary $GO_VERSION $GO_VERSION/bin/go
|
2018-08-04 02:13:55 +00:00
|
|
|
done
|
2018-08-15 18:53:54 +00:00
|
|
|
register_binary go $GO_LATEST/bin/go
|
|
|
|
register_binary gofmt $GO_LATEST/bin/gofmt
|
2018-08-04 02:13:55 +00:00
|
|
|
|
2018-08-07 21:54:07 +00:00
|
|
|
# Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
|
|
|
|
# Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
|
|
|
|
unset GOROOT
|
|
|
|
|
2018-08-04 02:13:55 +00:00
|
|
|
# Setup GOPATH for pre-module support.
|
2018-09-22 23:00:02 +00:00
|
|
|
export GOPATH=$TEST_DIR/gopath
|
2018-08-15 18:53:54 +00:00
|
|
|
MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
|
2018-09-22 23:00:02 +00:00
|
|
|
rm -rf gopath/src # best-effort delete
|
2018-08-15 18:53:54 +00:00
|
|
|
mkdir -p gopath/src/$(dirname $MODULE_PATH)
|
|
|
|
(cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
|
2018-08-04 02:13:55 +00:00
|
|
|
|
2018-08-15 18:53:54 +00:00
|
|
|
# Download dependencies using modules.
|
|
|
|
# For pre-module support, dump the dependencies in a vendor directory.
|
|
|
|
(cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
|
|
|
|
|
2018-08-04 02:13:55 +00:00
|
|
|
# Run tests across every supported version of Go.
|
2018-08-07 23:42:30 +00:00
|
|
|
LABELS=()
|
|
|
|
PIDS=()
|
|
|
|
OUTS=()
|
|
|
|
function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
|
|
|
|
trap cleanup EXIT
|
2018-08-04 02:13:55 +00:00
|
|
|
for GO_VERSION in ${GO_VERSIONS[@]}; do
|
2018-08-07 23:42:30 +00:00
|
|
|
# Run the go command in a background process.
|
|
|
|
function go() {
|
2018-08-15 18:53:54 +00:00
|
|
|
# Use a per-version Go cache to work around bugs in Go build caching.
|
|
|
|
# See https://golang.org/issue/26883
|
|
|
|
GO_CACHE="$TEST_DIR/cache.$GO_VERSION"
|
|
|
|
LABELS+=("$(echo "$GO_VERSION $@")")
|
2018-08-07 23:42:30 +00:00
|
|
|
OUT=$(mktemp)
|
2018-08-15 18:53:54 +00:00
|
|
|
(cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) &
|
2018-08-07 23:42:30 +00:00
|
|
|
PIDS+=($!)
|
|
|
|
OUTS+=($OUT)
|
2018-08-04 02:13:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:42:30 +00:00
|
|
|
go build ./...
|
|
|
|
go test -race ./...
|
2018-08-04 00:13:23 +00:00
|
|
|
go test -race -tags purego ./...
|
2018-08-07 23:42:30 +00:00
|
|
|
go test -race -tags proto1_legacy ./...
|
2018-08-15 18:53:54 +00:00
|
|
|
|
2018-10-16 00:34:43 +00:00
|
|
|
# Only run golden tests with the latest Go version since
|
|
|
|
# the test results are sensitive to the exact version used.
|
|
|
|
if [ $GO_VERSION = $GO_LATEST ]; then
|
|
|
|
go test -tags golden ./...
|
|
|
|
fi
|
|
|
|
|
2018-08-15 18:53:54 +00:00
|
|
|
unset go # to avoid confusing later invocations of "go"
|
2018-08-07 23:42:30 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# Wait for all processes to finish.
|
|
|
|
RET=0
|
|
|
|
for I in ${!PIDS[@]}; do
|
|
|
|
print "${LABELS[$I]}"
|
|
|
|
if ! wait ${PIDS[$I]}; then
|
|
|
|
cat ${OUTS[$I]} # only output upon error
|
|
|
|
RET=1
|
|
|
|
fi
|
2018-08-04 02:13:55 +00:00
|
|
|
done
|
2018-08-04 07:17:53 +00:00
|
|
|
|
2018-08-15 18:53:54 +00:00
|
|
|
# Run commands that produce output when there is a failure.
|
|
|
|
function check() {
|
|
|
|
OUT=$(cd $REPO_ROOT && "$@" 2>&1)
|
|
|
|
if [ ! -z "$OUT" ]; then
|
|
|
|
print "$@"
|
|
|
|
echo "$OUT"
|
|
|
|
RET=1
|
|
|
|
fi
|
|
|
|
}
|
2018-08-07 23:42:30 +00:00
|
|
|
|
2018-08-15 18:53:54 +00:00
|
|
|
# Check for stale or unformatted source files.
|
|
|
|
check go run ./internal/cmd/generate-types
|
2018-09-06 19:37:19 +00:00
|
|
|
check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go')
|
2018-08-04 07:17:53 +00:00
|
|
|
|
2018-08-15 18:53:54 +00:00
|
|
|
# Check for changed or untracked files.
|
|
|
|
check git diff --no-prefix HEAD
|
|
|
|
check git ls-files --others --exclude-standard
|
2018-08-04 07:17:53 +00:00
|
|
|
|
2018-08-07 23:42:30 +00:00
|
|
|
# Print termination status.
|
|
|
|
if [ $RET -eq 0 ]; then
|
|
|
|
echo -e "\x1b[32;1mPASS\x1b[0m"
|
|
|
|
else
|
|
|
|
echo -e "\x1b[31;1mFAIL\x1b[0m"
|
|
|
|
fi
|
|
|
|
exit $RET
|