Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 5 | // +build ignore |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 6 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 7 | package main |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 8 | |
| 9 | import ( |
| 10 | "archive/tar" |
| 11 | "bytes" |
| 12 | "compress/gzip" |
| 13 | "flag" |
| 14 | "fmt" |
| 15 | "io" |
| 16 | "io/ioutil" |
| 17 | "net/http" |
| 18 | "os" |
| 19 | "os/exec" |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 20 | "path" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 21 | "path/filepath" |
| 22 | "regexp" |
| 23 | "runtime" |
| 24 | "strings" |
| 25 | "testing" |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 26 | "time" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | regenerate = flag.Bool("regenerate", false, "regenerate files") |
| 31 | |
Joe Tsai | 9d19e5c | 2019-04-03 15:44:11 -0700 | [diff] [blame] | 32 | protobufVersion = "3.7.1" |
| 33 | golangVersions = []string{"1.9.7", "1.10.8", "1.11.6", "1.12.1"} |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 34 | golangLatest = golangVersions[len(golangVersions)-1] |
| 35 | |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 36 | // purgeTimeout determines the maximum age of unused sub-directories. |
| 37 | purgeTimeout = 30 * 24 * time.Hour // 1 month |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 38 | |
| 39 | // Variables initialized by mustInitDeps. |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 40 | goPath string |
| 41 | modulePath string |
| 42 | protobufPath string |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | func Test(t *testing.T) { |
| 46 | mustInitDeps(t) |
| 47 | |
| 48 | if *regenerate { |
| 49 | t.Run("Generate", func(t *testing.T) { |
| 50 | fmt.Print(mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-types", "-execute")) |
| 51 | fmt.Print(mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-protos", "-execute")) |
| 52 | files := strings.Split(strings.TrimSpace(mustRunCommand(t, ".", "git", "ls-files", "*.go")), "\n") |
| 53 | mustRunCommand(t, ".", append([]string{"gofmt", "-w"}, files...)...) |
| 54 | }) |
| 55 | t.SkipNow() |
| 56 | } |
| 57 | |
| 58 | for _, v := range golangVersions { |
| 59 | t.Run("Go"+v, func(t *testing.T) { |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 60 | runGo := func(label, workDir string, args ...string) { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 61 | args[0] += v |
| 62 | t.Run(label, func(t *testing.T) { |
| 63 | t.Parallel() |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 64 | mustRunCommand(t, workDir, args...) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 65 | }) |
| 66 | } |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 67 | workDir := filepath.Join(goPath, "src", modulePath) |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 68 | runGo("TestNormal", workDir, "go", "test", "-race", "./...") |
| 69 | runGo("TestPureGo", workDir, "go", "test", "-race", "-tags", "purego", "./...") |
Damien Neil | cc2b078 | 2019-04-05 14:33:10 -0700 | [diff] [blame] | 70 | runGo("TestReflect", workDir, "go", "test", "-race", "-tags", "protoreflect", "./...") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 71 | if v == golangLatest { |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 72 | runGo("TestProto1Legacy", workDir, "go", "test", "-race", "-tags", "proto1_legacy", "./...") |
| 73 | runGo("TestProtocGenGo", "cmd/protoc-gen-go/testdata", "go", "test") |
| 74 | runGo("TestProtocGenGoGRPC", "cmd/protoc-gen-go-grpc/testdata", "go", "test") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 75 | } |
| 76 | }) |
| 77 | } |
| 78 | |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 79 | t.Run("ConformanceTests", func(t *testing.T) { |
| 80 | driverPath := filepath.Join("internal", "cmd", "conformance") |
| 81 | driver := filepath.Join(driverPath, "conformance.sh") |
| 82 | failureList := filepath.Join(driverPath, "failure_list_go.txt") |
| 83 | runner := filepath.Join(protobufPath, "conformance", "conformance-test-runner") |
| 84 | mustRunCommand(t, ".", runner, "--failure_list", failureList, "--enforce_recommended", driver) |
| 85 | }) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 86 | t.Run("GeneratedGoFiles", func(t *testing.T) { |
| 87 | diff := mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-types") |
| 88 | if strings.TrimSpace(diff) != "" { |
| 89 | t.Fatalf("stale generated files:\n%v", diff) |
| 90 | } |
| 91 | diff = mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-protos") |
| 92 | if strings.TrimSpace(diff) != "" { |
| 93 | t.Fatalf("stale generated files:\n%v", diff) |
| 94 | } |
| 95 | }) |
| 96 | t.Run("FormattedGoFiles", func(t *testing.T) { |
| 97 | files := strings.Split(strings.TrimSpace(mustRunCommand(t, ".", "git", "ls-files", "*.go")), "\n") |
| 98 | diff := mustRunCommand(t, ".", append([]string{"gofmt", "-d"}, files...)...) |
| 99 | if strings.TrimSpace(diff) != "" { |
| 100 | t.Fatalf("unformatted source files:\n%v", diff) |
| 101 | } |
| 102 | }) |
| 103 | t.Run("CommittedGitChanges", func(t *testing.T) { |
| 104 | diff := mustRunCommand(t, ".", "git", "diff", "--no-prefix", "HEAD") |
| 105 | if strings.TrimSpace(diff) != "" { |
| 106 | t.Fatalf("uncommitted changes:\n%v", diff) |
| 107 | } |
| 108 | }) |
| 109 | t.Run("TrackedGitFiles", func(t *testing.T) { |
| 110 | diff := mustRunCommand(t, ".", "git", "ls-files", "--others", "--exclude-standard") |
| 111 | if strings.TrimSpace(diff) != "" { |
| 112 | t.Fatalf("untracked files:\n%v", diff) |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | func mustInitDeps(t *testing.T) { |
| 118 | check := func(err error) { |
| 119 | t.Helper() |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Determine the directory to place the test directory. |
| 126 | repoRoot, err := os.Getwd() |
| 127 | check(err) |
| 128 | testDir := filepath.Join(repoRoot, ".cache") |
| 129 | check(os.MkdirAll(testDir, 0775)) |
| 130 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 131 | // Travis-CI has a hard-coded timeout where it kills the test after |
| 132 | // 10 minutes of a lack of activity on stdout. |
| 133 | // We work around this restriction by periodically printing the timestamp. |
| 134 | ticker := time.NewTicker(5 * time.Minute) |
| 135 | done := make(chan struct{}) |
| 136 | go func() { |
| 137 | now := time.Now() |
| 138 | for { |
| 139 | select { |
| 140 | case t := <-ticker.C: |
| 141 | fmt.Printf("\tt=%0.fmin\n", t.Sub(now).Minutes()) |
| 142 | case <-done: |
| 143 | return |
| 144 | } |
| 145 | } |
| 146 | }() |
| 147 | defer close(done) |
| 148 | defer ticker.Stop() |
| 149 | |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 150 | // Delete the current directory if non-empty, |
| 151 | // which only occurs if a dependency failed to initialize properly. |
| 152 | var workingDir string |
| 153 | defer func() { |
| 154 | if workingDir != "" { |
| 155 | os.RemoveAll(workingDir) // best-effort |
| 156 | } |
| 157 | }() |
| 158 | |
| 159 | // Delete other sub-directories that are no longer relevant. |
| 160 | defer func() { |
| 161 | subDirs := map[string]bool{"bin": true, "gocache": true, "gopath": true} |
| 162 | subDirs["protobuf-"+protobufVersion] = true |
| 163 | for _, v := range golangVersions { |
| 164 | subDirs["go"+v] = true |
| 165 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 166 | |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 167 | now := time.Now() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 168 | fis, _ := ioutil.ReadDir(testDir) |
| 169 | for _, fi := range fis { |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 170 | if subDirs[fi.Name()] { |
| 171 | os.Chtimes(filepath.Join(testDir, fi.Name()), now, now) // best-effort |
| 172 | continue |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 173 | } |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 174 | if now.Sub(fi.ModTime()) < purgeTimeout { |
| 175 | continue |
| 176 | } |
| 177 | fmt.Printf("delete %v\n", fi.Name()) |
| 178 | os.RemoveAll(filepath.Join(testDir, fi.Name())) // best-effort |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 179 | } |
| 180 | }() |
| 181 | |
| 182 | // The bin directory contains symlinks to each tool by version. |
| 183 | // It is safe to delete this directory and run the test script from scratch. |
| 184 | binPath := filepath.Join(testDir, "bin") |
| 185 | check(os.RemoveAll(binPath)) |
| 186 | check(os.Mkdir(binPath, 0775)) |
| 187 | check(os.Setenv("PATH", binPath+":"+os.Getenv("PATH"))) |
| 188 | registerBinary := func(name, path string) { |
| 189 | check(os.Symlink(path, filepath.Join(binPath, name))) |
| 190 | } |
| 191 | |
| 192 | // Download and build the protobuf toolchain. |
| 193 | // We avoid downloading the pre-compiled binaries since they do not contain |
| 194 | // the conformance test runner. |
| 195 | workingDir = filepath.Join(testDir, "protobuf-"+protobufVersion) |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 196 | protobufPath = workingDir |
| 197 | if _, err := os.Stat(protobufPath); err != nil { |
| 198 | fmt.Printf("download %v\n", filepath.Base(protobufPath)) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 199 | url := fmt.Sprintf("https://github.com/google/protobuf/releases/download/v%v/protobuf-all-%v.tar.gz", protobufVersion, protobufVersion) |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 200 | downloadArchive(check, protobufPath, url, "protobuf-"+protobufVersion) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 201 | |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 202 | fmt.Printf("build %v\n", filepath.Base(protobufPath)) |
| 203 | mustRunCommand(t, protobufPath, "./autogen.sh") |
| 204 | mustRunCommand(t, protobufPath, "./configure") |
| 205 | mustRunCommand(t, protobufPath, "make") |
| 206 | mustRunCommand(t, filepath.Join(protobufPath, "conformance"), "make") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 207 | } |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 208 | // The benchmark directory isn't present in the release download, |
| 209 | // so fetch needed files directly. |
| 210 | for _, path := range benchmarkProtos { |
| 211 | src := fmt.Sprintf("https://raw.githubusercontent.com/protocolbuffers/protobuf/v%v/%v", protobufVersion, path) |
| 212 | dst := filepath.Join(protobufPath, path) |
| 213 | if _, err := os.Stat(dst); err != nil { |
| 214 | downloadFile(check, dst, src) |
| 215 | } |
| 216 | } |
| 217 | benchdataPath := filepath.Join(testDir, "benchdata") |
| 218 | for _, path := range []string{ |
| 219 | "benchmarks/datasets/google_message1/proto2/dataset.google_message1_proto2.pb", |
| 220 | "benchmarks/datasets/google_message1/proto3/dataset.google_message1_proto3.pb", |
| 221 | "benchmarks/datasets/google_message2/dataset.google_message2.pb", |
| 222 | } { |
| 223 | src := fmt.Sprintf("https://raw.githubusercontent.com/protocolbuffers/protobuf/v%v/%v", protobufVersion, path) |
| 224 | dst := filepath.Join(benchdataPath, filepath.Base(path)) |
| 225 | if _, err := os.Stat(dst); err != nil { |
| 226 | downloadFile(check, dst, src) |
| 227 | } |
| 228 | } |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 229 | patchProtos(check, protobufPath) |
| 230 | check(os.Setenv("PROTOBUF_ROOT", protobufPath)) // for generate-protos |
| 231 | registerBinary("conform-test-runner", filepath.Join(protobufPath, "conformance", "conformance-test-runner")) |
| 232 | registerBinary("protoc", filepath.Join(protobufPath, "src", "protoc")) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 233 | workingDir = "" |
| 234 | |
| 235 | // Download each Go toolchain version. |
| 236 | for _, v := range golangVersions { |
| 237 | workingDir = filepath.Join(testDir, "go"+v) |
| 238 | if _, err := os.Stat(workingDir); err != nil { |
| 239 | fmt.Printf("download %v\n", filepath.Base(workingDir)) |
| 240 | url := fmt.Sprintf("https://dl.google.com/go/go%v.%v-%v.tar.gz", v, runtime.GOOS, runtime.GOARCH) |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 241 | downloadArchive(check, workingDir, url, "go") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 242 | } |
| 243 | registerBinary("go"+v, filepath.Join(workingDir, "bin", "go")) |
| 244 | } |
| 245 | registerBinary("go", filepath.Join(testDir, "go"+golangLatest, "bin", "go")) |
| 246 | registerBinary("gofmt", filepath.Join(testDir, "go"+golangLatest, "bin", "gofmt")) |
| 247 | workingDir = "" |
| 248 | |
| 249 | // Travis-CI sets GOROOT, which confuses invocations of the Go toolchain. |
| 250 | // Explicitly clear GOROOT, so each toolchain uses their default GOROOT. |
| 251 | check(os.Unsetenv("GOROOT")) |
| 252 | |
| 253 | // Set a cache directory within the test directory. |
| 254 | check(os.Setenv("GOCACHE", filepath.Join(testDir, "gocache"))) |
| 255 | |
| 256 | // Setup GOPATH for pre-module support (i.e., go1.10 and earlier). |
| 257 | goPath = filepath.Join(testDir, "gopath") |
| 258 | modulePath = strings.TrimSpace(mustRunCommand(t, testDir, "go", "list", "-m", "-f", "{{.Path}}")) |
| 259 | check(os.RemoveAll(filepath.Join(goPath, "src"))) |
| 260 | check(os.MkdirAll(filepath.Join(goPath, "src", filepath.Dir(modulePath)), 0775)) |
| 261 | check(os.Symlink(repoRoot, filepath.Join(goPath, "src", modulePath))) |
| 262 | mustRunCommand(t, repoRoot, "go", "mod", "tidy") |
| 263 | mustRunCommand(t, repoRoot, "go", "mod", "vendor") |
| 264 | check(os.Setenv("GOPATH", goPath)) |
| 265 | } |
| 266 | |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 267 | func downloadFile(check func(error), dstPath, srcURL string) { |
| 268 | resp, err := http.Get(srcURL) |
| 269 | check(err) |
| 270 | defer resp.Body.Close() |
| 271 | |
| 272 | check(os.MkdirAll(filepath.Dir(dstPath), 0775)) |
| 273 | f, err := os.Create(dstPath) |
| 274 | check(err) |
| 275 | |
| 276 | _, err = io.Copy(f, resp.Body) |
| 277 | check(err) |
| 278 | } |
| 279 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 280 | func downloadArchive(check func(error), dstPath, srcURL, skipPrefix string) { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 281 | check(os.RemoveAll(dstPath)) |
| 282 | |
| 283 | resp, err := http.Get(srcURL) |
| 284 | check(err) |
| 285 | defer resp.Body.Close() |
| 286 | |
| 287 | zr, err := gzip.NewReader(resp.Body) |
| 288 | check(err) |
| 289 | |
| 290 | tr := tar.NewReader(zr) |
| 291 | for { |
| 292 | h, err := tr.Next() |
| 293 | if err == io.EOF { |
| 294 | return |
| 295 | } |
| 296 | check(err) |
| 297 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 298 | // Skip directories or files outside the prefix directory. |
| 299 | if len(skipPrefix) > 0 { |
| 300 | if !strings.HasPrefix(h.Name, skipPrefix) { |
| 301 | continue |
| 302 | } |
| 303 | if len(h.Name) > len(skipPrefix) && h.Name[len(skipPrefix)] != '/' { |
| 304 | continue |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | path := strings.TrimPrefix(strings.TrimPrefix(h.Name, skipPrefix), "/") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 309 | path = filepath.Join(dstPath, filepath.FromSlash(path)) |
| 310 | mode := os.FileMode(h.Mode & 0777) |
| 311 | switch h.Typeflag { |
| 312 | case tar.TypeReg: |
| 313 | b, err := ioutil.ReadAll(tr) |
| 314 | check(err) |
| 315 | check(ioutil.WriteFile(path, b, mode)) |
| 316 | case tar.TypeDir: |
| 317 | check(os.Mkdir(path, mode)) |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // patchProtos patches proto files with v2 locations of Go packages. |
| 323 | // TODO: Commit these changes upstream. |
| 324 | func patchProtos(check func(error), repoRoot string) { |
| 325 | javaPackageRx := regexp.MustCompile(`^option\s+java_package\s*=\s*".*"\s*;\s*$`) |
| 326 | goPackageRx := regexp.MustCompile(`^option\s+go_package\s*=\s*".*"\s*;\s*$`) |
| 327 | files := map[string]string{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 328 | "src/google/protobuf/any.proto": "google.golang.org/protobuf/types/known/anypb", |
| 329 | "src/google/protobuf/api.proto": "google.golang.org/protobuf/types/known/apipb", |
| 330 | "src/google/protobuf/duration.proto": "google.golang.org/protobuf/types/known/durationpb", |
| 331 | "src/google/protobuf/empty.proto": "google.golang.org/protobuf/types/known/emptypb", |
| 332 | "src/google/protobuf/field_mask.proto": "google.golang.org/protobuf/types/known/fieldmaskpb", |
| 333 | "src/google/protobuf/source_context.proto": "google.golang.org/protobuf/types/known/sourcecontextpb", |
| 334 | "src/google/protobuf/struct.proto": "google.golang.org/protobuf/types/known/structpb", |
| 335 | "src/google/protobuf/timestamp.proto": "google.golang.org/protobuf/types/known/timestamppb", |
| 336 | "src/google/protobuf/type.proto": "google.golang.org/protobuf/types/known/typepb", |
| 337 | "src/google/protobuf/wrappers.proto": "google.golang.org/protobuf/types/known/wrapperspb", |
| 338 | "src/google/protobuf/descriptor.proto": "google.golang.org/protobuf/types/descriptorpb", |
| 339 | "src/google/protobuf/compiler/plugin.proto": "google.golang.org/protobuf/types/pluginpb", |
| 340 | "conformance/conformance.proto": "google.golang.org/protobuf/internal/testprotos/conformance", |
| 341 | "src/google/protobuf/test_messages_proto2.proto": "google.golang.org/protobuf/internal/testprotos/conformance", |
| 342 | "src/google/protobuf/test_messages_proto3.proto": "google.golang.org/protobuf/internal/testprotos/conformance", |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 343 | } |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 344 | for _, p := range benchmarkProtos { |
| 345 | files[p] = path.Dir("google.golang.org/protobuf/internal/testprotos/" + p) |
| 346 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 347 | for pbpath, gopath := range files { |
| 348 | b, err := ioutil.ReadFile(filepath.Join(repoRoot, pbpath)) |
| 349 | check(err) |
| 350 | ss := strings.Split(string(b), "\n") |
| 351 | |
| 352 | // Locate java_package and (possible) go_package options. |
| 353 | javaPackageIdx, goPackageIdx := -1, -1 |
| 354 | for i, s := range ss { |
| 355 | if javaPackageIdx < 0 && javaPackageRx.MatchString(s) { |
| 356 | javaPackageIdx = i |
| 357 | } |
| 358 | if goPackageIdx < 0 && goPackageRx.MatchString(s) { |
| 359 | goPackageIdx = i |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Ensure the proto file has the correct go_package option. |
| 364 | opt := `option go_package = "` + gopath + `";` |
| 365 | if goPackageIdx >= 0 { |
| 366 | if ss[goPackageIdx] == opt { |
| 367 | continue // no changes needed |
| 368 | } |
| 369 | ss[goPackageIdx] = opt |
| 370 | } else { |
| 371 | // Insert go_package option before java_package option. |
| 372 | ss = append(ss[:javaPackageIdx], append([]string{opt}, ss[javaPackageIdx:]...)...) |
| 373 | } |
| 374 | |
| 375 | fmt.Println("patch " + pbpath) |
| 376 | b = []byte(strings.Join(ss, "\n")) |
| 377 | check(ioutil.WriteFile(filepath.Join(repoRoot, pbpath), b, 0664)) |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | func mustRunCommand(t *testing.T, dir string, args ...string) string { |
| 382 | t.Helper() |
| 383 | stdout := new(bytes.Buffer) |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 384 | stderr := new(bytes.Buffer) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 385 | cmd := exec.Command(args[0], args[1:]...) |
| 386 | cmd.Dir = dir |
| 387 | cmd.Env = append(os.Environ(), "PWD="+dir) |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 388 | cmd.Stdout = stdout |
| 389 | cmd.Stderr = stderr |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 390 | if err := cmd.Run(); err != nil { |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 391 | t.Fatalf("executing (%v): %v\n%s%s", strings.Join(args, " "), err, stdout.String(), stderr.String()) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 392 | } |
| 393 | return stdout.String() |
| 394 | } |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 395 | |
| 396 | var benchmarkProtos = []string{ |
| 397 | "benchmarks/benchmarks.proto", |
| 398 | "benchmarks/datasets/google_message1/proto2/benchmark_message1_proto2.proto", |
| 399 | "benchmarks/datasets/google_message1/proto3/benchmark_message1_proto3.proto", |
| 400 | "benchmarks/datasets/google_message2/benchmark_message2.proto", |
| 401 | "benchmarks/datasets/google_message3/benchmark_message3.proto", |
| 402 | "benchmarks/datasets/google_message3/benchmark_message3_1.proto", |
| 403 | "benchmarks/datasets/google_message3/benchmark_message3_2.proto", |
| 404 | "benchmarks/datasets/google_message3/benchmark_message3_3.proto", |
| 405 | "benchmarks/datasets/google_message3/benchmark_message3_4.proto", |
| 406 | "benchmarks/datasets/google_message3/benchmark_message3_5.proto", |
| 407 | "benchmarks/datasets/google_message3/benchmark_message3_6.proto", |
| 408 | "benchmarks/datasets/google_message3/benchmark_message3_7.proto", |
| 409 | "benchmarks/datasets/google_message3/benchmark_message3_8.proto", |
| 410 | "benchmarks/datasets/google_message4/benchmark_message4.proto", |
| 411 | "benchmarks/datasets/google_message4/benchmark_message4_1.proto", |
| 412 | "benchmarks/datasets/google_message4/benchmark_message4_2.proto", |
| 413 | "benchmarks/datasets/google_message4/benchmark_message4_3.proto", |
| 414 | } |