1
0
mirror of https://github.com/golang/go synced 2024-11-26 07:27:59 -07:00

os: replace os.MkdirTemp with T.TempDir

Updates #45402

Change-Id: Ib8e62a13ddff884e4d34b3a0fdc9a10db2b68da6
Reviewed-on: https://go-review.googlesource.com/c/go/+/308109
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Manlio Perillo 2021-04-07 21:27:27 +02:00 committed by Tobias Klauser
parent 8518aac314
commit 4d7d7a4c50
8 changed files with 45 additions and 175 deletions

View File

@ -54,13 +54,7 @@ func testErrNotExist(name string) string {
} }
func TestErrIsNotExist(t *testing.T) { func TestErrIsNotExist(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "_Go_ErrIsNotExist") tmpDir := t.TempDir()
if err != nil {
t.Fatalf("create ErrIsNotExist tempdir: %s", err)
return
}
defer os.RemoveAll(tmpDir)
name := filepath.Join(tmpDir, "NotExists") name := filepath.Join(tmpDir, "NotExists")
if s := testErrNotExist(name); s != "" { if s := testErrNotExist(name); s != "" {
t.Fatal(s) t.Fatal(s)

View File

@ -31,12 +31,7 @@ func TestFifoEOF(t *testing.T) {
t.Skip("skipping on OpenBSD; issue 25877") t.Skip("skipping on OpenBSD; issue 25877")
} }
dir, err := os.MkdirTemp("", "TestFifoEOF") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
fifoName := filepath.Join(dir, "fifo") fifoName := filepath.Join(dir, "fifo")
if err := syscall.Mkfifo(fifoName, 0600); err != nil { if err := syscall.Mkfifo(fifoName, 0600); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -617,11 +617,7 @@ func TestReaddirNValues(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("test.short; skipping") t.Skip("test.short; skipping")
} }
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatalf("TempDir: %v", err)
}
defer RemoveAll(dir)
for i := 1; i <= 105; i++ { for i := 1; i <= 105; i++ {
f, err := Create(filepath.Join(dir, fmt.Sprintf("%d", i))) f, err := Create(filepath.Join(dir, fmt.Sprintf("%d", i)))
if err != nil { if err != nil {
@ -716,11 +712,7 @@ func TestReaddirStatFailures(t *testing.T) {
// testing it wouldn't work. // testing it wouldn't work.
t.Skipf("skipping test on %v", runtime.GOOS) t.Skipf("skipping test on %v", runtime.GOOS)
} }
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatalf("TempDir: %v", err)
}
defer RemoveAll(dir)
touch(t, filepath.Join(dir, "good1")) touch(t, filepath.Join(dir, "good1"))
touch(t, filepath.Join(dir, "x")) // will disappear or have an error touch(t, filepath.Join(dir, "x")) // will disappear or have an error
touch(t, filepath.Join(dir, "good2")) touch(t, filepath.Join(dir, "good2"))
@ -1949,22 +1941,16 @@ func TestAppend(t *testing.T) {
func TestStatDirWithTrailingSlash(t *testing.T) { func TestStatDirWithTrailingSlash(t *testing.T) {
// Create new temporary directory and arrange to clean it up. // Create new temporary directory and arrange to clean it up.
path, err := os.MkdirTemp("", "_TestStatDirWithSlash_") path := t.TempDir()
if err != nil {
t.Fatalf("TempDir: %s", err)
}
defer RemoveAll(path)
// Stat of path should succeed. // Stat of path should succeed.
_, err = Stat(path) if _, err := Stat(path); err != nil {
if err != nil {
t.Fatalf("stat %s failed: %s", path, err) t.Fatalf("stat %s failed: %s", path, err)
} }
// Stat of path+"/" should succeed too. // Stat of path+"/" should succeed too.
path += "/" path += "/"
_, err = Stat(path) if _, err := Stat(path); err != nil {
if err != nil {
t.Fatalf("stat %s failed: %s", path, err) t.Fatalf("stat %s failed: %s", path, err)
} }
} }
@ -2091,12 +2077,7 @@ func TestLargeWriteToConsole(t *testing.T) {
func TestStatDirModeExec(t *testing.T) { func TestStatDirModeExec(t *testing.T) {
const mode = 0111 const mode = 0111
path, err := os.MkdirTemp("", "go-build") path := t.TempDir()
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer RemoveAll(path)
if err := Chmod(path, 0777); err != nil { if err := Chmod(path, 0777); err != nil {
t.Fatalf("Chmod %q 0777: %v", path, err) t.Fatalf("Chmod %q 0777: %v", path, err)
} }
@ -2160,12 +2141,7 @@ func TestStatStdin(t *testing.T) {
func TestStatRelativeSymlink(t *testing.T) { func TestStatRelativeSymlink(t *testing.T) {
testenv.MustHaveSymlink(t) testenv.MustHaveSymlink(t)
tmpdir, err := os.MkdirTemp("", "TestStatRelativeSymlink") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tmpdir)
target := filepath.Join(tmpdir, "target") target := filepath.Join(tmpdir, "target")
f, err := Create(target) f, err := Create(target)
if err != nil { if err != nil {

View File

@ -626,11 +626,7 @@ func TestOpenVolumeName(t *testing.T) {
} }
func TestDeleteReadOnly(t *testing.T) { func TestDeleteReadOnly(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "TestDeleteReadOnly") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
p := filepath.Join(tmpdir, "a") p := filepath.Join(tmpdir, "a")
// This sets FILE_ATTRIBUTE_READONLY. // This sets FILE_ATTRIBUTE_READONLY.
f, err := os.OpenFile(p, os.O_CREATE, 0400) f, err := os.OpenFile(p, os.O_CREATE, 0400)
@ -796,11 +792,7 @@ func compareCommandLineToArgvWithSyscall(t *testing.T, cmd string) {
} }
func TestCmdArgs(t *testing.T) { func TestCmdArgs(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "TestCmdArgs") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
const prog = ` const prog = `
package main package main
@ -815,8 +807,7 @@ func main() {
} }
` `
src := filepath.Join(tmpdir, "main.go") src := filepath.Join(tmpdir, "main.go")
err = os.WriteFile(src, []byte(prog), 0666) if err := os.WriteFile(src, []byte(prog), 0666); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -963,21 +954,14 @@ func TestSymlinkCreation(t *testing.T) {
} }
t.Parallel() t.Parallel()
temp, err := os.MkdirTemp("", "TestSymlinkCreation") temp := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(temp)
dummyFile := filepath.Join(temp, "file") dummyFile := filepath.Join(temp, "file")
err = os.WriteFile(dummyFile, []byte(""), 0644) if err := os.WriteFile(dummyFile, []byte(""), 0644); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
linkFile := filepath.Join(temp, "link") linkFile := filepath.Join(temp, "link")
err = os.Symlink(dummyFile, linkFile) if err := os.Symlink(dummyFile, linkFile); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }

View File

@ -6,7 +6,6 @@ package os_test
import ( import (
"internal/testenv" "internal/testenv"
"os"
. "os" . "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -78,27 +77,19 @@ func TestMkdirAll(t *testing.T) {
func TestMkdirAllWithSymlink(t *testing.T) { func TestMkdirAllWithSymlink(t *testing.T) {
testenv.MustHaveSymlink(t) testenv.MustHaveSymlink(t)
tmpDir, err := os.MkdirTemp("", "TestMkdirAllWithSymlink-") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tmpDir)
dir := tmpDir + "/dir" dir := tmpDir + "/dir"
err = Mkdir(dir, 0755) if err := Mkdir(dir, 0755); err != nil {
if err != nil {
t.Fatalf("Mkdir %s: %s", dir, err) t.Fatalf("Mkdir %s: %s", dir, err)
} }
link := tmpDir + "/link" link := tmpDir + "/link"
err = Symlink("dir", link) if err := Symlink("dir", link); err != nil {
if err != nil {
t.Fatalf("Symlink %s: %s", link, err) t.Fatalf("Symlink %s: %s", link, err)
} }
path := link + "/foo" path := link + "/foo"
err = MkdirAll(path, 0755) if err := MkdirAll(path, 0755); err != nil {
if err != nil {
t.Errorf("MkdirAll %q: %s", path, err) t.Errorf("MkdirAll %q: %s", path, err)
} }
} }

View File

@ -50,31 +50,21 @@ func TestFixLongPath(t *testing.T) {
} }
func TestMkdirAllLongPath(t *testing.T) { func TestMkdirAllLongPath(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "TestMkdirAllLongPath") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
path := tmpDir path := tmpDir
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
path += `\another-path-component` path += `\another-path-component`
} }
err = os.MkdirAll(path, 0777) if err := os.MkdirAll(path, 0777); err != nil {
if err != nil {
t.Fatalf("MkdirAll(%q) failed; %v", path, err) t.Fatalf("MkdirAll(%q) failed; %v", path, err)
} }
err = os.RemoveAll(tmpDir) if err := os.RemoveAll(tmpDir); err != nil {
if err != nil {
t.Fatalf("RemoveAll(%q) failed; %v", tmpDir, err) t.Fatalf("RemoveAll(%q) failed; %v", tmpDir, err)
} }
} }
func TestMkdirAllExtendedLength(t *testing.T) { func TestMkdirAllExtendedLength(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "TestMkdirAllExtendedLength") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
const prefix = `\\?\` const prefix = `\\?\`
if len(tmpDir) < 4 || tmpDir[:4] != prefix { if len(tmpDir) < 4 || tmpDir[:4] != prefix {
@ -85,14 +75,12 @@ func TestMkdirAllExtendedLength(t *testing.T) {
tmpDir = prefix + fullPath tmpDir = prefix + fullPath
} }
path := tmpDir + `\dir\` path := tmpDir + `\dir\`
err = os.MkdirAll(path, 0777) if err := os.MkdirAll(path, 0777); err != nil {
if err != nil {
t.Fatalf("MkdirAll(%q) failed: %v", path, err) t.Fatalf("MkdirAll(%q) failed: %v", path, err)
} }
path = path + `.\dir2` path = path + `.\dir2`
err = os.MkdirAll(path, 0777) if err := os.MkdirAll(path, 0777); err == nil {
if err == nil {
t.Fatalf("MkdirAll(%q) should have failed, but did not", path) t.Fatalf("MkdirAll(%q) should have failed, but did not", path)
} }
} }

View File

@ -15,12 +15,7 @@ import (
) )
func TestRemoveAll(t *testing.T) { func TestRemoveAll(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "TestRemoveAll-") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tmpDir)
if err := RemoveAll(""); err != nil { if err := RemoveAll(""); err != nil {
t.Errorf("RemoveAll(\"\"): %v; want nil", err) t.Errorf("RemoveAll(\"\"): %v; want nil", err)
} }
@ -128,12 +123,7 @@ func TestRemoveAllLarge(t *testing.T) {
t.Skip("skipping in short mode") t.Skip("skipping in short mode")
} }
tmpDir, err := os.MkdirTemp("", "TestRemoveAll-") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "_TestRemoveAllLarge_") path := filepath.Join(tmpDir, "_TestRemoveAllLarge_")
// Make directory with 1000 files and remove. // Make directory with 1000 files and remove.
@ -236,12 +226,7 @@ func TestRemoveAllDot(t *testing.T) {
func TestRemoveAllDotDot(t *testing.T) { func TestRemoveAllDotDot(t *testing.T) {
t.Parallel() t.Parallel()
tempDir, err := os.MkdirTemp("", "TestRemoveAllDotDot-") tempDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tempDir)
subdir := filepath.Join(tempDir, "x") subdir := filepath.Join(tempDir, "x")
subsubdir := filepath.Join(subdir, "y") subsubdir := filepath.Join(subdir, "y")
if err := MkdirAll(subsubdir, 0777); err != nil { if err := MkdirAll(subsubdir, 0777); err != nil {
@ -261,12 +246,7 @@ func TestRemoveAllDotDot(t *testing.T) {
func TestRemoveReadOnlyDir(t *testing.T) { func TestRemoveReadOnlyDir(t *testing.T) {
t.Parallel() t.Parallel()
tempDir, err := os.MkdirTemp("", "TestRemoveReadOnlyDir-") tempDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tempDir)
subdir := filepath.Join(tempDir, "x") subdir := filepath.Join(tempDir, "x")
if err := Mkdir(subdir, 0); err != nil { if err := Mkdir(subdir, 0); err != nil {
t.Fatal(err) t.Fatal(err)
@ -298,12 +278,7 @@ func TestRemoveAllButReadOnlyAndPathError(t *testing.T) {
t.Parallel() t.Parallel()
tempDir, err := os.MkdirTemp("", "TestRemoveAllButReadOnly-") tempDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tempDir)
dirs := []string{ dirs := []string{
"a", "a",
"a/x", "a/x",
@ -347,7 +322,7 @@ func TestRemoveAllButReadOnlyAndPathError(t *testing.T) {
defer Chmod(d, 0777) defer Chmod(d, 0777)
} }
err = RemoveAll(tempDir) err := RemoveAll(tempDir)
if err == nil { if err == nil {
t.Fatal("RemoveAll succeeded unexpectedly") t.Fatal("RemoveAll succeeded unexpectedly")
} }
@ -389,12 +364,7 @@ func TestRemoveUnreadableDir(t *testing.T) {
t.Parallel() t.Parallel()
tempDir, err := os.MkdirTemp("", "TestRemoveAllButReadOnly-") tempDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tempDir)
target := filepath.Join(tempDir, "d0", "d1", "d2") target := filepath.Join(tempDir, "d0", "d1", "d2")
if err := MkdirAll(target, 0755); err != nil { if err := MkdirAll(target, 0755); err != nil {
t.Fatal(err) t.Fatal(err)
@ -413,12 +383,7 @@ func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) {
t.Skip("skipping in short mode") t.Skip("skipping in short mode")
} }
tmpDir, err := os.MkdirTemp("", "TestRemoveAll-") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "_TestRemoveAllWithMoreErrorThanReqSize_") path := filepath.Join(tmpDir, "_TestRemoveAllWithMoreErrorThanReqSize_")
// Make directory with 1025 read-only files. // Make directory with 1025 read-only files.
@ -443,7 +408,7 @@ func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) {
// This call should not hang, even on a platform that disallows file deletion // This call should not hang, even on a platform that disallows file deletion
// from read-only directories. // from read-only directories.
err = RemoveAll(path) err := RemoveAll(path)
if Getuid() == 0 { if Getuid() == 0 {
// On many platforms, root can remove files from read-only directories. // On many platforms, root can remove files from read-only directories.

View File

@ -185,30 +185,22 @@ func testSymlinkSameFile(t *testing.T, path, link string) {
func TestDirAndSymlinkStats(t *testing.T) { func TestDirAndSymlinkStats(t *testing.T) {
testenv.MustHaveSymlink(t) testenv.MustHaveSymlink(t)
tmpdir, err := os.MkdirTemp("", "TestDirAndSymlinkStats") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
dir := filepath.Join(tmpdir, "dir") dir := filepath.Join(tmpdir, "dir")
err = os.Mkdir(dir, 0777) if err := os.Mkdir(dir, 0777); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
testDirStats(t, dir) testDirStats(t, dir)
dirlink := filepath.Join(tmpdir, "link") dirlink := filepath.Join(tmpdir, "link")
err = os.Symlink(dir, dirlink) if err := os.Symlink(dir, dirlink); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
testSymlinkStats(t, dirlink, true) testSymlinkStats(t, dirlink, true)
testSymlinkSameFile(t, dir, dirlink) testSymlinkSameFile(t, dir, dirlink)
linklink := filepath.Join(tmpdir, "linklink") linklink := filepath.Join(tmpdir, "linklink")
err = os.Symlink(dirlink, linklink) if err := os.Symlink(dirlink, linklink); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
testSymlinkStats(t, linklink, true) testSymlinkStats(t, linklink, true)
@ -218,30 +210,22 @@ func TestDirAndSymlinkStats(t *testing.T) {
func TestFileAndSymlinkStats(t *testing.T) { func TestFileAndSymlinkStats(t *testing.T) {
testenv.MustHaveSymlink(t) testenv.MustHaveSymlink(t)
tmpdir, err := os.MkdirTemp("", "TestFileAndSymlinkStats") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
file := filepath.Join(tmpdir, "file") file := filepath.Join(tmpdir, "file")
err = os.WriteFile(file, []byte(""), 0644) if err := os.WriteFile(file, []byte(""), 0644); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
testFileStats(t, file) testFileStats(t, file)
filelink := filepath.Join(tmpdir, "link") filelink := filepath.Join(tmpdir, "link")
err = os.Symlink(file, filelink) if err := os.Symlink(file, filelink); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
testSymlinkStats(t, filelink, false) testSymlinkStats(t, filelink, false)
testSymlinkSameFile(t, file, filelink) testSymlinkSameFile(t, file, filelink)
linklink := filepath.Join(tmpdir, "linklink") linklink := filepath.Join(tmpdir, "linklink")
err = os.Symlink(filelink, linklink) if err := os.Symlink(filelink, linklink); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
testSymlinkStats(t, linklink, false) testSymlinkStats(t, linklink, false)
@ -252,20 +236,13 @@ func TestFileAndSymlinkStats(t *testing.T) {
func TestSymlinkWithTrailingSlash(t *testing.T) { func TestSymlinkWithTrailingSlash(t *testing.T) {
testenv.MustHaveSymlink(t) testenv.MustHaveSymlink(t)
tmpdir, err := os.MkdirTemp("", "TestSymlinkWithTrailingSlash") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
dir := filepath.Join(tmpdir, "dir") dir := filepath.Join(tmpdir, "dir")
err = os.Mkdir(dir, 0777) if err := os.Mkdir(dir, 0777); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
dirlink := filepath.Join(tmpdir, "link") dirlink := filepath.Join(tmpdir, "link")
err = os.Symlink(dir, dirlink) if err := os.Symlink(dir, dirlink); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
dirlinkWithSlash := dirlink + string(os.PathSeparator) dirlinkWithSlash := dirlink + string(os.PathSeparator)