Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.5.2 / internal / regtest / reg_test.go
1 // Copyright 2020 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
5 package regtest
6
7 import (
8         "context"
9         "flag"
10         "fmt"
11         "io/ioutil"
12         "os"
13         "runtime"
14         "testing"
15         "time"
16
17         "golang.org/x/tools/internal/lsp/cmd"
18         "golang.org/x/tools/internal/testenv"
19         "golang.org/x/tools/internal/tool"
20 )
21
22 var (
23         runSubprocessTests       = flag.Bool("enable_gopls_subprocess_tests", false, "run regtests against a gopls subprocess")
24         goplsBinaryPath          = flag.String("gopls_test_binary", "", "path to the gopls binary for use as a remote, for use with the -enable_gopls_subprocess_tests flag")
25         regtestTimeout           = flag.Duration("regtest_timeout", 20*time.Second, "default timeout for each regtest")
26         skipCleanup              = flag.Bool("regtest_skip_cleanup", false, "whether to skip cleaning up temp directories")
27         printGoroutinesOnFailure = flag.Bool("regtest_print_goroutines", false, "whether to print goroutines info on failure")
28 )
29
30 var runner *Runner
31
32 func run(t *testing.T, files string, f TestFunc) {
33         runner.Run(t, files, f)
34 }
35
36 func withOptions(opts ...RunOption) configuredRunner {
37         return configuredRunner{opts: opts}
38 }
39
40 type configuredRunner struct {
41         opts []RunOption
42 }
43
44 func (r configuredRunner) run(t *testing.T, files string, f TestFunc) {
45         runner.Run(t, files, f, r.opts...)
46 }
47
48 func TestMain(m *testing.M) {
49         flag.Parse()
50         if os.Getenv("_GOPLS_TEST_BINARY_RUN_AS_GOPLS") == "true" {
51                 tool.Main(context.Background(), cmd.New("gopls", "", nil, nil), os.Args[1:])
52                 os.Exit(0)
53         }
54
55         runner = &Runner{
56                 DefaultModes:             NormalModes,
57                 Timeout:                  *regtestTimeout,
58                 PrintGoroutinesOnFailure: *printGoroutinesOnFailure,
59                 SkipCleanup:              *skipCleanup,
60         }
61         if *runSubprocessTests {
62                 goplsPath := *goplsBinaryPath
63                 if goplsPath == "" {
64                         var err error
65                         goplsPath, err = os.Executable()
66                         if err != nil {
67                                 panic(fmt.Sprintf("finding test binary path: %v", err))
68                         }
69                 }
70                 runner.DefaultModes = NormalModes | SeparateProcess
71                 runner.GoplsPath = goplsPath
72         }
73         dir, err := ioutil.TempDir("", "gopls-regtest-")
74         if err != nil {
75                 panic(fmt.Errorf("creating regtest temp directory: %v", err))
76         }
77         runner.TempDir = dir
78
79         code := m.Run()
80         if err := runner.Close(); err != nil {
81                 fmt.Fprintf(os.Stderr, "closing test runner: %v\n", err)
82                 // Regtest cleanup is broken in go1.12 and earlier, and sometimes flakes on
83                 // Windows due to file locking, but this is OK for our CI.
84                 //
85                 // Fail on non-windows go1.13+.
86                 if testenv.Go1Point() >= 13 && runtime.GOOS != "windows" {
87                         os.Exit(1)
88                 }
89         }
90         os.Exit(code)
91 }