github.com/v2fly/tools@v0.100.0/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/v2fly/tools/internal/lsp/source"
    14  	"github.com/v2fly/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  	fname := uri.Filename()
    22  	out, _ := r.runGoplsCmd(t, "check", fname)
    23  	// parse got into a collection of reports
    24  	got := map[string]struct{}{}
    25  	for _, l := range strings.Split(out, "\n") {
    26  		if len(l) == 0 {
    27  			continue
    28  		}
    29  		// parse and reprint to normalize the span
    30  		bits := strings.SplitN(l, ": ", 2)
    31  		if len(bits) == 2 {
    32  			spn := span.Parse(strings.TrimSpace(bits[0]))
    33  			spn = span.New(spn.URI(), spn.Start(), span.Point{})
    34  			data, err := ioutil.ReadFile(fname)
    35  			if err != nil {
    36  				t.Fatal(err)
    37  			}
    38  			converter := span.NewContentConverter(fname, data)
    39  			s, err := spn.WithPosition(converter)
    40  			if err != nil {
    41  				t.Fatal(err)
    42  			}
    43  			l = fmt.Sprintf("%s: %s", s, strings.TrimSpace(bits[1]))
    44  		}
    45  		got[r.NormalizePrefix(l)] = struct{}{}
    46  	}
    47  	for _, diag := range want {
    48  		expect := fmt.Sprintf("%v:%v:%v: %v", uri.Filename(), diag.Range.Start.Line+1, diag.Range.Start.Character+1, diag.Message)
    49  		if diag.Range.Start.Character == 0 {
    50  			expect = fmt.Sprintf("%v:%v: %v", uri.Filename(), diag.Range.Start.Line+1, diag.Message)
    51  		}
    52  		expect = r.NormalizePrefix(expect)
    53  		_, found := got[expect]
    54  		if !found {
    55  			t.Errorf("missing diagnostic %q, %v", expect, got)
    56  		} else {
    57  			delete(got, expect)
    58  		}
    59  	}
    60  	for extra := range got {
    61  		t.Errorf("extra diagnostic %q", extra)
    62  	}
    63  }