xtest: add TempDir convenience method
This is more convenient than the existing `xtest.MustTempDir`, the name is chosen based on the test name.
While it is similar to the `testing.T.TempDir` method it allows to manually decide whether cleanup is desired or not.
GitOrigin-RevId:
@@ -20,6 +20,7 @@ import (
"encoding/hex"
"encoding/json"
"flag"
+ "fmt"
"io/ioutil"
"net"
"os"
@@ -126,6 +127,15 @@ func MustTempDir(dir, prefix string) (string, func()) {
}
}
+func TempDir(t testing.TB) (string, func()) {
+ testName := strings.NewReplacer("/", "_", "\\", "_", ":", "_").Replace(t.Name())
+ name, err := ioutil.TempDir("", fmt.Sprintf("%s_*", testName))
+ require.NoError(t, err)
+ return name, func() {
+ os.RemoveAll(name)
+ }
+}
+
// CopyDir copies "from" to "to", using the unix cp command.
func CopyDir(t testing.TB, from, to string) {
t.Helper()
