|
| 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 | + "os" |
| 9 | + "os/exec" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "golang.org/x/tools/go/analysis/passes/asmdecl" |
| 15 | + "golang.org/x/tools/go/analysis/passes/assign" |
| 16 | + "golang.org/x/tools/go/analysis/passes/atomic" |
| 17 | + "golang.org/x/tools/go/analysis/passes/bools" |
| 18 | + "golang.org/x/tools/go/analysis/passes/buildtag" |
| 19 | + "golang.org/x/tools/go/analysis/passes/cgocall" |
| 20 | + "golang.org/x/tools/go/analysis/passes/composite" |
| 21 | + "golang.org/x/tools/go/analysis/passes/copylock" |
| 22 | + "golang.org/x/tools/go/analysis/passes/directive" |
| 23 | + "golang.org/x/tools/go/analysis/passes/errorsas" |
| 24 | + "golang.org/x/tools/go/analysis/passes/framepointer" |
| 25 | + "golang.org/x/tools/go/analysis/passes/httpresponse" |
| 26 | + "golang.org/x/tools/go/analysis/passes/ifaceassert" |
| 27 | + "golang.org/x/tools/go/analysis/passes/loopclosure" |
| 28 | + "golang.org/x/tools/go/analysis/passes/lostcancel" |
| 29 | + "golang.org/x/tools/go/analysis/passes/nilfunc" |
| 30 | + "golang.org/x/tools/go/analysis/passes/printf" |
| 31 | + "golang.org/x/tools/go/analysis/passes/shift" |
| 32 | + "golang.org/x/tools/go/analysis/passes/sigchanyzer" |
| 33 | + "golang.org/x/tools/go/analysis/passes/stdmethods" |
| 34 | + "golang.org/x/tools/go/analysis/passes/stringintconv" |
| 35 | + "golang.org/x/tools/go/analysis/passes/structtag" |
| 36 | + "golang.org/x/tools/go/analysis/passes/testinggoroutine" |
| 37 | + "golang.org/x/tools/go/analysis/passes/tests" |
| 38 | + "golang.org/x/tools/go/analysis/passes/timeformat" |
| 39 | + "golang.org/x/tools/go/analysis/passes/unmarshal" |
| 40 | + "golang.org/x/tools/go/analysis/passes/unreachable" |
| 41 | + "golang.org/x/tools/go/analysis/passes/unusedresult" |
| 42 | + "golang.org/x/tools/go/analysis/unitchecker" |
| 43 | +) |
| 44 | + |
| 45 | +// vet is the entrypoint of this executable when ENTRYPOINT=vet. |
| 46 | +// Keep consistent with the actual vet in GOROOT/src/cmd/vet/main.go. |
| 47 | +func vet() { |
| 48 | + unitchecker.Main( |
| 49 | + asmdecl.Analyzer, |
| 50 | + assign.Analyzer, |
| 51 | + atomic.Analyzer, |
| 52 | + bools.Analyzer, |
| 53 | + buildtag.Analyzer, |
| 54 | + cgocall.Analyzer, |
| 55 | + composite.Analyzer, |
| 56 | + copylock.Analyzer, |
| 57 | + directive.Analyzer, |
| 58 | + errorsas.Analyzer, |
| 59 | + framepointer.Analyzer, |
| 60 | + httpresponse.Analyzer, |
| 61 | + ifaceassert.Analyzer, |
| 62 | + loopclosure.Analyzer, |
| 63 | + lostcancel.Analyzer, |
| 64 | + nilfunc.Analyzer, |
| 65 | + printf.Analyzer, |
| 66 | + shift.Analyzer, |
| 67 | + sigchanyzer.Analyzer, |
| 68 | + stdmethods.Analyzer, |
| 69 | + stringintconv.Analyzer, |
| 70 | + structtag.Analyzer, |
| 71 | + tests.Analyzer, |
| 72 | + testinggoroutine.Analyzer, |
| 73 | + timeformat.Analyzer, |
| 74 | + unmarshal.Analyzer, |
| 75 | + unreachable.Analyzer, |
| 76 | + // unsafeptr.Analyzer, // currently reports findings in runtime |
| 77 | + unusedresult.Analyzer, |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +// TestVetStdlib runs the same analyzers as the actual vet over the |
| 82 | +// standard library, using go vet and unitchecker, to ensure that |
| 83 | +// there are no findings. |
| 84 | +func TestVetStdlib(t *testing.T) { |
| 85 | + if testing.Short() { |
| 86 | + t.Skip("skipping in -short mode") |
| 87 | + } |
| 88 | + if version := runtime.Version(); !strings.HasPrefix(version, "devel") { |
| 89 | + t.Skipf("This test is only wanted on development branches where code can be easily fixed. Skipping because runtime.Version=%q.", version) |
| 90 | + } |
| 91 | + |
| 92 | + cmd := exec.Command("go", "vet", "-vettool="+os.Args[0], "std") |
| 93 | + cmd.Env = append(os.Environ(), "ENTRYPOINT=vet") |
| 94 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 95 | + t.Errorf("go vet std failed (%v):\n%s", err, out) |
| 96 | + } |
| 97 | +} |
0 commit comments