test: use t.TempDir to create temporary test directory

This pull request replaces `os.MkdirTemp` with `t.TempDir`. We can use the `t.TempDir` function from the `testing` package to create temporary directory. 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

```go
func TestFoo(t *testing.T) {
	// before
	tmpDir, err := os.MkdirTemp("", "")
	if err != nil {
		t.Fatalf("failed to create tmp directory: %v", err)
	}
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}

	// now
	tmpDir := t.TempDir()
}
```
This commit is contained in:
Eng Zer Jun
2023-03-17 04:40:05 +08:00
committed by GitHub
parent f202764973
commit c473dceda8
2 changed files with 2 additions and 18 deletions

View File

@@ -37,17 +37,7 @@ func applyCIMultiplier(timeout time.Duration) time.Duration {
} }
func TestYaegiCmdCancel(t *testing.T) { func TestYaegiCmdCancel(t *testing.T) {
tmp, err := os.MkdirTemp("", "yaegi-") tmp := t.TempDir()
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)
}
}()
yaegi := filepath.Join(tmp, "yaegi") yaegi := filepath.Join(tmp, "yaegi")
args := []string{"build"} args := []string{"build"}

View File

@@ -49,13 +49,7 @@ func Test_effectivePkg(t *testing.T) {
func Test_pkgDir(t *testing.T) { func Test_pkgDir(t *testing.T) {
// create GOPATH // create GOPATH
goPath, err := os.MkdirTemp("", "pkdir") goPath := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer func() {
_ = os.RemoveAll(goPath)
}()
// Create project // Create project
project := filepath.Join(goPath, "src", "guthib.com", "foo", "root") project := filepath.Join(goPath, "src", "guthib.com", "foo", "root")