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.

This commit also updates the unit test advice to use `T.TempDir` to
create temporary directory in tests.

Fixes: #3924

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-20 15:54:25 +08:00
parent 18dc578134
commit 59c7165ee1
37 changed files with 220 additions and 577 deletions

View File

@@ -51,11 +51,8 @@ func TestGzipAccepted(t *testing.T) {
func TestEnsureDir(t *testing.T) {
const testMode = 0755
tmpdir, err := os.MkdirTemp("", "TestEnsureDir")
assert := assert.New(t)
assert.NoError(err)
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()
// nolint: govet
testCases := []struct {
@@ -120,9 +117,7 @@ func TestEnsureDir(t *testing.T) {
func TestFirstValidExecutable(t *testing.T) {
assert := assert.New(t)
tmpdir, err := os.MkdirTemp("", "TestFirstValidPath")
assert.NoError(err)
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()
// nolint: govet
testCases := []struct {