Skip to content

proposal: x/tools/go/analysis/testchecker: analysis driver for tests #71478

@seankhliao

Description

@seankhliao

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProposalToolProposalIssues describing a requested change to a Go tool or command-line program.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions