From 1c22051eb602fce2b86c2d63d42666bdd2f90424 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Thu, 16 Mar 2023 22:16:08 +0800 Subject: [PATCH] test: use `t.TempDir` to create temporary test directory This commit replaces `os.MkdirTemp` 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 `os.MkdirTemp` 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 --- cmd/yaegi/yaegi_test.go | 12 +----------- interp/src_test.go | 8 +------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/cmd/yaegi/yaegi_test.go b/cmd/yaegi/yaegi_test.go index ef9e653f6..db005d4fc 100644 --- a/cmd/yaegi/yaegi_test.go +++ b/cmd/yaegi/yaegi_test.go @@ -37,17 +37,7 @@ func applyCIMultiplier(timeout time.Duration) time.Duration { } func TestYaegiCmdCancel(t *testing.T) { - tmp, err := os.MkdirTemp("", "yaegi-") - if err != nil { - t.Fatalf("failed to create tmp directory: %v", err) - } - defer func() { - err = os.RemoveAll(tmp) - if err != nil { - t.Errorf("failed to clean up %v: %v", tmp, err) - } - }() - + tmp := t.TempDir() yaegi := filepath.Join(tmp, "yaegi") args := []string{"build"} diff --git a/interp/src_test.go b/interp/src_test.go index 9944c23af..e69b98b14 100644 --- a/interp/src_test.go +++ b/interp/src_test.go @@ -49,13 +49,7 @@ func Test_effectivePkg(t *testing.T) { func Test_pkgDir(t *testing.T) { // create GOPATH - goPath, err := os.MkdirTemp("", "pkdir") - if err != nil { - t.Fatal(err) - } - defer func() { - _ = os.RemoveAll(goPath) - }() + goPath := t.TempDir() // Create project project := filepath.Join(goPath, "src", "guthib.com", "foo", "root")