1
0
mirror of https://github.com/golang/go synced 2024-11-26 17:46:57 -07:00

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 <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>
This commit is contained in:
Tobias Klauser 2021-04-07 11:48:53 +02:00 committed by Tobias Klauser
parent b55d900529
commit 4520da486b

View File

@ -19,15 +19,6 @@ import (
"time" "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. // testCreate creates an archive in the specified directory.
func testCreate(t *testing.T, dir string) { func testCreate(t *testing.T, dir string) {
name := filepath.Join(dir, "pack.a") 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. // 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. // Tests the rv and then the pv command on a new archive.
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
testCreate(t, dir) testCreate(t, dir)
} }
// Test that we can create an archive twice with the same name (Issue 8369). // Test that we can create an archive twice with the same name (Issue 8369).
func TestCreateTwice(t *testing.T) { func TestCreateTwice(t *testing.T) {
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
testCreate(t, dir) testCreate(t, dir)
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. // Test that we can create an archive, put some files in it, and get back a correct listing.
// Tests the tv command. // Tests the tv command.
func TestTableOfContents(t *testing.T) { func TestTableOfContents(t *testing.T) {
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
name := filepath.Join(dir, "pack.a") name := filepath.Join(dir, "pack.a")
ar := openArchive(name, os.O_RDWR|os.O_CREATE, nil) 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. // Test that we can create an archive, put some files in it, and get back a file.
// Tests the x command. // Tests the x command.
func TestExtract(t *testing.T) { func TestExtract(t *testing.T) {
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
name := filepath.Join(dir, "pack.a") name := filepath.Join(dir, "pack.a")
ar := openArchive(name, os.O_RDWR|os.O_CREATE, nil) ar := openArchive(name, os.O_RDWR|os.O_CREATE, nil)
// Add some entries by hand. // Add some entries by hand.
@ -173,8 +160,7 @@ func TestExtract(t *testing.T) {
func TestHello(t *testing.T) { func TestHello(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
hello := filepath.Join(dir, "hello.go") hello := filepath.Join(dir, "hello.go")
prog := ` prog := `
package main package main
@ -209,8 +195,7 @@ func TestLargeDefs(t *testing.T) {
} }
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
large := filepath.Join(dir, "large.go") large := filepath.Join(dir, "large.go")
f, err := os.Create(large) f, err := os.Create(large)
if err != nil { if err != nil {
@ -276,8 +261,7 @@ func TestLargeDefs(t *testing.T) {
func TestIssue21703(t *testing.T) { func TestIssue21703(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
const aSrc = `package a; const X = "\n!\n"` const aSrc = `package a; const X = "\n!\n"`
err := os.WriteFile(filepath.Join(dir, "a.go"), []byte(aSrc), 0666) 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) { func TestCreateWithCompilerObj(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
src := filepath.Join(dir, "p.go") src := filepath.Join(dir, "p.go")
prog := "package p; var X = 42\n" prog := "package p; var X = 42\n"
err := os.WriteFile(src, []byte(prog), 0666) err := os.WriteFile(src, []byte(prog), 0666)
@ -372,8 +355,7 @@ func TestCreateWithCompilerObj(t *testing.T) {
func TestRWithNonexistentFile(t *testing.T) { func TestRWithNonexistentFile(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir := tmpDir(t) dir := t.TempDir()
defer os.RemoveAll(dir)
src := filepath.Join(dir, "p.go") src := filepath.Join(dir, "p.go")
prog := "package p; var X = 42\n" prog := "package p; var X = 42\n"
err := os.WriteFile(src, []byte(prog), 0666) err := os.WriteFile(src, []byte(prog), 0666)