blob: 4587ffb4be743e6f92386df4eef62567cc3e2756 [file] [log] [blame]
Joe Tsai707894e2019-03-01 12:50:52 -08001// 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 Tsaif3987842019-03-02 13:35:17 -08005// +build ignore
Joe Tsai707894e2019-03-01 12:50:52 -08006
Joe Tsaif3987842019-03-02 13:35:17 -08007package main
Joe Tsai707894e2019-03-01 12:50:52 -08008
9import (
10 "archive/tar"
11 "bytes"
12 "compress/gzip"
Joe Tsai8de66752019-12-19 16:38:52 -080013 "crypto/sha256"
Joe Tsai707894e2019-03-01 12:50:52 -080014 "flag"
15 "fmt"
16 "io"
17 "io/ioutil"
18 "net/http"
19 "os"
20 "os/exec"
21 "path/filepath"
Damien Neil3a185602020-02-21 09:16:19 -080022 "regexp"
Joe Tsai707894e2019-03-01 12:50:52 -080023 "runtime"
24 "strings"
Joe Tsai62200db2019-07-11 17:34:10 -070025 "sync"
Joe Tsai707894e2019-03-01 12:50:52 -080026 "testing"
Joe Tsaif3987842019-03-02 13:35:17 -080027 "time"
Joe Tsai4f3de442019-08-07 19:33:51 -070028
Joe Tsai4ab2bc92020-03-10 17:38:07 -070029 "google.golang.org/protobuf/internal/version"
Joe Tsai707894e2019-03-01 12:50:52 -080030)
31
32var (
Joe Tsai4f3de442019-08-07 19:33:51 -070033 regenerate = flag.Bool("regenerate", false, "regenerate files")
34 buildRelease = flag.Bool("buildRelease", false, "build release binaries")
Joe Tsai707894e2019-03-01 12:50:52 -080035
Joe Tsai85f47622020-05-26 16:26:02 -070036 protobufVersion = "3.15.3"
37 protobufSHA256 = "" // ignored if protobufVersion is a git hash
Joe Tsai8de66752019-12-19 16:38:52 -080038
Joe Tsai731520d2021-02-25 17:12:53 -080039 golangVersions = []string{"1.9.7", "1.10.8", "1.11.13", "1.12.17", "1.13.15", "1.14.15", "1.15.9", "1.16.1"}
Joe Tsai8de66752019-12-19 16:38:52 -080040 golangLatest = golangVersions[len(golangVersions)-1]
41
42 staticcheckVersion = "2020.1.4"
43 staticcheckSHA256s = map[string]string{
Joe Tsai8de66752019-12-19 16:38:52 -080044 "darwin/amd64": "5706d101426c025e8f165309e0cb2932e54809eb035ff23ebe19df0f810699d8",
45 "linux/386": "e4dbf94e940678ae7108f0d22c7c2992339bc10a8fb384e7e734b1531a429a1c",
46 "linux/amd64": "09d2c2002236296de2c757df111fe3ae858b89f9e183f645ad01f8135c83c519",
47 }
Joe Tsai707894e2019-03-01 12:50:52 -080048
Joe Tsai9e88bc02019-03-12 01:30:40 -070049 // purgeTimeout determines the maximum age of unused sub-directories.
50 purgeTimeout = 30 * 24 * time.Hour // 1 month
Joe Tsai707894e2019-03-01 12:50:52 -080051
52 // Variables initialized by mustInitDeps.
Herbie Ongd64dceb2019-04-25 01:19:57 -070053 goPath string
54 modulePath string
55 protobufPath string
Joe Tsai707894e2019-03-01 12:50:52 -080056)
57
58func Test(t *testing.T) {
59 mustInitDeps(t)
Joe Tsai4f3de442019-08-07 19:33:51 -070060 mustHandleFlags(t)
Joe Tsai707894e2019-03-01 12:50:52 -080061
Damien Neil4d8936d2020-02-21 10:32:56 -080062 // Report dirt in the working tree quickly, rather than after
63 // going through all the presubmits.
64 //
65 // Fail the test late, so we can test uncommitted changes with -failfast.
Joe Tsaif75a3382019-12-19 17:42:41 -080066 gitDiff := mustRunCommand(t, "git", "diff", "HEAD")
67 if strings.TrimSpace(gitDiff) != "" {
68 fmt.Printf("WARNING: working tree contains uncommitted changes:\n%v\n", gitDiff)
Damien Neil4d8936d2020-02-21 10:32:56 -080069 }
Joe Tsaif75a3382019-12-19 17:42:41 -080070 gitUntracked := mustRunCommand(t, "git", "ls-files", "--others", "--exclude-standard")
71 if strings.TrimSpace(gitUntracked) != "" {
72 fmt.Printf("WARNING: working tree contains untracked files:\n%v\n", gitUntracked)
Damien Neil4d8936d2020-02-21 10:32:56 -080073 }
74
75 // Do the relatively fast checks up-front.
76 t.Run("GeneratedGoFiles", func(t *testing.T) {
77 diff := mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types")
78 if strings.TrimSpace(diff) != "" {
79 t.Fatalf("stale generated files:\n%v", diff)
80 }
81 diff = mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos")
82 if strings.TrimSpace(diff) != "" {
83 t.Fatalf("stale generated files:\n%v", diff)
84 }
85 })
86 t.Run("FormattedGoFiles", func(t *testing.T) {
87 files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n")
88 diff := mustRunCommand(t, append([]string{"gofmt", "-d"}, files...)...)
89 if strings.TrimSpace(diff) != "" {
90 t.Fatalf("unformatted source files:\n%v", diff)
91 }
92 })
93 t.Run("CopyrightHeaders", func(t *testing.T) {
94 files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go", "*.proto")), "\n")
95 mustHaveCopyrightHeader(t, files)
96 })
97
Joe Tsai62200db2019-07-11 17:34:10 -070098 var wg sync.WaitGroup
99 sema := make(chan bool, (runtime.NumCPU()+1)/2)
100 for i := range golangVersions {
101 goVersion := golangVersions[i]
102 goLabel := "Go" + goVersion
Joe Tsai467a9cd2020-07-08 10:26:03 -0700103 runGo := func(label string, cmd command, args ...string) {
Joe Tsai62200db2019-07-11 17:34:10 -0700104 wg.Add(1)
105 sema <- true
106 go func() {
107 defer wg.Done()
108 defer func() { <-sema }()
109 t.Run(goLabel+"/"+label, func(t *testing.T) {
110 args[0] += goVersion
Joe Tsai467a9cd2020-07-08 10:26:03 -0700111 cmd.mustRun(t, args...)
Joe Tsai707894e2019-03-01 12:50:52 -0800112 })
Joe Tsai62200db2019-07-11 17:34:10 -0700113 }()
114 }
115
116 workDir := filepath.Join(goPath, "src", modulePath)
Joe Tsai467a9cd2020-07-08 10:26:03 -0700117 runGo("Normal", command{Dir: workDir}, "go", "test", "-race", "./...")
118 runGo("PureGo", command{Dir: workDir}, "go", "test", "-race", "-tags", "purego", "./...")
119 runGo("Reflect", command{Dir: workDir}, "go", "test", "-race", "-tags", "protoreflect", "./...")
Joe Tsai62200db2019-07-11 17:34:10 -0700120 if goVersion == golangLatest {
Joe Tsai467a9cd2020-07-08 10:26:03 -0700121 runGo("ProtoLegacy", command{Dir: workDir}, "go", "test", "-race", "-tags", "protolegacy", "./...")
122 runGo("ProtocGenGo", command{Dir: "cmd/protoc-gen-go/testdata"}, "go", "test")
123 runGo("Conformance", command{Dir: "internal/conformance"}, "go", "test", "-execute")
124
125 // Only run the 32-bit compatability tests for Linux;
126 // avoid Darwin since 10.15 dropped support i386 code execution.
127 if runtime.GOOS == "linux" {
128 runGo("Arch32Bit", command{Dir: workDir, Env: append(os.Environ(), "GOARCH=386")}, "go", "test", "./...")
129 }
Joe Tsai62200db2019-07-11 17:34:10 -0700130 }
Joe Tsai707894e2019-03-01 12:50:52 -0800131 }
Joe Tsai62200db2019-07-11 17:34:10 -0700132 wg.Wait()
Joe Tsai707894e2019-03-01 12:50:52 -0800133
Joe Tsai8de66752019-12-19 16:38:52 -0800134 t.Run("GoStaticCheck", func(t *testing.T) {
135 checks := []string{
136 "all", // start with all checks enabled
137 "-SA1019", // disable deprecated usage check
138 "-S*", // disable code simplication checks
139 "-ST*", // disable coding style checks
140 "-U*", // disable unused declaration checks
141 }
142 out := mustRunCommand(t, "staticcheck", "-checks="+strings.Join(checks, ","), "-fail=none", "./...")
143
144 // Filter out findings from certain paths.
145 var findings []string
146 for _, finding := range strings.Split(strings.TrimSpace(out), "\n") {
147 switch {
148 case strings.HasPrefix(finding, "internal/testprotos/legacy/"):
149 default:
150 findings = append(findings, finding)
151 }
152 }
153 if len(findings) > 0 {
154 t.Fatalf("staticcheck findings:\n%v", strings.Join(findings, "\n"))
155 }
156 })
Joe Tsai707894e2019-03-01 12:50:52 -0800157 t.Run("CommittedGitChanges", func(t *testing.T) {
Joe Tsaif75a3382019-12-19 17:42:41 -0800158 if strings.TrimSpace(gitDiff) != "" {
Damien Neil4d8936d2020-02-21 10:32:56 -0800159 t.Fatalf("uncommitted changes")
Joe Tsai707894e2019-03-01 12:50:52 -0800160 }
161 })
162 t.Run("TrackedGitFiles", func(t *testing.T) {
Joe Tsaif75a3382019-12-19 17:42:41 -0800163 if strings.TrimSpace(gitUntracked) != "" {
Damien Neil4d8936d2020-02-21 10:32:56 -0800164 t.Fatalf("untracked files")
Joe Tsai707894e2019-03-01 12:50:52 -0800165 }
166 })
167}
168
169func mustInitDeps(t *testing.T) {
170 check := func(err error) {
171 t.Helper()
172 if err != nil {
173 t.Fatal(err)
174 }
175 }
176
177 // Determine the directory to place the test directory.
178 repoRoot, err := os.Getwd()
179 check(err)
180 testDir := filepath.Join(repoRoot, ".cache")
181 check(os.MkdirAll(testDir, 0775))
182
183 // Delete the current directory if non-empty,
184 // which only occurs if a dependency failed to initialize properly.
185 var workingDir string
Joe Tsaidb5c9002020-09-05 11:24:42 -0700186 finishedDirs := map[string]bool{}
Joe Tsai707894e2019-03-01 12:50:52 -0800187 defer func() {
188 if workingDir != "" {
189 os.RemoveAll(workingDir) // best-effort
190 }
191 }()
Joe Tsaidb5c9002020-09-05 11:24:42 -0700192 startWork := func(name string) string {
193 workingDir = filepath.Join(testDir, name)
194 return workingDir
195 }
196 finishWork := func() {
197 finishedDirs[workingDir] = true
198 workingDir = ""
199 }
Joe Tsai707894e2019-03-01 12:50:52 -0800200
201 // Delete other sub-directories that are no longer relevant.
202 defer func() {
Joe Tsai9e88bc02019-03-12 01:30:40 -0700203 now := time.Now()
Joe Tsai707894e2019-03-01 12:50:52 -0800204 fis, _ := ioutil.ReadDir(testDir)
205 for _, fi := range fis {
Joe Tsaidb5c9002020-09-05 11:24:42 -0700206 dir := filepath.Join(testDir, fi.Name())
207 if finishedDirs[dir] {
208 os.Chtimes(dir, now, now) // best-effort
Joe Tsai9e88bc02019-03-12 01:30:40 -0700209 continue
Joe Tsai707894e2019-03-01 12:50:52 -0800210 }
Joe Tsai9e88bc02019-03-12 01:30:40 -0700211 if now.Sub(fi.ModTime()) < purgeTimeout {
212 continue
213 }
214 fmt.Printf("delete %v\n", fi.Name())
Joe Tsaidb5c9002020-09-05 11:24:42 -0700215 os.RemoveAll(dir) // best-effort
Joe Tsai707894e2019-03-01 12:50:52 -0800216 }
217 }()
218
219 // The bin directory contains symlinks to each tool by version.
220 // It is safe to delete this directory and run the test script from scratch.
Joe Tsaidb5c9002020-09-05 11:24:42 -0700221 binPath := startWork("bin")
Joe Tsai707894e2019-03-01 12:50:52 -0800222 check(os.RemoveAll(binPath))
223 check(os.Mkdir(binPath, 0775))
224 check(os.Setenv("PATH", binPath+":"+os.Getenv("PATH")))
225 registerBinary := func(name, path string) {
226 check(os.Symlink(path, filepath.Join(binPath, name)))
227 }
Joe Tsaidb5c9002020-09-05 11:24:42 -0700228 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800229
230 // Download and build the protobuf toolchain.
231 // We avoid downloading the pre-compiled binaries since they do not contain
232 // the conformance test runner.
Joe Tsaidb5c9002020-09-05 11:24:42 -0700233 protobufPath = startWork("protobuf-" + protobufVersion)
Herbie Ongd64dceb2019-04-25 01:19:57 -0700234 if _, err := os.Stat(protobufPath); err != nil {
235 fmt.Printf("download %v\n", filepath.Base(protobufPath))
Joe Tsai387873d2020-04-28 14:44:38 -0700236 if isCommit := strings.Trim(protobufVersion, "0123456789abcdef") == ""; isCommit {
237 command{Dir: testDir}.mustRun(t, "git", "clone", "https://github.com/protocolbuffers/protobuf", "protobuf-"+protobufVersion)
238 command{Dir: protobufPath}.mustRun(t, "git", "checkout", protobufVersion)
239 } else {
240 url := fmt.Sprintf("https://github.com/google/protobuf/releases/download/v%v/protobuf-all-%v.tar.gz", protobufVersion, protobufVersion)
Joe Tsai8de66752019-12-19 16:38:52 -0800241 downloadArchive(check, protobufPath, url, "protobuf-"+protobufVersion, protobufSHA256)
Joe Tsai387873d2020-04-28 14:44:38 -0700242 }
Joe Tsai707894e2019-03-01 12:50:52 -0800243
Herbie Ongd64dceb2019-04-25 01:19:57 -0700244 fmt.Printf("build %v\n", filepath.Base(protobufPath))
Joe Tsai7164af52019-08-07 19:27:43 -0700245 command{Dir: protobufPath}.mustRun(t, "./autogen.sh")
246 command{Dir: protobufPath}.mustRun(t, "./configure")
247 command{Dir: protobufPath}.mustRun(t, "make")
248 command{Dir: filepath.Join(protobufPath, "conformance")}.mustRun(t, "make")
Joe Tsai707894e2019-03-01 12:50:52 -0800249 }
Herbie Ongd64dceb2019-04-25 01:19:57 -0700250 check(os.Setenv("PROTOBUF_ROOT", protobufPath)) // for generate-protos
251 registerBinary("conform-test-runner", filepath.Join(protobufPath, "conformance", "conformance-test-runner"))
252 registerBinary("protoc", filepath.Join(protobufPath, "src", "protoc"))
Joe Tsaidb5c9002020-09-05 11:24:42 -0700253 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800254
255 // Download each Go toolchain version.
256 for _, v := range golangVersions {
Joe Tsaidb5c9002020-09-05 11:24:42 -0700257 goDir := startWork("go" + v)
258 if _, err := os.Stat(goDir); err != nil {
259 fmt.Printf("download %v\n", filepath.Base(goDir))
Joe Tsai707894e2019-03-01 12:50:52 -0800260 url := fmt.Sprintf("https://dl.google.com/go/go%v.%v-%v.tar.gz", v, runtime.GOOS, runtime.GOARCH)
Joe Tsaidb5c9002020-09-05 11:24:42 -0700261 downloadArchive(check, goDir, url, "go", "") // skip SHA256 check as we fetch over https from a trusted domain
Joe Tsai707894e2019-03-01 12:50:52 -0800262 }
Joe Tsaidb5c9002020-09-05 11:24:42 -0700263 registerBinary("go"+v, filepath.Join(goDir, "bin", "go"))
264 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800265 }
266 registerBinary("go", filepath.Join(testDir, "go"+golangLatest, "bin", "go"))
267 registerBinary("gofmt", filepath.Join(testDir, "go"+golangLatest, "bin", "gofmt"))
Joe Tsai707894e2019-03-01 12:50:52 -0800268
Joe Tsai8de66752019-12-19 16:38:52 -0800269 // Download the staticcheck tool.
Joe Tsaidb5c9002020-09-05 11:24:42 -0700270 checkDir := startWork("staticcheck-" + staticcheckVersion)
271 if _, err := os.Stat(checkDir); err != nil {
272 fmt.Printf("download %v\n", filepath.Base(checkDir))
Joe Tsai8de66752019-12-19 16:38:52 -0800273 url := fmt.Sprintf("https://github.com/dominikh/go-tools/releases/download/%v/staticcheck_%v_%v.tar.gz", staticcheckVersion, runtime.GOOS, runtime.GOARCH)
Joe Tsaidb5c9002020-09-05 11:24:42 -0700274 downloadArchive(check, checkDir, url, "staticcheck", staticcheckSHA256s[runtime.GOOS+"/"+runtime.GOARCH])
Joe Tsai8de66752019-12-19 16:38:52 -0800275 }
Joe Tsaidb5c9002020-09-05 11:24:42 -0700276 registerBinary("staticcheck", filepath.Join(checkDir, "staticcheck"))
277 finishWork()
Joe Tsai8de66752019-12-19 16:38:52 -0800278
Joe Tsaie7ab1122021-03-02 17:09:55 -0800279 // GitHub actions sets GOROOT, which confuses invocations of the Go toolchain.
Joe Tsai707894e2019-03-01 12:50:52 -0800280 // Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
281 check(os.Unsetenv("GOROOT"))
282
Joe Tsai6a2180f2019-07-11 16:34:17 -0700283 // Set a cache directory outside the test directory.
284 check(os.Setenv("GOCACHE", filepath.Join(repoRoot, ".gocache")))
Joe Tsai707894e2019-03-01 12:50:52 -0800285
286 // Setup GOPATH for pre-module support (i.e., go1.10 and earlier).
Joe Tsaidb5c9002020-09-05 11:24:42 -0700287 goPath = startWork("gopath")
Joe Tsai7164af52019-08-07 19:27:43 -0700288 modulePath = strings.TrimSpace(command{Dir: testDir}.mustRun(t, "go", "list", "-m", "-f", "{{.Path}}"))
Joe Tsai707894e2019-03-01 12:50:52 -0800289 check(os.RemoveAll(filepath.Join(goPath, "src")))
290 check(os.MkdirAll(filepath.Join(goPath, "src", filepath.Dir(modulePath)), 0775))
291 check(os.Symlink(repoRoot, filepath.Join(goPath, "src", modulePath)))
Joe Tsai7164af52019-08-07 19:27:43 -0700292 command{Dir: repoRoot}.mustRun(t, "go", "mod", "tidy")
293 command{Dir: repoRoot}.mustRun(t, "go", "mod", "vendor")
Joe Tsai707894e2019-03-01 12:50:52 -0800294 check(os.Setenv("GOPATH", goPath))
Joe Tsaidb5c9002020-09-05 11:24:42 -0700295 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800296}
297
Damien Neila80229e2019-06-20 12:53:48 -0700298func downloadFile(check func(error), dstPath, srcURL string) {
299 resp, err := http.Get(srcURL)
300 check(err)
301 defer resp.Body.Close()
302
303 check(os.MkdirAll(filepath.Dir(dstPath), 0775))
304 f, err := os.Create(dstPath)
305 check(err)
306
307 _, err = io.Copy(f, resp.Body)
308 check(err)
309}
310
Joe Tsai8de66752019-12-19 16:38:52 -0800311func downloadArchive(check func(error), dstPath, srcURL, skipPrefix, wantSHA256 string) {
Joe Tsai707894e2019-03-01 12:50:52 -0800312 check(os.RemoveAll(dstPath))
313
314 resp, err := http.Get(srcURL)
315 check(err)
316 defer resp.Body.Close()
317
Joe Tsai8de66752019-12-19 16:38:52 -0800318 var r io.Reader = resp.Body
319 if wantSHA256 != "" {
320 b, err := ioutil.ReadAll(resp.Body)
321 check(err)
322 r = bytes.NewReader(b)
323
324 if gotSHA256 := fmt.Sprintf("%x", sha256.Sum256(b)); gotSHA256 != wantSHA256 {
325 check(fmt.Errorf("checksum validation error:\ngot %v\nwant %v", gotSHA256, wantSHA256))
326 }
327 }
328
329 zr, err := gzip.NewReader(r)
Joe Tsai707894e2019-03-01 12:50:52 -0800330 check(err)
331
332 tr := tar.NewReader(zr)
333 for {
334 h, err := tr.Next()
335 if err == io.EOF {
336 return
337 }
338 check(err)
339
Joe Tsaif3987842019-03-02 13:35:17 -0800340 // Skip directories or files outside the prefix directory.
341 if len(skipPrefix) > 0 {
342 if !strings.HasPrefix(h.Name, skipPrefix) {
343 continue
344 }
345 if len(h.Name) > len(skipPrefix) && h.Name[len(skipPrefix)] != '/' {
346 continue
347 }
348 }
349
350 path := strings.TrimPrefix(strings.TrimPrefix(h.Name, skipPrefix), "/")
Joe Tsai707894e2019-03-01 12:50:52 -0800351 path = filepath.Join(dstPath, filepath.FromSlash(path))
352 mode := os.FileMode(h.Mode & 0777)
353 switch h.Typeflag {
354 case tar.TypeReg:
355 b, err := ioutil.ReadAll(tr)
356 check(err)
357 check(ioutil.WriteFile(path, b, mode))
358 case tar.TypeDir:
359 check(os.Mkdir(path, mode))
360 }
361 }
362}
363
Joe Tsai4f3de442019-08-07 19:33:51 -0700364func mustHandleFlags(t *testing.T) {
365 if *regenerate {
366 t.Run("Generate", func(t *testing.T) {
367 fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types", "-execute"))
368 fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos", "-execute"))
369 files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n")
370 mustRunCommand(t, append([]string{"gofmt", "-w"}, files...)...)
371 })
372 }
373 if *buildRelease {
374 t.Run("BuildRelease", func(t *testing.T) {
Joe Tsai4ab2bc92020-03-10 17:38:07 -0700375 v := version.String()
Joe Tsai4f3de442019-08-07 19:33:51 -0700376 for _, goos := range []string{"linux", "darwin", "windows"} {
377 for _, goarch := range []string{"386", "amd64"} {
Joe Tsai5ebc0b42020-09-08 11:32:36 -0700378 // Avoid Darwin since 10.15 dropped support for i386.
379 if goos == "darwin" && goarch == "386" {
380 continue
381 }
382
Joe Tsai4f3de442019-08-07 19:33:51 -0700383 binPath := filepath.Join("bin", fmt.Sprintf("protoc-gen-go.%v.%v.%v", v, goos, goarch))
384
385 // Build the binary.
386 cmd := command{Env: append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch)}
Joe Tsai576cfb32019-09-04 22:50:42 -0700387 cmd.mustRun(t, "go", "build", "-trimpath", "-ldflags", "-s -w -buildid=", "-o", binPath, "./cmd/protoc-gen-go")
Joe Tsai4f3de442019-08-07 19:33:51 -0700388
389 // Archive and compress the binary.
390 in, err := ioutil.ReadFile(binPath)
391 if err != nil {
392 t.Fatal(err)
393 }
394 out := new(bytes.Buffer)
395 gz, _ := gzip.NewWriterLevel(out, gzip.BestCompression)
396 gz.Comment = fmt.Sprintf("protoc-gen-go VERSION=%v GOOS=%v GOARCH=%v", v, goos, goarch)
397 tw := tar.NewWriter(gz)
398 tw.WriteHeader(&tar.Header{
399 Name: "protoc-gen-go",
400 Mode: int64(0775),
401 Size: int64(len(in)),
402 })
403 tw.Write(in)
404 tw.Close()
405 gz.Close()
406 if err := ioutil.WriteFile(binPath+".tar.gz", out.Bytes(), 0664); err != nil {
407 t.Fatal(err)
408 }
409 }
410 }
411 })
412 }
413 if *regenerate || *buildRelease {
414 t.SkipNow()
415 }
416}
417
Damien Neil3a185602020-02-21 09:16:19 -0800418var copyrightRegex = []*regexp.Regexp{
419 regexp.MustCompile(`^// Copyright \d\d\d\d The Go Authors\. All rights reserved.
420// Use of this source code is governed by a BSD-style
421// license that can be found in the LICENSE file\.
422`),
423 // Generated .pb.go files from main protobuf repo.
424 regexp.MustCompile(`^// Protocol Buffers - Google's data interchange format
425// Copyright \d\d\d\d Google Inc\. All rights reserved\.
426`),
427}
428
Damien Neil3a185602020-02-21 09:16:19 -0800429func mustHaveCopyrightHeader(t *testing.T, files []string) {
430 var bad []string
431File:
432 for _, file := range files {
Damien Neil3a185602020-02-21 09:16:19 -0800433 b, err := ioutil.ReadFile(file)
434 if err != nil {
435 t.Fatal(err)
436 }
437 for _, re := range copyrightRegex {
438 if loc := re.FindIndex(b); loc != nil && loc[0] == 0 {
439 continue File
440 }
441 }
442 bad = append(bad, file)
443 }
444 if len(bad) > 0 {
445 t.Fatalf("files with missing/bad copyright headers:\n %v", strings.Join(bad, "\n "))
446 }
447}
448
Joe Tsai7164af52019-08-07 19:27:43 -0700449type command struct {
450 Dir string
451 Env []string
452}
453
454func (c command) mustRun(t *testing.T, args ...string) string {
Joe Tsai707894e2019-03-01 12:50:52 -0800455 t.Helper()
456 stdout := new(bytes.Buffer)
Herbie Ong4630b3d2019-03-19 16:42:01 -0700457 stderr := new(bytes.Buffer)
Joe Tsai707894e2019-03-01 12:50:52 -0800458 cmd := exec.Command(args[0], args[1:]...)
Joe Tsai7164af52019-08-07 19:27:43 -0700459 cmd.Dir = "."
460 if c.Dir != "" {
461 cmd.Dir = c.Dir
462 }
463 cmd.Env = os.Environ()
464 if c.Env != nil {
465 cmd.Env = c.Env
466 }
467 cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
Herbie Ong4630b3d2019-03-19 16:42:01 -0700468 cmd.Stdout = stdout
469 cmd.Stderr = stderr
Joe Tsai707894e2019-03-01 12:50:52 -0800470 if err := cmd.Run(); err != nil {
Herbie Ong4630b3d2019-03-19 16:42:01 -0700471 t.Fatalf("executing (%v): %v\n%s%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
Joe Tsai707894e2019-03-01 12:50:52 -0800472 }
473 return stdout.String()
474}
Damien Neila80229e2019-06-20 12:53:48 -0700475
Joe Tsai7164af52019-08-07 19:27:43 -0700476func mustRunCommand(t *testing.T, args ...string) string {
477 t.Helper()
478 return command{}.mustRun(t, args...)
479}