From cf35168f0a9cf457f0622cd89cff84bdbe610d58 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Thu, 5 May 2022 21:09:41 +0800 Subject: test: use `T.TempDir` to create temporary test directory 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 --- pkg/machine/qemu/config_test.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'pkg/machine/qemu/config_test.go') diff --git a/pkg/machine/qemu/config_test.go b/pkg/machine/qemu/config_test.go index 3f92881fa..4d96ec6e7 100644 --- a/pkg/machine/qemu/config_test.go +++ b/pkg/machine/qemu/config_test.go @@ -52,23 +52,15 @@ func TestMachineFile_GetPath(t *testing.T) { func TestNewMachineFile(t *testing.T) { empty := "" - homedir, err := os.MkdirTemp("/tmp", "homedir") - if err != nil { - panic(err) - } - defer os.RemoveAll(homedir) - longTemp, err := os.MkdirTemp("/tmp", "tmpdir") - if err != nil { - panic(err) - } - defer os.RemoveAll(longTemp) + homedir := t.TempDir() + longTemp := t.TempDir() oldhome := os.Getenv("HOME") os.Setenv("HOME", homedir) //nolint: tenv defer os.Setenv("HOME", oldhome) p := "/var/tmp/podman/my.sock" longp := filepath.Join(longTemp, utils.RandomString(100), "my.sock") - err = os.MkdirAll(filepath.Dir(longp), 0755) + err := os.MkdirAll(filepath.Dir(longp), 0755) if err != nil { panic(err) } -- cgit v1.2.3-54-g00ecf