Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
seankhliao opened this issue Jan 29, 2025 · 4 comments
Closed
Labels
Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program.
Milestone

Comments

@seankhliao
Copy link
Member

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.

@gopherbot gopherbot added this to the Proposal milestone Jan 29, 2025
@seankhliao seankhliao changed the title proposal: x/tools/go/analysis/testchecker: proposal: x/tools/go/analysis/testchecker: analysis driver for tests Jan 29, 2025
@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Jan 29, 2025
@seankhliao
Copy link
Member Author

I think I should fold this into #61336

@adonovan
Copy link
Member

Interesting timing: we have been having a lot of conversations recently about the possibility of dynamically loaded analyzers that live in the module, enabling projects to define their own rules, either for internal housekeeping or to help their users detect misuse of their APIs. If done right, merely reading code that depends on package P would cause your editor to highlight misuses of P's API. Obviously there are a lot of technical problems to solve--compiling or interpreting; security; performance; UX; accessibility of the notation to non-experts--but a dynamic system would also be a solution to this problem.

@seankhliao
Copy link
Member Author

I also saw a small ecosystem around linting terraform providers (which are usually a lot of crud code to turn declarative IaC to api calls) built around https://github.com/bflad/tfproviderlint
This seems to extend into the users part you mention.

Otherwise, most of what I've seen is around project internal housekeeping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program.
Projects
None yet
Development

No branches or pull requests

4 participants