From 4520da486b6d236090b1d98ce4707c5bcd19cb70 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 7 Apr 2021 11:48:53 +0200 Subject: [PATCH] cmd/pack: use testing.T.TempDir in tests Change-Id: I7371259cf5d64f04698ae1477c3de1255664178d Reviewed-on: https://go-review.googlesource.com/c/go/+/307969 Run-TryBot: Tobias Klauser Reviewed-by: Dave Cheney Reviewed-by: Than McIntosh Trust: Tobias Klauser TryBot-Result: Go Bot --- src/cmd/pack/pack_test.go | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/cmd/pack/pack_test.go b/src/cmd/pack/pack_test.go index 118376f9df..7842b562dc 100644 --- a/src/cmd/pack/pack_test.go +++ b/src/cmd/pack/pack_test.go @@ -19,15 +19,6 @@ import ( "time" ) -// tmpDir creates a temporary directory and returns its name. -func tmpDir(t *testing.T) string { - name, err := os.MkdirTemp("", "pack") - if err != nil { - t.Fatal(err) - } - return name -} - // testCreate creates an archive in the specified directory. func testCreate(t *testing.T, dir string) { name := filepath.Join(dir, "pack.a") @@ -57,15 +48,13 @@ func testCreate(t *testing.T, dir string) { // Test that we can create an archive, write to it, and get the same contents back. // Tests the rv and then the pv command on a new archive. func TestCreate(t *testing.T) { - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() testCreate(t, dir) } // Test that we can create an archive twice with the same name (Issue 8369). func TestCreateTwice(t *testing.T) { - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() testCreate(t, dir) testCreate(t, dir) } @@ -73,8 +62,7 @@ func TestCreateTwice(t *testing.T) { // Test that we can create an archive, put some files in it, and get back a correct listing. // Tests the tv command. func TestTableOfContents(t *testing.T) { - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() name := filepath.Join(dir, "pack.a") ar := openArchive(name, os.O_RDWR|os.O_CREATE, nil) @@ -131,8 +119,7 @@ func TestTableOfContents(t *testing.T) { // Test that we can create an archive, put some files in it, and get back a file. // Tests the x command. func TestExtract(t *testing.T) { - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() name := filepath.Join(dir, "pack.a") ar := openArchive(name, os.O_RDWR|os.O_CREATE, nil) // Add some entries by hand. @@ -173,8 +160,7 @@ func TestExtract(t *testing.T) { func TestHello(t *testing.T) { testenv.MustHaveGoBuild(t) - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() hello := filepath.Join(dir, "hello.go") prog := ` package main @@ -209,8 +195,7 @@ func TestLargeDefs(t *testing.T) { } testenv.MustHaveGoBuild(t) - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() large := filepath.Join(dir, "large.go") f, err := os.Create(large) if err != nil { @@ -276,8 +261,7 @@ func TestLargeDefs(t *testing.T) { func TestIssue21703(t *testing.T) { testenv.MustHaveGoBuild(t) - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() const aSrc = `package a; const X = "\n!\n"` err := os.WriteFile(filepath.Join(dir, "a.go"), []byte(aSrc), 0666) @@ -307,8 +291,7 @@ func TestIssue21703(t *testing.T) { func TestCreateWithCompilerObj(t *testing.T) { testenv.MustHaveGoBuild(t) - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() src := filepath.Join(dir, "p.go") prog := "package p; var X = 42\n" err := os.WriteFile(src, []byte(prog), 0666) @@ -372,8 +355,7 @@ func TestCreateWithCompilerObj(t *testing.T) { func TestRWithNonexistentFile(t *testing.T) { testenv.MustHaveGoBuild(t) - dir := tmpDir(t) - defer os.RemoveAll(dir) + dir := t.TempDir() src := filepath.Join(dir, "p.go") prog := "package p; var X = 42\n" err := os.WriteFile(src, []byte(prog), 0666) -- 2.48.1