-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Description
Proposal Details
I propose a new helper for running x/tools/go/analysis/passes over the files in a module, and reporting the results as a test pass/fail.
// Test runs the given analyzers against the given package patterns.
// Each package matching the pattern and analyzer is reported as a sub test,
// in the format "<analyzer.Name>/<package path>".
// It fails the subtest if there are any findings.
func Test(t *testing.T, analyzers []*analysis.Analyzer, patterns []string)Example usage:
import (
"testing"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/appends"
"golang.org/x/tools/go/analysis/passes/asmdecl"
"golang.org/x/tools/go/analysis/testchecker"
)
func TestLint(t *testint.T) {
analyzers := []*analysis.Analyzer{
appends.Analyzer,
asmdecl.Analyzer,
}
testchecker.Test(t, analyzers, "./...")
}Go has a rich ecosystem of static analysis, much of which is built on top of x/tools/go/analysis.
While these can aide developers while authoring code, like when integrated into gopls https://github.com/golang/tools/blob/master/gopls/doc/analyzers.md , many projects also enforce a common set of checks during CI.
Today, there are a variety of options to do that, for example, a random sampling:
- this repo
- has a main package built using multichecker
- builds and runs the analyzer in CI
- (I think it's part of some online course, there are many copies of the same name / approach)
- this repo runs analysis binary in a test
- this repo installs and runs an analyzer directly (shadow)
- this repo runs a meta linter in git hooks and again as a ci jon
- this repo runs it in a makefile
Go already runs a set of vet checks by default during go test, these are high confidence checks that apply to everyone. Individual projects can usually be stricter and enforce more checks by default.
I think a helper to directly integrate linters into tests can ease the sprawl of complexity in ensuring projects continue to adhere to their desired quality gates.