|
5 | 5 | package moddeps_test
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "bytes" |
8 | 9 | "encoding/json"
|
9 | 10 | "fmt"
|
10 | 11 | "internal/testenv"
|
@@ -123,10 +124,38 @@ func TestAllDependencies(t *testing.T) {
|
123 | 124 | t.Skip("skipping because a diff command with support for --recursive and --unified flags is unavailable")
|
124 | 125 | }
|
125 | 126 |
|
| 127 | + // We're going to check the standard modules for tidiness, so we need a usable |
| 128 | + // GOMODCACHE. If the default directory doesn't exist, use a temporary |
| 129 | + // directory instead. (That can occur, for example, when running under |
| 130 | + // run.bash with GO_TEST_SHORT=0: run.bash sets GOPATH=/nonexist-gopath, and |
| 131 | + // GO_TEST_SHORT=0 causes it to run this portion of the test.) |
| 132 | + var modcacheEnv []string |
| 133 | + { |
| 134 | + out, err := exec.Command(goBin, "env", "GOMODCACHE").Output() |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("%s env GOMODCACHE: %v", goBin, err) |
| 137 | + } |
| 138 | + modcacheOk := false |
| 139 | + if gomodcache := string(bytes.TrimSpace(out)); gomodcache != "" { |
| 140 | + if _, err := os.Stat(gomodcache); err == nil { |
| 141 | + modcacheOk = true |
| 142 | + } |
| 143 | + } |
| 144 | + if !modcacheOk { |
| 145 | + modcacheEnv = []string{ |
| 146 | + "GOMODCACHE=" + t.TempDir(), |
| 147 | + "GOFLAGS=" + os.Getenv("GOFLAGS") + " -modcacherw", // Allow t.TempDir() to clean up subdirectories. |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
126 | 152 | // Build the bundle binary at the golang.org/x/tools
|
127 | 153 | // module version specified in GOROOT/src/cmd/go.mod.
|
128 | 154 | bundleDir := t.TempDir()
|
129 |
| - r := runner{Dir: filepath.Join(runtime.GOROOT(), "src/cmd")} |
| 155 | + r := runner{ |
| 156 | + Dir: filepath.Join(runtime.GOROOT(), "src/cmd"), |
| 157 | + Env: append(os.Environ(), modcacheEnv...), |
| 158 | + } |
130 | 159 | r.run(t, goBin, "build", "-mod=readonly", "-o", bundleDir, "golang.org/x/tools/cmd/bundle")
|
131 | 160 |
|
132 | 161 | var gorootCopyDir string
|
@@ -160,7 +189,7 @@ func TestAllDependencies(t *testing.T) {
|
160 | 189 | }
|
161 | 190 | r := runner{
|
162 | 191 | Dir: filepath.Join(gorootCopyDir, rel),
|
163 |
| - Env: append(os.Environ(), |
| 192 | + Env: append(append(os.Environ(), modcacheEnv...), |
164 | 193 | // Set GOROOT.
|
165 | 194 | "GOROOT="+gorootCopyDir,
|
166 | 195 | // Explicitly override PWD and clear GOROOT_FINAL so that GOROOT=gorootCopyDir is definitely used.
|
|
0 commit comments