]> Cypherpunks repositories - gostls13.git/commitdiff
os: use t.TempDir in TestMkdirTemp, TestCreateTemp
authorKir Kolyshkin <kolyshkin@gmail.com>
Mon, 19 Aug 2024 22:46:15 +0000 (15:46 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 20 Aug 2024 16:06:08 +0000 (16:06 +0000)
This simplifies tests a little bit.

Change-Id: I910e3c97cfd20b26951d2a4909d86b5be06bde56
Reviewed-on: https://go-review.googlesource.com/c/go/+/606899
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/os/tempfile_test.go

index 82f0aabda07fdc6acc9dc40c8d659a6423691368..f2b4ffa7500cac8b4b6ab3e33906ba5324ec2c3d 100644 (file)
@@ -17,13 +17,7 @@ import (
 func TestCreateTemp(t *testing.T) {
        t.Parallel()
 
-       dir, err := MkdirTemp("", "TestCreateTempBadDir")
-       if err != nil {
-               t.Fatal(err)
-       }
-       defer RemoveAll(dir)
-
-       nonexistentDir := filepath.Join(dir, "_not_exists_")
+       nonexistentDir := filepath.Join(t.TempDir(), "_not_exists_")
        f, err := CreateTemp(nonexistentDir, "foo")
        if f != nil || err == nil {
                t.Errorf("CreateTemp(%q, `foo`) = %v, %v", nonexistentDir, f, err)
@@ -57,11 +51,7 @@ func TestCreateTempPattern(t *testing.T) {
 func TestCreateTempBadPattern(t *testing.T) {
        t.Parallel()
 
-       tmpDir, err := MkdirTemp("", t.Name())
-       if err != nil {
-               t.Fatal(err)
-       }
-       defer RemoveAll(tmpDir)
+       tmpDir := t.TempDir()
 
        const sep = string(PathSeparator)
        tests := []struct {
@@ -152,14 +142,8 @@ func TestMkdirTemp(t *testing.T) {
 func TestMkdirTempBadDir(t *testing.T) {
        t.Parallel()
 
-       dir, err := MkdirTemp("", "MkdirTempBadDir")
-       if err != nil {
-               t.Fatal(err)
-       }
-       defer RemoveAll(dir)
-
-       badDir := filepath.Join(dir, "not-exist")
-       _, err = MkdirTemp(badDir, "foo")
+       badDir := filepath.Join(t.TempDir(), "not-exist")
+       _, err := MkdirTemp(badDir, "foo")
        if pe, ok := err.(*fs.PathError); !ok || !IsNotExist(err) || pe.Path != badDir {
                t.Errorf("TempDir error = %#v; want PathError for path %q satisfying IsNotExist", err, badDir)
        }
@@ -168,11 +152,7 @@ func TestMkdirTempBadDir(t *testing.T) {
 func TestMkdirTempBadPattern(t *testing.T) {
        t.Parallel()
 
-       tmpDir, err := MkdirTemp("", t.Name())
-       if err != nil {
-               t.Fatal(err)
-       }
-       defer RemoveAll(tmpDir)
+       tmpDir := t.TempDir()
 
        const sep = string(PathSeparator)
        tests := []struct {