Debian Codegraph @

gopls/internal/settings/vet_test.go

raw symbols SCIP
1 // Copyright 2024 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 settings_test
6
7 import (
8 "encoding/json"
9 "fmt"
10 "os/exec"
11 "strings"
12 "testing"
13
14 "golang.org/x/tools/gopls/internal/doc"
15 "golang.org/x/tools/internal/testenv"
16 )
17
18 // TestVetSuite ensures that gopls's analyser suite is a superset of vet's.
19 //
20 // This test may fail spuriously if gopls/doc/generate.TestGenerated
21 // fails. In that case retry after re-running the JSON generator.
22 func TestVetSuite(t *testing.T) {
23 testenv.NeedsTool(t, "go")
24
25 // Read gopls' suite from the API JSON.
26 goplsAnalyzers := make(map[string]bool)
27 var api doc.API
28 if err := json.Unmarshal([]byte(doc.JSON), &api); err != nil {
29 t.Fatal(err)
30 }
31 for _, a := range api.Analyzers {
32 goplsAnalyzers[a.Name] = true
33 }
34
35 // Read vet's suite by parsing its help message.
36 cmd := exec.Command("go", "tool", "vet", "help")
37 cmd.Stdout = new(strings.Builder)
38 if err := cmd.Run(); err != nil {
39 t.Fatalf("failed to run vet: %v", err)
40 }
41 out := fmt.Sprint(cmd.Stdout)
42 _, out, _ = strings.Cut(out, "Registered analyzers:\n\n")
43 out, _, _ = strings.Cut(out, "\n\n")
44 for line := range strings.SplitSeq(out, "\n") {
45 name := strings.Fields(line)[0]
46 if !goplsAnalyzers[name] {
47 t.Errorf("gopls lacks vet analyzer %q", name)
48 }
49 }
50 }