]> Cypherpunks repositories - gostls13.git/commitdiff
testing: make TempDir work for subtests
authorAndrei Tudor Călin <mail@acln.ro>
Thu, 23 Apr 2020 01:04:33 +0000 (04:04 +0300)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 23 Apr 2020 01:34:16 +0000 (01:34 +0000)
ioutil.TempDir doesn't like path separators in its pattern. Modify
(*common).TempDir to replace path separators with underscores before
using the test name as a pattern for ioutil.TempDir.

Fixes #38465.

Change-Id: I9e8ae48b99648b2bf9f561762e845165aff01972
Reviewed-on: https://go-review.googlesource.com/c/go/+/229399
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/testing/testing.go
src/testing/testing_test.go

index d546f56478c4d759d2dd1f98736a9d46ab470b53..44bccd384ed8df6ef8fe780b17832abfa6c8487f 100644 (file)
@@ -806,7 +806,12 @@ func (c *common) Cleanup(f func()) {
 func (c *common) TempDir() string {
        c.tempDirOnce.Do(func() {
                c.Helper()
-               c.tempDir, c.tempDirErr = ioutil.TempDir("", c.Name())
+
+               // ioutil.TempDir doesn't like path separators in its pattern,
+               // so mangle the name to accommodate subtests.
+               pattern := strings.ReplaceAll(c.Name(), "/", "_")
+
+               c.tempDir, c.tempDirErr = ioutil.TempDir("", pattern)
                if c.tempDirErr == nil {
                        c.Cleanup(func() {
                                if err := os.RemoveAll(c.tempDir); err != nil {
index afb35a96d4522e000bfbea46335f17fbb9c29147..07757a8482f2b31611a8f6fdaa132246bee5fbcd 100644 (file)
@@ -19,6 +19,11 @@ func TestMain(m *testing.M) {
 }
 
 func TestTempDir(t *testing.T) {
+       testTempDir(t)
+       t.Run("InSubtest", testTempDir)
+}
+
+func testTempDir(t *testing.T) {
        dirCh := make(chan string, 1)
        t.Cleanup(func() {
                // Verify directory has been removed.