Skip to content

testing: allow specify temp dir by GOTMPDIR environment variable #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/cmd/go/internal/help/helpdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,10 @@ General-purpose environment variables:
The name of checksum database to use and optionally its public key and
URL. See https://golang.org/ref/mod#authenticating.
GOTMPDIR
The directory where the go command will write
temporary source files, packages, and binaries.
Temporary directory used by the go command and testing package.
Overrides the platform-specific temporary directory such as "/tmp".
The go command and testing package will write temporary source files,
packages, and binaries here.
GOTOOLCHAIN
Controls which Go toolchain is used. See https://go.dev/doc/toolchain.
GOVCS
Expand Down
4 changes: 3 additions & 1 deletion src/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,8 @@ func (c *common) Cleanup(f func()) {
// all its subtests complete.
// Each subsequent call to TempDir returns a unique directory;
// if the directory creation fails, TempDir terminates the test by calling Fatal.
// If the environment variable GOTMPDIR is set, the temporary directory will
// be created somewhere beneath it.
func (c *common) TempDir() string {
c.checkFuzzFn("TempDir")
// Use a single parent directory for all the temporary directories
Expand Down Expand Up @@ -1362,7 +1364,7 @@ func (c *common) TempDir() string {
return -1
}
pattern = strings.Map(mapper, pattern)
c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern)
c.tempDir, c.tempDirErr = os.MkdirTemp(os.Getenv("GOTMPDIR"), pattern)
if c.tempDirErr == nil {
c.Cleanup(func() {
if err := removeAll(c.tempDir); err != nil {
Expand Down
33 changes: 33 additions & 0 deletions src/testing/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ func testTempDir(t *testing.T) {
}
}

func TestTempDirGOTMPDIR(t *testing.T) {
// The first call to t.TempDir will create a parent temporary directory
// that will contain all temporary directories created by TempDir.
//
// Use os.TempDir (not t.TempDir) to get a temporary directory,
// set GOTMPDIR to that directory,
// and then verify that t.TempDir creates a directory in GOTMPDIR.
customTmpDir := filepath.Join(os.TempDir(), "custom-gotmpdir-test")
if err := os.MkdirAll(customTmpDir, 0777); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(customTmpDir)

t.Setenv("GOTMPDIR", customTmpDir)

dir := t.TempDir()
if dir == "" {
t.Fatal("expected dir")
}

if !strings.HasPrefix(dir, customTmpDir) {
t.Errorf("TempDir did not use GOTMPDIR: got %q, want prefix %q", dir, customTmpDir)
}

fi, err := os.Stat(dir)
if err != nil {
t.Fatal(err)
}
if !fi.IsDir() {
t.Errorf("dir %q is not a dir", dir)
}
}

func TestSetenv(t *testing.T) {
tests := []struct {
name string
Expand Down