1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-06 00:03:32 +00:00

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

@@ -46,18 +46,17 @@ func cleanupVFS(t *testing.T, vfs *VFS) {
}
// Create a new VFS
func newTestVFSOpt(t *testing.T, opt *vfscommon.Options) (r *fstest.Run, vfs *VFS, cleanup func()) {
func newTestVFSOpt(t *testing.T, opt *vfscommon.Options) (r *fstest.Run, vfs *VFS) {
r = fstest.NewRun(t)
vfs = New(r.Fremote, opt)
cleanup = func() {
t.Cleanup(func() {
cleanupVFS(t, vfs)
r.Finalise()
}
return r, vfs, cleanup
})
return r, vfs
}
// Create a new VFS with default options
func newTestVFS(t *testing.T) (r *fstest.Run, vfs *VFS, cleanup func()) {
func newTestVFS(t *testing.T) (r *fstest.Run, vfs *VFS) {
return newTestVFSOpt(t, nil)
}
@@ -136,7 +135,7 @@ func TestVFSNew(t *testing.T) {
checkActiveCacheEntries(0)
r, vfs, cleanup := newTestVFS(t)
r, vfs := newTestVFS(t)
// Check making a VFS with nil options
var defaultOpt = vfscommon.DefaultOpt
@@ -158,7 +157,7 @@ func TestVFSNew(t *testing.T) {
checkActiveCacheEntries(1)
cleanup()
cleanupVFS(t, vfs)
checkActiveCacheEntries(0)
}
@@ -169,8 +168,7 @@ func TestVFSNewWithOpts(t *testing.T) {
opt.DirPerms = 0777
opt.FilePerms = 0666
opt.Umask = 0002
_, vfs, cleanup := newTestVFSOpt(t, &opt)
defer cleanup()
_, vfs := newTestVFSOpt(t, &opt)
assert.Equal(t, os.FileMode(0775)|os.ModeDir, vfs.Opt.DirPerms)
assert.Equal(t, os.FileMode(0664), vfs.Opt.FilePerms)
@@ -178,8 +176,7 @@ func TestVFSNewWithOpts(t *testing.T) {
// TestRoot checks root directory is present and correct
func TestVFSRoot(t *testing.T) {
_, vfs, cleanup := newTestVFS(t)
defer cleanup()
_, vfs := newTestVFS(t)
root, err := vfs.Root()
require.NoError(t, err)
@@ -189,8 +186,7 @@ func TestVFSRoot(t *testing.T) {
}
func TestVFSStat(t *testing.T) {
r, vfs, cleanup := newTestVFS(t)
defer cleanup()
r, vfs := newTestVFS(t)
file1 := r.WriteObject(context.Background(), "file1", "file1 contents", t1)
file2 := r.WriteObject(context.Background(), "dir/file2", "file2 contents", t2)
@@ -225,8 +221,7 @@ func TestVFSStat(t *testing.T) {
}
func TestVFSStatParent(t *testing.T) {
r, vfs, cleanup := newTestVFS(t)
defer cleanup()
r, vfs := newTestVFS(t)
file1 := r.WriteObject(context.Background(), "file1", "file1 contents", t1)
file2 := r.WriteObject(context.Background(), "dir/file2", "file2 contents", t2)
@@ -258,8 +253,7 @@ func TestVFSStatParent(t *testing.T) {
}
func TestVFSOpenFile(t *testing.T) {
r, vfs, cleanup := newTestVFS(t)
defer cleanup()
r, vfs := newTestVFS(t)
file1 := r.WriteObject(context.Background(), "file1", "file1 contents", t1)
file2 := r.WriteObject(context.Background(), "dir/file2", "file2 contents", t2)
@@ -293,8 +287,7 @@ func TestVFSOpenFile(t *testing.T) {
}
func TestVFSRename(t *testing.T) {
r, vfs, cleanup := newTestVFS(t)
defer cleanup()
r, vfs := newTestVFS(t)
features := r.Fremote.Features()
if features.Move == nil && features.Copy == nil {
@@ -322,8 +315,7 @@ func TestVFSRename(t *testing.T) {
}
func TestVFSStatfs(t *testing.T) {
r, vfs, cleanup := newTestVFS(t)
defer cleanup()
r, vfs := newTestVFS(t)
// pre-conditions
assert.Nil(t, vfs.usage)