test: use T.TempDir to create temporary test directory

The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-01-30 00:24:56 +08:00
committed by Nick Craig-Wood
parent 18c24014da
commit 8cf76f5e11
10 changed files with 26 additions and 94 deletions

View File

@ -13,15 +13,6 @@ import (
"github.com/stretchr/testify/require"
)
// Create a test directory then tidy up
func testDir(t *testing.T) (string, func()) {
dir, err := ioutil.TempDir("", "rclone-test")
require.NoError(t, err)
return dir, func() {
assert.NoError(t, os.RemoveAll(dir))
}
}
// This lists dir and checks the listing is as expected without checking the size
func checkListingNoSize(t *testing.T, dir string, want []string) {
var got []string
@ -46,8 +37,7 @@ func checkListing(t *testing.T, dir string, want []string) {
// Test we can rename an open file
func TestOpenFileRename(t *testing.T) {
dir, tidy := testDir(t)
defer tidy()
dir := t.TempDir()
filepath := path.Join(dir, "file1")
f, err := Create(filepath)
@ -71,8 +61,7 @@ func TestOpenFileRename(t *testing.T) {
// Test we can delete an open file
func TestOpenFileDelete(t *testing.T) {
dir, tidy := testDir(t)
defer tidy()
dir := t.TempDir()
filepath := path.Join(dir, "file1")
f, err := Create(filepath)
@ -103,8 +92,7 @@ func TestOpenFileDelete(t *testing.T) {
// Smoke test the Open, OpenFile and Create functions
func TestOpenFileOperations(t *testing.T) {
dir, tidy := testDir(t)
defer tidy()
dir := t.TempDir()
filepath := path.Join(dir, "file1")