test.bash: more test script improvements

Changes made:
* create a /bin directory for symlinks and prepend it to the PATH
* include the "go" prefix in the Go toolchain versions
* move setting up GOPATH before calling "go mod vendor" since modules dump their
results inside a special directory within the GOPATH
* setup a per-version Go cache to 1) work around a bug in the toolchain and
2) to allow Travis to also cache this state
* factor out common logic into a check function that ensures stdout is empty

Change-Id: I9400452b85a66f019cb616aaded33610f2c1eb2f
Reviewed-on: https://go-review.googlesource.com/129402
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/test.bash b/test.bash
index 2c65d76..b5d9b65 100755
--- a/test.bash
+++ b/test.bash
@@ -7,14 +7,17 @@
 
 function print() { echo -e "\x1b[1m> $@\x1b[0m"; }
 
-# Create a test directory.
-# The Go and protobuf toolchains used for testing will be cached here.
+# 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.
 TEST_DIR=/tmp/golang-protobuf-test
-if [ ! -d $TEST_DIR ]; then
-	print "mkdir $TEST_DIR"
-	mkdir -p $TEST_DIR
-fi
-cd $TEST_DIR
+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
 
 # Download and build the protobuf toolchain.
 # We avoid downloading the pre-compiled binaries since they do not contain
@@ -26,37 +29,37 @@
 	(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
-export PATH=$TEST_DIR/$PROTOBUF_DIR/src:$TEST_DIR/$PROTOBUF_DIR/conformance:$PATH
+register_binary conformance-test-runner $PROTOBUF_DIR/conformance/conformance-test-runner
+register_binary protoc $PROTOBUF_DIR/src/protoc
 
 # Download each Go toolchain version.
-GO_LATEST=1.11rc1
-GO_VERSIONS=(1.10.3 $GO_LATEST)
+GO_LATEST=go1.11rc1
+GO_VERSIONS=(go1.10.3 $GO_LATEST)
 for GO_VERSION in ${GO_VERSIONS[@]}; do
-	GO_DIR=go$GO_VERSION
-	if [ ! -d $GO_DIR ]; then
-		print "download $GO_DIR"
+	if [ ! -d $GO_VERSION ]; then
+		print "download $GO_VERSION"
 		GOOS=$(uname | tr '[:upper:]' '[:lower:]')
-		(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
+		(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
 	fi
+	register_binary $GO_VERSION $GO_VERSION/bin/go
 done
+register_binary go $GO_LATEST/bin/go
+register_binary gofmt $GO_LATEST/bin/gofmt
 
 # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
 # Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
 unset GOROOT
 
+# Setup GOPATH for pre-module support.
+MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
+rm -f gopath/src/$MODULE_PATH # best-effort delete
+mkdir -p gopath/src/$(dirname $MODULE_PATH)
+(cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
+export GOPATH=$TEST_DIR/gopath
+
 # Download dependencies using modules.
 # For pre-module support, dump the dependencies in a vendor directory.
-# TODO: use GOFLAGS="-mod=readonly" when https://golang.org/issue/26850 is fixed.
-GO_LATEST_BIN=$TEST_DIR/go$GO_LATEST/bin/go
-(cd $REPO_ROOT && $GO_LATEST_BIN mod tidy && $GO_LATEST_BIN mod vendor) || exit 1
-
-# Setup GOPATH for pre-module support.
-MODULE_PATH=$(cd $REPO_ROOT && $GO_LATEST_BIN list -m -f "{{.Path}}")
-if [ ! -d gopath/src/$MODULE_PATH ]; then
-	mkdir -p gopath/src/$(dirname $MODULE_PATH)
-	(cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
-fi
-export GOPATH=$TEST_DIR/gopath
+(cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
 
 # Run tests across every supported version of Go.
 LABELS=()
@@ -67,10 +70,12 @@
 for GO_VERSION in ${GO_VERSIONS[@]}; do
 	# Run the go command in a background process.
 	function go() {
-		GO_BIN=go$GO_VERSION/bin/go
-		LABELS+=("$(echo "go$GO_VERSION $@")")
+		# 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 $@")")
 		OUT=$(mktemp)
-		(cd $GOPATH/src/$MODULE_PATH && $TEST_DIR/$GO_BIN $@ &> $OUT) &
+		(cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) &
 		PIDS+=($!)
 		OUTS+=($OUT)
 	}
@@ -79,6 +84,8 @@
 	go test -race ./...
 	go test -race -tags purego ./...
 	go test -race -tags proto1_legacy ./...
+
+	unset go # to avoid confusing later invocations of "go"
 done
 
 # Wait for all processes to finish.
@@ -91,37 +98,23 @@
 	fi
 done
 
-# Check for stale generated source files.
-GEN_DIFF=$(cd $REPO_ROOT && ${GO_LATEST_BIN} run ./internal/cmd/generate-types 2>&1)
-if [ ! -z "$GEN_DIFF" ]; then
-	print "go run ./internal/cmd/generate-types"
-	echo "$GEN_DIFF"
-	RET=1
-fi
+# 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
+}
 
-# Check for unformatted Go source files.
-FMT_DIFF=$(cd $REPO_ROOT && ${GO_LATEST_BIN}fmt -d . 2>&1)
-if [ ! -z "$FMT_DIFF" ]; then
-	print "gofmt -d ."
-	echo "$FMT_DIFF"
-	RET=1
-fi
+# Check for stale or unformatted source files.
+check go run ./internal/cmd/generate-types
+check gofmt -d .
 
-# Check for changed files.
-GIT_DIFF=$(cd $REPO_ROOT && git diff --no-prefix HEAD 2>&1)
-if [ ! -z "$GIT_DIFF" ]; then
-	print "git diff HEAD"
-	echo "$GIT_DIFF"
-	RET=1
-fi
-
-# Check for untracked files.
-GIT_UNTRACKED=$(cd $REPO_ROOT && git ls-files --others --exclude-standard 2>&1)
-if [ ! -z "$GIT_UNTRACKED" ]; then
-	print "git ls-files --others --exclude-standard"
-	echo "$GIT_UNTRACKED"
-	RET=1
-fi
+# Check for changed or untracked files.
+check git diff --no-prefix HEAD
+check git ls-files --others --exclude-standard
 
 # Print termination status.
 if [ $RET -eq 0 ]; then