diff --git a/src/cmd/internal/moddeps/moddeps_test.go b/src/cmd/internal/moddeps/moddeps_test.go index 41220645c6e..b9ed292cf9b 100644 --- a/src/cmd/internal/moddeps/moddeps_test.go +++ b/src/cmd/internal/moddeps/moddeps_test.go @@ -13,6 +13,8 @@ import ( "io/fs" "os" "path/filepath" + "slices" + "sort" "strings" "sync" "testing" @@ -48,13 +50,15 @@ func TestAllDependencies(t *testing.T) { // This short test does NOT ensure that the vendored contents match // the unmodified contents of the corresponding dependency versions. t.Run(m.Path+"(quick)", func(t *testing.T) { + t.Logf("module %s in directory %s", m.Path, m.Dir) + if m.hasVendor { // Load all of the packages in the module to ensure that their // dependencies are vendored. If any imported package is missing, // 'go list -deps' will fail when attempting to load it. cmd := testenv.Command(t, goBin, "list", "-mod=vendor", "-deps", "./...") - cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Dir = m.Dir + cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Stderr = new(strings.Builder) _, err := cmd.Output() if err != nil { @@ -67,8 +71,8 @@ func TestAllDependencies(t *testing.T) { // There is no vendor directory, so the module must have no dependencies. // Check that the list of active modules contains only the main module. cmd := testenv.Command(t, goBin, "list", "-mod=readonly", "-m", "all") - cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Dir = m.Dir + cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Stderr = new(strings.Builder) out, err := cmd.Output() if err != nil { @@ -170,6 +174,8 @@ func TestAllDependencies(t *testing.T) { } t.Run(m.Path+"(thorough)", func(t *testing.T) { + t.Logf("module %s in directory %s", m.Path, m.Dir) + defer func() { if t.Failed() { // The test failed, which means it's possible the GOROOT copy @@ -189,8 +195,7 @@ func TestAllDependencies(t *testing.T) { Env: append(append(os.Environ(), modcacheEnv...), // Set GOROOT. "GOROOT="+gorootCopyDir, - // Explicitly override PWD and clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used. - "PWD="+filepath.Join(gorootCopyDir, rel), + // Explicitly clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used. "GOROOT_FINAL=", // Add GOROOTcopy/bin and bundleDir to front of PATH. "PATH="+filepath.Join(gorootCopyDir, "bin")+string(filepath.ListSeparator)+ @@ -249,6 +254,7 @@ func packagePattern(modulePath string) string { // deemed safe to share for the purpose of the TestAllDependencies test. func makeGOROOTCopy(t *testing.T) string { t.Helper() + gorootCopyDir := t.TempDir() err := filepath.Walk(testenv.GOROOT(t), func(src string, info os.FileInfo, err error) error { if err != nil { @@ -309,6 +315,7 @@ func makeGOROOTCopy(t *testing.T) string { if err != nil { t.Fatal(err) } + t.Logf("copied GOROOT from %s to %s", testenv.GOROOT(t), gorootCopyDir) return gorootCopyDir } @@ -322,7 +329,10 @@ func (r runner) run(t *testing.T, args ...string) { t.Helper() cmd := testenv.Command(t, args[0], args[1:]...) cmd.Dir = r.Dir - cmd.Env = r.Env + cmd.Env = slices.Clip(r.Env) + if r.Dir != "" { + cmd.Env = append(cmd.Env, "PWD="+r.Dir) + } out, err := cmd.CombinedOutput() if err != nil { t.Logf("> %s\n", strings.Join(args, " ")) @@ -462,8 +472,8 @@ func findGorootModules(t *testing.T) []gorootModule { // Use 'go list' to describe the module contained in this directory (but // not its dependencies). cmd := testenv.Command(t, goBin, "list", "-json", "-m") - cmd.Env = append(os.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Dir = dir + cmd.Env = append(cmd.Environ(), "GO111MODULE=on", "GOWORK=off") cmd.Stderr = new(strings.Builder) out, err := cmd.Output() if err != nil { @@ -507,6 +517,9 @@ func findGorootModules(t *testing.T) []gorootModule { break } } + sort.Slice(goroot.modules, func(i, j int) bool { + return goroot.modules[i].Dir < goroot.modules[j].Dir + }) }) if goroot.err != nil { t.Fatal(goroot.err)