1
0
mirror of https://github.com/golang/go synced 2024-11-25 20:37:56 -07:00

cmd/internal: use t.TempDir in tests

Change-Id: I3d4c66793afa3769a8450e2d65093a0f9115596e
Reviewed-on: https://go-review.googlesource.com/c/go/+/611043
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Kir Kolyshkin 2024-09-04 18:59:49 -07:00 committed by Gopher Robot
parent a77b93c0b2
commit 8f2486d2e0
7 changed files with 18 additions and 66 deletions

View File

@ -96,11 +96,7 @@ func TestIssue41621LargeNumberOfRelocations(t *testing.T) {
} }
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
tmpdir, err := os.MkdirTemp("", "lotsofrelocs") tmpdir := t.TempDir()
if err != nil {
t.Fatalf("can't create temp directory: %v\n", err)
}
defer os.RemoveAll(tmpdir)
// Emit testcase. // Emit testcase.
var w bytes.Buffer var w bytes.Buffer
@ -109,7 +105,7 @@ func TestIssue41621LargeNumberOfRelocations(t *testing.T) {
fmt.Fprintf(&w, "\t\"%d\",\n", i) fmt.Fprintf(&w, "\t\"%d\",\n", i)
} }
w.WriteString(issue41621epilog) w.WriteString(issue41621epilog)
err = os.WriteFile(tmpdir+"/large.go", w.Bytes(), 0666) err := os.WriteFile(tmpdir+"/large.go", w.Bytes(), 0666)
if err != nil { if err != nil {
t.Fatalf("can't write output: %v\n", err) t.Fatalf("can't write output: %v\n", err)
} }

View File

@ -25,11 +25,7 @@ func TestLargeBranch(t *testing.T) {
} }
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir, err := os.MkdirTemp("", "testlargebranch") dir := t.TempDir()
if err != nil {
t.Fatalf("Could not create directory: %v", err)
}
defer os.RemoveAll(dir)
// Generate a very large function. // Generate a very large function.
buf := bytes.NewBuffer(make([]byte, 0, genBufSize)) buf := bytes.NewBuffer(make([]byte, 0, genBufSize))

View File

@ -98,14 +98,10 @@ func TestSymbolTooLarge(t *testing.T) { // Issue 42054
t.Skip("skip on 32-bit architectures") t.Skip("skip on 32-bit architectures")
} }
tmpdir, err := os.MkdirTemp("", "TestSymbolTooLarge") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
src := filepath.Join(tmpdir, "p.go") src := filepath.Join(tmpdir, "p.go")
err = os.WriteFile(src, []byte("package p; var x [1<<32]byte"), 0666) err := os.WriteFile(src, []byte("package p; var x [1<<32]byte"), 0666)
if err != nil { if err != nil {
t.Fatalf("failed to write source file: %v\n", err) t.Fatalf("failed to write source file: %v\n", err)
} }

View File

@ -167,11 +167,7 @@ PNOP
func TestPfxAlign(t *testing.T) { func TestPfxAlign(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir, err := os.MkdirTemp("", "testpfxalign") dir := t.TempDir()
if err != nil {
t.Fatalf("could not create directory: %v", err)
}
defer os.RemoveAll(dir)
pgms := []struct { pgms := []struct {
text []byte text []byte
@ -188,7 +184,7 @@ func TestPfxAlign(t *testing.T) {
for _, pgm := range pgms { for _, pgm := range pgms {
tmpfile := filepath.Join(dir, "x.s") tmpfile := filepath.Join(dir, "x.s")
err = os.WriteFile(tmpfile, pgm.text, 0644) err := os.WriteFile(tmpfile, pgm.text, 0644)
if err != nil { if err != nil {
t.Fatalf("can't write output: %v\n", err) t.Fatalf("can't write output: %v\n", err)
} }
@ -217,11 +213,7 @@ func TestLarge(t *testing.T) {
} }
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir, err := os.MkdirTemp("", "testlarge") dir := t.TempDir()
if err != nil {
t.Fatalf("could not create directory: %v", err)
}
defer os.RemoveAll(dir)
// A few interesting test cases for long conditional branch fixups // A few interesting test cases for long conditional branch fixups
tests := []struct { tests := []struct {
@ -281,7 +273,7 @@ func TestLarge(t *testing.T) {
gen(buf, test.jmpinsn) gen(buf, test.jmpinsn)
tmpfile := filepath.Join(dir, "x.s") tmpfile := filepath.Join(dir, "x.s")
err = os.WriteFile(tmpfile, buf.Bytes(), 0644) err := os.WriteFile(tmpfile, buf.Bytes(), 0644)
if err != nil { if err != nil {
t.Fatalf("can't write output: %v\n", err) t.Fatalf("can't write output: %v\n", err)
} }
@ -336,16 +328,12 @@ func TestPCalign(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir, err := os.MkdirTemp("", "testpcalign") dir := t.TempDir()
if err != nil {
t.Fatalf("could not create directory: %v", err)
}
defer os.RemoveAll(dir)
// generate a test with valid uses of PCALIGN // generate a test with valid uses of PCALIGN
tmpfile := filepath.Join(dir, "x.s") tmpfile := filepath.Join(dir, "x.s")
err = os.WriteFile(tmpfile, []byte(validPCAlignSrc), 0644) err := os.WriteFile(tmpfile, []byte(validPCAlignSrc), 0644)
if err != nil { if err != nil {
t.Fatalf("can't write output: %v\n", err) t.Fatalf("can't write output: %v\n", err)
} }

View File

@ -24,11 +24,7 @@ func TestLargeBranch(t *testing.T) {
} }
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir, err := os.MkdirTemp("", "testlargebranch") dir := t.TempDir()
if err != nil {
t.Fatalf("Could not create directory: %v", err)
}
defer os.RemoveAll(dir)
// Generate a very large function. // Generate a very large function.
buf := bytes.NewBuffer(make([]byte, 0, 7000000)) buf := bytes.NewBuffer(make([]byte, 0, 7000000))
@ -67,11 +63,7 @@ func TestLargeCall(t *testing.T) {
} }
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
dir, err := os.MkdirTemp("", "testlargecall") dir := t.TempDir()
if err != nil {
t.Fatalf("could not create directory: %v", err)
}
defer os.RemoveAll(dir)
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module largecall"), 0644); err != nil { if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module largecall"), 0644); err != nil {
t.Fatalf("Failed to write file: %v\n", err) t.Fatalf("Failed to write file: %v\n", err)
@ -196,11 +188,7 @@ func genLargeJump(buf *bytes.Buffer) {
// Issue 20348. // Issue 20348.
func TestNoRet(t *testing.T) { func TestNoRet(t *testing.T) {
dir, err := os.MkdirTemp("", "testnoret") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
tmpfile := filepath.Join(dir, "x.s") tmpfile := filepath.Join(dir, "x.s")
if err := os.WriteFile(tmpfile, []byte("TEXT ·stub(SB),$0-0\nNOP\n"), 0644); err != nil { if err := os.WriteFile(tmpfile, []byte("TEXT ·stub(SB),$0-0\nNOP\n"), 0644); err != nil {
t.Fatal(err) t.Fatal(err)
@ -213,11 +201,7 @@ func TestNoRet(t *testing.T) {
} }
func TestImmediateSplitting(t *testing.T) { func TestImmediateSplitting(t *testing.T) {
dir, err := os.MkdirTemp("", "testimmsplit") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
tmpfile := filepath.Join(dir, "x.s") tmpfile := filepath.Join(dir, "x.s")
asm := ` asm := `
TEXT _stub(SB),$0-0 TEXT _stub(SB),$0-0

View File

@ -83,11 +83,7 @@ func normalize(s string) string {
} }
func asmOutput(t *testing.T, s string) []byte { func asmOutput(t *testing.T, s string) []byte {
tmpdir, err := os.MkdirTemp("", "progedittest") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpfile, err := os.Create(filepath.Join(tmpdir, "input.s")) tmpfile, err := os.Create(filepath.Join(tmpdir, "input.s"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -31,12 +31,8 @@ func main() {
` `
func objdumpOutput(t *testing.T, mname, source string) []byte { func objdumpOutput(t *testing.T, mname, source string) []byte {
tmpdir, err := os.MkdirTemp("", mname) tmpdir := t.TempDir()
if err != nil { err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte(fmt.Sprintf("module %s\n", mname)), 0666)
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
err = os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte(fmt.Sprintf("module %s\n", mname)), 0666)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }