2020-02-06 17:50:37 -07:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package regtest
|
|
|
|
|
|
|
|
import (
|
2020-02-10 09:34:13 -07:00
|
|
|
"context"
|
|
|
|
"flag"
|
2020-02-09 12:44:03 -07:00
|
|
|
"fmt"
|
2020-02-06 17:50:37 -07:00
|
|
|
"os"
|
2020-02-10 09:34:13 -07:00
|
|
|
"path/filepath"
|
2020-02-06 17:50:37 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-02-09 12:44:03 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp"
|
2020-02-10 09:34:13 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
2020-02-09 12:44:03 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/lsprpc"
|
2020-02-10 09:34:13 -07:00
|
|
|
"golang.org/x/tools/internal/tool"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
runSubprocessTests = flag.Bool("enable_gopls_subprocess_tests", false, "run regtests against a gopls subprocess")
|
|
|
|
goplsBinaryPath = flag.String("gopls_test_binary", "", "path to the gopls binary for use as a remote, for use with the -gopls_subprocess_testmode flag")
|
2020-02-06 17:50:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var runner *Runner
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2020-02-10 09:34:13 -07:00
|
|
|
flag.Parse()
|
|
|
|
if os.Getenv("_GOPLS_TEST_BINARY_RUN_AS_GOPLS") == "true" {
|
|
|
|
tool.Main(context.Background(), cmd.New("gopls", "", nil, nil), os.Args[1:])
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2020-02-09 12:44:03 -07:00
|
|
|
// Override functions that would shut down the test process
|
|
|
|
defer func(lspExit, forwarderExit func(code int)) {
|
|
|
|
lsp.ServerExitFunc = lspExit
|
|
|
|
lsprpc.ForwarderExitFunc = forwarderExit
|
|
|
|
}(lsp.ServerExitFunc, lsprpc.ForwarderExitFunc)
|
|
|
|
// None of these regtests should be able to shut down a server process.
|
|
|
|
lsp.ServerExitFunc = func(code int) {
|
|
|
|
panic(fmt.Sprintf("LSP server exited with code %d", code))
|
|
|
|
}
|
|
|
|
// We don't want our forwarders to exit, but it's OK if they would have.
|
|
|
|
lsprpc.ForwarderExitFunc = func(code int) {}
|
2020-02-10 09:34:13 -07:00
|
|
|
|
2020-02-20 08:33:58 -07:00
|
|
|
const testTimeout = 60 * time.Second
|
2020-02-10 09:34:13 -07:00
|
|
|
if *runSubprocessTests {
|
|
|
|
goplsPath := *goplsBinaryPath
|
|
|
|
if goplsPath == "" {
|
|
|
|
var err error
|
|
|
|
goplsPath, err = testBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("finding test binary path: %v", err))
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 08:33:58 -07:00
|
|
|
runner = NewTestRunner(NormalModes|SeparateProcess, testTimeout, goplsPath)
|
2020-02-10 09:34:13 -07:00
|
|
|
} else {
|
2020-02-20 08:33:58 -07:00
|
|
|
runner = NewTestRunner(NormalModes, testTimeout, "")
|
2020-02-10 09:34:13 -07:00
|
|
|
}
|
|
|
|
code := m.Run()
|
|
|
|
runner.Close()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBinaryPath() (string, error) {
|
|
|
|
pth := os.Args[0]
|
|
|
|
if !filepath.IsAbs(pth) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err == nil {
|
|
|
|
return "", fmt.Errorf("os.Getwd: %v", err)
|
|
|
|
}
|
|
|
|
pth = filepath.Join(cwd, pth)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(pth); err != nil {
|
|
|
|
return "", fmt.Errorf("os.Stat: %v", err)
|
|
|
|
}
|
|
|
|
return pth, nil
|
2020-02-06 17:50:37 -07:00
|
|
|
}
|