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 func run(t *testing.T, files string, f TestFunc) {
33 runner.Run(t, files, f)
36 func withOptions(opts ...RunOption) configuredRunner {
37 return configuredRunner{opts: opts}
40 type configuredRunner struct {
44 func (r configuredRunner) run(t *testing.T, files string, f TestFunc) {
45 runner.Run(t, files, f, r.opts...)
48 func TestMain(m *testing.M) {
50 if os.Getenv("_GOPLS_TEST_BINARY_RUN_AS_GOPLS") == "true" {
51 tool.Main(context.Background(), cmd.New("gopls", "", nil, nil), os.Args[1:])
56 DefaultModes: NormalModes,
57 Timeout: *regtestTimeout,
58 PrintGoroutinesOnFailure: *printGoroutinesOnFailure,
59 SkipCleanup: *skipCleanup,
61 if *runSubprocessTests {
62 goplsPath := *goplsBinaryPath
65 goplsPath, err = os.Executable()
67 panic(fmt.Sprintf("finding test binary path: %v", err))
70 runner.DefaultModes = NormalModes | SeparateProcess
71 runner.GoplsPath = goplsPath
73 dir, err := ioutil.TempDir("", "gopls-regtest-")
75 panic(fmt.Errorf("creating regtest temp directory: %v", err))
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.
85 // Fail on non-windows go1.13+.
86 if testenv.Go1Point() >= 13 && runtime.GOOS != "windows" {