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 | |
Damien Neil | 3a9e1dc | 2021-12-06 14:18:55 -0800 | [diff] [blame] | 5 | //go:build ignore |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 6 | // +build ignore |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 7 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 8 | package main |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 9 | |
| 10 | import ( |
| 11 | "archive/tar" |
Damien Neil | 50a8591 | 2021-05-20 11:37:23 -0700 | [diff] [blame] | 12 | "archive/zip" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 13 | "bytes" |
| 14 | "compress/gzip" |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 15 | "crypto/sha256" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 16 | "flag" |
| 17 | "fmt" |
| 18 | "io" |
| 19 | "io/ioutil" |
| 20 | "net/http" |
| 21 | "os" |
| 22 | "os/exec" |
| 23 | "path/filepath" |
Damien Neil | 3a18560 | 2020-02-21 09:16:19 -0800 | [diff] [blame] | 24 | "regexp" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 25 | "runtime" |
| 26 | "strings" |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 27 | "sync" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 28 | "testing" |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 29 | "time" |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 30 | |
Joe Tsai | 4ab2bc9 | 2020-03-10 17:38:07 -0700 | [diff] [blame] | 31 | "google.golang.org/protobuf/internal/version" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var ( |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 35 | regenerate = flag.Bool("regenerate", false, "regenerate files") |
| 36 | buildRelease = flag.Bool("buildRelease", false, "build release binaries") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 37 | |
Joe Tsai | 85f4762 | 2020-05-26 16:26:02 -0700 | [diff] [blame] | 38 | protobufVersion = "3.15.3" |
| 39 | protobufSHA256 = "" // ignored if protobufVersion is a git hash |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 40 | |
Damien Neil | e5db296 | 2021-12-06 14:03:51 -0800 | [diff] [blame] | 41 | golangVersions = []string{"1.11.13", "1.12.17", "1.13.15", "1.14.15", "1.15.15", "1.16.10", "1.17.3"} |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 42 | golangLatest = golangVersions[len(golangVersions)-1] |
| 43 | |
| 44 | staticcheckVersion = "2020.1.4" |
| 45 | staticcheckSHA256s = map[string]string{ |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 46 | "darwin/amd64": "5706d101426c025e8f165309e0cb2932e54809eb035ff23ebe19df0f810699d8", |
| 47 | "linux/386": "e4dbf94e940678ae7108f0d22c7c2992339bc10a8fb384e7e734b1531a429a1c", |
| 48 | "linux/amd64": "09d2c2002236296de2c757df111fe3ae858b89f9e183f645ad01f8135c83c519", |
| 49 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 50 | |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 51 | // purgeTimeout determines the maximum age of unused sub-directories. |
| 52 | purgeTimeout = 30 * 24 * time.Hour // 1 month |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 53 | |
| 54 | // Variables initialized by mustInitDeps. |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 55 | goPath string |
| 56 | modulePath string |
| 57 | protobufPath string |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 58 | ) |
| 59 | |
| 60 | func Test(t *testing.T) { |
| 61 | mustInitDeps(t) |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 62 | mustHandleFlags(t) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 63 | |
Damien Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame] | 64 | // 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 Tsai | f75a338 | 2019-12-19 17:42:41 -0800 | [diff] [blame] | 68 | 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 Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame] | 71 | } |
Joe Tsai | f75a338 | 2019-12-19 17:42:41 -0800 | [diff] [blame] | 72 | 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 Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame] | 75 | } |
| 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 Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 100 | 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 Tsai | 467a9cd | 2020-07-08 10:26:03 -0700 | [diff] [blame] | 105 | runGo := func(label string, cmd command, args ...string) { |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 106 | 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 Tsai | 467a9cd | 2020-07-08 10:26:03 -0700 | [diff] [blame] | 113 | cmd.mustRun(t, args...) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 114 | }) |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 115 | }() |
| 116 | } |
| 117 | |
| 118 | workDir := filepath.Join(goPath, "src", modulePath) |
Joe Tsai | 467a9cd | 2020-07-08 10:26:03 -0700 | [diff] [blame] | 119 | 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 Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 122 | if goVersion == golangLatest { |
Joe Tsai | 467a9cd | 2020-07-08 10:26:03 -0700 | [diff] [blame] | 123 | 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 Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 132 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 133 | } |
Joe Tsai | 62200db | 2019-07-11 17:34:10 -0700 | [diff] [blame] | 134 | wg.Wait() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 135 | |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 136 | 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 Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 159 | t.Run("CommittedGitChanges", func(t *testing.T) { |
Joe Tsai | f75a338 | 2019-12-19 17:42:41 -0800 | [diff] [blame] | 160 | if strings.TrimSpace(gitDiff) != "" { |
Damien Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame] | 161 | t.Fatalf("uncommitted changes") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 162 | } |
| 163 | }) |
| 164 | t.Run("TrackedGitFiles", func(t *testing.T) { |
Joe Tsai | f75a338 | 2019-12-19 17:42:41 -0800 | [diff] [blame] | 165 | if strings.TrimSpace(gitUntracked) != "" { |
Damien Neil | 4d8936d | 2020-02-21 10:32:56 -0800 | [diff] [blame] | 166 | t.Fatalf("untracked files") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 167 | } |
| 168 | }) |
| 169 | } |
| 170 | |
| 171 | func 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 Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 188 | finishedDirs := map[string]bool{} |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 189 | defer func() { |
| 190 | if workingDir != "" { |
| 191 | os.RemoveAll(workingDir) // best-effort |
| 192 | } |
| 193 | }() |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 194 | 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 Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 202 | |
| 203 | // Delete other sub-directories that are no longer relevant. |
| 204 | defer func() { |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 205 | now := time.Now() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 206 | fis, _ := ioutil.ReadDir(testDir) |
| 207 | for _, fi := range fis { |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 208 | dir := filepath.Join(testDir, fi.Name()) |
| 209 | if finishedDirs[dir] { |
| 210 | os.Chtimes(dir, now, now) // best-effort |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 211 | continue |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 212 | } |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 213 | if now.Sub(fi.ModTime()) < purgeTimeout { |
| 214 | continue |
| 215 | } |
| 216 | fmt.Printf("delete %v\n", fi.Name()) |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 217 | os.RemoveAll(dir) // best-effort |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 218 | } |
| 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 Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 223 | binPath := startWork("bin") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 224 | 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 Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 230 | finishWork() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 231 | |
| 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 Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 235 | protobufPath = startWork("protobuf-" + protobufVersion) |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 236 | if _, err := os.Stat(protobufPath); err != nil { |
| 237 | fmt.Printf("download %v\n", filepath.Base(protobufPath)) |
Joe Tsai | 387873d | 2020-04-28 14:44:38 -0700 | [diff] [blame] | 238 | 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 Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 243 | downloadArchive(check, protobufPath, url, "protobuf-"+protobufVersion, protobufSHA256) |
Joe Tsai | 387873d | 2020-04-28 14:44:38 -0700 | [diff] [blame] | 244 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 245 | |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 246 | fmt.Printf("build %v\n", filepath.Base(protobufPath)) |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 247 | 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 Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 251 | } |
Herbie Ong | d64dceb | 2019-04-25 01:19:57 -0700 | [diff] [blame] | 252 | 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 Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 255 | finishWork() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 256 | |
| 257 | // Download each Go toolchain version. |
| 258 | for _, v := range golangVersions { |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 259 | goDir := startWork("go" + v) |
| 260 | if _, err := os.Stat(goDir); err != nil { |
| 261 | fmt.Printf("download %v\n", filepath.Base(goDir)) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 262 | url := fmt.Sprintf("https://dl.google.com/go/go%v.%v-%v.tar.gz", v, runtime.GOOS, runtime.GOARCH) |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 263 | downloadArchive(check, goDir, url, "go", "") // skip SHA256 check as we fetch over https from a trusted domain |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 264 | } |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 265 | registerBinary("go"+v, filepath.Join(goDir, "bin", "go")) |
| 266 | finishWork() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 267 | } |
| 268 | registerBinary("go", filepath.Join(testDir, "go"+golangLatest, "bin", "go")) |
| 269 | registerBinary("gofmt", filepath.Join(testDir, "go"+golangLatest, "bin", "gofmt")) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 270 | |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 271 | // Download the staticcheck tool. |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 272 | checkDir := startWork("staticcheck-" + staticcheckVersion) |
| 273 | if _, err := os.Stat(checkDir); err != nil { |
| 274 | fmt.Printf("download %v\n", filepath.Base(checkDir)) |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 275 | url := fmt.Sprintf("https://github.com/dominikh/go-tools/releases/download/%v/staticcheck_%v_%v.tar.gz", staticcheckVersion, runtime.GOOS, runtime.GOARCH) |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 276 | downloadArchive(check, checkDir, url, "staticcheck", staticcheckSHA256s[runtime.GOOS+"/"+runtime.GOARCH]) |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 277 | } |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 278 | registerBinary("staticcheck", filepath.Join(checkDir, "staticcheck")) |
| 279 | finishWork() |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 280 | |
Joe Tsai | e7ab112 | 2021-03-02 17:09:55 -0800 | [diff] [blame] | 281 | // GitHub actions sets GOROOT, which confuses invocations of the Go toolchain. |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 282 | // Explicitly clear GOROOT, so each toolchain uses their default GOROOT. |
| 283 | check(os.Unsetenv("GOROOT")) |
| 284 | |
Joe Tsai | 6a2180f | 2019-07-11 16:34:17 -0700 | [diff] [blame] | 285 | // Set a cache directory outside the test directory. |
| 286 | check(os.Setenv("GOCACHE", filepath.Join(repoRoot, ".gocache"))) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 287 | |
| 288 | // Setup GOPATH for pre-module support (i.e., go1.10 and earlier). |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 289 | goPath = startWork("gopath") |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 290 | 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] | 291 | 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 Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 294 | command{Dir: repoRoot}.mustRun(t, "go", "mod", "tidy") |
| 295 | command{Dir: repoRoot}.mustRun(t, "go", "mod", "vendor") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 296 | check(os.Setenv("GOPATH", goPath)) |
Joe Tsai | db5c900 | 2020-09-05 11:24:42 -0700 | [diff] [blame] | 297 | finishWork() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 300 | func 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 Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 313 | func downloadArchive(check func(error), dstPath, srcURL, skipPrefix, wantSHA256 string) { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 314 | check(os.RemoveAll(dstPath)) |
| 315 | |
| 316 | resp, err := http.Get(srcURL) |
| 317 | check(err) |
| 318 | defer resp.Body.Close() |
| 319 | |
Joe Tsai | 8de6675 | 2019-12-19 16:38:52 -0800 | [diff] [blame] | 320 | 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 Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 332 | 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 Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 342 | // 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 Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 353 | 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 Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 366 | func 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 Tsai | 4ab2bc9 | 2020-03-10 17:38:07 -0700 | [diff] [blame] | 377 | v := version.String() |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 378 | for _, goos := range []string{"linux", "darwin", "windows"} { |
| 379 | for _, goarch := range []string{"386", "amd64"} { |
Joe Tsai | 5ebc0b4 | 2020-09-08 11:32:36 -0700 | [diff] [blame] | 380 | // Avoid Darwin since 10.15 dropped support for i386. |
| 381 | if goos == "darwin" && goarch == "386" { |
| 382 | continue |
| 383 | } |
| 384 | |
Joe Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 385 | 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 Tsai | 576cfb3 | 2019-09-04 22:50:42 -0700 | [diff] [blame] | 389 | 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] | 390 | |
| 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 Neil | 50a8591 | 2021-05-20 11:37:23 -0700 | [diff] [blame] | 397 | 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 Tsai | 4f3de44 | 2019-08-07 19:33:51 -0700 | [diff] [blame] | 422 | t.Fatal(err) |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | }) |
| 427 | } |
| 428 | if *regenerate || *buildRelease { |
| 429 | t.SkipNow() |
| 430 | } |
| 431 | } |
| 432 | |
Damien Neil | 3a18560 | 2020-02-21 09:16:19 -0800 | [diff] [blame] | 433 | var 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 Neil | 3a18560 | 2020-02-21 09:16:19 -0800 | [diff] [blame] | 444 | func mustHaveCopyrightHeader(t *testing.T, files []string) { |
| 445 | var bad []string |
| 446 | File: |
| 447 | for _, file := range files { |
Damien Neil | 3a18560 | 2020-02-21 09:16:19 -0800 | [diff] [blame] | 448 | 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 Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 464 | type command struct { |
| 465 | Dir string |
| 466 | Env []string |
| 467 | } |
| 468 | |
| 469 | func (c command) mustRun(t *testing.T, args ...string) string { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 470 | t.Helper() |
| 471 | stdout := new(bytes.Buffer) |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 472 | stderr := new(bytes.Buffer) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 473 | cmd := exec.Command(args[0], args[1:]...) |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 474 | 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 Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 483 | cmd.Stdout = stdout |
| 484 | cmd.Stderr = stderr |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 485 | if err := cmd.Run(); err != nil { |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 486 | 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] | 487 | } |
| 488 | return stdout.String() |
| 489 | } |
Damien Neil | a80229e | 2019-06-20 12:53:48 -0700 | [diff] [blame] | 490 | |
Joe Tsai | 7164af5 | 2019-08-07 19:27:43 -0700 | [diff] [blame] | 491 | func mustRunCommand(t *testing.T, args ...string) string { |
| 492 | t.Helper() |
| 493 | return command{}.mustRun(t, args...) |
| 494 | } |