Skip to content

Commit 41e925b

Browse files
committed
testing: replace all GOOS-specific path separators in TempDir
For GOOS=windows the path separator characters '\' and ':' also need be replaced. Updates #38465 Change-Id: If7c8cf93058c87d7df6cda140e82fd76578fe699 Reviewed-on: https://go-review.googlesource.com/c/go/+/229837 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 49f10f3 commit 41e925b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/testing/testing.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,11 @@ func (c *common) Cleanup(f func()) {
797797
}
798798
}
799799

800+
var (
801+
rOnce sync.Once
802+
r *strings.Replacer
803+
)
804+
800805
// TempDir returns a temporary directory for the test to use.
801806
// It is lazily created on first access, and calls t.Fatal if the directory
802807
// creation fails.
@@ -809,7 +814,10 @@ func (c *common) TempDir() string {
809814

810815
// ioutil.TempDir doesn't like path separators in its pattern,
811816
// so mangle the name to accommodate subtests.
812-
pattern := strings.ReplaceAll(c.Name(), "/", "_")
817+
rOnce.Do(func() {
818+
r = strings.NewReplacer("/", "_", "\\", "_", ":", "_")
819+
})
820+
pattern := r.Replace(c.Name())
813821

814822
c.tempDir, c.tempDirErr = ioutil.TempDir("", pattern)
815823
if c.tempDirErr == nil {

src/testing/testing_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func TestMain(m *testing.M) {
2121
func TestTempDir(t *testing.T) {
2222
testTempDir(t)
2323
t.Run("InSubtest", testTempDir)
24+
t.Run("test/subtest", testTempDir)
25+
t.Run("test\\subtest", testTempDir)
26+
t.Run("test:subtest", testTempDir)
27+
t.Run("test/..", testTempDir)
28+
t.Run("../test", testTempDir)
2429
}
2530

2631
func testTempDir(t *testing.T) {

0 commit comments

Comments
 (0)