blob: 98e37e1a1553ee41005833ae88bd2f9cda4e47d0 [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 Tsaif8505dac2018-09-06 12:37:19 -070013TEST_DIR=$REPO_ROOT/.cache
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
Damien Neila6c374a2018-09-19 11:43:35 -070034# Allow protoc to find google/protobuf/*.proto.
35rm -rf $PROTOBUF_DIR/src/include
36mkdir -p $PROTOBUF_DIR/src/include
37ln -s ../google $PROTOBUF_DIR/src/include/google
Joe Tsai1bed4542018-08-03 19:13:55 -070038
39# Download each Go toolchain version.
Herbie Ong402382e2019-01-02 15:58:39 -080040GO_LATEST=go1.11.4
41GO_VERSIONS=(go1.9.7 go1.10.7 $GO_LATEST)
Joe Tsai1bed4542018-08-03 19:13:55 -070042for GO_VERSION in ${GO_VERSIONS[@]}; do
Joe Tsai913ace22018-08-15 11:53:54 -070043 if [ ! -d $GO_VERSION ]; then
44 print "download $GO_VERSION"
Joe Tsai1bed4542018-08-03 19:13:55 -070045 GOOS=$(uname | tr '[:upper:]' '[:lower:]')
Joe Tsai913ace22018-08-15 11:53:54 -070046 (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 -070047 fi
Joe Tsai913ace22018-08-15 11:53:54 -070048 register_binary $GO_VERSION $GO_VERSION/bin/go
Joe Tsai1bed4542018-08-03 19:13:55 -070049done
Joe Tsai913ace22018-08-15 11:53:54 -070050register_binary go $GO_LATEST/bin/go
51register_binary gofmt $GO_LATEST/bin/gofmt
Joe Tsai1bed4542018-08-03 19:13:55 -070052
Joe Tsaid1e65e62018-08-07 14:54:07 -070053# Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
54# Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
55unset GOROOT
56
Joe Tsai913ace22018-08-15 11:53:54 -070057# Setup GOPATH for pre-module support.
Joe Tsaib4af2c62018-09-22 16:00:02 -070058export GOPATH=$TEST_DIR/gopath
Joe Tsai913ace22018-08-15 11:53:54 -070059MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
Joe Tsaib4af2c62018-09-22 16:00:02 -070060rm -rf gopath/src # best-effort delete
Joe Tsai913ace22018-08-15 11:53:54 -070061mkdir -p gopath/src/$(dirname $MODULE_PATH)
62(cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
Joe Tsai913ace22018-08-15 11:53:54 -070063
Joe Tsaied2fbe02018-08-04 00:17:53 -070064# Download dependencies using modules.
65# For pre-module support, dump the dependencies in a vendor directory.
Joe Tsai913ace22018-08-15 11:53:54 -070066(cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
Joe Tsai1bed4542018-08-03 19:13:55 -070067
68# Run tests across every supported version of Go.
Joe Tsaiea04a132018-08-07 16:42:30 -070069LABELS=()
70PIDS=()
71OUTS=()
72function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
73trap cleanup EXIT
Joe Tsai1bed4542018-08-03 19:13:55 -070074for GO_VERSION in ${GO_VERSIONS[@]}; do
Joe Tsaiea04a132018-08-07 16:42:30 -070075 # Run the go command in a background process.
76 function go() {
Joe Tsai913ace22018-08-15 11:53:54 -070077 # Use a per-version Go cache to work around bugs in Go build caching.
78 # See https://golang.org/issue/26883
79 GO_CACHE="$TEST_DIR/cache.$GO_VERSION"
80 LABELS+=("$(echo "$GO_VERSION $@")")
Joe Tsaiea04a132018-08-07 16:42:30 -070081 OUT=$(mktemp)
Joe Tsai913ace22018-08-15 11:53:54 -070082 (cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) &
Joe Tsaiea04a132018-08-07 16:42:30 -070083 PIDS+=($!)
84 OUTS+=($OUT)
Joe Tsai1bed4542018-08-03 19:13:55 -070085 }
86
Joe Tsaiea04a132018-08-07 16:42:30 -070087 go build ./...
88 go test -race ./...
Joe Tsaidbc9a122018-08-03 17:13:23 -070089 go test -race -tags purego ./...
Joe Tsaiea04a132018-08-07 16:42:30 -070090 go test -race -tags proto1_legacy ./...
Joe Tsai913ace22018-08-15 11:53:54 -070091
Joe Tsai42fa50d2018-10-15 17:34:43 -070092 # Only run golden tests with the latest Go version since
93 # the test results are sensitive to the exact version used.
94 if [ $GO_VERSION = $GO_LATEST ]; then
95 go test -tags golden ./...
96 fi
97
Joe Tsai913ace22018-08-15 11:53:54 -070098 unset go # to avoid confusing later invocations of "go"
Joe Tsai1bed4542018-08-03 19:13:55 -070099done
Joe Tsaied2fbe02018-08-04 00:17:53 -0700100
Joe Tsaiea04a132018-08-07 16:42:30 -0700101# Wait for all processes to finish.
102RET=0
103for I in ${!PIDS[@]}; do
104 print "${LABELS[$I]}"
105 if ! wait ${PIDS[$I]}; then
106 cat ${OUTS[$I]} # only output upon error
107 RET=1
108 fi
109done
110
Joe Tsai913ace22018-08-15 11:53:54 -0700111# Run commands that produce output when there is a failure.
112function check() {
113 OUT=$(cd $REPO_ROOT && "$@" 2>&1)
114 if [ ! -z "$OUT" ]; then
115 print "$@"
116 echo "$OUT"
117 RET=1
118 fi
119}
Joe Tsaiea04a132018-08-07 16:42:30 -0700120
Joe Tsai913ace22018-08-15 11:53:54 -0700121# Check for stale or unformatted source files.
122check go run ./internal/cmd/generate-types
Joe Tsaif8505dac2018-09-06 12:37:19 -0700123check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go')
Joe Tsaiea04a132018-08-07 16:42:30 -0700124
Joe Tsai913ace22018-08-15 11:53:54 -0700125# Check for changed or untracked files.
126check git diff --no-prefix HEAD
127check git ls-files --others --exclude-standard
Joe Tsaied2fbe02018-08-04 00:17:53 -0700128
Joe Tsaiea04a132018-08-07 16:42:30 -0700129# Print termination status.
130if [ $RET -eq 0 ]; then
131 echo -e "\x1b[32;1mPASS\x1b[0m"
132else
133 echo -e "\x1b[31;1mFAIL\x1b[0m"
134fi
135exit $RET