2018-12-14 13:46:12 -07:00
|
|
|
// Copyright 2019 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 cmd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2019-03-15 09:11:23 -06:00
|
|
|
"runtime"
|
2018-12-14 13:46:12 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
2019-04-16 13:47:48 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-12-14 13:46:12 -07:00
|
|
|
"golang.org/x/tools/internal/tool"
|
|
|
|
)
|
|
|
|
|
2019-03-25 16:30:55 -06:00
|
|
|
const (
|
2019-04-19 09:10:38 -06:00
|
|
|
expectedDefinitionsCount = 28
|
2019-03-25 16:30:55 -06:00
|
|
|
expectedTypeDefinitionsCount = 2
|
|
|
|
)
|
|
|
|
|
2019-04-29 12:57:27 -06:00
|
|
|
type godefMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
plainGodef = godefMode(1 << iota)
|
|
|
|
jsonGoDef
|
|
|
|
)
|
|
|
|
|
|
|
|
var godefModes = []godefMode{
|
|
|
|
plainGodef,
|
|
|
|
jsonGoDef,
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
|
|
|
|
func TestDefinitionHelpExample(t *testing.T) {
|
2019-06-24 14:34:21 -06:00
|
|
|
// TODO: https://golang.org/issue/32794.
|
|
|
|
t.Skip()
|
2019-03-15 09:11:23 -06:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
t.Skip("not all source files are available on android")
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not get wd: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
thisFile := filepath.Join(dir, "definition.go")
|
2019-03-18 13:07:21 -06:00
|
|
|
baseArgs := []string{"query", "definition"}
|
2019-04-27 20:45:06 -06:00
|
|
|
expect := regexp.MustCompile(`(?s)^[\w/\\:_-]+flag[/\\]flag.go:\d+:\d+-\d+: defined here as FlagSet struct {.*}$`)
|
2019-03-18 12:40:57 -06:00
|
|
|
for _, query := range []string{
|
|
|
|
fmt.Sprintf("%v:%v:%v", thisFile, cmd.ExampleLine, cmd.ExampleColumn),
|
|
|
|
fmt.Sprintf("%v:#%v", thisFile, cmd.ExampleOffset)} {
|
2019-03-18 13:07:21 -06:00
|
|
|
args := append(baseArgs, query)
|
|
|
|
got := captureStdOut(t, func() {
|
2019-09-19 15:50:14 -06:00
|
|
|
_ = tool.Run(tests.Context(t), cmd.New("gopls-test", "", nil), args)
|
2019-03-18 13:07:21 -06:00
|
|
|
})
|
|
|
|
if !expect.MatchString(got) {
|
|
|
|
t.Errorf("test with %v\nexpected:\n%s\ngot:\n%s", args, expect, got)
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Definition(t *testing.T, data tests.Definitions) {
|
2019-06-24 14:34:21 -06:00
|
|
|
// TODO: https://golang.org/issue/32794.
|
|
|
|
t.Skip()
|
2019-04-16 13:47:48 -06:00
|
|
|
for _, d := range data {
|
2019-05-15 15:58:16 -06:00
|
|
|
if d.IsType || d.OnlyHover {
|
|
|
|
// TODO: support type definition, hover queries
|
2019-04-16 13:47:48 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-04-29 12:57:27 -06:00
|
|
|
d.Src = span.New(d.Src.URI(), span.NewPoint(0, 0, d.Src.Start().Offset()), span.Point{})
|
|
|
|
for _, mode := range godefModes {
|
|
|
|
args := []string{"-remote=internal", "query"}
|
|
|
|
tag := d.Name + "-definition"
|
|
|
|
if mode&jsonGoDef != 0 {
|
|
|
|
tag += "-json"
|
|
|
|
args = append(args, "-json")
|
|
|
|
}
|
|
|
|
args = append(args, "definition")
|
|
|
|
uri := d.Src.URI()
|
|
|
|
args = append(args, fmt.Sprint(d.Src))
|
|
|
|
got := captureStdOut(t, func() {
|
2019-08-12 18:54:11 -06:00
|
|
|
app := cmd.New("gopls-test", r.data.Config.Dir, r.data.Exported.Config.Env)
|
2019-09-19 15:50:14 -06:00
|
|
|
_ = tool.Run(r.ctx, app, args)
|
2019-04-29 12:57:27 -06:00
|
|
|
})
|
|
|
|
got = normalizePaths(r.data, got)
|
|
|
|
if mode&jsonGoDef != 0 && runtime.GOOS == "windows" {
|
|
|
|
got = strings.Replace(got, "file:///", "file://", -1)
|
|
|
|
}
|
2019-06-06 11:51:00 -06:00
|
|
|
expect := strings.TrimSpace(string(r.data.Golden(tag, uri.Filename(), func() ([]byte, error) {
|
2019-04-27 20:45:06 -06:00
|
|
|
return []byte(got), nil
|
2019-04-29 12:57:27 -06:00
|
|
|
})))
|
|
|
|
if expect != "" && !strings.HasPrefix(got, expect) {
|
|
|
|
t.Errorf("definition %v failed with %#v expected:\n%q\ngot:\n%q", tag, args, expect, got)
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
2019-03-25 16:30:55 -06:00
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
|
|
|
}
|