test: use T.TempDir to create temporary test directory (#44947)

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-03-22 15:43:29 +01:00
committed by GitHub
parent 52fec0d7db
commit b56848f006
6 changed files with 14 additions and 76 deletions
+2 -8
View File
@@ -3,7 +3,6 @@ package testinfra
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
@@ -96,12 +95,7 @@ func SetUpDatabase(t *testing.T, grafDir string) *sqlstore.SQLStore {
func CreateGrafDir(t *testing.T, opts ...GrafanaOpts) (string, string) {
t.Helper()
tmpDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(tmpDir)
assert.NoError(t, err)
})
tmpDir := t.TempDir()
// Search upwards in directory tree for project root
var rootDir string
@@ -125,7 +119,7 @@ func CreateGrafDir(t *testing.T, opts ...GrafanaOpts) (string, string) {
require.True(t, found, "Couldn't detect project root directory")
cfgDir := filepath.Join(tmpDir, "conf")
err = os.MkdirAll(cfgDir, 0750)
err := os.MkdirAll(cfgDir, 0750)
require.NoError(t, err)
dataDir := filepath.Join(tmpDir, "data")
// nolint:gosec