Debian Codegraph @

go/analysis/unitchecker/vet_std_test.go

raw symbols SCIP
1 // Copyright 2023 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 unitchecker_test
6
7 import (
8 "go/version"
9 "os"
10 "os/exec"
11 "runtime"
12 "slices"
13 "strings"
14 "testing"
15
16 "golang.org/x/tools/go/analysis"
17 "golang.org/x/tools/go/analysis/passes/unreachable"
18 "golang.org/x/tools/go/analysis/passes/unsafeptr"
19 "golang.org/x/tools/go/analysis/suite/vet"
20 "golang.org/x/tools/go/analysis/unitchecker"
21 )
22
23 // vetmain is the entrypoint of this executable when ENTRYPOINT=vet.
24 func vetmain() {
25 suite := slices.Clone(vet.Suite)
26 suite = slices.DeleteFunc(suite, func(a *analysis.Analyzer) bool {
27 // This logic mirrors code in cmd/go/internal/work.Builder.vet
28 // to tailor the default analyzer suite used by go vet/test in GOROOT.
29 // (See https://go.dev/issue/79622.)
30 return a == unsafeptr.Analyzer || a == unreachable.Analyzer
31 })
32
33 unitchecker.Main(suite...)
34 }
35
36 // TestVetStdlib runs the same analyzers as the actual vet over the
37 // standard library, using go vet and unitchecker, to ensure that
38 // there are no findings.
39 func TestVetStdlib(t *testing.T) {
40 if testing.Short() {
41 t.Skip("skipping in -short mode")
42 }
43 if builder := os.Getenv("GO_BUILDER_NAME"); builder != "" && !strings.HasPrefix(builder, "x_tools-gotip-") {
44 // Run on builders like x_tools-gotip-linux-amd64-longtest,
45 // skip on others like x_tools-go1.24-linux-amd64-longtest.
46 t.Skipf("This test is only wanted on development branches where code can be easily fixed. Skipping on non-gotip builder %q.", builder)
47 } else if v := runtime.Version(); !strings.Contains(v, "devel") || version.Compare(v, version.Lang(v)) != 0 {
48 // Run on versions like "go1.25-devel_9ce47e66e8 Wed Mar 26 03:48:50 2025 -0700",
49 // skip on others like "go1.24.2" or "go1.24.2-devel_[…]".
50 t.Skipf("This test is only wanted on development versions where code can be easily fixed. Skipping on non-gotip version %q.", v)
51 }
52
53 cmd := exec.Command("go", "vet", "-vettool="+os.Args[0], "std")
54 cmd.Env = append(os.Environ(), "ENTRYPOINT=vet")
55 if out, err := cmd.CombinedOutput(); err != nil {
56 t.Errorf("go vet std failed (%v):\n%s", err, out)
57 }
58 }