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
|
|
|
|
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-19 08:18:21 -07:00
|
|
|
resetExitFuncs := lsprpc.OverrideExitFuncsForTest()
|
|
|
|
defer resetExitFuncs()
|
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
|
|
|
}
|