github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/cmd/test/check.go (about)

     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.
     4  
     5  package cmdtest
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/april1989/origin-go-tools/internal/lsp/source"
    14  	"github.com/april1989/origin-go-tools/internal/span"
    15  )
    16  
    17  func (r *runner) Diagnostics(t *testing.T, uri span.URI, want []*source.Diagnostic) {
    18  	if len(want) == 1 && want[0].Message == "" {
    19  		return
    20  	}
    21  	if strings.Contains(uri.Filename(), "circular") {
    22  		t.Skip("skipping circular diagnostics tests due to golang/go#36265")
    23  	}
    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") {
    29  		if len(l) == 0 {
    30  			continue
    31  		}
    32  		// parse and reprint to normalize the span
    33  		bits := strings.SplitN(l, ": ", 2)
    34  		if len(bits) == 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)
    38  			if err != nil {
    39  				t.Fatal(err)
    40  			}
    41  			converter := span.NewContentConverter(fname, data)
    42  			s, err := spn.WithPosition(converter)
    43  			if err != nil {
    44  				t.Fatal(err)
    45  			}
    46  			l = fmt.Sprintf("%s: %s", s, strings.TrimSpace(bits[1]))
    47  		}
    48  		got[r.NormalizePrefix(l)] = struct{}{}
    49  	}
    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)
    54  		}
    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") {
    58  			continue
    59  		}
    60  		_, found := got[expect]
    61  		if !found {
    62  			t.Errorf("missing diagnostic %q, %v", expect, got)
    63  		} else {
    64  			delete(got, expect)
    65  		}
    66  	}
    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") {
    70  			continue
    71  		}
    72  		t.Errorf("extra diagnostic %q", extra)
    73  	}
    74  }