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.
17 "golang.org/x/tools/internal/lsp/cmd"
18 "golang.org/x/tools/internal/testenv"
19 "golang.org/x/tools/internal/tool"
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")
32 type regtestRunner interface {
33 Run(t *testing.T, files string, f TestFunc)
36 func Run(t *testing.T, files string, f TestFunc) {
37 runner.Run(t, files, f)
40 func WithOptions(opts ...RunOption) configuredRunner {
41 return configuredRunner{opts: opts}
44 type configuredRunner struct {
48 func (r configuredRunner) Run(t *testing.T, files string, f TestFunc) {
49 runner.Run(t, files, f, r.opts...)
52 type RunMultiple []struct {
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)
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{
74 func DefaultModes() Mode {
75 normal := Singleton | Experimental
76 if slowGOOS[runtime.GOOS] && testing.Short() {
79 if *runSubprocessTests {
80 return normal | SeparateProcess
85 // Main sets up and tears down the shared regtest state.
86 func Main(m *testing.M) {
87 testenv.ExitIfSmallMachine()
90 if os.Getenv("_GOPLS_TEST_BINARY_RUN_AS_GOPLS") == "true" {
91 tool.Main(context.Background(), cmd.New("gopls", "", nil, nil), os.Args[1:])
96 DefaultModes: DefaultModes(),
97 Timeout: *regtestTimeout,
98 PrintGoroutinesOnFailure: *printGoroutinesOnFailure,
99 SkipCleanup: *skipCleanup,
101 if *runSubprocessTests {
102 goplsPath := *goplsBinaryPath
105 goplsPath, err = os.Executable()
107 panic(fmt.Sprintf("finding test binary path: %v", err))
110 runner.GoplsPath = goplsPath
112 dir, err := ioutil.TempDir("", "gopls-regtest-")
114 panic(fmt.Errorf("creating regtest temp directory: %v", err))
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.
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" {