test: replace defer cleanup with t.Cleanup

Reference: https://pkg.go.dev/testing#T.Cleanup
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-12-08 20:43:53 +08:00 committed by Nick Craig-Wood
parent be783a1856
commit 8e507075d1
34 changed files with 217 additions and 555 deletions

View file

@ -15,8 +15,8 @@ import (
"github.com/stretchr/testify/require"
)
func dirCreate(t *testing.T) (r *fstest.Run, vfs *VFS, dir *Dir, item fstest.Item, cleanup func()) {
r, vfs, cleanup = newTestVFS(t)
func dirCreate(t *testing.T) (r *fstest.Run, vfs *VFS, dir *Dir, item fstest.Item) {
r, vfs = newTestVFS(t)
file1 := r.WriteObject(context.Background(), "dir/file1", "file1 contents", t1)
r.CheckRemoteItems(t, file1)
@ -25,12 +25,11 @@ func dirCreate(t *testing.T) (r *fstest.Run, vfs *VFS, dir *Dir, item fstest.Ite
require.NoError(t, err)
require.True(t, node.IsDir())
return r, vfs, node.(*Dir), file1, cleanup
return r, vfs, node.(*Dir), file1
}
func TestDirMethods(t *testing.T) {
_, vfs, dir, _, cleanup := dirCreate(t)
defer cleanup()
_, vfs, dir, _ := dirCreate(t)
// String
assert.Equal(t, "dir/", dir.String())
@ -81,8 +80,7 @@ func TestDirMethods(t *testing.T) {
}
func TestDirForgetAll(t *testing.T) {
_, vfs, dir, file1, cleanup := dirCreate(t)
defer cleanup()
_, vfs, dir, file1 := dirCreate(t)
// Make sure / and dir are in cache
_, err := vfs.Stat(file1.Path)
@ -109,8 +107,7 @@ func TestDirForgetAll(t *testing.T) {
}
func TestDirForgetPath(t *testing.T) {
_, vfs, dir, file1, cleanup := dirCreate(t)
defer cleanup()
_, vfs, dir, file1 := dirCreate(t)
// Make sure / and dir are in cache
_, err := vfs.Stat(file1.Path)
@ -141,8 +138,7 @@ func TestDirForgetPath(t *testing.T) {
}
func TestDirWalk(t *testing.T) {
r, vfs, _, file1, cleanup := dirCreate(t)
defer cleanup()
r, vfs, _, file1 := dirCreate(t)
file2 := r.WriteObject(context.Background(), "fil/a/b/c", "super long file", t1)
r.CheckRemoteItems(t, file1, file2)
@ -210,8 +206,7 @@ func TestDirWalk(t *testing.T) {
}
func TestDirSetModTime(t *testing.T) {
_, vfs, dir, _, cleanup := dirCreate(t)
defer cleanup()
_, vfs, dir, _ := dirCreate(t)
err := dir.SetModTime(t1)
require.NoError(t, err)
@ -227,8 +222,7 @@ func TestDirSetModTime(t *testing.T) {
}
func TestDirStat(t *testing.T) {
_, _, dir, _, cleanup := dirCreate(t)
defer cleanup()
_, _, dir, _ := dirCreate(t)
node, err := dir.Stat("file1")
require.NoError(t, err)
@ -253,8 +247,7 @@ func checkListing(t *testing.T, dir *Dir, want []string) {
}
func TestDirReadDirAll(t *testing.T) {
r, vfs, cleanup := newTestVFS(t)
defer cleanup()
r, vfs := newTestVFS(t)
file1 := r.WriteObject(context.Background(), "dir/file1", "file1 contents", t1)
file2 := r.WriteObject(context.Background(), "dir/file2", "file2- contents", t2)
@ -334,8 +327,7 @@ func TestDirReadDirAll(t *testing.T) {
}
func TestDirOpen(t *testing.T) {
_, _, dir, _, cleanup := dirCreate(t)
defer cleanup()
_, _, dir, _ := dirCreate(t)
fd, err := dir.Open(os.O_RDONLY)
require.NoError(t, err)
@ -348,8 +340,7 @@ func TestDirOpen(t *testing.T) {
}
func TestDirCreate(t *testing.T) {
_, vfs, dir, _, cleanup := dirCreate(t)
defer cleanup()
_, vfs, dir, _ := dirCreate(t)
file, err := dir.Create("potato", os.O_WRONLY|os.O_CREATE)
require.NoError(t, err)
@ -388,8 +379,7 @@ func TestDirCreate(t *testing.T) {
}
func TestDirMkdir(t *testing.T) {
r, vfs, dir, file1, cleanup := dirCreate(t)
defer cleanup()
r, vfs, dir, file1 := dirCreate(t)
_, err := dir.Mkdir("file1")
assert.Error(t, err)
@ -410,8 +400,7 @@ func TestDirMkdir(t *testing.T) {
}
func TestDirMkdirSub(t *testing.T) {
r, vfs, dir, file1, cleanup := dirCreate(t)
defer cleanup()
r, vfs, dir, file1 := dirCreate(t)
_, err := dir.Mkdir("file1")
assert.Error(t, err)
@ -436,8 +425,7 @@ func TestDirMkdirSub(t *testing.T) {
}
func TestDirRemove(t *testing.T) {
r, vfs, dir, _, cleanup := dirCreate(t)
defer cleanup()
r, vfs, dir, _ := dirCreate(t)
// check directory is there
node, err := vfs.Stat("dir")
@ -476,8 +464,7 @@ func TestDirRemove(t *testing.T) {
}
func TestDirRemoveAll(t *testing.T) {
r, vfs, dir, _, cleanup := dirCreate(t)
defer cleanup()
r, vfs, dir, _ := dirCreate(t)
// Remove the directory and contents
err := dir.RemoveAll()
@ -498,8 +485,7 @@ func TestDirRemoveAll(t *testing.T) {
}
func TestDirRemoveName(t *testing.T) {
r, vfs, dir, _, cleanup := dirCreate(t)
defer cleanup()
r, vfs, dir, _ := dirCreate(t)
err := dir.RemoveName("file1")
require.NoError(t, err)
@ -518,8 +504,7 @@ func TestDirRemoveName(t *testing.T) {
}
func TestDirRename(t *testing.T) {
r, vfs, dir, file1, cleanup := dirCreate(t)
defer cleanup()
r, vfs, dir, file1 := dirCreate(t)
features := r.Fremote.Features()
if features.DirMove == nil && features.Move == nil && features.Copy == nil {