Skip to content

Commit ba9e1dd

Browse files
aiskgopherbot
authored andcommitted
testing: allow specify temp dir by GOTMPDIR environment variable
Allow change the default temp directory returned by t.TempDir() by environment variable GOTMPDIR. Fixes #61585 Change-Id: Iba969bb02744e106cf15d80e0eda0245a55fc290 GitHub-Last-Rev: aeacea0 GitHub-Pull-Request: #74844 Reviewed-on: https://go-review.googlesource.com/c/go/+/692455 Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Sean Liao <[email protected]> Reviewed-by: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 9f6936b commit ba9e1dd

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/cmd/go/alldocs.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/help/helpdoc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,10 @@ General-purpose environment variables:
573573
The name of checksum database to use and optionally its public key and
574574
URL. See https://golang.org/ref/mod#authenticating.
575575
GOTMPDIR
576-
The directory where the go command will write
577-
temporary source files, packages, and binaries.
576+
Temporary directory used by the go command and testing package.
577+
Overrides the platform-specific temporary directory such as "/tmp".
578+
The go command and testing package will write temporary source files,
579+
packages, and binaries here.
578580
GOTOOLCHAIN
579581
Controls which Go toolchain is used. See https://go.dev/doc/toolchain.
580582
GOVCS

src/testing/testing.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,8 @@ func (c *common) Cleanup(f func()) {
13181318
// all its subtests complete.
13191319
// Each subsequent call to TempDir returns a unique directory;
13201320
// if the directory creation fails, TempDir terminates the test by calling Fatal.
1321+
// If the environment variable GOTMPDIR is set, the temporary directory will
1322+
// be created somewhere beneath it.
13211323
func (c *common) TempDir() string {
13221324
c.checkFuzzFn("TempDir")
13231325
// Use a single parent directory for all the temporary directories
@@ -1362,7 +1364,7 @@ func (c *common) TempDir() string {
13621364
return -1
13631365
}
13641366
pattern = strings.Map(mapper, pattern)
1365-
c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern)
1367+
c.tempDir, c.tempDirErr = os.MkdirTemp(os.Getenv("GOTMPDIR"), pattern)
13661368
if c.tempDirErr == nil {
13671369
c.Cleanup(func() {
13681370
if err := removeAll(c.tempDir); err != nil {

src/testing/testing_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,39 @@ func testTempDir(t *testing.T) {
147147
}
148148
}
149149

150+
func TestTempDirGOTMPDIR(t *testing.T) {
151+
// The first call to t.TempDir will create a parent temporary directory
152+
// that will contain all temporary directories created by TempDir.
153+
//
154+
// Use os.TempDir (not t.TempDir) to get a temporary directory,
155+
// set GOTMPDIR to that directory,
156+
// and then verify that t.TempDir creates a directory in GOTMPDIR.
157+
customTmpDir := filepath.Join(os.TempDir(), "custom-gotmpdir-test")
158+
if err := os.MkdirAll(customTmpDir, 0777); err != nil {
159+
t.Fatal(err)
160+
}
161+
defer os.RemoveAll(customTmpDir)
162+
163+
t.Setenv("GOTMPDIR", customTmpDir)
164+
165+
dir := t.TempDir()
166+
if dir == "" {
167+
t.Fatal("expected dir")
168+
}
169+
170+
if !strings.HasPrefix(dir, customTmpDir) {
171+
t.Errorf("TempDir did not use GOTMPDIR: got %q, want prefix %q", dir, customTmpDir)
172+
}
173+
174+
fi, err := os.Stat(dir)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
if !fi.IsDir() {
179+
t.Errorf("dir %q is not a dir", dir)
180+
}
181+
}
182+
150183
func TestSetenv(t *testing.T) {
151184
tests := []struct {
152185
name string

0 commit comments

Comments
 (0)