Procházet zdrojové kódy

go.mod: hardcode a local checkout of the v2 API

Add a test.bash script based on the v2 test script
that ensures the local checkout is in the same location and
that it is up-to-date.

Change-Id: Ia452b9cb272d2e3ecc5f57d68d1dd0e08142316d
Reviewed-on: https://go-review.googlesource.com/136537
Reviewed-by: Damien Neil <dneil@google.com>
Joe Tsai před 7 roky
rodič
revize
419ede8f8c
5 změnil soubory, kde provedl 117 přidání a 0 odebrání
  1. 3 0
      .gitignore
  2. 3 0
      go.mod
  3. 4 0
      go.sum
  4. 4 0
      proto/lib.go
  5. 103 0
      test.bash

+ 3 - 0
.gitignore

@@ -1,3 +1,6 @@
+.cache
+vendor
+
 .DS_Store
 *.[568ao]
 *.ao

+ 3 - 0
go.mod

@@ -4,4 +4,7 @@ require (
 	golang.org/x/net v0.0.0-20180906233101-161cd47e91fd // indirect
 	golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f
 	google.golang.org/genproto v0.0.0-20180831171423-11092d34479b
+	google.golang.org/proto v0.0.0
 )
+
+replace google.golang.org/proto => ./.cache/go-protobuf-v2

+ 4 - 0
go.sum

@@ -1,6 +1,10 @@
+github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
+golang.org/x/net v0.0.0-20180821023952-922f4815f713/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
 golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/tools v0.0.0-20180904205237-0aa4b8830f48/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 google.golang.org/genproto v0.0.0-20180831171423-11092d34479b h1:lohp5blsw53GBXtLyLNaTXPXS9pJ1tiTw61ZHUoE9Qw=
 google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=

+ 4 - 0
proto/lib.go

@@ -271,6 +271,10 @@ import (
 	"sort"
 	"strconv"
 	"sync"
+
+	// Add a bogus dependency on the v2 API to ensure the Go toolchain does not
+	// remove our dependency from the go.mod file.
+	_ "google.golang.org/proto/reflect/protoreflect"
 )
 
 // RequiredNotSetError is an error type returned by either Marshal or Unmarshal.

+ 103 - 0
test.bash

@@ -0,0 +1,103 @@
+#!/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)"
+
+function print() { echo -e "\x1b[1m> $@\x1b[0m"; }
+
+# 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=$REPO_ROOT/.cache
+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
+# the conformance test runner.
+PROTOBUF_VERSION=3.6.1
+PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
+if [ ! -d $PROTOBUF_DIR ]; then
+	print "download and build $PROTOBUF_DIR"
+	(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
+register_binary conformance-test-runner $PROTOBUF_DIR/conformance/conformance-test-runner
+register_binary protoc $PROTOBUF_DIR/src/protoc
+
+# Download and update the v2 golang/protobuf code.
+if [ ! -d go-protobuf-v2 ]; then
+	print "download v2 golang/protobuf"
+	git clone https://go.googlesource.com/protobuf go-protobuf-v2
+fi
+print "update v2 golang/protobuf"
+(cd go-protobuf-v2 && git fetch && git pull)
+
+# Download the Go toolchain.
+GO_VERSION=go1.11
+if [ ! -d $GO_VERSION ]; then
+	print "download $GO_VERSION"
+	GOOS=$(uname | tr '[:upper:]' '[:lower:]')
+	(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 $GO_VERSION/bin/go
+register_binary gofmt $GO_VERSION/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.
+(cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
+
+# Build and run all targets
+RET=0
+function go() {
+	print "$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"
+	(cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@") || RET=1
+}
+go build ./...
+go test ./...
+
+# 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 stale or unformatted source files.
+check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go')
+
+# 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
+	echo -e "\x1b[32;1mPASS\x1b[0m"
+else
+	echo -e "\x1b[31;1mFAIL\x1b[0m"
+fi
+exit $RET