blob: 383d6c5ee46ac7cbf9f10f7488ff8981dec2666b [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 Tsai913ace22018-08-15 11:53:54 -070010# The test directory contains the Go and protobuf toolchains used for testing.
11# The bin directory contains symlinks to each tool by version.
12# It is safe to delete this directory and run the test script from scratch.
Joe Tsai1bed4542018-08-03 19:13:55 -070013TEST_DIR=/tmp/golang-protobuf-test
Joe Tsai913ace22018-08-15 11:53:54 -070014mkdir -p $TEST_DIR/bin
15function register_binary() {
16 rm -f $TEST_DIR/bin/$1 # best-effort delete
17 ln -s $TEST_DIR/$2 $TEST_DIR/bin/$1
18}
19export PATH=$TEST_DIR/bin:$PATH
20cd $TEST_DIR # install toolchains relative to test directory
Joe Tsai1bed4542018-08-03 19:13:55 -070021
22# Download and build the protobuf toolchain.
23# We avoid downloading the pre-compiled binaries since they do not contain
24# the conformance test runner.
25PROTOBUF_VERSION=3.6.1
26PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
27if [ ! -d $PROTOBUF_DIR ]; then
Joe Tsaiea04a132018-08-07 16:42:30 -070028 print "download and build $PROTOBUF_DIR"
Joe Tsai1bed4542018-08-03 19:13:55 -070029 (curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1
30 (cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1
31fi
Joe Tsai913ace22018-08-15 11:53:54 -070032register_binary conformance-test-runner $PROTOBUF_DIR/conformance/conformance-test-runner
33register_binary protoc $PROTOBUF_DIR/src/protoc
Joe Tsai1bed4542018-08-03 19:13:55 -070034
35# Download each Go toolchain version.
Joe Tsaic046d862018-08-23 16:37:24 -070036GO_LATEST=go1.11rc2
Joe Tsai913ace22018-08-15 11:53:54 -070037GO_VERSIONS=(go1.10.3 $GO_LATEST)
Joe Tsai1bed4542018-08-03 19:13:55 -070038for GO_VERSION in ${GO_VERSIONS[@]}; do
Joe Tsai913ace22018-08-15 11:53:54 -070039 if [ ! -d $GO_VERSION ]; then
40 print "download $GO_VERSION"
Joe Tsai1bed4542018-08-03 19:13:55 -070041 GOOS=$(uname | tr '[:upper:]' '[:lower:]')
Joe Tsai913ace22018-08-15 11:53:54 -070042 (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
Joe Tsai1bed4542018-08-03 19:13:55 -070043 fi
Joe Tsai913ace22018-08-15 11:53:54 -070044 register_binary $GO_VERSION $GO_VERSION/bin/go
Joe Tsai1bed4542018-08-03 19:13:55 -070045done
Joe Tsai913ace22018-08-15 11:53:54 -070046register_binary go $GO_LATEST/bin/go
47register_binary gofmt $GO_LATEST/bin/gofmt
Joe Tsai1bed4542018-08-03 19:13:55 -070048
Joe Tsaid1e65e62018-08-07 14:54:07 -070049# Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
50# Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
51unset GOROOT
52
Joe Tsai913ace22018-08-15 11:53:54 -070053# Setup GOPATH for pre-module support.
54MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
55rm -f gopath/src/$MODULE_PATH # best-effort delete
56mkdir -p gopath/src/$(dirname $MODULE_PATH)
57(cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
58export GOPATH=$TEST_DIR/gopath
59
Joe Tsaied2fbe02018-08-04 00:17:53 -070060# Download dependencies using modules.
61# For pre-module support, dump the dependencies in a vendor directory.
Joe Tsai913ace22018-08-15 11:53:54 -070062(cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
Joe Tsai1bed4542018-08-03 19:13:55 -070063
64# Run tests across every supported version of Go.
Joe Tsaiea04a132018-08-07 16:42:30 -070065LABELS=()
66PIDS=()
67OUTS=()
68function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
69trap cleanup EXIT
Joe Tsai1bed4542018-08-03 19:13:55 -070070for GO_VERSION in ${GO_VERSIONS[@]}; do
Joe Tsaiea04a132018-08-07 16:42:30 -070071 # Run the go command in a background process.
72 function go() {
Joe Tsai913ace22018-08-15 11:53:54 -070073 # Use a per-version Go cache to work around bugs in Go build caching.
74 # See https://golang.org/issue/26883
75 GO_CACHE="$TEST_DIR/cache.$GO_VERSION"
76 LABELS+=("$(echo "$GO_VERSION $@")")
Joe Tsaiea04a132018-08-07 16:42:30 -070077 OUT=$(mktemp)
Joe Tsai913ace22018-08-15 11:53:54 -070078 (cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) &
Joe Tsaiea04a132018-08-07 16:42:30 -070079 PIDS+=($!)
80 OUTS+=($OUT)
Joe Tsai1bed4542018-08-03 19:13:55 -070081 }
82
Joe Tsaiea04a132018-08-07 16:42:30 -070083 go build ./...
84 go test -race ./...
Joe Tsaidbc9a122018-08-03 17:13:23 -070085 go test -race -tags purego ./...
Joe Tsaiea04a132018-08-07 16:42:30 -070086 go test -race -tags proto1_legacy ./...
Joe Tsai913ace22018-08-15 11:53:54 -070087
88 unset go # to avoid confusing later invocations of "go"
Joe Tsai1bed4542018-08-03 19:13:55 -070089done
Joe Tsaied2fbe02018-08-04 00:17:53 -070090
Joe Tsaiea04a132018-08-07 16:42:30 -070091# Wait for all processes to finish.
92RET=0
93for I in ${!PIDS[@]}; do
94 print "${LABELS[$I]}"
95 if ! wait ${PIDS[$I]}; then
96 cat ${OUTS[$I]} # only output upon error
97 RET=1
98 fi
99done
100
Joe Tsai913ace22018-08-15 11:53:54 -0700101# Run commands that produce output when there is a failure.
102function check() {
103 OUT=$(cd $REPO_ROOT && "$@" 2>&1)
104 if [ ! -z "$OUT" ]; then
105 print "$@"
106 echo "$OUT"
107 RET=1
108 fi
109}
Joe Tsaiea04a132018-08-07 16:42:30 -0700110
Joe Tsai913ace22018-08-15 11:53:54 -0700111# Check for stale or unformatted source files.
112check go run ./internal/cmd/generate-types
113check gofmt -d .
Joe Tsaiea04a132018-08-07 16:42:30 -0700114
Joe Tsai913ace22018-08-15 11:53:54 -0700115# Check for changed or untracked files.
116check git diff --no-prefix HEAD
117check git ls-files --others --exclude-standard
Joe Tsaied2fbe02018-08-04 00:17:53 -0700118
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