]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/pack: use testing.T.TempDir in tests
authorTobias Klauser <tklauser@distanz.ch>
Wed, 7 Apr 2021 09:48:53 +0000 (11:48 +0200)
committerTobias Klauser <tobias.klauser@gmail.com>
Wed, 7 Apr 2021 13:24:10 +0000 (13:24 +0000)
Change-Id: I7371259cf5d64f04698ae1477c3de1255664178d
Reviewed-on: https://go-review.googlesource.com/c/go/+/307969
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Than McIntosh <thanm@google.com>
Trust: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/pack/pack_test.go

index 118376f9df6b88337a0362a29282dc51fbe3961c..7842b562dc3c1406895292865f4c4e4c4d4c08e3 100644 (file)
@@ -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)