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-20 23:49:46 +08:00 committed by Max Mazurov
parent d60ce8321f
commit 8f1cfd72ac
5 changed files with 8 additions and 39 deletions

View file

@ -45,11 +45,7 @@ import (
// See newTestQueueDir to create testing queue from an existing directory.
// It is called responsibility to remove queue directory created by this function.
func newTestQueue(t *testing.T, target module.DeliveryTarget) *Queue {
dir, err := ioutil.TempDir("", "maddy-tests-queue")
if err != nil {
t.Fatal("failed to create temporary directory for queue:", err)
}
return newTestQueueDir(t, target, dir)
return newTestQueueDir(t, target, t.TempDir())
}
func cleanQueue(t *testing.T, q *Queue) {
@ -57,9 +53,6 @@ func cleanQueue(t *testing.T, q *Queue) {
if err := q.Close(); err != nil {
t.Fatal("queue.Close:", err)
}
if err := os.RemoveAll(q.location); err != nil {
t.Fatal("os.RemoveAll", err)
}
}
func newTestQueueDir(t *testing.T, target module.DeliveryTarget, dir string) *Queue {