.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.6.9 / internal / regtest / regtest.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 type regtestRunner interface {
33         Run(t *testing.T, files string, f TestFunc)
34 }
35
36 func Run(t *testing.T, files string, f TestFunc) {
37         runner.Run(t, files, f)
38 }
39
40 func WithOptions(opts ...RunOption) configuredRunner {
41         return configuredRunner{opts: opts}
42 }
43
44 type configuredRunner struct {
45         opts []RunOption
46 }
47
48 func (r configuredRunner) Run(t *testing.T, files string, f TestFunc) {
49         runner.Run(t, files, f, r.opts...)
50 }
51
52 type RunMultiple []struct {
53         Name   string
54         Runner regtestRunner
55 }
56
57 func (r RunMultiple) Run(t *testing.T, files string, f TestFunc) {
58         for _, runner := range r {
59                 t.Run(runner.Name, func(t *testing.T) {
60                         runner.Runner.Run(t, files, f)
61                 })
62         }
63 }
64
65 // The regtests run significantly slower on these operating systems, due to (we
66 // believe) kernel locking behavior. Only run in singleton mode on these
67 // operating system when using -short.
68 var slowGOOS = map[string]bool{
69         "darwin":  true,
70         "openbsd": true,
71         "plan9":   true,
72 }
73
74 func DefaultModes() Mode {
75         normal := Singleton | Experimental
76         if slowGOOS[runtime.GOOS] && testing.Short() {
77                 normal = Singleton
78         }
79         if *runSubprocessTests {
80                 return normal | SeparateProcess
81         }
82         return normal
83 }
84
85 // Main sets up and tears down the shared regtest state.
86 func Main(m *testing.M) {
87         testenv.ExitIfSmallMachine()
88
89         flag.Parse()
90         if os.Getenv("_GOPLS_TEST_BINARY_RUN_AS_GOPLS") == "true" {
91                 tool.Main(context.Background(), cmd.New("gopls", "", nil, nil), os.Args[1:])
92                 os.Exit(0)
93         }
94
95         runner = &Runner{
96                 DefaultModes:             DefaultModes(),
97                 Timeout:                  *regtestTimeout,
98                 PrintGoroutinesOnFailure: *printGoroutinesOnFailure,
99                 SkipCleanup:              *skipCleanup,
100         }
101         if *runSubprocessTests {
102                 goplsPath := *goplsBinaryPath
103                 if goplsPath == "" {
104                         var err error
105                         goplsPath, err = os.Executable()
106                         if err != nil {
107                                 panic(fmt.Sprintf("finding test binary path: %v", err))
108                         }
109                 }
110                 runner.GoplsPath = goplsPath
111         }
112         dir, err := ioutil.TempDir("", "gopls-regtest-")
113         if err != nil {
114                 panic(fmt.Errorf("creating regtest temp directory: %v", err))
115         }
116         runner.TempDir = dir
117
118         code := m.Run()
119         if err := runner.Close(); err != nil {
120                 fmt.Fprintf(os.Stderr, "closing test runner: %v\n", err)
121                 // Regtest cleanup is broken in go1.12 and earlier, and sometimes flakes on
122                 // Windows due to file locking, but this is OK for our CI.
123                 //
124                 // Fail on go1.13+, except for windows and android which have shutdown problems.
125                 if testenv.Go1Point() >= 13 && runtime.GOOS != "windows" && runtime.GOOS != "android" {
126                         os.Exit(1)
127                 }
128         }
129         os.Exit(code)
130 }