all: use testing.T.TempDir instead os.MkdirTemp

This saves code and reduces clutter.

Change-Id: I81ec37f6e0a61b21abbea3b9672c6566f34b1a9e
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/477935
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/cmd/govulncheck_sandbox/govulncheck_sandbox_test.go b/cmd/govulncheck_sandbox/govulncheck_sandbox_test.go
index 59fb837..536d504 100644
--- a/cmd/govulncheck_sandbox/govulncheck_sandbox_test.go
+++ b/cmd/govulncheck_sandbox/govulncheck_sandbox_test.go
@@ -26,17 +26,7 @@
 		t.Skip("cannot run on Windows")
 	}
 
-	tempDir, err := os.MkdirTemp("", "installGovulncheck")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer func() {
-		if err := os.RemoveAll(tempDir); err != nil {
-			t.Fatal(err)
-		}
-	}()
-
-	govulncheckPath, err := buildtest.BuildGovulncheck(tempDir)
+	govulncheckPath, err := buildtest.BuildGovulncheck(t.TempDir())
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/internal/buildtest/buildtest.go b/internal/buildtest/buildtest.go
index c6a93bb..33b7ab3 100644
--- a/internal/buildtest/buildtest.go
+++ b/internal/buildtest/buildtest.go
@@ -23,9 +23,8 @@
 
 // GoBuild runs "go build" on dir using the additional environment variables in
 // envVarVals, which should be an alternating list of variables and values.
-// It returns the path to the resulting binary, and a function
-// to call when finished with the binary.
-func GoBuild(t *testing.T, dir, tags string, envVarVals ...string) (binaryPath string, cleanup func()) {
+// It returns the path to the resulting binary.
+func GoBuild(t *testing.T, dir, tags string, envVarVals ...string) (binaryPath string) {
 	switch runtime.GOOS {
 	case "android", "js", "ios":
 		t.Skipf("skipping on OS without 'go build' %s", runtime.GOOS)
@@ -47,14 +46,11 @@
 		t.Skipf("skipping unsupported GOOS/GOARCH pair %s", gg)
 	}
 
-	tmpDir, err := os.MkdirTemp("", "buildtest")
-	if err != nil {
-		t.Fatal(err)
-	}
 	abs, err := filepath.Abs(dir)
 	if err != nil {
 		t.Fatal(err)
 	}
+	tmpDir := t.TempDir()
 	binaryPath = filepath.Join(tmpDir, filepath.Base(abs))
 	var exeSuffix string
 	if runtime.GOOS == "windows" {
@@ -77,7 +73,7 @@
 	if err := cmd.Run(); err != nil {
 		t.Fatal(err)
 	}
-	return binaryPath + exeSuffix, func() { os.RemoveAll(tmpDir) }
+	return binaryPath + exeSuffix
 }
 
 // lookEnv looks for name in env, a list of "VAR=VALUE" strings. It returns
diff --git a/internal/buildtest/buildtest_test.go b/internal/buildtest/buildtest_test.go
index 9d64764..3ec4f82 100644
--- a/internal/buildtest/buildtest_test.go
+++ b/internal/buildtest/buildtest_test.go
@@ -5,22 +5,11 @@
 package buildtest
 
 import (
-	"os"
 	"testing"
 )
 
 func TestBuildGovulncheck(t *testing.T) {
-	tempDir, err := os.MkdirTemp("", "testBuildGovulncheck")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer func() {
-		if err := os.RemoveAll(tempDir); err != nil {
-			t.Fatal(err)
-		}
-	}()
-
-	if _, err := BuildGovulncheck(tempDir); err != nil {
+	if _, err := BuildGovulncheck(t.TempDir()); err != nil {
 		t.Fatal(err)
 	}
 }
diff --git a/internal/modules/modules_test.go b/internal/modules/modules_test.go
index b0ea71d..fbda4bd 100644
--- a/internal/modules/modules_test.go
+++ b/internal/modules/modules_test.go
@@ -7,7 +7,6 @@
 import (
 	"archive/zip"
 	"bytes"
-	"os"
 	"path/filepath"
 	"testing"
 )
@@ -44,17 +43,7 @@
 		t.Fatal(err)
 	}
 
-	// Create space to unpack the module.
-	tempDir, err := os.MkdirTemp("", "testWriteZip")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer func() {
-		if err := os.RemoveAll(tempDir); err != nil {
-			t.Fatal(err)
-		}
-	}()
-
+	tempDir := t.TempDir()
 	if err := writeZip(r, tempDir, ""); err != nil {
 		t.Error(err)
 	}
diff --git a/internal/worker/analysis_test.go b/internal/worker/analysis_test.go
index fc31cce..4ddb60d 100644
--- a/internal/worker/analysis_test.go
+++ b/internal/worker/analysis_test.go
@@ -21,8 +21,7 @@
 )
 
 func TestRunAnalysisBinary(t *testing.T) {
-	binPath, cleanup := buildtest.GoBuild(t, "testdata/analyzer", "")
-	defer cleanup()
+	binPath := buildtest.GoBuild(t, "testdata/analyzer", "")
 
 	got, err := runAnalysisBinary(nil, binPath, "-name Fact", "testdata/module")
 	if err != nil {
@@ -95,8 +94,7 @@
 		modulePath = "a.com/m"
 		version    = "v1.2.3"
 	)
-	binaryPath, cleanup := buildtest.GoBuild(t, "testdata/analyzer", "")
-	defer cleanup()
+	binaryPath := buildtest.GoBuild(t, "testdata/analyzer", "")
 	proxyClient, cleanup2 := proxytest.SetupTestClient(t, []*proxytest.Module{
 		{
 			ModulePath: modulePath,