1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/tools/internal/lsp/source"
14 "golang.org/x/tools/internal/span"
17 func (r *runner) Diagnostics(t *testing.T, uri span.URI, want []*source.Diagnostic) {
18 if len(want) == 1 && want[0].Message == "" {
21 if strings.Contains(uri.Filename(), "circular") {
22 t.Skip("skipping circular diagnostics tests due to golang/go#36265")
24 fname := uri.Filename()
25 out, _ := r.runGoplsCmd(t, "check", fname)
26 // parse got into a collection of reports
27 got := map[string]struct{}{}
28 for _, l := range strings.Split(out, "\n") {
32 // parse and reprint to normalize the span
33 bits := strings.SplitN(l, ": ", 2)
35 spn := span.Parse(strings.TrimSpace(bits[0]))
36 spn = span.New(spn.URI(), spn.Start(), span.Point{})
37 data, err := ioutil.ReadFile(fname)
41 converter := span.NewContentConverter(fname, data)
42 s, err := spn.WithPosition(converter)
46 l = fmt.Sprintf("%s: %s", s, strings.TrimSpace(bits[1]))
48 got[r.NormalizePrefix(l)] = struct{}{}
50 for _, diag := range want {
51 expect := fmt.Sprintf("%v:%v:%v: %v", uri.Filename(), diag.Range.Start.Line+1, diag.Range.Start.Character+1, diag.Message)
52 if diag.Range.Start.Character == 0 {
53 expect = fmt.Sprintf("%v:%v: %v", uri.Filename(), diag.Range.Start.Line+1, diag.Message)
55 expect = r.NormalizePrefix(expect)
56 // Skip the badimport and import cycle not allowed test for now, until we do a better job with diagnostic ranges.
57 if strings.Contains(uri.Filename(), "badimport") || strings.Contains(expect, "import cycle") {
60 _, found := got[expect]
62 t.Errorf("missing diagnostic %q, %v", expect, got)
67 for extra := range got {
68 // Skip the badimport and import cycle not allowed test for now, until we do a better job with diagnostic ranges.
69 if strings.Contains(extra, "badimport") || strings.Contains(extra, "import cycle") {
72 t.Errorf("extra diagnostic %q", extra)