all: use T.TempDir to create temporary test directory (#8280)
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
+4
-20
@@ -20,17 +20,13 @@ import (
|
||||
|
||||
func setup(t *testing.T) (*BasicFilesystem, string) {
|
||||
t.Helper()
|
||||
dir, err := os.MkdirTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
return newBasicFilesystem(dir), dir
|
||||
}
|
||||
|
||||
func TestChmodFile(t *testing.T) {
|
||||
fs, dir := setup(t)
|
||||
path := filepath.Join(dir, "file")
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
defer os.Chmod(path, 0666)
|
||||
|
||||
@@ -71,7 +67,6 @@ func TestChownFile(t *testing.T) {
|
||||
|
||||
fs, dir := setup(t)
|
||||
path := filepath.Join(dir, "file")
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
defer os.Chmod(path, 0666)
|
||||
|
||||
@@ -108,7 +103,6 @@ func TestChownFile(t *testing.T) {
|
||||
func TestChmodDir(t *testing.T) {
|
||||
fs, dir := setup(t)
|
||||
path := filepath.Join(dir, "dir")
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
mode := os.FileMode(0755)
|
||||
if runtime.GOOS == "windows" {
|
||||
@@ -141,7 +135,6 @@ func TestChmodDir(t *testing.T) {
|
||||
func TestChtimes(t *testing.T) {
|
||||
fs, dir := setup(t)
|
||||
path := filepath.Join(dir, "file")
|
||||
defer os.RemoveAll(dir)
|
||||
fd, err := os.Create(path)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -166,7 +159,6 @@ func TestChtimes(t *testing.T) {
|
||||
func TestCreate(t *testing.T) {
|
||||
fs, dir := setup(t)
|
||||
path := filepath.Join(dir, "file")
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
t.Errorf("exists?")
|
||||
@@ -190,7 +182,6 @@ func TestCreateSymlink(t *testing.T) {
|
||||
|
||||
fs, dir := setup(t)
|
||||
path := filepath.Join(dir, "file")
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
if err := fs.CreateSymlink("blah", "file"); err != nil {
|
||||
t.Error(err)
|
||||
@@ -215,7 +206,6 @@ func TestCreateSymlink(t *testing.T) {
|
||||
|
||||
func TestDirNames(t *testing.T) {
|
||||
fs, dir := setup(t)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
// Case differences
|
||||
testCases := []string{
|
||||
@@ -244,8 +234,7 @@ func TestDirNames(t *testing.T) {
|
||||
|
||||
func TestNames(t *testing.T) {
|
||||
// Tests that all names are without the root directory.
|
||||
fs, dir := setup(t)
|
||||
defer os.RemoveAll(dir)
|
||||
fs, _ := setup(t)
|
||||
|
||||
expected := "file"
|
||||
fd, err := fs.Create(expected)
|
||||
@@ -284,8 +273,7 @@ func TestNames(t *testing.T) {
|
||||
|
||||
func TestGlob(t *testing.T) {
|
||||
// Tests that all names are without the root directory.
|
||||
fs, dir := setup(t)
|
||||
defer os.RemoveAll(dir)
|
||||
fs, _ := setup(t)
|
||||
|
||||
for _, dirToCreate := range []string{
|
||||
filepath.Join("a", "test", "b"),
|
||||
@@ -344,8 +332,7 @@ func TestGlob(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUsage(t *testing.T) {
|
||||
fs, dir := setup(t)
|
||||
defer os.RemoveAll(dir)
|
||||
fs, _ := setup(t)
|
||||
usage, err := fs.Usage(".")
|
||||
if err != nil {
|
||||
if runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "solaris" {
|
||||
@@ -577,18 +564,15 @@ func TestRel(t *testing.T) {
|
||||
|
||||
func TestBasicWalkSkipSymlink(t *testing.T) {
|
||||
_, dir := setup(t)
|
||||
defer os.RemoveAll(dir)
|
||||
testWalkSkipSymlink(t, FilesystemTypeBasic, dir)
|
||||
}
|
||||
|
||||
func TestWalkTraverseDirJunct(t *testing.T) {
|
||||
_, dir := setup(t)
|
||||
defer os.RemoveAll(dir)
|
||||
testWalkTraverseDirJunct(t, FilesystemTypeBasic, dir)
|
||||
}
|
||||
|
||||
func TestWalkInfiniteRecursion(t *testing.T) {
|
||||
_, dir := setup(t)
|
||||
defer os.RemoveAll(dir)
|
||||
testWalkInfiniteRecursion(t, FilesystemTypeBasic, dir)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,6 @@ func TestResolveWindows83(t *testing.T) {
|
||||
dir = fs.resolveWin83(dir)
|
||||
fs = newBasicFilesystem(dir)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
shortAbs, _ := fs.rooted("LFDATA~1")
|
||||
long := "LFDataTool"
|
||||
@@ -80,7 +79,6 @@ func TestIsWindows83(t *testing.T) {
|
||||
dir = fs.resolveWin83(dir)
|
||||
fs = newBasicFilesystem(dir)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
tempTop, _ := fs.rooted(TempName("baz"))
|
||||
tempBelow, _ := fs.rooted(filepath.Join("foo", "bar", TempName("baz")))
|
||||
|
||||
@@ -9,7 +9,6 @@ package fs
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
@@ -28,8 +27,7 @@ func TestRealCase(t *testing.T) {
|
||||
testRealCase(t, newFakeFilesystem(t.Name()+"?insens=true"))
|
||||
})
|
||||
t.Run("actual", func(t *testing.T) {
|
||||
fsys, tmpDir := setup(t)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
fsys, _ := setup(t)
|
||||
testRealCase(t, fsys)
|
||||
})
|
||||
}
|
||||
@@ -83,8 +81,7 @@ func TestRealCaseSensitive(t *testing.T) {
|
||||
testRealCaseSensitive(t, newFakeFilesystem(t.Name()))
|
||||
})
|
||||
t.Run("actual", func(t *testing.T) {
|
||||
fsys, tmpDir := setup(t)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
fsys, _ := setup(t)
|
||||
testRealCaseSensitive(t, fsys)
|
||||
})
|
||||
}
|
||||
@@ -124,8 +121,7 @@ func TestCaseFSStat(t *testing.T) {
|
||||
testCaseFSStat(t, newFakeFilesystem(t.Name()+"?insens=true"))
|
||||
})
|
||||
t.Run("actual", func(t *testing.T) {
|
||||
fsys, tmpDir := setup(t)
|
||||
defer os.RemoveAll(tmpDir)
|
||||
fsys, _ := setup(t)
|
||||
testCaseFSStat(t, fsys)
|
||||
})
|
||||
}
|
||||
|
||||
+1
-14
@@ -204,7 +204,6 @@ func TestFakeFSCaseSensitive(t *testing.T) {
|
||||
}
|
||||
|
||||
testDir, sensitive := createTestDir(t)
|
||||
defer removeTestDir(t, testDir)
|
||||
if sensitive {
|
||||
filesystems = append(filesystems, testFS{runtime.GOOS, newBasicFilesystem(testDir)})
|
||||
}
|
||||
@@ -240,7 +239,6 @@ func TestFakeFSCaseInsensitive(t *testing.T) {
|
||||
}
|
||||
|
||||
testDir, sensitive := createTestDir(t)
|
||||
defer removeTestDir(t, testDir)
|
||||
if !sensitive {
|
||||
filesystems = append(filesystems, testFS{runtime.GOOS, newBasicFilesystem(testDir)})
|
||||
}
|
||||
@@ -251,10 +249,7 @@ func TestFakeFSCaseInsensitive(t *testing.T) {
|
||||
func createTestDir(t *testing.T) (string, bool) {
|
||||
t.Helper()
|
||||
|
||||
testDir, err := os.MkdirTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatalf("could not create temporary dir for testing: %s", err)
|
||||
}
|
||||
testDir := t.TempDir()
|
||||
|
||||
if fd, err := os.Create(filepath.Join(testDir, ".stfolder")); err != nil {
|
||||
t.Fatalf("could not create .stfolder: %s", err)
|
||||
@@ -273,14 +268,6 @@ func createTestDir(t *testing.T) (string, bool) {
|
||||
return testDir, sensitive
|
||||
}
|
||||
|
||||
func removeTestDir(t *testing.T, testDir string) {
|
||||
t.Helper()
|
||||
|
||||
if err := os.RemoveAll(testDir); err != nil {
|
||||
t.Fatalf("could not remove test directory: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func runTests(t *testing.T, tests []test, filesystems []testFS) {
|
||||
for _, filesystem := range filesystems {
|
||||
for _, test := range tests {
|
||||
|
||||
+4
-10
@@ -82,11 +82,7 @@ func TestMtimeFS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMtimeFSWalk(t *testing.T) {
|
||||
dir, err := os.MkdirTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = os.RemoveAll(dir) }()
|
||||
dir := t.TempDir()
|
||||
|
||||
mtimefs, walkFs := newMtimeFSWithWalk(dir, make(mapStore))
|
||||
underlying := mtimefs.Filesystem
|
||||
@@ -136,11 +132,7 @@ func TestMtimeFSWalk(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMtimeFSOpen(t *testing.T) {
|
||||
dir, err := os.MkdirTemp("", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = os.RemoveAll(dir) }()
|
||||
dir := t.TempDir()
|
||||
|
||||
mtimefs := newMtimeFS(dir, make(mapStore))
|
||||
underlying := mtimefs.Filesystem
|
||||
@@ -177,6 +169,8 @@ func TestMtimeFSOpen(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
info, err := fd.Stat()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user