Skip to content

Commit 3063382

Browse files
committed
Prefer a new function to avoid clash
1 parent 5f81103 commit 3063382

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

go/analysis/analysistest/analysistest.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,11 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
240240
return r
241241
}
242242

243-
// Run applies an analysis to the packages denoted by the "go list" patterns.
243+
// RunModule applies an analysis to the packages denoted by the "go list" patterns.
244244
//
245-
// It loads the packages from the specified GOPATH-style project
246-
// directory using golang.org/x/tools/go/packages, runs the analysis on
247-
// them, and checks that each analysis emits the expected diagnostics
248-
// and facts specified by the contents of '// want ...' comments in the
245+
// It loads the packages from the module using golang.org/x/tools/go/packages,
246+
// runs the analysis on them, and checks that each analysis emits the expected
247+
// diagnostics and facts specified by the contents of '// want ...' comments in the
249248
// package's source files. It treats a comment of the form
250249
// "//...// want..." or "/*...// want... */" as if it starts at 'want'
251250
//
@@ -278,7 +277,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
278277
// Run also returns a Result for each package for which analysis was
279278
// attempted, even if unsuccessful. It is safe for a test to ignore all
280279
// the results, but a test may use it to perform additional checks.
281-
func Run(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Result {
280+
func RunModule(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Result {
282281
if t, ok := t.(testing.TB); ok {
283282
testenv.NeedsGoPackages(t)
284283
}
@@ -300,12 +299,20 @@ func Run(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Res
300299
return results
301300
}
302301

302+
// Run applies an analysis to the packages denoted by the "go list" patterns.
303+
// It performs the same analysis as [RunModule], but uses GOPATH instead.
304+
func Run(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Result {
305+
os.Setenv("GOPATH", dir)
306+
os.Setenv("GO111MODULE", "off")
307+
os.Setenv("GOPROXY", "off")
308+
return RunModule(t, dir, a, patterns...)
309+
}
310+
303311
// A Result holds the result of applying an analyzer to a package.
304312
type Result = checker.TestAnalyzerResult
305313

306314
// loadPackages uses go/packages to load a specified packages (from source, with
307-
// dependencies) from dir, which is the root of a GOPATH-style project
308-
// tree. It returns an error if any package had an error, or the pattern
315+
// dependencies) from dir. It returns an error if any package had an error, or the pattern
309316
// matched no packages.
310317
func loadPackages(a *analysis.Analyzer, dir string, patterns ...string) ([]*packages.Package, error) {
311318
// packages.Load loads the real standard library, not a minimal

go/analysis/analysistest/analysistest_test.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,30 @@ package analysistest_test
66

77
import (
88
"fmt"
9-
"log"
109
"os"
1110
"reflect"
1211
"strings"
1312
"testing"
1413

14+
"golang.org/x/tools/go/analysis"
1515
"golang.org/x/tools/go/analysis/analysistest"
1616
"golang.org/x/tools/go/analysis/passes/findcall"
1717
"golang.org/x/tools/internal/testenv"
1818
)
1919

20-
func init() {
20+
// TestTheTest tests the analysistest testing infrastructure.
21+
func TestTheTest(t *testing.T) {
22+
testenv.NeedsTool(t, "go")
23+
2124
// This test currently requires GOPATH mode.
2225
// Explicitly disabling module mode should suffice, but
2326
// we'll also turn off GOPROXY just for good measure.
2427
if err := os.Setenv("GO111MODULE", "off"); err != nil {
25-
log.Fatal(err)
28+
t.Fatal(err)
2629
}
2730
if err := os.Setenv("GOPROXY", "off"); err != nil {
28-
log.Fatal(err)
31+
t.Fatal(err)
2932
}
30-
}
31-
32-
// TestTheTest tests the analysistest testing infrastructure.
33-
func TestTheTest(t *testing.T) {
34-
testenv.NeedsTool(t, "go")
3533

3634
// We'll simulate a partly failing test of the findcall analysis,
3735
// which (by default) reports calls to functions named 'println'.
@@ -160,3 +158,12 @@ type errorfunc func(string)
160158
func (f errorfunc) Errorf(format string, args ...interface{}) {
161159
f(fmt.Sprintf(format, args...))
162160
}
161+
162+
func TestModule(t *testing.T) {
163+
// the analyzer does nothing, but it will fail if the module cannot be loaded
164+
var happyAnalyzer = &analysis.Analyzer{
165+
Name: "happy",
166+
Run: func(p *analysis.Pass) (interface{}, error) { return nil, nil }}
167+
168+
analysistest.RunModule(t, analysistest.TestData(), happyAnalyzer, "golang.org/x/tools/go/analysis/analysistest/testdata/mod")
169+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package mod

0 commit comments

Comments
 (0)