path/filepath: replace os.MkdirTemp with T.TempDir

Change-Id: I6d78e0e742cb0e7f5ea3f430e9cec0f5d1ee03e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/307652
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: Emmanuel Odeke <emmanuel@orijtech.com>
This commit is contained in:
ianwoolf 2021-04-06 22:05:45 +08:00 committed by Emmanuel Odeke
parent 0bc4605ead
commit 7da8490cbb

View file

@ -477,11 +477,7 @@ func testWalk(t *testing.T, walk func(string, fs.WalkDirFunc) error, errVisit in
defer restore() defer restore()
} }
tmpDir, err := os.MkdirTemp("", "TestWalk") tmpDir := t.TempDir()
if err != nil {
t.Fatal("creating temp dir:", err)
}
defer os.RemoveAll(tmpDir)
origDir, err := os.Getwd() origDir, err := os.Getwd()
if err != nil { if err != nil {
@ -581,11 +577,7 @@ func touch(t *testing.T, name string) {
} }
func TestWalkSkipDirOnFile(t *testing.T) { func TestWalkSkipDirOnFile(t *testing.T) {
td, err := os.MkdirTemp("", "walktest") td := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil { if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil {
t.Fatal(err) t.Fatal(err)
@ -609,7 +601,7 @@ func TestWalkSkipDirOnFile(t *testing.T) {
check := func(t *testing.T, walk func(root string) error, root string) { check := func(t *testing.T, walk func(root string) error, root string) {
t.Helper() t.Helper()
sawFoo2 = false sawFoo2 = false
err = walk(root) err := walk(root)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -631,11 +623,7 @@ func TestWalkSkipDirOnFile(t *testing.T) {
} }
func TestWalkFileError(t *testing.T) { func TestWalkFileError(t *testing.T) {
td, err := os.MkdirTemp("", "walktest") td := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
touch(t, filepath.Join(td, "foo")) touch(t, filepath.Join(td, "foo"))
touch(t, filepath.Join(td, "bar")) touch(t, filepath.Join(td, "bar"))
@ -656,7 +644,7 @@ func TestWalkFileError(t *testing.T) {
return os.Lstat(path) return os.Lstat(path)
} }
got := map[string]error{} got := map[string]error{}
err = filepath.Walk(td, func(path string, fi fs.FileInfo, err error) error { err := filepath.Walk(td, func(path string, fi fs.FileInfo, err error) error {
rel, _ := filepath.Rel(td, path) rel, _ := filepath.Rel(td, path)
got[filepath.ToSlash(rel)] = err got[filepath.ToSlash(rel)] = err
return nil return nil
@ -910,14 +898,11 @@ func testEvalSymlinksAfterChdir(t *testing.T, wd, path, want string) {
func TestEvalSymlinks(t *testing.T) { func TestEvalSymlinks(t *testing.T) {
testenv.MustHaveSymlink(t) testenv.MustHaveSymlink(t)
tmpDir, err := os.MkdirTemp("", "evalsymlink") tmpDir := t.TempDir()
if err != nil {
t.Fatal("creating temp dir:", err)
}
defer os.RemoveAll(tmpDir)
// /tmp may itself be a symlink! Avoid the confusion, although // /tmp may itself be a symlink! Avoid the confusion, although
// it means trusting the thing we're testing. // it means trusting the thing we're testing.
var err error
tmpDir, err = filepath.EvalSymlinks(tmpDir) tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil { if err != nil {
t.Fatal("eval symlink for tmp dir:", err) t.Fatal("eval symlink for tmp dir:", err)
@ -996,14 +981,10 @@ func TestEvalSymlinksIsNotExist(t *testing.T) {
func TestIssue13582(t *testing.T) { func TestIssue13582(t *testing.T) {
testenv.MustHaveSymlink(t) testenv.MustHaveSymlink(t)
tmpDir, err := os.MkdirTemp("", "issue13582") 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, 0755) err := os.Mkdir(dir, 0755)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -1083,12 +1064,7 @@ var absTests = []string{
} }
func TestAbs(t *testing.T) { func TestAbs(t *testing.T) {
root, err := os.MkdirTemp("", "TestAbs") root := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(root)
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
t.Fatal("getwd failed: ", err) t.Fatal("getwd failed: ", err)
@ -1154,11 +1130,7 @@ func TestAbs(t *testing.T) {
// We test it separately from all other absTests because the empty string is not // We test it separately from all other absTests because the empty string is not
// a valid path, so it can't be used with os.Stat. // a valid path, so it can't be used with os.Stat.
func TestAbsEmptyString(t *testing.T) { func TestAbsEmptyString(t *testing.T) {
root, err := os.MkdirTemp("", "TestAbsEmptyString") root := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(root)
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
@ -1376,11 +1348,7 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
} }
func testWalkSymlink(t *testing.T, mklink func(target, link string) error) { func testWalkSymlink(t *testing.T, mklink func(target, link string) error) {
tmpdir, err := os.MkdirTemp("", "testWalkSymlink") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
@ -1426,14 +1394,10 @@ func TestWalkSymlink(t *testing.T) {
} }
func TestIssue29372(t *testing.T) { func TestIssue29372(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "TestIssue29372") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "file.txt") path := filepath.Join(tmpDir, "file.txt")
err = os.WriteFile(path, nil, 0644) err := os.WriteFile(path, nil, 0644)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -1462,11 +1426,7 @@ func TestEvalSymlinksAboveRoot(t *testing.T) {
t.Parallel() t.Parallel()
tmpDir, err := os.MkdirTemp("", "TestEvalSymlinksAboveRoot") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
evalTmpDir, err := filepath.EvalSymlinks(tmpDir) evalTmpDir, err := filepath.EvalSymlinks(tmpDir)
if err != nil { if err != nil {