2019-09-18 21:51:24 -06: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 cmdtest contains the test suite for the command line behavior of gopls.
|
|
|
|
package cmdtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-11-21 17:58:38 -07:00
|
|
|
"fmt"
|
2019-09-18 21:51:24 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages/packagestest"
|
2019-11-21 17:58:38 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
2019-09-26 11:56:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-09-18 21:51:24 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2019-09-26 11:56:23 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-11-21 17:58:38 -07:00
|
|
|
"golang.org/x/tools/internal/tool"
|
2019-09-18 21:51:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type runner struct {
|
2019-11-21 17:58:38 -07:00
|
|
|
exporter packagestest.Exporter
|
|
|
|
data *tests.Data
|
|
|
|
ctx context.Context
|
|
|
|
options func(*source.Options)
|
|
|
|
normalizers []normalizer
|
|
|
|
}
|
|
|
|
|
|
|
|
type normalizer struct {
|
|
|
|
path string
|
|
|
|
slashed string
|
|
|
|
escaped string
|
|
|
|
fragment string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunner(exporter packagestest.Exporter, data *tests.Data, ctx context.Context, options func(*source.Options)) *runner {
|
|
|
|
r := &runner{
|
|
|
|
exporter: exporter,
|
|
|
|
data: data,
|
|
|
|
ctx: ctx,
|
|
|
|
options: options,
|
|
|
|
normalizers: make([]normalizer, 0, len(data.Exported.Modules)),
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
// build the path normalizing patterns
|
|
|
|
for _, m := range data.Exported.Modules {
|
|
|
|
for fragment := range m.Files {
|
|
|
|
n := normalizer{
|
|
|
|
path: data.Exported.File(m.Name, fragment),
|
|
|
|
fragment: fragment,
|
|
|
|
}
|
|
|
|
if n.slashed = filepath.ToSlash(n.path); n.slashed == n.path {
|
|
|
|
n.slashed = ""
|
|
|
|
}
|
|
|
|
quoted := strconv.Quote(n.path)
|
|
|
|
if n.escaped = quoted[1 : len(quoted)-1]; n.escaped == n.path {
|
|
|
|
n.escaped = ""
|
|
|
|
}
|
|
|
|
r.normalizers = append(r.normalizers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.CompletionSnippet, placeholders bool, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) UnimportedCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) DeepCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) FuzzyCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-17 09:10:48 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) CaseSensitiveCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-26 05:59:06 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:56:23 -06:00
|
|
|
func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
|
2019-09-18 21:51:24 -06:00
|
|
|
//TODO: add command line completions tests when it works
|
|
|
|
}
|
|
|
|
|
2019-11-21 17:58:38 -07:00
|
|
|
func (r *runner) RunGoplsCmd(t testing.TB, args ...string) (string, string) {
|
|
|
|
rStdout, wStdout, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
oldStdout := os.Stdout
|
|
|
|
rStderr, wStderr, err := os.Pipe()
|
2019-09-18 21:51:24 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
oldStderr := os.Stderr
|
2019-09-18 21:51:24 -06:00
|
|
|
defer func() {
|
2019-11-21 17:58:38 -07:00
|
|
|
os.Stdout = oldStdout
|
|
|
|
os.Stderr = oldStderr
|
|
|
|
wStdout.Close()
|
|
|
|
rStdout.Close()
|
|
|
|
wStderr.Close()
|
|
|
|
rStderr.Close()
|
2019-09-18 21:51:24 -06:00
|
|
|
}()
|
2019-11-21 17:58:38 -07:00
|
|
|
os.Stdout = wStdout
|
|
|
|
os.Stderr = wStderr
|
2019-11-20 12:26:02 -07:00
|
|
|
app := cmd.New("gopls-test", r.data.Config.Dir, r.data.Exported.Config.Env, r.options)
|
2019-11-21 17:58:38 -07:00
|
|
|
err = tool.Run(tests.Context(t),
|
2019-11-20 12:26:02 -07:00
|
|
|
app,
|
2019-11-21 17:58:38 -07:00
|
|
|
append([]string{"-remote=internal"}, args...))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
}
|
|
|
|
wStdout.Close()
|
|
|
|
wStderr.Close()
|
|
|
|
stdout, err := ioutil.ReadAll(rStdout)
|
2019-09-18 21:51:24 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
stderr, err := ioutil.ReadAll(rStderr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return string(stdout), string(stderr)
|
|
|
|
}
|
|
|
|
|
2020-01-21 14:01:59 -07:00
|
|
|
// NormalizeGoplsCmd runs the gopls command and normalizes its output.
|
2019-11-21 17:58:38 -07:00
|
|
|
func (r *runner) NormalizeGoplsCmd(t testing.TB, args ...string) (string, string) {
|
|
|
|
stdout, stderr := r.RunGoplsCmd(t, args...)
|
|
|
|
return r.Normalize(stdout), r.Normalize(stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NormalizePrefix normalizes a single path at the front of the input string.
|
|
|
|
func (r *runner) NormalizePrefix(s string) string {
|
|
|
|
for _, n := range r.normalizers {
|
|
|
|
if t := strings.TrimPrefix(s, n.path); t != s {
|
|
|
|
return n.fragment + t
|
|
|
|
}
|
|
|
|
if t := strings.TrimPrefix(s, n.slashed); t != s {
|
|
|
|
return n.fragment + t
|
|
|
|
}
|
|
|
|
if t := strings.TrimPrefix(s, n.escaped); t != s {
|
|
|
|
return n.fragment + t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 17:58:38 -07:00
|
|
|
// Normalize replaces all paths present in s with just the fragment portion
|
2019-09-18 21:51:24 -06:00
|
|
|
// this is used to make golden files not depend on the temporary paths of the files
|
2019-11-21 17:58:38 -07:00
|
|
|
func (r *runner) Normalize(s string) string {
|
2019-09-18 21:51:24 -06:00
|
|
|
type entry struct {
|
|
|
|
path string
|
|
|
|
index int
|
|
|
|
fragment string
|
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
match := make([]entry, 0, len(r.normalizers))
|
2019-09-18 21:51:24 -06:00
|
|
|
// collect the initial state of all the matchers
|
2019-11-21 17:58:38 -07:00
|
|
|
for _, n := range r.normalizers {
|
|
|
|
index := strings.Index(s, n.path)
|
|
|
|
if index >= 0 {
|
|
|
|
match = append(match, entry{n.path, index, n.fragment})
|
|
|
|
}
|
|
|
|
if n.slashed != "" {
|
|
|
|
index := strings.Index(s, n.slashed)
|
2019-09-18 21:51:24 -06:00
|
|
|
if index >= 0 {
|
2019-11-21 17:58:38 -07:00
|
|
|
match = append(match, entry{n.slashed, index, n.fragment})
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
2019-11-21 17:58:38 -07:00
|
|
|
}
|
|
|
|
if n.escaped != "" {
|
|
|
|
index := strings.Index(s, n.escaped)
|
|
|
|
if index >= 0 {
|
|
|
|
match = append(match, entry{n.escaped, index, n.fragment})
|
2019-09-18 21:51:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// result should be the same or shorter than the input
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, len(s)))
|
|
|
|
last := 0
|
|
|
|
for {
|
|
|
|
// find the nearest path match to the start of the buffer
|
|
|
|
next := -1
|
|
|
|
nearest := len(s)
|
|
|
|
for i, c := range match {
|
|
|
|
if c.index >= 0 && nearest > c.index {
|
|
|
|
nearest = c.index
|
|
|
|
next = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there are no matches, we copy the rest of the string and are done
|
|
|
|
if next < 0 {
|
|
|
|
buf.WriteString(s[last:])
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
// we have a match
|
|
|
|
n := &match[next]
|
|
|
|
// copy up to the start of the match
|
|
|
|
buf.WriteString(s[last:n.index])
|
|
|
|
// skip over the filename
|
|
|
|
last = n.index + len(n.path)
|
|
|
|
// add in the fragment instead
|
|
|
|
buf.WriteString(n.fragment)
|
|
|
|
// see what the next match for this path is
|
|
|
|
n.index = strings.Index(s[last:], n.path)
|
|
|
|
if n.index >= 0 {
|
|
|
|
n.index += last
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|