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 (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2019-03-15 09:11:23 -06:00
|
|
|
"runtime"
|
2018-12-14 13:46:12 -07:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages/packagestest"
|
|
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
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 (
|
|
|
|
expectedDefinitionsCount = 25
|
|
|
|
expectedTypeDefinitionsCount = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
type definition struct {
|
|
|
|
src span.Span
|
|
|
|
flags string
|
|
|
|
def span.Span
|
|
|
|
match string
|
|
|
|
}
|
|
|
|
|
|
|
|
type definitions map[span.Span]definition
|
|
|
|
|
2018-12-14 13:46:12 -07:00
|
|
|
var verifyGuru = flag.Bool("verify-guru", false, "Check that the guru compatability matches")
|
|
|
|
|
|
|
|
func TestDefinitionHelpExample(t *testing.T) {
|
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-03-20 08:12:34 -06:00
|
|
|
expect := regexp.MustCompile(`^[\w/\\:_-]+flag[/\\]flag.go:\d+:\d+-\d+: defined here as type flag.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() {
|
|
|
|
tool.Main(context.Background(), &cmd.Application{}, args)
|
|
|
|
})
|
|
|
|
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-03-25 16:30:55 -06:00
|
|
|
func (l definitions) godef(src, def span.Span) {
|
|
|
|
l[src] = definition{
|
|
|
|
src: src,
|
|
|
|
def: def,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l definitions) typdef(src, def span.Span) {
|
|
|
|
l[src] = definition{
|
|
|
|
src: src,
|
|
|
|
def: def,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l definitions) definition(src span.Span, flags string, def span.Span, match string) {
|
|
|
|
l[src] = definition{
|
|
|
|
src: src,
|
|
|
|
flags: flags,
|
|
|
|
def: def,
|
|
|
|
match: match,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l definitions) testDefinitions(t *testing.T, e *packagestest.Exported) {
|
|
|
|
if len(l) != expectedDefinitionsCount {
|
|
|
|
t.Errorf("got %v definitions expected %v", len(l), expectedDefinitionsCount)
|
|
|
|
}
|
|
|
|
for _, d := range l {
|
|
|
|
args := []string{"query"}
|
|
|
|
if d.flags != "" {
|
|
|
|
args = append(args, strings.Split(d.flags, " ")...)
|
|
|
|
}
|
|
|
|
args = append(args, "definition")
|
|
|
|
src := span.New(d.src.URI(), span.NewPoint(0, 0, d.src.Start().Offset()), span.Point{})
|
|
|
|
args = append(args, fmt.Sprint(src))
|
|
|
|
app := &cmd.Application{}
|
|
|
|
app.Config = *e.Config
|
|
|
|
got := captureStdOut(t, func() {
|
|
|
|
tool.Main(context.Background(), app, args)
|
|
|
|
})
|
|
|
|
if d.match == "" {
|
|
|
|
expect := fmt.Sprint(d.def)
|
|
|
|
if !strings.HasPrefix(got, expect) {
|
|
|
|
t.Errorf("definition %v\nexpected:\n%s\ngot:\n%s", args, expect, got)
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
2019-03-25 16:30:55 -06:00
|
|
|
} else {
|
|
|
|
expect := os.Expand(d.match, func(name string) string {
|
2018-12-14 13:46:12 -07:00
|
|
|
switch name {
|
|
|
|
case "file":
|
2019-03-25 16:30:55 -06:00
|
|
|
fname, _ := d.def.URI().Filename()
|
2019-03-22 14:43:20 -06:00
|
|
|
return fname
|
2018-12-14 13:46:12 -07:00
|
|
|
case "efile":
|
2019-03-25 16:30:55 -06:00
|
|
|
fname, _ := d.def.URI().Filename()
|
2019-03-22 14:43:20 -06:00
|
|
|
qfile := strconv.Quote(fname)
|
2018-12-14 13:46:12 -07:00
|
|
|
return qfile[1 : len(qfile)-1]
|
2019-02-19 19:11:15 -07:00
|
|
|
case "euri":
|
2019-03-25 16:30:55 -06:00
|
|
|
quri := strconv.Quote(string(d.def.URI()))
|
2019-02-19 19:11:15 -07:00
|
|
|
return quri[1 : len(quri)-1]
|
2018-12-14 13:46:12 -07:00
|
|
|
case "line":
|
2019-03-25 16:30:55 -06:00
|
|
|
return fmt.Sprint(d.def.Start().Line())
|
2018-12-14 13:46:12 -07:00
|
|
|
case "col":
|
2019-03-25 16:30:55 -06:00
|
|
|
return fmt.Sprint(d.def.Start().Column())
|
2018-12-14 13:46:12 -07:00
|
|
|
case "offset":
|
2019-03-25 16:30:55 -06:00
|
|
|
return fmt.Sprint(d.def.Start().Offset())
|
2018-12-14 13:46:12 -07:00
|
|
|
case "eline":
|
2019-03-25 16:30:55 -06:00
|
|
|
return fmt.Sprint(d.def.End().Line())
|
2018-12-14 13:46:12 -07:00
|
|
|
case "ecol":
|
2019-03-25 16:30:55 -06:00
|
|
|
return fmt.Sprint(d.def.End().Column())
|
2018-12-14 13:46:12 -07:00
|
|
|
case "eoffset":
|
2019-03-25 16:30:55 -06:00
|
|
|
return fmt.Sprint(d.def.End().Offset())
|
2018-12-14 13:46:12 -07:00
|
|
|
default:
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
})
|
2019-03-25 16:30:55 -06:00
|
|
|
if expect != got {
|
|
|
|
t.Errorf("definition %v\nexpected:\n%s\ngot:\n%s", args, expect, got)
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
if *verifyGuru {
|
2019-03-25 16:30:55 -06:00
|
|
|
moduleMode := e.File(e.Modules[0].Name, "go.mod") != ""
|
2018-12-14 13:46:12 -07:00
|
|
|
var guruArgs []string
|
|
|
|
runGuru := false
|
2019-03-25 16:30:55 -06:00
|
|
|
if !moduleMode {
|
|
|
|
for _, arg := range args {
|
|
|
|
switch {
|
|
|
|
case arg == "query":
|
|
|
|
// just ignore this one
|
|
|
|
case arg == "-json":
|
|
|
|
guruArgs = append(guruArgs, arg)
|
|
|
|
case arg == "-emulate=guru":
|
|
|
|
// if we don't see this one we should not run guru
|
|
|
|
runGuru = true
|
|
|
|
case strings.HasPrefix(arg, "-"):
|
|
|
|
// unknown flag, ignore it
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
guruArgs = append(guruArgs, arg)
|
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if runGuru {
|
|
|
|
cmd := exec.Command("guru", guruArgs...)
|
2019-03-25 16:30:55 -06:00
|
|
|
cmd.Env = e.Config.Env
|
2018-12-14 13:46:12 -07:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not run guru %v: %v\n%s", guruArgs, err, out)
|
|
|
|
} else {
|
|
|
|
guru := strings.TrimSpace(string(out))
|
|
|
|
if !strings.HasPrefix(expect, guru) {
|
|
|
|
t.Errorf("definition %v\nexpected:\n%s\nguru gave:\n%s", args, expect, guru)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 16:30:55 -06:00
|
|
|
}
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 16:30:55 -06:00
|
|
|
func (l definitions) testTypeDefinitions(t *testing.T, e *packagestest.Exported) {
|
|
|
|
if len(l) != expectedTypeDefinitionsCount {
|
|
|
|
t.Errorf("got %v definitions expected %v", len(l), expectedTypeDefinitionsCount)
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|
2019-03-25 16:30:55 -06:00
|
|
|
//TODO: add command line type definition tests when it works
|
2018-12-14 13:46:12 -07:00
|
|
|
}
|