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" |
| 20 | "path/filepath" |
Damien Neil | 3a18560 | 2020-02-21 09:16:19 -0800 | [diff] [blame] | 21 | "regexp" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 22 | "runtime" |
| 23 | "strings" |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 24 | "sync" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 25 | "testing" |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 26 | "time" |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 27 | |
| 28 | "google.golang.org/protobuf/runtime/protoimpl" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | var ( |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 32 | regenerate = flag.Bool("regenerate", false, "regenerate files") |
| 33 | buildRelease = flag.Bool("buildRelease", false, "build release binaries") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 34 | |
Joe Tsai | abd06a8 | 2019-08-16 00:39:27 -0700 | [diff] [blame] | 35 | protobufVersion = "3.9.1" |
Herbie Ong | 3924625 | 2019-09-06 11:20:30 -0700 | [diff] [blame] | 36 | golangVersions = []string{"1.9.7", "1.10.8", "1.11.13", "1.12.9", "1.13"} |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 37 | golangLatest = golangVersions[len(golangVersions)-1] |
| 38 | |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 39 | // purgeTimeout determines the maximum age of unused sub-directories. |
| 40 | purgeTimeout = 30 * 24 * time.Hour // 1 month |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 41 | |
| 42 | // Variables initialized by mustInitDeps. |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 43 | goPath string |
| 44 | modulePath string |
| 45 | protobufPath string |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | func Test(t *testing.T) { |
| 49 | mustInitDeps(t) |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 50 | mustHandleFlags(t) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 51 | |
Damien Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame^] | 52 | // Report dirt in the working tree quickly, rather than after |
| 53 | // going through all the presubmits. |
| 54 | // |
| 55 | // Fail the test late, so we can test uncommitted changes with -failfast. |
| 56 | diff := mustRunCommand(t, "git", "diff", "--compact-summary", "HEAD") |
| 57 | if strings.TrimSpace(diff) != "" { |
| 58 | fmt.Printf("WARNING: working tree contains uncommitted changes:\n%v\n", diff) |
| 59 | } |
| 60 | untracked := mustRunCommand(t, "git", "ls-files", "--others", "--exclude-standard") |
| 61 | if strings.TrimSpace(untracked) != "" { |
| 62 | fmt.Printf("WARNING: working tree contains untracked files:\n%v", untracked) |
| 63 | } |
| 64 | |
| 65 | // Do the relatively fast checks up-front. |
| 66 | t.Run("GeneratedGoFiles", func(t *testing.T) { |
| 67 | diff := mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types") |
| 68 | if strings.TrimSpace(diff) != "" { |
| 69 | t.Fatalf("stale generated files:\n%v", diff) |
| 70 | } |
| 71 | diff = mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos") |
| 72 | if strings.TrimSpace(diff) != "" { |
| 73 | t.Fatalf("stale generated files:\n%v", diff) |
| 74 | } |
| 75 | }) |
| 76 | t.Run("FormattedGoFiles", func(t *testing.T) { |
| 77 | files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n") |
| 78 | diff := mustRunCommand(t, append([]string{"gofmt", "-d"}, files...)...) |
| 79 | if strings.TrimSpace(diff) != "" { |
| 80 | t.Fatalf("unformatted source files:\n%v", diff) |
| 81 | } |
| 82 | }) |
| 83 | t.Run("CopyrightHeaders", func(t *testing.T) { |
| 84 | files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go", "*.proto")), "\n") |
| 85 | mustHaveCopyrightHeader(t, files) |
| 86 | }) |
| 87 | |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 88 | var wg sync.WaitGroup |
| 89 | sema := make(chan bool, (runtime.NumCPU()+1)/2) |
| 90 | for i := range golangVersions { |
| 91 | goVersion := golangVersions[i] |
| 92 | goLabel := "Go" + goVersion |
| 93 | runGo := func(label, workDir string, args ...string) { |
| 94 | wg.Add(1) |
| 95 | sema <- true |
| 96 | go func() { |
| 97 | defer wg.Done() |
| 98 | defer func() { <-sema }() |
| 99 | t.Run(goLabel+"/"+label, func(t *testing.T) { |
| 100 | args[0] += goVersion |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 101 | command{Dir: workDir}.mustRun(t, args...) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 102 | }) |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 103 | }() |
| 104 | } |
| 105 | |
| 106 | workDir := filepath.Join(goPath, "src", modulePath) |
| 107 | runGo("Normal", workDir, "go", "test", "-race", "./...") |
| 108 | runGo("PureGo", workDir, "go", "test", "-race", "-tags", "purego", "./...") |
| 109 | runGo("Reflect", workDir, "go", "test", "-race", "-tags", "protoreflect", "./...") |
| 110 | if goVersion == golangLatest { |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 111 | runGo("ProtoLegacy", workDir, "go", "test", "-race", "-tags", "protolegacy", "./...") |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 112 | runGo("ProtocGenGo", "cmd/protoc-gen-go/testdata", "go", "test") |
Joe Tsai | dd271b6 | 2019-08-16 01:09:33 -0700 | [diff] [blame] | 113 | runGo("Conformance", "internal/conformance", "go", "test", "-execute") |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 114 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 115 | } |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 116 | wg.Wait() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 117 | |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 118 | t.Run("CommittedGitChanges", func(t *testing.T) { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 119 | if strings.TrimSpace(diff) != "" { |
Damien Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame^] | 120 | t.Fatalf("uncommitted changes") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 121 | } |
| 122 | }) |
| 123 | t.Run("TrackedGitFiles", func(t *testing.T) { |
Damien Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame^] | 124 | if strings.TrimSpace(untracked) != "" { |
| 125 | t.Fatalf("untracked files") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 126 | } |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | func mustInitDeps(t *testing.T) { |
| 131 | check := func(err error) { |
| 132 | t.Helper() |
| 133 | if err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Determine the directory to place the test directory. |
| 139 | repoRoot, err := os.Getwd() |
| 140 | check(err) |
| 141 | testDir := filepath.Join(repoRoot, ".cache") |
| 142 | check(os.MkdirAll(testDir, 0775)) |
| 143 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 144 | // Travis-CI has a hard-coded timeout where it kills the test after |
| 145 | // 10 minutes of a lack of activity on stdout. |
| 146 | // We work around this restriction by periodically printing the timestamp. |
| 147 | ticker := time.NewTicker(5 * time.Minute) |
| 148 | done := make(chan struct{}) |
| 149 | go func() { |
| 150 | now := time.Now() |
| 151 | for { |
| 152 | select { |
| 153 | case t := <-ticker.C: |
| 154 | fmt.Printf("\tt=%0.fmin\n", t.Sub(now).Minutes()) |
| 155 | case <-done: |
| 156 | return |
| 157 | } |
| 158 | } |
| 159 | }() |
| 160 | defer close(done) |
| 161 | defer ticker.Stop() |
| 162 | |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 163 | // Delete the current directory if non-empty, |
| 164 | // which only occurs if a dependency failed to initialize properly. |
| 165 | var workingDir string |
| 166 | defer func() { |
| 167 | if workingDir != "" { |
| 168 | os.RemoveAll(workingDir) // best-effort |
| 169 | } |
| 170 | }() |
| 171 | |
| 172 | // Delete other sub-directories that are no longer relevant. |
| 173 | defer func() { |
| 174 | subDirs := map[string]bool{"bin": true, "gocache": true, "gopath": true} |
| 175 | subDirs["protobuf-"+protobufVersion] = true |
| 176 | for _, v := range golangVersions { |
| 177 | subDirs["go"+v] = true |
| 178 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 179 | |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 180 | now := time.Now() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 181 | fis, _ := ioutil.ReadDir(testDir) |
| 182 | for _, fi := range fis { |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 183 | if subDirs[fi.Name()] { |
| 184 | os.Chtimes(filepath.Join(testDir, fi.Name()), now, now) // best-effort |
| 185 | continue |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 186 | } |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 187 | if now.Sub(fi.ModTime()) < purgeTimeout { |
| 188 | continue |
| 189 | } |
| 190 | fmt.Printf("delete %v\n", fi.Name()) |
| 191 | os.RemoveAll(filepath.Join(testDir, fi.Name())) // best-effort |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 192 | } |
| 193 | }() |
| 194 | |
| 195 | // The bin directory contains symlinks to each tool by version. |
| 196 | // It is safe to delete this directory and run the test script from scratch. |
| 197 | binPath := filepath.Join(testDir, "bin") |
| 198 | check(os.RemoveAll(binPath)) |
| 199 | check(os.Mkdir(binPath, 0775)) |
| 200 | check(os.Setenv("PATH", binPath+":"+os.Getenv("PATH"))) |
| 201 | registerBinary := func(name, path string) { |
| 202 | check(os.Symlink(path, filepath.Join(binPath, name))) |
| 203 | } |
| 204 | |
| 205 | // Download and build the protobuf toolchain. |
| 206 | // We avoid downloading the pre-compiled binaries since they do not contain |
| 207 | // the conformance test runner. |
| 208 | workingDir = filepath.Join(testDir, "protobuf-"+protobufVersion) |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 209 | protobufPath = workingDir |
| 210 | if _, err := os.Stat(protobufPath); err != nil { |
| 211 | fmt.Printf("download %v\n", filepath.Base(protobufPath)) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 212 | 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] | 213 | downloadArchive(check, protobufPath, url, "protobuf-"+protobufVersion) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 214 | |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 215 | fmt.Printf("build %v\n", filepath.Base(protobufPath)) |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 216 | command{Dir: protobufPath}.mustRun(t, "./autogen.sh") |
| 217 | command{Dir: protobufPath}.mustRun(t, "./configure") |
| 218 | command{Dir: protobufPath}.mustRun(t, "make") |
| 219 | command{Dir: filepath.Join(protobufPath, "conformance")}.mustRun(t, "make") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 220 | } |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 221 | // The benchmark directory isn't present in the release download, |
| 222 | // so fetch needed files directly. |
| 223 | for _, path := range benchmarkProtos { |
| 224 | src := fmt.Sprintf("https://raw.githubusercontent.com/protocolbuffers/protobuf/v%v/%v", protobufVersion, path) |
| 225 | dst := filepath.Join(protobufPath, path) |
| 226 | if _, err := os.Stat(dst); err != nil { |
| 227 | downloadFile(check, dst, src) |
| 228 | } |
| 229 | } |
| 230 | benchdataPath := filepath.Join(testDir, "benchdata") |
| 231 | for _, path := range []string{ |
| 232 | "benchmarks/datasets/google_message1/proto2/dataset.google_message1_proto2.pb", |
| 233 | "benchmarks/datasets/google_message1/proto3/dataset.google_message1_proto3.pb", |
| 234 | "benchmarks/datasets/google_message2/dataset.google_message2.pb", |
| 235 | } { |
| 236 | src := fmt.Sprintf("https://raw.githubusercontent.com/protocolbuffers/protobuf/v%v/%v", protobufVersion, path) |
| 237 | dst := filepath.Join(benchdataPath, filepath.Base(path)) |
| 238 | if _, err := os.Stat(dst); err != nil { |
| 239 | downloadFile(check, dst, src) |
| 240 | } |
| 241 | } |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 242 | check(os.Setenv("PROTOBUF_ROOT", protobufPath)) // for generate-protos |
| 243 | registerBinary("conform-test-runner", filepath.Join(protobufPath, "conformance", "conformance-test-runner")) |
| 244 | registerBinary("protoc", filepath.Join(protobufPath, "src", "protoc")) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 245 | workingDir = "" |
| 246 | |
| 247 | // Download each Go toolchain version. |
| 248 | for _, v := range golangVersions { |
| 249 | workingDir = filepath.Join(testDir, "go"+v) |
| 250 | if _, err := os.Stat(workingDir); err != nil { |
| 251 | fmt.Printf("download %v\n", filepath.Base(workingDir)) |
| 252 | 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] | 253 | downloadArchive(check, workingDir, url, "go") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 254 | } |
| 255 | registerBinary("go"+v, filepath.Join(workingDir, "bin", "go")) |
| 256 | } |
| 257 | registerBinary("go", filepath.Join(testDir, "go"+golangLatest, "bin", "go")) |
| 258 | registerBinary("gofmt", filepath.Join(testDir, "go"+golangLatest, "bin", "gofmt")) |
| 259 | workingDir = "" |
| 260 | |
| 261 | // Travis-CI sets GOROOT, which confuses invocations of the Go toolchain. |
| 262 | // Explicitly clear GOROOT, so each toolchain uses their default GOROOT. |
| 263 | check(os.Unsetenv("GOROOT")) |
| 264 | |
Joe Tsai | 6a2180f | 2019-07-11 16:34:17 -0700 | [diff] [blame] | 265 | // Set a cache directory outside the test directory. |
| 266 | check(os.Setenv("GOCACHE", filepath.Join(repoRoot, ".gocache"))) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 267 | |
| 268 | // Setup GOPATH for pre-module support (i.e., go1.10 and earlier). |
| 269 | goPath = filepath.Join(testDir, "gopath") |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 270 | modulePath = strings.TrimSpace(command{Dir: testDir}.mustRun(t, "go", "list", "-m", "-f", "{{.Path}}")) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 271 | check(os.RemoveAll(filepath.Join(goPath, "src"))) |
| 272 | check(os.MkdirAll(filepath.Join(goPath, "src", filepath.Dir(modulePath)), 0775)) |
| 273 | check(os.Symlink(repoRoot, filepath.Join(goPath, "src", modulePath))) |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 274 | command{Dir: repoRoot}.mustRun(t, "go", "mod", "tidy") |
| 275 | command{Dir: repoRoot}.mustRun(t, "go", "mod", "vendor") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 276 | check(os.Setenv("GOPATH", goPath)) |
| 277 | } |
| 278 | |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 279 | func downloadFile(check func(error), dstPath, srcURL string) { |
| 280 | resp, err := http.Get(srcURL) |
| 281 | check(err) |
| 282 | defer resp.Body.Close() |
| 283 | |
| 284 | check(os.MkdirAll(filepath.Dir(dstPath), 0775)) |
| 285 | f, err := os.Create(dstPath) |
| 286 | check(err) |
| 287 | |
| 288 | _, err = io.Copy(f, resp.Body) |
| 289 | check(err) |
| 290 | } |
| 291 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 292 | func downloadArchive(check func(error), dstPath, srcURL, skipPrefix string) { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 293 | check(os.RemoveAll(dstPath)) |
| 294 | |
| 295 | resp, err := http.Get(srcURL) |
| 296 | check(err) |
| 297 | defer resp.Body.Close() |
| 298 | |
| 299 | zr, err := gzip.NewReader(resp.Body) |
| 300 | check(err) |
| 301 | |
| 302 | tr := tar.NewReader(zr) |
| 303 | for { |
| 304 | h, err := tr.Next() |
| 305 | if err == io.EOF { |
| 306 | return |
| 307 | } |
| 308 | check(err) |
| 309 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 310 | // Skip directories or files outside the prefix directory. |
| 311 | if len(skipPrefix) > 0 { |
| 312 | if !strings.HasPrefix(h.Name, skipPrefix) { |
| 313 | continue |
| 314 | } |
| 315 | if len(h.Name) > len(skipPrefix) && h.Name[len(skipPrefix)] != '/' { |
| 316 | continue |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | path := strings.TrimPrefix(strings.TrimPrefix(h.Name, skipPrefix), "/") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 321 | path = filepath.Join(dstPath, filepath.FromSlash(path)) |
| 322 | mode := os.FileMode(h.Mode & 0777) |
| 323 | switch h.Typeflag { |
| 324 | case tar.TypeReg: |
| 325 | b, err := ioutil.ReadAll(tr) |
| 326 | check(err) |
| 327 | check(ioutil.WriteFile(path, b, mode)) |
| 328 | case tar.TypeDir: |
| 329 | check(os.Mkdir(path, mode)) |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 334 | func mustHandleFlags(t *testing.T) { |
| 335 | if *regenerate { |
| 336 | t.Run("Generate", func(t *testing.T) { |
| 337 | fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types", "-execute")) |
| 338 | fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos", "-execute")) |
| 339 | files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n") |
| 340 | mustRunCommand(t, append([]string{"gofmt", "-w"}, files...)...) |
| 341 | }) |
| 342 | } |
| 343 | if *buildRelease { |
| 344 | t.Run("BuildRelease", func(t *testing.T) { |
| 345 | v := protoimpl.VersionString() |
| 346 | for _, goos := range []string{"linux", "darwin", "windows"} { |
| 347 | for _, goarch := range []string{"386", "amd64"} { |
| 348 | binPath := filepath.Join("bin", fmt.Sprintf("protoc-gen-go.%v.%v.%v", v, goos, goarch)) |
| 349 | |
| 350 | // Build the binary. |
| 351 | cmd := command{Env: append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch)} |
Joe Tsai | 576cfb3 | 2019-09-04 22:50:42 -0700 | [diff] [blame] | 352 | cmd.mustRun(t, "go", "build", "-trimpath", "-ldflags", "-s -w -buildid=", "-o", binPath, "./cmd/protoc-gen-go") |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 353 | |
| 354 | // Archive and compress the binary. |
| 355 | in, err := ioutil.ReadFile(binPath) |
| 356 | if err != nil { |
| 357 | t.Fatal(err) |
| 358 | } |
| 359 | out := new(bytes.Buffer) |
| 360 | gz, _ := gzip.NewWriterLevel(out, gzip.BestCompression) |
| 361 | gz.Comment = fmt.Sprintf("protoc-gen-go VERSION=%v GOOS=%v GOARCH=%v", v, goos, goarch) |
| 362 | tw := tar.NewWriter(gz) |
| 363 | tw.WriteHeader(&tar.Header{ |
| 364 | Name: "protoc-gen-go", |
| 365 | Mode: int64(0775), |
| 366 | Size: int64(len(in)), |
| 367 | }) |
| 368 | tw.Write(in) |
| 369 | tw.Close() |
| 370 | gz.Close() |
| 371 | if err := ioutil.WriteFile(binPath+".tar.gz", out.Bytes(), 0664); err != nil { |
| 372 | t.Fatal(err) |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | }) |
| 377 | } |
| 378 | if *regenerate || *buildRelease { |
| 379 | t.SkipNow() |
| 380 | } |
| 381 | } |
| 382 | |
Damien Neil | 3a18560 | 2020-02-21 09:16:19 -0800 | [diff] [blame] | 383 | var copyrightRegex = []*regexp.Regexp{ |
| 384 | regexp.MustCompile(`^// Copyright \d\d\d\d The Go Authors\. All rights reserved. |
| 385 | // Use of this source code is governed by a BSD-style |
| 386 | // license that can be found in the LICENSE file\. |
| 387 | `), |
| 388 | // Generated .pb.go files from main protobuf repo. |
| 389 | regexp.MustCompile(`^// Protocol Buffers - Google's data interchange format |
| 390 | // Copyright \d\d\d\d Google Inc\. All rights reserved\. |
| 391 | `), |
| 392 | } |
| 393 | |
| 394 | var noCopyrightHeader = []string{ |
| 395 | // Missing copyright header upstream. |
| 396 | "internal/testprotos/benchmarks/datasets/", |
| 397 | } |
| 398 | |
| 399 | func mustHaveCopyrightHeader(t *testing.T, files []string) { |
| 400 | var bad []string |
| 401 | File: |
| 402 | for _, file := range files { |
| 403 | for _, prefix := range noCopyrightHeader { |
| 404 | if strings.HasPrefix(file, prefix) { |
| 405 | continue File |
| 406 | } |
| 407 | } |
| 408 | b, err := ioutil.ReadFile(file) |
| 409 | if err != nil { |
| 410 | t.Fatal(err) |
| 411 | } |
| 412 | for _, re := range copyrightRegex { |
| 413 | if loc := re.FindIndex(b); loc != nil && loc[0] == 0 { |
| 414 | continue File |
| 415 | } |
| 416 | } |
| 417 | bad = append(bad, file) |
| 418 | } |
| 419 | if len(bad) > 0 { |
| 420 | t.Fatalf("files with missing/bad copyright headers:\n %v", strings.Join(bad, "\n ")) |
| 421 | } |
| 422 | } |
| 423 | |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 424 | type command struct { |
| 425 | Dir string |
| 426 | Env []string |
| 427 | } |
| 428 | |
| 429 | func (c command) mustRun(t *testing.T, args ...string) string { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 430 | t.Helper() |
| 431 | stdout := new(bytes.Buffer) |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 432 | stderr := new(bytes.Buffer) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 433 | cmd := exec.Command(args[0], args[1:]...) |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 434 | cmd.Dir = "." |
| 435 | if c.Dir != "" { |
| 436 | cmd.Dir = c.Dir |
| 437 | } |
| 438 | cmd.Env = os.Environ() |
| 439 | if c.Env != nil { |
| 440 | cmd.Env = c.Env |
| 441 | } |
| 442 | cmd.Env = append(cmd.Env, "PWD="+cmd.Dir) |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 443 | cmd.Stdout = stdout |
| 444 | cmd.Stderr = stderr |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 445 | if err := cmd.Run(); err != nil { |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 446 | 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] | 447 | } |
| 448 | return stdout.String() |
| 449 | } |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 450 | |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 451 | func mustRunCommand(t *testing.T, args ...string) string { |
| 452 | t.Helper() |
| 453 | return command{}.mustRun(t, args...) |
| 454 | } |
| 455 | |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 456 | var benchmarkProtos = []string{ |
| 457 | "benchmarks/benchmarks.proto", |
| 458 | "benchmarks/datasets/google_message1/proto2/benchmark_message1_proto2.proto", |
| 459 | "benchmarks/datasets/google_message1/proto3/benchmark_message1_proto3.proto", |
| 460 | "benchmarks/datasets/google_message2/benchmark_message2.proto", |
| 461 | "benchmarks/datasets/google_message3/benchmark_message3.proto", |
| 462 | "benchmarks/datasets/google_message3/benchmark_message3_1.proto", |
| 463 | "benchmarks/datasets/google_message3/benchmark_message3_2.proto", |
| 464 | "benchmarks/datasets/google_message3/benchmark_message3_3.proto", |
| 465 | "benchmarks/datasets/google_message3/benchmark_message3_4.proto", |
| 466 | "benchmarks/datasets/google_message3/benchmark_message3_5.proto", |
| 467 | "benchmarks/datasets/google_message3/benchmark_message3_6.proto", |
| 468 | "benchmarks/datasets/google_message3/benchmark_message3_7.proto", |
| 469 | "benchmarks/datasets/google_message3/benchmark_message3_8.proto", |
| 470 | "benchmarks/datasets/google_message4/benchmark_message4.proto", |
| 471 | "benchmarks/datasets/google_message4/benchmark_message4_1.proto", |
| 472 | "benchmarks/datasets/google_message4/benchmark_message4_2.proto", |
| 473 | "benchmarks/datasets/google_message4/benchmark_message4_3.proto", |
| 474 | } |