mirror of
https://github.com/pgsty/minio.git
synced 2026-07-20 04:30:26 +03:00
test: use T.TempDir to create temporary test directory (#15400)
This commit replaces `ioutil.TempDir` 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 `ioutil.TempDir`
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:
+5
-14
@@ -67,25 +67,16 @@ type result struct {
|
||||
entries []string
|
||||
}
|
||||
|
||||
func mustSetupDir(t *testing.T) string {
|
||||
// Create unique test directory.
|
||||
dir, err := ioutil.TempDir(globalTestTmpDir, "minio-list-dir")
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to setup directory, %s", err)
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
// Test to read empty directory.
|
||||
func setupTestReadDirEmpty(t *testing.T) (testResults []result) {
|
||||
// Add empty entry slice for this test directory.
|
||||
testResults = append(testResults, result{mustSetupDir(t), []string{}})
|
||||
testResults = append(testResults, result{t.TempDir(), []string{}})
|
||||
return testResults
|
||||
}
|
||||
|
||||
// Test to read non-empty directory with only files.
|
||||
func setupTestReadDirFiles(t *testing.T) (testResults []result) {
|
||||
dir := mustSetupDir(t)
|
||||
dir := t.TempDir()
|
||||
entries := []string{}
|
||||
for i := 0; i < 10; i++ {
|
||||
name := fmt.Sprintf("file-%d", i)
|
||||
@@ -107,7 +98,7 @@ func setupTestReadDirFiles(t *testing.T) (testResults []result) {
|
||||
|
||||
// Test to read non-empty directory with directories and files.
|
||||
func setupTestReadDirGeneric(t *testing.T) (testResults []result) {
|
||||
dir := mustSetupDir(t)
|
||||
dir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(dir, "mydir"), 0o777); err != nil {
|
||||
t.Fatalf("Unable to create prefix directory \"mydir\", %s", err)
|
||||
}
|
||||
@@ -134,7 +125,7 @@ func setupTestReadDirSymlink(t *testing.T) (testResults []result) {
|
||||
t.Skip("symlinks not available on windows")
|
||||
return nil
|
||||
}
|
||||
dir := mustSetupDir(t)
|
||||
dir := t.TempDir()
|
||||
entries := []string{}
|
||||
for i := 0; i < 10; i++ {
|
||||
name1 := fmt.Sprintf("file-%d", i)
|
||||
@@ -241,7 +232,7 @@ func TestReadDirN(t *testing.T) {
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
dir := mustSetupDir(t)
|
||||
dir := t.TempDir()
|
||||
|
||||
for c := 1; c <= testCase.numFiles; c++ {
|
||||
err := ioutil.WriteFile(filepath.Join(dir, fmt.Sprintf("%d", c)), []byte{}, os.ModePerm)
|
||||
|
||||
Reference in New Issue
Block a user