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-02-01 11:47:04 +00:00
committed by Nick Craig-Wood
parent 18c24014da
commit 8cf76f5e11
10 changed files with 26 additions and 94 deletions
+3 -15
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")
+2 -4
View File
@@ -15,8 +15,7 @@ import (
// Basic test from golang's os/path_test.go
func TestMkdirAll(t *testing.T) {
tmpDir, tidy := testDir(t)
defer tidy()
tmpDir := t.TempDir()
path := tmpDir + "/dir/./dir2"
err := MkdirAll(path, 0777)
@@ -99,8 +98,7 @@ func checkMkdirAllSubdirs(t *testing.T, path string, valid bool, errormsg string
// Testing paths on existing drive
func TestMkdirAllOnDrive(t *testing.T) {
path, tidy := testDir(t)
defer tidy()
path := t.TempDir()
dir, err := os.Stat(path)
require.NoError(t, err)