cmd/go: use local loader state in test

This change converts the import test to use a local module loader
state variable, instead of the dependency on global state.

This commit is part of the overall effort to eliminate global
modloader state.

Change-Id: I5687090c160bf64ce36356768f7cf74333fab724
Reviewed-on: https://go-review.googlesource.com/c/go/+/711138
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Ian Alexander 2025-10-10 08:58:31 -04:00
parent ca379b1c56
commit 54e3adc533

View file

@ -56,25 +56,25 @@ var importTests = []struct {
}
func TestQueryImport(t *testing.T) {
loaderstate := NewState()
loaderstate.RootMode = NoRoot
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")
oldAllowMissingModuleImports := allowMissingModuleImports
oldRootMode := LoaderState.RootMode
defer func() {
allowMissingModuleImports = oldAllowMissingModuleImports
LoaderState.RootMode = oldRootMode
}()
allowMissingModuleImports = true
LoaderState.RootMode = NoRoot
ctx := context.Background()
rs := LoadModFile(LoaderState, ctx)
rs := LoadModFile(loaderstate, ctx)
for _, tt := range importTests {
t.Run(strings.ReplaceAll(tt.path, "/", "_"), func(t *testing.T) {
// Note that there is no build list, so Import should always fail.
m, err := queryImport(LoaderState, ctx, tt.path, rs)
m, err := queryImport(loaderstate, ctx, tt.path, rs)
if tt.err == "" {
if err != nil {