sdk: Stop making a special /tmp/consul-test directory for testutil.TempFile and testutil.TempDir (#10494)

This commit is contained in:
R.B. Boyer 2021-06-24 15:46:23 -05:00 committed by GitHub
parent c232a544a4
commit f408c4d387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 24 deletions

View File

@ -1,34 +1,12 @@
package testutil
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
// tmpdir is the base directory for all temporary directories
// and files created with TempDir and TempFile. This could be
// achieved by setting a system environment variable but then
// the test execution would depend on whether or not the
// environment variable is set.
//
// On macOS the temp base directory is quite long and that
// triggers a problem with some tests that bind to UNIX sockets
// where the filename seems to be too long. Using a shorter name
// fixes this and makes the paths more readable.
//
// It also provides a single base directory for cleanup.
var tmpdir = "/tmp/consul-test"
func init() {
if err := os.MkdirAll(tmpdir, 0755); err != nil {
fmt.Printf("Cannot create %s. Reverting to /tmp\n", tmpdir)
tmpdir = "/tmp"
}
}
var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true"
// TempDir creates a temporary directory within tmpdir with the name 'testname-name'.
@ -41,7 +19,7 @@ func TempDir(t testing.TB, name string) string {
}
name = t.Name() + "-" + name
name = strings.Replace(name, "/", "_", -1)
d, err := ioutil.TempDir(tmpdir, name)
d, err := ioutil.TempDir("", name)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -67,7 +45,7 @@ func TempFile(t testing.TB, name string) *os.File {
}
name = t.Name() + "-" + name
name = strings.Replace(name, "/", "_", -1)
f, err := ioutil.TempFile(tmpdir, name)
f, err := ioutil.TempFile("", name)
if err != nil {
t.Fatalf("err: %s", err)
}