test.bash 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/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. REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. function print() { echo -e "\x1b[1m> $@\x1b[0m"; }
  7. # The test directory contains the Go and protobuf toolchains used for testing.
  8. # The bin directory contains symlinks to each tool by version.
  9. # It is safe to delete this directory and run the test script from scratch.
  10. TEST_DIR=$REPO_ROOT/.cache
  11. mkdir -p $TEST_DIR/bin
  12. function register_binary() {
  13. rm -f $TEST_DIR/bin/$1 # best-effort delete
  14. ln -s $TEST_DIR/$2 $TEST_DIR/bin/$1
  15. }
  16. export PATH=$TEST_DIR/bin:$PATH
  17. cd $TEST_DIR # install toolchains relative to test directory
  18. # Download and build the protobuf toolchain.
  19. # We avoid downloading the pre-compiled binaries since they do not contain
  20. # the conformance test runner.
  21. PROTOBUF_VERSION=3.6.1
  22. PROTOBUF_DIR="protobuf-$PROTOBUF_VERSION"
  23. if [ ! -d $PROTOBUF_DIR ]; then
  24. print "download and build $PROTOBUF_DIR"
  25. (curl -s -L https://github.com/google/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-all-$PROTOBUF_VERSION.tar.gz | tar -zxf -) || exit 1
  26. (cd $PROTOBUF_DIR && ./configure && make && cd conformance && make) || exit 1
  27. fi
  28. register_binary conformance-test-runner $PROTOBUF_DIR/conformance/conformance-test-runner
  29. register_binary protoc $PROTOBUF_DIR/src/protoc
  30. export PROTOBUF_ROOT=$TEST_DIR/$PROTOBUF_DIR
  31. # Patch proto files in the toolchain with new locations of Go packages.
  32. # TODO: these changes should be committed upstream.
  33. if ! grep -q "option go_package =" $PROTOBUF_ROOT/conformance/conformance.proto; then
  34. (cat << EOF
  35. --- a/conformance/conformance.proto 2018-07-30 15:16:10.000000000 -0700
  36. +++ b/conformance/conformance.proto 2019-01-20 03:03:47.000000000 -0800
  37. @@ -32,0 +33 @@
  38. +option go_package = "github.com/golang/protobuf/v2/internal/testprotos/conformance;conformance_proto";
  39. --- a/src/google/protobuf/any.proto 2018-07-23 13:56:42.000000000 -0700
  40. +++ b/src/google/protobuf/any.proto 2019-01-20 02:58:13.000000000 -0800
  41. @@ -36 +36 @@
  42. -option go_package = "github.com/golang/protobuf/ptypes/any";
  43. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  44. --- a/src/google/protobuf/api.proto 2018-07-23 13:56:42.000000000 -0700
  45. +++ b/src/google/protobuf/api.proto 2019-01-20 03:00:58.000000000 -0800
  46. @@ -43 +43 @@
  47. -option go_package = "google.golang.org/genproto/protobuf/api;api";
  48. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  49. --- a/src/google/protobuf/compiler/plugin.proto 2018-07-23 13:56:42.000000000 -0700
  50. +++ b/src/google/protobuf/compiler/plugin.proto 2019-01-20 03:03:49.000000000 -0800
  51. @@ -52 +52 @@
  52. -option go_package = "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go";
  53. +option go_package = "github.com/golang/protobuf/v2/types/plugin;plugin_proto";
  54. --- a/src/google/protobuf/descriptor.proto 2018-07-23 13:56:42.000000000 -0700
  55. +++ b/src/google/protobuf/descriptor.proto 2019-01-20 03:03:52.000000000 -0800
  56. @@ -43 +43 @@
  57. -option go_package = "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor";
  58. +option go_package = "github.com/golang/protobuf/v2/types/descriptor;descriptor_proto";
  59. --- a/src/google/protobuf/duration.proto 2018-07-23 13:56:42.000000000 -0700
  60. +++ b/src/google/protobuf/duration.proto 2019-01-20 03:00:55.000000000 -0800
  61. @@ -37 +37 @@
  62. -option go_package = "github.com/golang/protobuf/ptypes/duration";
  63. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  64. --- a/src/google/protobuf/empty.proto 2018-07-23 13:56:42.000000000 -0700
  65. +++ b/src/google/protobuf/empty.proto 2019-01-20 03:00:52.000000000 -0800
  66. @@ -36 +36 @@
  67. -option go_package = "github.com/golang/protobuf/ptypes/empty";
  68. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  69. --- a/src/google/protobuf/field_mask.proto 2018-07-23 13:56:42.000000000 -0700
  70. +++ b/src/google/protobuf/field_mask.proto 2019-01-20 03:00:50.000000000 -0800
  71. @@ -40 +40 @@
  72. -option go_package = "google.golang.org/genproto/protobuf/field_mask;field_mask";
  73. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  74. --- a/src/google/protobuf/source_context.proto 2018-07-23 13:56:42.000000000 -0700
  75. +++ b/src/google/protobuf/source_context.proto 2019-01-20 03:00:47.000000000 -0800
  76. @@ -40 +40 @@
  77. -option go_package = "google.golang.org/genproto/protobuf/source_context;source_context";
  78. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  79. --- a/src/google/protobuf/struct.proto 2018-07-23 13:56:42.000000000 -0700
  80. +++ b/src/google/protobuf/struct.proto 2019-01-20 03:00:42.000000000 -0800
  81. @@ -37 +37 @@
  82. -option go_package = "github.com/golang/protobuf/ptypes/struct;structpb";
  83. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  84. --- a/src/google/protobuf/timestamp.proto 2018-07-23 13:56:42.000000000 -0700
  85. +++ b/src/google/protobuf/timestamp.proto 2019-01-20 03:00:40.000000000 -0800
  86. @@ -37 +37 @@
  87. -option go_package = "github.com/golang/protobuf/ptypes/timestamp";
  88. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  89. --- a/src/google/protobuf/type.proto 2018-07-23 13:56:42.000000000 -0700
  90. +++ b/src/google/protobuf/type.proto 2019-01-20 03:03:44.000000000 -0800
  91. @@ -44 +44 @@
  92. -option go_package = "google.golang.org/genproto/protobuf/ptype;ptype";
  93. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  94. --- a/src/google/protobuf/wrappers.proto 2018-07-23 13:56:42.000000000 -0700
  95. +++ b/src/google/protobuf/wrappers.proto 2019-01-20 03:03:45.000000000 -0800
  96. @@ -42 +42 @@
  97. -option go_package = "github.com/golang/protobuf/ptypes/wrappers";
  98. +option go_package = "github.com/golang/protobuf/v2/types/known;known_proto";
  99. EOF
  100. ) | patch -d $PROTOBUF_ROOT -p1
  101. fi
  102. # Download each Go toolchain version.
  103. GO_LATEST=go1.11.4
  104. GO_VERSIONS=(go1.9.7 go1.10.7 $GO_LATEST)
  105. for GO_VERSION in ${GO_VERSIONS[@]}; do
  106. if [ ! -d $GO_VERSION ]; then
  107. print "download $GO_VERSION"
  108. GOOS=$(uname | tr '[:upper:]' '[:lower:]')
  109. (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
  110. fi
  111. register_binary $GO_VERSION $GO_VERSION/bin/go
  112. done
  113. register_binary go $GO_LATEST/bin/go
  114. register_binary gofmt $GO_LATEST/bin/gofmt
  115. # Travis-CI sets GOROOT, which confuses later invocations of the Go toolchains.
  116. # Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
  117. unset GOROOT
  118. # Setup GOPATH for pre-module support.
  119. export GOPATH=$TEST_DIR/gopath
  120. MODULE_PATH=$(cd $REPO_ROOT && go list -m -f "{{.Path}}")
  121. rm -rf gopath/src # best-effort delete
  122. mkdir -p gopath/src/$(dirname $MODULE_PATH)
  123. (cd gopath/src/$(dirname $MODULE_PATH) && ln -s $REPO_ROOT $(basename $MODULE_PATH))
  124. # Download dependencies using modules.
  125. # For pre-module support, dump the dependencies in a vendor directory.
  126. (cd $REPO_ROOT && go mod tidy && go mod vendor) || exit 1
  127. # Regenerate Go source files.
  128. if [ "$1" == "-regenerate" ]; then
  129. cd $REPO_ROOT
  130. go run ./internal/cmd/generate-types -execute || exit 1
  131. go run ./internal/cmd/generate-protos -execute || exit 1
  132. gofmt -w $(git ls-files '*.go') || exit 1
  133. exit 0
  134. fi
  135. # Run tests across every supported version of Go.
  136. LABELS=()
  137. PIDS=()
  138. OUTS=()
  139. function cleanup() { for OUT in ${OUTS[@]}; do rm $OUT; done; }
  140. trap cleanup EXIT
  141. for GO_VERSION in ${GO_VERSIONS[@]}; do
  142. # Run the go command in a background process.
  143. function go() {
  144. # Use a per-version Go cache to work around bugs in Go build caching.
  145. # See https://golang.org/issue/26883
  146. GO_CACHE="$TEST_DIR/cache.$GO_VERSION"
  147. LABELS+=("$(echo "$GO_VERSION $@")")
  148. OUT=$(mktemp)
  149. (cd $GOPATH/src/$MODULE_PATH && GOCACHE=$GO_CACHE $GO_VERSION "$@" &> $OUT) &
  150. PIDS+=($!)
  151. OUTS+=($OUT)
  152. }
  153. # TODO: "go build" does not descend into testdata, which means that
  154. # generated .pb.go files are not being built.
  155. go build ./...
  156. go test -race ./...
  157. go test -race -tags purego ./...
  158. go test -race -tags proto1_legacy ./...
  159. unset go # to avoid confusing later invocations of "go"
  160. done
  161. # Wait for all processes to finish.
  162. RET=0
  163. for I in ${!PIDS[@]}; do
  164. print "${LABELS[$I]}"
  165. if ! wait ${PIDS[$I]}; then
  166. cat ${OUTS[$I]} # only output upon error
  167. RET=1
  168. fi
  169. done
  170. # Run commands that produce output when there is a failure.
  171. function check() {
  172. OUT=$(cd $REPO_ROOT && "$@" 2>&1)
  173. if [ ! -z "$OUT" ]; then
  174. print "$@"
  175. echo "$OUT"
  176. RET=1
  177. fi
  178. }
  179. # Check for stale or unformatted source files.
  180. check go run ./internal/cmd/generate-types
  181. check go run ./internal/cmd/generate-protos
  182. check gofmt -d $(cd $REPO_ROOT && git ls-files '*.go')
  183. # Check for changed or untracked files.
  184. check git diff --no-prefix HEAD
  185. check git ls-files --others --exclude-standard
  186. # Print termination status.
  187. if [ $RET -eq 0 ]; then
  188. echo -e "\x1b[32;1mPASS\x1b[0m"
  189. else
  190. echo -e "\x1b[31;1mFAIL\x1b[0m"
  191. fi
  192. exit $RET