File: vet_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (50 lines) | stat: -rw-r--r-- 1,374 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2024 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 settings_test

import (
	"encoding/json"
	"fmt"
	"os/exec"
	"strings"
	"testing"

	"golang.org/x/tools/gopls/internal/doc"
	"golang.org/x/tools/internal/testenv"
)

// TestVetSuite ensures that gopls's analyser suite is a superset of vet's.
//
// This test may fail spuriously if gopls/doc/generate.TestGenerated
// fails. In that case retry after re-running the JSON generator.
func TestVetSuite(t *testing.T) {
	testenv.NeedsTool(t, "go")

	// Read gopls' suite from the API JSON.
	goplsAnalyzers := make(map[string]bool)
	var api doc.API
	if err := json.Unmarshal([]byte(doc.JSON), &api); err != nil {
		t.Fatal(err)
	}
	for _, a := range api.Analyzers {
		goplsAnalyzers[a.Name] = true
	}

	// Read vet's suite by parsing its help message.
	cmd := exec.Command("go", "tool", "vet", "help")
	cmd.Stdout = new(strings.Builder)
	if err := cmd.Run(); err != nil {
		t.Fatalf("failed to run vet: %v", err)
	}
	out := fmt.Sprint(cmd.Stdout)
	_, out, _ = strings.Cut(out, "Registered analyzers:\n\n")
	out, _, _ = strings.Cut(out, "\n\n")
	for _, line := range strings.Split(out, "\n") {
		name := strings.Fields(line)[0]
		if !goplsAnalyzers[name] {
			t.Errorf("gopls lacks vet analyzer %q", name)
		}
	}
}