1
0
mirror of https://github.com/golang/go synced 2024-11-19 04:44:41 -07:00
go/internal/lsp/cmd/test/check.go
Ian Cottrell 0ae87fff1b internal/lsp: fix a race in the command line tests
except the race was a symptom of a larger problem, so the fix
actually invovles cleaning up the way we run command line tests
totally to have common shared infrastructure, and also to clean up
the way we handle errors and paths into the temporary directory

Fixes: golang/go#35436
Change-Id: I4c5602607bb70e082056132baa3d4b0f8df6b13b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208269
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-11-22 16:15:56 +00:00

72 lines
2.0 KiB
Go

// 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
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
)
func (r *runner) Diagnostics(t *testing.T, uri span.URI, want []source.Diagnostic) {
if len(want) == 1 && want[0].Message == "" {
return
}
fname := uri.Filename()
out, _ := r.RunGoplsCmd(t, "check", fname)
// parse got into a collection of reports
got := map[string]struct{}{}
for _, l := range strings.Split(out, "\n") {
if len(l) == 0 {
continue
}
// 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{})
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]))
}
got[r.NormalizePrefix(l)] = struct{}{}
}
for _, diag := range want {
expect := fmt.Sprintf("%v:%v:%v: %v", 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", uri.Filename(), diag.Range.Start.Line+1, diag.Message)
}
expect = r.NormalizePrefix(expect)
// Skip the badimport test for now, until we do a better job with diagnostic ranges.
if strings.Contains(uri.Filename(), "badimport") {
continue
}
_, found := got[expect]
if !found {
t.Errorf("missing diagnostic %q, %v", expect, got)
} else {
delete(got, expect)
}
}
for extra := range got {
// Skip the badimport test for now, until we do a better job with diagnostic ranges.
if strings.Contains(extra, "badimport") {
continue
}
t.Errorf("extra diagnostic %q", extra)
}
}