2019-02-08 14:16:57 -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.
|
|
|
|
|
2019-09-18 21:51:24 -06:00
|
|
|
package cmdtest
|
2019-02-08 14:16:57 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-08-14 18:12:18 -06:00
|
|
|
"io/ioutil"
|
2019-02-08 14:16:57 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-12 18:54:11 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/cmd"
|
2019-04-16 13:47:48 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2019-02-08 14:16:57 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
"golang.org/x/tools/internal/tool"
|
|
|
|
)
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Diagnostics(t *testing.T, data tests.Diagnostics) {
|
|
|
|
for uri, want := range data {
|
2019-02-08 14:16:57 -07:00
|
|
|
if len(want) == 1 && want[0].Message == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-06 11:51:00 -06:00
|
|
|
fname := uri.Filename()
|
2019-05-07 11:54:07 -06:00
|
|
|
args := []string{"-remote=internal", "check", fname}
|
2019-08-12 18:54:11 -06:00
|
|
|
app := cmd.New("gopls-test", r.data.Config.Dir, r.data.Exported.Config.Env)
|
2019-09-18 21:51:24 -06:00
|
|
|
out := CaptureStdOut(t, func() {
|
2019-09-19 15:50:14 -06:00
|
|
|
_ = tool.Run(r.ctx, app, args)
|
2019-02-08 14:16:57 -07:00
|
|
|
})
|
|
|
|
// parse got into a collection of reports
|
|
|
|
got := map[string]struct{}{}
|
|
|
|
for _, l := range strings.Split(out, "\n") {
|
2019-04-16 13:47:48 -06:00
|
|
|
if len(l) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-08 14:16:57 -07:00
|
|
|
// parse and reprint to normalize the span
|
|
|
|
bits := strings.SplitN(l, ": ", 2)
|
|
|
|
if len(bits) == 2 {
|
|
|
|
spn := span.Parse(strings.TrimSpace(bits[0]))
|
|
|
|
spn = span.New(spn.URI(), spn.Start(), span.Point{})
|
2019-08-14 18:12:18 -06:00
|
|
|
data, err := ioutil.ReadFile(fname)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
converter := span.NewContentConverter(fname, data)
|
|
|
|
s, err := spn.WithPosition(converter)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
l = fmt.Sprintf("%s: %s", s, strings.TrimSpace(bits[1]))
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
|
|
|
got[l] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, diag := range want {
|
2019-08-14 18:12:18 -06:00
|
|
|
expect := fmt.Sprintf("%v:%v:%v: %v", diag.URI.Filename(), diag.Range.Start.Line+1, diag.Range.Start.Character+1, diag.Message)
|
|
|
|
if diag.Range.Start.Character == 0 {
|
|
|
|
expect = fmt.Sprintf("%v:%v: %v", diag.URI.Filename(), diag.Range.Start.Line+1, diag.Message)
|
|
|
|
}
|
2019-02-08 14:16:57 -07:00
|
|
|
_, found := got[expect]
|
|
|
|
if !found {
|
|
|
|
t.Errorf("missing diagnostic %q", expect)
|
|
|
|
} else {
|
|
|
|
delete(got, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for extra, _ := range got {
|
|
|
|
t.Errorf("extra diagnostic %q", extra)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|