summaryrefslogtreecommitdiff
path: root/pkg/machine/qemu/config_test.go
diff options
context:
space:
mode:
authorEng Zer Jun <engzerjun@gmail.com>2022-05-05 21:09:41 +0800
committerEng Zer Jun <engzerjun@gmail.com>2022-05-05 21:09:41 +0800
commitcf35168f0a9cf457f0622cd89cff84bdbe610d58 (patch)
treedb3b776a982b2a6b615a2a7c9ee45ea6f89c9776 /pkg/machine/qemu/config_test.go
parentb1e9ea38e5a680afb9294cb8a451311991d94765 (diff)
downloadpodman-cf35168f0a9cf457f0622cd89cff84bdbe610d58.tar.gz
podman-cf35168f0a9cf457f0622cd89cff84bdbe610d58.tar.bz2
podman-cf35168f0a9cf457f0622cd89cff84bdbe610d58.zip
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 <engzerjun@gmail.com>
Diffstat (limited to 'pkg/machine/qemu/config_test.go')
-rw-r--r--pkg/machine/qemu/config_test.go14
1 files changed, 3 insertions, 11 deletions
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)
}