blob: 8a4f7af7d9e29115026e4589b9d81b439a583985 [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
Damien Neil3a9e1dc2021-12-06 14:18:55 -08005//go:build ignore
Joe Tsaif3987842019-03-02 13:35:17 -08006// +build ignore
Joe Tsai707894e2019-03-01 12:50:52 -08007
Joe Tsaif3987842019-03-02 13:35:17 -08008package main
Joe Tsai707894e2019-03-01 12:50:52 -08009
10import (
11 "archive/tar"
Damien Neil50a85912021-05-20 11:37:23 -070012 "archive/zip"
Joe Tsai707894e2019-03-01 12:50:52 -080013 "bytes"
14 "compress/gzip"
Joe Tsai8de66752019-12-19 16:38:52 -080015 "crypto/sha256"
Joe Tsai707894e2019-03-01 12:50:52 -080016 "flag"
17 "fmt"
18 "io"
19 "io/ioutil"
20 "net/http"
21 "os"
22 "os/exec"
23 "path/filepath"
Damien Neil3a185602020-02-21 09:16:19 -080024 "regexp"
Joe Tsai707894e2019-03-01 12:50:52 -080025 "runtime"
26 "strings"
Joe Tsai62200db2019-07-11 17:34:10 -070027 "sync"
Joe Tsai707894e2019-03-01 12:50:52 -080028 "testing"
Joe Tsaif3987842019-03-02 13:35:17 -080029 "time"
Joe Tsai4f3de442019-08-07 19:33:51 -070030
Joe Tsai4ab2bc92020-03-10 17:38:07 -070031 "google.golang.org/protobuf/internal/version"
Joe Tsai707894e2019-03-01 12:50:52 -080032)
33
34var (
Joe Tsai4f3de442019-08-07 19:33:51 -070035 regenerate = flag.Bool("regenerate", false, "regenerate files")
36 buildRelease = flag.Bool("buildRelease", false, "build release binaries")
Joe Tsai707894e2019-03-01 12:50:52 -080037
Joe Tsai85f47622020-05-26 16:26:02 -070038 protobufVersion = "3.15.3"
39 protobufSHA256 = "" // ignored if protobufVersion is a git hash
Joe Tsai8de66752019-12-19 16:38:52 -080040
Damien Neile5db2962021-12-06 14:03:51 -080041 golangVersions = []string{"1.11.13", "1.12.17", "1.13.15", "1.14.15", "1.15.15", "1.16.10", "1.17.3"}
Joe Tsai8de66752019-12-19 16:38:52 -080042 golangLatest = golangVersions[len(golangVersions)-1]
43
44 staticcheckVersion = "2020.1.4"
45 staticcheckSHA256s = map[string]string{
Joe Tsai8de66752019-12-19 16:38:52 -080046 "darwin/amd64": "5706d101426c025e8f165309e0cb2932e54809eb035ff23ebe19df0f810699d8",
47 "linux/386": "e4dbf94e940678ae7108f0d22c7c2992339bc10a8fb384e7e734b1531a429a1c",
48 "linux/amd64": "09d2c2002236296de2c757df111fe3ae858b89f9e183f645ad01f8135c83c519",
49 }
Joe Tsai707894e2019-03-01 12:50:52 -080050
Joe Tsai9e88bc02019-03-12 01:30:40 -070051 // purgeTimeout determines the maximum age of unused sub-directories.
52 purgeTimeout = 30 * 24 * time.Hour // 1 month
Joe Tsai707894e2019-03-01 12:50:52 -080053
54 // Variables initialized by mustInitDeps.
Herbie Ongd64dceb2019-04-25 01:19:57 -070055 goPath string
56 modulePath string
57 protobufPath string
Joe Tsai707894e2019-03-01 12:50:52 -080058)
59
60func Test(t *testing.T) {
61 mustInitDeps(t)
Joe Tsai4f3de442019-08-07 19:33:51 -070062 mustHandleFlags(t)
Joe Tsai707894e2019-03-01 12:50:52 -080063
Damien Neil4d8936d2020-02-21 10:32:56 -080064 // Report dirt in the working tree quickly, rather than after
65 // going through all the presubmits.
66 //
67 // Fail the test late, so we can test uncommitted changes with -failfast.
Joe Tsaif75a3382019-12-19 17:42:41 -080068 gitDiff := mustRunCommand(t, "git", "diff", "HEAD")
69 if strings.TrimSpace(gitDiff) != "" {
70 fmt.Printf("WARNING: working tree contains uncommitted changes:\n%v\n", gitDiff)
Damien Neil4d8936d2020-02-21 10:32:56 -080071 }
Joe Tsaif75a3382019-12-19 17:42:41 -080072 gitUntracked := mustRunCommand(t, "git", "ls-files", "--others", "--exclude-standard")
73 if strings.TrimSpace(gitUntracked) != "" {
74 fmt.Printf("WARNING: working tree contains untracked files:\n%v\n", gitUntracked)
Damien Neil4d8936d2020-02-21 10:32:56 -080075 }
76
77 // Do the relatively fast checks up-front.
78 t.Run("GeneratedGoFiles", func(t *testing.T) {
79 diff := mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types")
80 if strings.TrimSpace(diff) != "" {
81 t.Fatalf("stale generated files:\n%v", diff)
82 }
83 diff = mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos")
84 if strings.TrimSpace(diff) != "" {
85 t.Fatalf("stale generated files:\n%v", diff)
86 }
87 })
88 t.Run("FormattedGoFiles", func(t *testing.T) {
89 files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n")
90 diff := mustRunCommand(t, append([]string{"gofmt", "-d"}, files...)...)
91 if strings.TrimSpace(diff) != "" {
92 t.Fatalf("unformatted source files:\n%v", diff)
93 }
94 })
95 t.Run("CopyrightHeaders", func(t *testing.T) {
96 files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go", "*.proto")), "\n")
97 mustHaveCopyrightHeader(t, files)
98 })
99
Joe Tsai62200db2019-07-11 17:34:10 -0700100 var wg sync.WaitGroup
101 sema := make(chan bool, (runtime.NumCPU()+1)/2)
102 for i := range golangVersions {
103 goVersion := golangVersions[i]
104 goLabel := "Go" + goVersion
Joe Tsai467a9cd2020-07-08 10:26:03 -0700105 runGo := func(label string, cmd command, args ...string) {
Joe Tsai62200db2019-07-11 17:34:10 -0700106 wg.Add(1)
107 sema <- true
108 go func() {
109 defer wg.Done()
110 defer func() { <-sema }()
111 t.Run(goLabel+"/"+label, func(t *testing.T) {
112 args[0] += goVersion
Joe Tsai467a9cd2020-07-08 10:26:03 -0700113 cmd.mustRun(t, args...)
Joe Tsai707894e2019-03-01 12:50:52 -0800114 })
Joe Tsai62200db2019-07-11 17:34:10 -0700115 }()
116 }
117
118 workDir := filepath.Join(goPath, "src", modulePath)
Joe Tsai467a9cd2020-07-08 10:26:03 -0700119 runGo("Normal", command{Dir: workDir}, "go", "test", "-race", "./...")
120 runGo("PureGo", command{Dir: workDir}, "go", "test", "-race", "-tags", "purego", "./...")
121 runGo("Reflect", command{Dir: workDir}, "go", "test", "-race", "-tags", "protoreflect", "./...")
Joe Tsai62200db2019-07-11 17:34:10 -0700122 if goVersion == golangLatest {
Joe Tsai467a9cd2020-07-08 10:26:03 -0700123 runGo("ProtoLegacy", command{Dir: workDir}, "go", "test", "-race", "-tags", "protolegacy", "./...")
124 runGo("ProtocGenGo", command{Dir: "cmd/protoc-gen-go/testdata"}, "go", "test")
125 runGo("Conformance", command{Dir: "internal/conformance"}, "go", "test", "-execute")
126
127 // Only run the 32-bit compatability tests for Linux;
128 // avoid Darwin since 10.15 dropped support i386 code execution.
129 if runtime.GOOS == "linux" {
130 runGo("Arch32Bit", command{Dir: workDir, Env: append(os.Environ(), "GOARCH=386")}, "go", "test", "./...")
131 }
Joe Tsai62200db2019-07-11 17:34:10 -0700132 }
Joe Tsai707894e2019-03-01 12:50:52 -0800133 }
Joe Tsai62200db2019-07-11 17:34:10 -0700134 wg.Wait()
Joe Tsai707894e2019-03-01 12:50:52 -0800135
Joe Tsai8de66752019-12-19 16:38:52 -0800136 t.Run("GoStaticCheck", func(t *testing.T) {
137 checks := []string{
138 "all", // start with all checks enabled
139 "-SA1019", // disable deprecated usage check
140 "-S*", // disable code simplication checks
141 "-ST*", // disable coding style checks
142 "-U*", // disable unused declaration checks
143 }
144 out := mustRunCommand(t, "staticcheck", "-checks="+strings.Join(checks, ","), "-fail=none", "./...")
145
146 // Filter out findings from certain paths.
147 var findings []string
148 for _, finding := range strings.Split(strings.TrimSpace(out), "\n") {
149 switch {
150 case strings.HasPrefix(finding, "internal/testprotos/legacy/"):
151 default:
152 findings = append(findings, finding)
153 }
154 }
155 if len(findings) > 0 {
156 t.Fatalf("staticcheck findings:\n%v", strings.Join(findings, "\n"))
157 }
158 })
Joe Tsai707894e2019-03-01 12:50:52 -0800159 t.Run("CommittedGitChanges", func(t *testing.T) {
Joe Tsaif75a3382019-12-19 17:42:41 -0800160 if strings.TrimSpace(gitDiff) != "" {
Damien Neil4d8936d2020-02-21 10:32:56 -0800161 t.Fatalf("uncommitted changes")
Joe Tsai707894e2019-03-01 12:50:52 -0800162 }
163 })
164 t.Run("TrackedGitFiles", func(t *testing.T) {
Joe Tsaif75a3382019-12-19 17:42:41 -0800165 if strings.TrimSpace(gitUntracked) != "" {
Damien Neil4d8936d2020-02-21 10:32:56 -0800166 t.Fatalf("untracked files")
Joe Tsai707894e2019-03-01 12:50:52 -0800167 }
168 })
169}
170
171func mustInitDeps(t *testing.T) {
172 check := func(err error) {
173 t.Helper()
174 if err != nil {
175 t.Fatal(err)
176 }
177 }
178
179 // Determine the directory to place the test directory.
180 repoRoot, err := os.Getwd()
181 check(err)
182 testDir := filepath.Join(repoRoot, ".cache")
183 check(os.MkdirAll(testDir, 0775))
184
185 // Delete the current directory if non-empty,
186 // which only occurs if a dependency failed to initialize properly.
187 var workingDir string
Joe Tsaidb5c9002020-09-05 11:24:42 -0700188 finishedDirs := map[string]bool{}
Joe Tsai707894e2019-03-01 12:50:52 -0800189 defer func() {
190 if workingDir != "" {
191 os.RemoveAll(workingDir) // best-effort
192 }
193 }()
Joe Tsaidb5c9002020-09-05 11:24:42 -0700194 startWork := func(name string) string {
195 workingDir = filepath.Join(testDir, name)
196 return workingDir
197 }
198 finishWork := func() {
199 finishedDirs[workingDir] = true
200 workingDir = ""
201 }
Joe Tsai707894e2019-03-01 12:50:52 -0800202
203 // Delete other sub-directories that are no longer relevant.
204 defer func() {
Joe Tsai9e88bc02019-03-12 01:30:40 -0700205 now := time.Now()
Joe Tsai707894e2019-03-01 12:50:52 -0800206 fis, _ := ioutil.ReadDir(testDir)
207 for _, fi := range fis {
Joe Tsaidb5c9002020-09-05 11:24:42 -0700208 dir := filepath.Join(testDir, fi.Name())
209 if finishedDirs[dir] {
210 os.Chtimes(dir, now, now) // best-effort
Joe Tsai9e88bc02019-03-12 01:30:40 -0700211 continue
Joe Tsai707894e2019-03-01 12:50:52 -0800212 }
Joe Tsai9e88bc02019-03-12 01:30:40 -0700213 if now.Sub(fi.ModTime()) < purgeTimeout {
214 continue
215 }
216 fmt.Printf("delete %v\n", fi.Name())
Joe Tsaidb5c9002020-09-05 11:24:42 -0700217 os.RemoveAll(dir) // best-effort
Joe Tsai707894e2019-03-01 12:50:52 -0800218 }
219 }()
220
221 // The bin directory contains symlinks to each tool by version.
222 // It is safe to delete this directory and run the test script from scratch.
Joe Tsaidb5c9002020-09-05 11:24:42 -0700223 binPath := startWork("bin")
Joe Tsai707894e2019-03-01 12:50:52 -0800224 check(os.RemoveAll(binPath))
225 check(os.Mkdir(binPath, 0775))
226 check(os.Setenv("PATH", binPath+":"+os.Getenv("PATH")))
227 registerBinary := func(name, path string) {
228 check(os.Symlink(path, filepath.Join(binPath, name)))
229 }
Joe Tsaidb5c9002020-09-05 11:24:42 -0700230 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800231
232 // Download and build the protobuf toolchain.
233 // We avoid downloading the pre-compiled binaries since they do not contain
234 // the conformance test runner.
Joe Tsaidb5c9002020-09-05 11:24:42 -0700235 protobufPath = startWork("protobuf-" + protobufVersion)
Herbie Ongd64dceb2019-04-25 01:19:57 -0700236 if _, err := os.Stat(protobufPath); err != nil {
237 fmt.Printf("download %v\n", filepath.Base(protobufPath))
Joe Tsai387873d2020-04-28 14:44:38 -0700238 if isCommit := strings.Trim(protobufVersion, "0123456789abcdef") == ""; isCommit {
239 command{Dir: testDir}.mustRun(t, "git", "clone", "https://github.com/protocolbuffers/protobuf", "protobuf-"+protobufVersion)
240 command{Dir: protobufPath}.mustRun(t, "git", "checkout", protobufVersion)
241 } else {
242 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 -0800243 downloadArchive(check, protobufPath, url, "protobuf-"+protobufVersion, protobufSHA256)
Joe Tsai387873d2020-04-28 14:44:38 -0700244 }
Joe Tsai707894e2019-03-01 12:50:52 -0800245
Herbie Ongd64dceb2019-04-25 01:19:57 -0700246 fmt.Printf("build %v\n", filepath.Base(protobufPath))
Joe Tsai7164af52019-08-07 19:27:43 -0700247 command{Dir: protobufPath}.mustRun(t, "./autogen.sh")
248 command{Dir: protobufPath}.mustRun(t, "./configure")
249 command{Dir: protobufPath}.mustRun(t, "make")
250 command{Dir: filepath.Join(protobufPath, "conformance")}.mustRun(t, "make")
Joe Tsai707894e2019-03-01 12:50:52 -0800251 }
Herbie Ongd64dceb2019-04-25 01:19:57 -0700252 check(os.Setenv("PROTOBUF_ROOT", protobufPath)) // for generate-protos
253 registerBinary("conform-test-runner", filepath.Join(protobufPath, "conformance", "conformance-test-runner"))
254 registerBinary("protoc", filepath.Join(protobufPath, "src", "protoc"))
Joe Tsaidb5c9002020-09-05 11:24:42 -0700255 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800256
257 // Download each Go toolchain version.
258 for _, v := range golangVersions {
Joe Tsaidb5c9002020-09-05 11:24:42 -0700259 goDir := startWork("go" + v)
260 if _, err := os.Stat(goDir); err != nil {
261 fmt.Printf("download %v\n", filepath.Base(goDir))
Joe Tsai707894e2019-03-01 12:50:52 -0800262 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 -0700263 downloadArchive(check, goDir, url, "go", "") // skip SHA256 check as we fetch over https from a trusted domain
Joe Tsai707894e2019-03-01 12:50:52 -0800264 }
Joe Tsaidb5c9002020-09-05 11:24:42 -0700265 registerBinary("go"+v, filepath.Join(goDir, "bin", "go"))
266 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800267 }
268 registerBinary("go", filepath.Join(testDir, "go"+golangLatest, "bin", "go"))
269 registerBinary("gofmt", filepath.Join(testDir, "go"+golangLatest, "bin", "gofmt"))
Joe Tsai707894e2019-03-01 12:50:52 -0800270
Joe Tsai8de66752019-12-19 16:38:52 -0800271 // Download the staticcheck tool.
Joe Tsaidb5c9002020-09-05 11:24:42 -0700272 checkDir := startWork("staticcheck-" + staticcheckVersion)
273 if _, err := os.Stat(checkDir); err != nil {
274 fmt.Printf("download %v\n", filepath.Base(checkDir))
Joe Tsai8de66752019-12-19 16:38:52 -0800275 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 -0700276 downloadArchive(check, checkDir, url, "staticcheck", staticcheckSHA256s[runtime.GOOS+"/"+runtime.GOARCH])
Joe Tsai8de66752019-12-19 16:38:52 -0800277 }
Joe Tsaidb5c9002020-09-05 11:24:42 -0700278 registerBinary("staticcheck", filepath.Join(checkDir, "staticcheck"))
279 finishWork()
Joe Tsai8de66752019-12-19 16:38:52 -0800280
Joe Tsaie7ab1122021-03-02 17:09:55 -0800281 // GitHub actions sets GOROOT, which confuses invocations of the Go toolchain.
Joe Tsai707894e2019-03-01 12:50:52 -0800282 // Explicitly clear GOROOT, so each toolchain uses their default GOROOT.
283 check(os.Unsetenv("GOROOT"))
284
Joe Tsai6a2180f2019-07-11 16:34:17 -0700285 // Set a cache directory outside the test directory.
286 check(os.Setenv("GOCACHE", filepath.Join(repoRoot, ".gocache")))
Joe Tsai707894e2019-03-01 12:50:52 -0800287
288 // Setup GOPATH for pre-module support (i.e., go1.10 and earlier).
Joe Tsaidb5c9002020-09-05 11:24:42 -0700289 goPath = startWork("gopath")
Joe Tsai7164af52019-08-07 19:27:43 -0700290 modulePath = strings.TrimSpace(command{Dir: testDir}.mustRun(t, "go", "list", "-m", "-f", "{{.Path}}"))
Joe Tsai707894e2019-03-01 12:50:52 -0800291 check(os.RemoveAll(filepath.Join(goPath, "src")))
292 check(os.MkdirAll(filepath.Join(goPath, "src", filepath.Dir(modulePath)), 0775))
293 check(os.Symlink(repoRoot, filepath.Join(goPath, "src", modulePath)))
Joe Tsai7164af52019-08-07 19:27:43 -0700294 command{Dir: repoRoot}.mustRun(t, "go", "mod", "tidy")
295 command{Dir: repoRoot}.mustRun(t, "go", "mod", "vendor")
Joe Tsai707894e2019-03-01 12:50:52 -0800296 check(os.Setenv("GOPATH", goPath))
Joe Tsaidb5c9002020-09-05 11:24:42 -0700297 finishWork()
Joe Tsai707894e2019-03-01 12:50:52 -0800298}
299
Damien Neila80229e2019-06-20 12:53:48 -0700300func downloadFile(check func(error), dstPath, srcURL string) {
301 resp, err := http.Get(srcURL)
302 check(err)
303 defer resp.Body.Close()
304
305 check(os.MkdirAll(filepath.Dir(dstPath), 0775))
306 f, err := os.Create(dstPath)
307 check(err)
308
309 _, err = io.Copy(f, resp.Body)
310 check(err)
311}
312
Joe Tsai8de66752019-12-19 16:38:52 -0800313func downloadArchive(check func(error), dstPath, srcURL, skipPrefix, wantSHA256 string) {
Joe Tsai707894e2019-03-01 12:50:52 -0800314 check(os.RemoveAll(dstPath))
315
316 resp, err := http.Get(srcURL)
317 check(err)
318 defer resp.Body.Close()
319
Joe Tsai8de66752019-12-19 16:38:52 -0800320 var r io.Reader = resp.Body
321 if wantSHA256 != "" {
322 b, err := ioutil.ReadAll(resp.Body)
323 check(err)
324 r = bytes.NewReader(b)
325
326 if gotSHA256 := fmt.Sprintf("%x", sha256.Sum256(b)); gotSHA256 != wantSHA256 {
327 check(fmt.Errorf("checksum validation error:\ngot %v\nwant %v", gotSHA256, wantSHA256))
328 }
329 }
330
331 zr, err := gzip.NewReader(r)
Joe Tsai707894e2019-03-01 12:50:52 -0800332 check(err)
333
334 tr := tar.NewReader(zr)
335 for {
336 h, err := tr.Next()
337 if err == io.EOF {
338 return
339 }
340 check(err)
341
Joe Tsaif3987842019-03-02 13:35:17 -0800342 // Skip directories or files outside the prefix directory.
343 if len(skipPrefix) > 0 {
344 if !strings.HasPrefix(h.Name, skipPrefix) {
345 continue
346 }
347 if len(h.Name) > len(skipPrefix) && h.Name[len(skipPrefix)] != '/' {
348 continue
349 }
350 }
351
352 path := strings.TrimPrefix(strings.TrimPrefix(h.Name, skipPrefix), "/")
Joe Tsai707894e2019-03-01 12:50:52 -0800353 path = filepath.Join(dstPath, filepath.FromSlash(path))
354 mode := os.FileMode(h.Mode & 0777)
355 switch h.Typeflag {
356 case tar.TypeReg:
357 b, err := ioutil.ReadAll(tr)
358 check(err)
359 check(ioutil.WriteFile(path, b, mode))
360 case tar.TypeDir:
361 check(os.Mkdir(path, mode))
362 }
363 }
364}
365
Joe Tsai4f3de442019-08-07 19:33:51 -0700366func mustHandleFlags(t *testing.T) {
367 if *regenerate {
368 t.Run("Generate", func(t *testing.T) {
369 fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-types", "-execute"))
370 fmt.Print(mustRunCommand(t, "go", "run", "-tags", "protolegacy", "./internal/cmd/generate-protos", "-execute"))
371 files := strings.Split(strings.TrimSpace(mustRunCommand(t, "git", "ls-files", "*.go")), "\n")
372 mustRunCommand(t, append([]string{"gofmt", "-w"}, files...)...)
373 })
374 }
375 if *buildRelease {
376 t.Run("BuildRelease", func(t *testing.T) {
Joe Tsai4ab2bc92020-03-10 17:38:07 -0700377 v := version.String()
Joe Tsai4f3de442019-08-07 19:33:51 -0700378 for _, goos := range []string{"linux", "darwin", "windows"} {
379 for _, goarch := range []string{"386", "amd64"} {
Joe Tsai5ebc0b42020-09-08 11:32:36 -0700380 // Avoid Darwin since 10.15 dropped support for i386.
381 if goos == "darwin" && goarch == "386" {
382 continue
383 }
384
Joe Tsai4f3de442019-08-07 19:33:51 -0700385 binPath := filepath.Join("bin", fmt.Sprintf("protoc-gen-go.%v.%v.%v", v, goos, goarch))
386
387 // Build the binary.
388 cmd := command{Env: append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch)}
Joe Tsai576cfb32019-09-04 22:50:42 -0700389 cmd.mustRun(t, "go", "build", "-trimpath", "-ldflags", "-s -w -buildid=", "-o", binPath, "./cmd/protoc-gen-go")
Joe Tsai4f3de442019-08-07 19:33:51 -0700390
391 // Archive and compress the binary.
392 in, err := ioutil.ReadFile(binPath)
393 if err != nil {
394 t.Fatal(err)
395 }
396 out := new(bytes.Buffer)
Damien Neil50a85912021-05-20 11:37:23 -0700397 suffix := ""
398 comment := fmt.Sprintf("protoc-gen-go VERSION=%v GOOS=%v GOARCH=%v", v, goos, goarch)
399 switch goos {
400 case "windows":
401 suffix = ".zip"
402 zw := zip.NewWriter(out)
403 zw.SetComment(comment)
404 fw, _ := zw.Create("protoc-gen-go.exe")
405 fw.Write(in)
406 zw.Close()
407 default:
408 suffix = ".tar.gz"
409 gz, _ := gzip.NewWriterLevel(out, gzip.BestCompression)
410 gz.Comment = comment
411 tw := tar.NewWriter(gz)
412 tw.WriteHeader(&tar.Header{
413 Name: "protoc-gen-go",
414 Mode: int64(0775),
415 Size: int64(len(in)),
416 })
417 tw.Write(in)
418 tw.Close()
419 gz.Close()
420 }
421 if err := ioutil.WriteFile(binPath+suffix, out.Bytes(), 0664); err != nil {
Joe Tsai4f3de442019-08-07 19:33:51 -0700422 t.Fatal(err)
423 }
424 }
425 }
426 })
427 }
428 if *regenerate || *buildRelease {
429 t.SkipNow()
430 }
431}
432
Damien Neil3a185602020-02-21 09:16:19 -0800433var copyrightRegex = []*regexp.Regexp{
434 regexp.MustCompile(`^// Copyright \d\d\d\d The Go Authors\. All rights reserved.
435// Use of this source code is governed by a BSD-style
436// license that can be found in the LICENSE file\.
437`),
438 // Generated .pb.go files from main protobuf repo.
439 regexp.MustCompile(`^// Protocol Buffers - Google's data interchange format
440// Copyright \d\d\d\d Google Inc\. All rights reserved\.
441`),
442}
443
Damien Neil3a185602020-02-21 09:16:19 -0800444func mustHaveCopyrightHeader(t *testing.T, files []string) {
445 var bad []string
446File:
447 for _, file := range files {
Damien Neil3a185602020-02-21 09:16:19 -0800448 b, err := ioutil.ReadFile(file)
449 if err != nil {
450 t.Fatal(err)
451 }
452 for _, re := range copyrightRegex {
453 if loc := re.FindIndex(b); loc != nil && loc[0] == 0 {
454 continue File
455 }
456 }
457 bad = append(bad, file)
458 }
459 if len(bad) > 0 {
460 t.Fatalf("files with missing/bad copyright headers:\n %v", strings.Join(bad, "\n "))
461 }
462}
463
Joe Tsai7164af52019-08-07 19:27:43 -0700464type command struct {
465 Dir string
466 Env []string
467}
468
469func (c command) mustRun(t *testing.T, args ...string) string {
Joe Tsai707894e2019-03-01 12:50:52 -0800470 t.Helper()
471 stdout := new(bytes.Buffer)
Herbie Ong4630b3d2019-03-19 16:42:01 -0700472 stderr := new(bytes.Buffer)
Joe Tsai707894e2019-03-01 12:50:52 -0800473 cmd := exec.Command(args[0], args[1:]...)
Joe Tsai7164af52019-08-07 19:27:43 -0700474 cmd.Dir = "."
475 if c.Dir != "" {
476 cmd.Dir = c.Dir
477 }
478 cmd.Env = os.Environ()
479 if c.Env != nil {
480 cmd.Env = c.Env
481 }
482 cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
Herbie Ong4630b3d2019-03-19 16:42:01 -0700483 cmd.Stdout = stdout
484 cmd.Stderr = stderr
Joe Tsai707894e2019-03-01 12:50:52 -0800485 if err := cmd.Run(); err != nil {
Herbie Ong4630b3d2019-03-19 16:42:01 -0700486 t.Fatalf("executing (%v): %v\n%s%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
Joe Tsai707894e2019-03-01 12:50:52 -0800487 }
488 return stdout.String()
489}
Damien Neila80229e2019-06-20 12:53:48 -0700490
Joe Tsai7164af52019-08-07 19:27:43 -0700491func mustRunCommand(t *testing.T, args ...string) string {
492 t.Helper()
493 return command{}.mustRun(t, args...)
494}