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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user