Skip to content

Commit e9188f8

Browse files
committed
go/analysis/analysistest: Validate analyzers in Run
The standalone analysis drivers (e.g. {single,multi,unit}checker) all call Validate immediately within main, but somehow we forgot to do this within analysistest, leading one user practicing test-first development to get confused. This change adds the missing assertion. Fixes golang/go#59164 Change-Id: I02c316ff1dae0a345e80b488c012707a8d0f93ce Reviewed-on: https://go-review.googlesource.com/c/tools/+/479737 Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent cc511f4 commit e9188f8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

go/analysis/analysistest/analysistest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ func Run(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Res
289289
return nil
290290
}
291291

292+
if err := analysis.Validate([]*analysis.Analyzer{a}); err != nil {
293+
t.Errorf("Validate: %v", err)
294+
return nil
295+
}
296+
292297
results := checker.TestAnalyzer(a, pkgs)
293298
for _, result := range results {
294299
if result.Err != nil {

go/analysis/internal/checker/checker.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func RegisterFlags() {
7575
// Run loads the packages specified by args using go/packages,
7676
// then applies the specified analyzers to them.
7777
// Analysis flags must already have been set.
78+
// Analyzers must be valid according to [analysis.Validate].
7879
// It provides most of the logic for the main functions of both the
7980
// singlechecker and the multi-analysis commands.
8081
// It returns the appropriate exit code.
@@ -142,16 +143,19 @@ func Run(args []string, analyzers []*analysis.Analyzer) (exitcode int) {
142143
// TODO: filter analyzers based on RunDespiteError?
143144
}
144145

145-
// Print the results.
146+
// Run the analysis.
146147
roots := analyze(initial, analyzers)
147148

149+
// Apply fixes.
148150
if Fix {
149151
if err := applyFixes(roots); err != nil {
150152
// Fail when applying fixes failed.
151153
log.Print(err)
152154
return 1
153155
}
154156
}
157+
158+
// Print the results.
155159
return printDiagnostics(roots)
156160
}
157161

@@ -211,8 +215,9 @@ func loadingError(initial []*packages.Package) error {
211215
return err
212216
}
213217

214-
// TestAnalyzer applies an analysis to a set of packages (and their
218+
// TestAnalyzer applies an analyzer to a set of packages (and their
215219
// dependencies if necessary) and returns the results.
220+
// The analyzer must be valid according to [analysis.Validate].
216221
//
217222
// Facts about pkg are returned in a map keyed by object; package facts
218223
// have a nil key.

0 commit comments

Comments
 (0)