Skip to content

Commit 2e12c5d

Browse files
committed
cmd/go: use local state object in test.runTest
This commit modifies `test.runTest` to construct a new modload.State object using the new constructor instead of the current global `modload.LoaderState` variable. This commit is part of the overall effort to eliminate global modloader state. [git-generate] cd src/cmd/go/internal/test rf ' add test.go:/func runTest\(/-0 var moduleLoaderState *modload.State ex { import "cmd/go/internal/modload"; modload.LoaderState -> moduleLoaderState } add runTest://+0 moduleLoaderState := modload.NewState() rm test.go:/var moduleLoaderState \*modload.State/ ' Change-Id: Ia387160f30818714ab578c5b78713e532cd394df Reviewed-on: https://go-review.googlesource.com/c/go/+/711127 Reviewed-by: Michael Matloob <matloob@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@google.com>
1 parent fe345ff commit 2e12c5d

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/cmd/go/internal/test/test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,9 @@ var defaultVetFlags = []string{
682682
}
683683

684684
func runTest(ctx context.Context, cmd *base.Command, args []string) {
685+
moduleLoaderState := modload.NewState()
685686
pkgArgs, testArgs = testFlags(args)
686-
modload.InitWorkfile(modload.LoaderState) // The test command does custom flag processing; initialize workspaces after that.
687+
modload.InitWorkfile(moduleLoaderState) // The test command does custom flag processing; initialize workspaces after that.
687688

688689
if cfg.DebugTrace != "" {
689690
var close func() error
@@ -704,13 +705,13 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
704705

705706
work.FindExecCmd() // initialize cached result
706707

707-
work.BuildInit(modload.LoaderState)
708+
work.BuildInit(moduleLoaderState)
708709
work.VetFlags = testVet.flags
709710
work.VetExplicit = testVet.explicit
710711
work.VetTool = base.Tool("vet")
711712

712713
pkgOpts := load.PackageOpts{ModResolveTests: true}
713-
pkgs = load.PackagesAndErrors(modload.LoaderState, ctx, pkgOpts, pkgArgs)
714+
pkgs = load.PackagesAndErrors(moduleLoaderState, ctx, pkgOpts, pkgArgs)
714715
// We *don't* call load.CheckPackageErrors here because we want to report
715716
// loading errors as per-package test setup errors later.
716717
if len(pkgs) == 0 {
@@ -736,12 +737,12 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
736737
// the module cache (or permanently alter the behavior of std tests for all
737738
// users) by writing the failing input to the package's testdata directory.
738739
// (See https://golang.org/issue/48495 and test_fuzz_modcache.txt.)
739-
mainMods := modload.LoaderState.MainModules
740+
mainMods := moduleLoaderState.MainModules
740741
if m := pkgs[0].Module; m != nil && m.Path != "" {
741742
if !mainMods.Contains(m.Path) {
742743
base.Fatalf("cannot use -fuzz flag on package outside the main module")
743744
}
744-
} else if pkgs[0].Standard && modload.Enabled(modload.LoaderState) {
745+
} else if pkgs[0].Standard && modload.Enabled(moduleLoaderState) {
745746
// Because packages in 'std' and 'cmd' are part of the standard library,
746747
// they are only treated as part of a module in 'go mod' subcommands and
747748
// 'go get'. However, we still don't want to accidentally corrupt their
@@ -854,7 +855,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
854855
}
855856
}
856857

857-
b := work.NewBuilder("", modload.LoaderState.VendorDirOrEmpty)
858+
b := work.NewBuilder("", moduleLoaderState.VendorDirOrEmpty)
858859
defer func() {
859860
if err := b.Close(); err != nil {
860861
base.Fatal(err)
@@ -872,8 +873,8 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
872873

873874
// Select for coverage all dependencies matching the -coverpkg
874875
// patterns.
875-
plist := load.TestPackageList(modload.LoaderState, ctx, pkgOpts, pkgs)
876-
testCoverPkgs = load.SelectCoverPackages(modload.LoaderState, plist, match, "test")
876+
plist := load.TestPackageList(moduleLoaderState, ctx, pkgOpts, pkgs)
877+
testCoverPkgs = load.SelectCoverPackages(moduleLoaderState, plist, match, "test")
877878
if len(testCoverPkgs) > 0 {
878879
// create a new singleton action that will collect up the
879880
// meta-data files from all of the packages mentioned in
@@ -951,7 +952,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
951952
"testing": true,
952953
"time": true,
953954
}
954-
for _, p := range load.TestPackageList(modload.LoaderState, ctx, pkgOpts, pkgs) {
955+
for _, p := range load.TestPackageList(moduleLoaderState, ctx, pkgOpts, pkgs) {
955956
if !skipInstrumentation[p.ImportPath] {
956957
p.Internal.FuzzInstrument = true
957958
}
@@ -981,7 +982,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
981982
// happens we'll wind up building the Q compile action
982983
// before updating its deps to include sync/atomic).
983984
if cfg.BuildCoverMode == "atomic" && p.ImportPath != "sync/atomic" {
984-
load.EnsureImport(modload.LoaderState, p, "sync/atomic")
985+
load.EnsureImport(moduleLoaderState, p, "sync/atomic")
985986
}
986987
// Tag the package for static meta-data generation if no
987988
// test files (this works only with the new coverage
@@ -1048,7 +1049,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
10481049
reportSetupFailed(firstErrPkg, firstErrPkg.Error)
10491050
continue
10501051
}
1051-
buildTest, runTest, printTest, perr, err := builderTest(modload.LoaderState, b, ctx, pkgOpts, p, allImports[p], writeCoverMetaAct)
1052+
buildTest, runTest, printTest, perr, err := builderTest(moduleLoaderState, b, ctx, pkgOpts, p, allImports[p], writeCoverMetaAct)
10521053
if err != nil {
10531054
reportErr(perr, err)
10541055
reportSetupFailed(perr, err)

0 commit comments

Comments
 (0)