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

runtime: replace os.MkdirTemp with T.TempDir

Updates #45402

Change-Id: I3aa82fc2486b4de49b45388bbab24f5ffe558f91
Reviewed-on: https://go-review.googlesource.com/c/go/+/307989
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
ianwoolf 2021-04-07 20:16:58 +08:00 committed by Emmanuel Odeke
parent 2123dfba65
commit a7e16abb22
7 changed files with 31 additions and 113 deletions

View File

@ -85,11 +85,7 @@ func TestCrashDumpsAllThreads(t *testing.T) {
t.Parallel() t.Parallel()
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(crashDumpsAllThreadsSource), 0666); err != nil { if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(crashDumpsAllThreadsSource), 0666); err != nil {
t.Fatalf("failed to create Go file: %v", err) t.Fatalf("failed to create Go file: %v", err)

View File

@ -20,11 +20,7 @@ import (
) )
func TestOutput(t *testing.T) { func TestOutput(t *testing.T) {
pkgdir, err := os.MkdirTemp("", "go-build-race-output") pkgdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(pkgdir)
out, err := exec.Command(testenv.GoToolPath(t), "install", "-race", "-pkgdir="+pkgdir, "testing").CombinedOutput() out, err := exec.Command(testenv.GoToolPath(t), "install", "-race", "-pkgdir="+pkgdir, "testing").CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("go install -race: %v\n%s", err, out) t.Fatalf("go install -race: %v\n%s", err, out)
@ -35,11 +31,7 @@ func TestOutput(t *testing.T) {
t.Logf("test %v runs only on %v, skipping: ", test.name, test.goos) t.Logf("test %v runs only on %v, skipping: ", test.name, test.goos)
continue continue
} }
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
source := "main.go" source := "main.go"
if test.run == "test" { if test.run == "test" {
source = "main_test.go" source = "main_test.go"

View File

@ -17,7 +17,7 @@ import (
func TestNoRaceIOFile(t *testing.T) { func TestNoRaceIOFile(t *testing.T) {
x := 0 x := 0
path, _ := os.MkdirTemp("", "race_test") path := t.TempDir()
fname := filepath.Join(path, "data") fname := filepath.Join(path, "data")
go func() { go func() {
x = 42 x = 42

View File

@ -169,11 +169,7 @@ func testGdbPython(t *testing.T, cgo bool) {
checkGdbVersion(t) checkGdbVersion(t)
checkGdbPython(t) checkGdbPython(t)
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString("package main\n") buf.WriteString("package main\n")
@ -194,7 +190,7 @@ func testGdbPython(t *testing.T, cgo bool) {
} }
} }
err = os.WriteFile(filepath.Join(dir, "main.go"), src, 0644) err := os.WriteFile(filepath.Join(dir, "main.go"), src, 0644)
if err != nil { if err != nil {
t.Fatalf("failed to create file: %v", err) t.Fatalf("failed to create file: %v", err)
} }
@ -403,15 +399,11 @@ func TestGdbBacktrace(t *testing.T) {
t.Parallel() t.Parallel()
checkGdbVersion(t) checkGdbVersion(t)
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
// Build the source code. // Build the source code.
src := filepath.Join(dir, "main.go") src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(backtraceSource), 0644) err := os.WriteFile(src, []byte(backtraceSource), 0644)
if err != nil { if err != nil {
t.Fatalf("failed to create file: %v", err) t.Fatalf("failed to create file: %v", err)
} }
@ -481,15 +473,11 @@ func TestGdbAutotmpTypes(t *testing.T) {
t.Skip("TestGdbAutotmpTypes is too slow on aix/ppc64") t.Skip("TestGdbAutotmpTypes is too slow on aix/ppc64")
} }
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
// Build the source code. // Build the source code.
src := filepath.Join(dir, "main.go") src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(autotmpTypeSource), 0644) err := os.WriteFile(src, []byte(autotmpTypeSource), 0644)
if err != nil { if err != nil {
t.Fatalf("failed to create file: %v", err) t.Fatalf("failed to create file: %v", err)
} }
@ -550,15 +538,11 @@ func TestGdbConst(t *testing.T) {
t.Parallel() t.Parallel()
checkGdbVersion(t) checkGdbVersion(t)
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
// Build the source code. // Build the source code.
src := filepath.Join(dir, "main.go") src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(constsSource), 0644) err := os.WriteFile(src, []byte(constsSource), 0644)
if err != nil { if err != nil {
t.Fatalf("failed to create file: %v", err) t.Fatalf("failed to create file: %v", err)
} }
@ -617,15 +601,11 @@ func TestGdbPanic(t *testing.T) {
t.Parallel() t.Parallel()
checkGdbVersion(t) checkGdbVersion(t)
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
// Build the source code. // Build the source code.
src := filepath.Join(dir, "main.go") src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(panicSource), 0644) err := os.WriteFile(src, []byte(panicSource), 0644)
if err != nil { if err != nil {
t.Fatalf("failed to create file: %v", err) t.Fatalf("failed to create file: %v", err)
} }
@ -695,15 +675,11 @@ func TestGdbInfCallstack(t *testing.T) {
t.Parallel() t.Parallel()
checkGdbVersion(t) checkGdbVersion(t)
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
// Build the source code. // Build the source code.
src := filepath.Join(dir, "main.go") src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(InfCallstackSource), 0644) err := os.WriteFile(src, []byte(InfCallstackSource), 0644)
if err != nil { if err != nil {
t.Fatalf("failed to create file: %v", err) t.Fatalf("failed to create file: %v", err)
} }

View File

@ -142,14 +142,10 @@ func TestLldbPython(t *testing.T) {
checkLldbPython(t) checkLldbPython(t)
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
src := filepath.Join(dir, "main.go") src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(lldbHelloSource), 0644) err := os.WriteFile(src, []byte(lldbHelloSource), 0644)
if err != nil { if err != nil {
t.Fatalf("failed to create src file: %v", err) t.Fatalf("failed to create src file: %v", err)
} }

View File

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"internal/testenv" "internal/testenv"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -29,11 +28,7 @@ func TestVectoredHandlerDontCrashOnLibrary(t *testing.T) {
testenv.MustHaveExecPath(t, "gcc") testenv.MustHaveExecPath(t, "gcc")
testprog.Lock() testprog.Lock()
defer testprog.Unlock() defer testprog.Unlock()
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
// build go dll // build go dll
dll := filepath.Join(dir, "testwinlib.dll") dll := filepath.Join(dir, "testwinlib.dll")
@ -157,11 +152,7 @@ func TestLibraryCtrlHandler(t *testing.T) {
testenv.MustHaveExecPath(t, "gcc") testenv.MustHaveExecPath(t, "gcc")
testprog.Lock() testprog.Lock()
defer testprog.Unlock() defer testprog.Unlock()
dir, err := os.MkdirTemp("", "go-build") dir := t.TempDir()
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
// build go dll // build go dll
dll := filepath.Join(dir, "dummy.dll") dll := filepath.Join(dir, "dummy.dll")

View File

@ -543,11 +543,7 @@ func TestStdcallAndCDeclCallbacks(t *testing.T) {
if _, err := exec.LookPath("gcc"); err != nil { if _, err := exec.LookPath("gcc"); err != nil {
t.Skip("skipping test: gcc is missing") t.Skip("skipping test: gcc is missing")
} }
tmp, err := os.MkdirTemp("", "TestCDeclCallback") tmp := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmp)
oldRegs := runtime.SetIntArgRegs(abi.IntArgRegs) oldRegs := runtime.SetIntArgRegs(abi.IntArgRegs)
defer runtime.SetIntArgRegs(oldRegs) defer runtime.SetIntArgRegs(oldRegs)
@ -702,14 +698,10 @@ uintptr_t cfunc(callback f, uintptr_t n) {
return r; return r;
} }
` `
tmpdir, err := os.MkdirTemp("", "TestReturnAfterStackGrowInCallback") tmpdir := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmpdir)
srcname := "mydll.c" srcname := "mydll.c"
err = os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0) err := os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -771,14 +763,10 @@ uintptr_t cfunc(uintptr_t a, double b, float c, double d) {
return 0; return 0;
} }
` `
tmpdir, err := os.MkdirTemp("", "TestFloatArgs") tmpdir := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmpdir)
srcname := "mydll.c" srcname := "mydll.c"
err = os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0) err := os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -833,14 +821,10 @@ double cfuncDouble(uintptr_t a, double b, float c, double d) {
return 0; return 0;
} }
` `
tmpdir, err := os.MkdirTemp("", "TestFloatReturn") tmpdir := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmpdir)
srcname := "mydll.c" srcname := "mydll.c"
err = os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0) err := os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -1048,16 +1032,7 @@ func TestDLLPreloadMitigation(t *testing.T) {
t.Skip("skipping test: gcc is missing") t.Skip("skipping test: gcc is missing")
} }
tmpdir, err := os.MkdirTemp("", "TestDLLPreloadMitigation") tmpdir := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer func() {
err := os.RemoveAll(tmpdir)
if err != nil {
t.Error(err)
}
}()
dir0, err := os.Getwd() dir0, err := os.Getwd()
if err != nil { if err != nil {
@ -1135,11 +1110,7 @@ func TestBigStackCallbackSyscall(t *testing.T) {
t.Fatal("Abs failed: ", err) t.Fatal("Abs failed: ", err)
} }
tmpdir, err := os.MkdirTemp("", "TestBigStackCallback") tmpdir := t.TempDir()
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmpdir)
outname := "mydll.dll" outname := "mydll.dll"
cmd := exec.Command("gcc", "-shared", "-s", "-Werror", "-o", outname, srcname) cmd := exec.Command("gcc", "-shared", "-s", "-Werror", "-o", outname, srcname)
@ -1284,14 +1255,10 @@ func BenchmarkOsYield(b *testing.B) {
} }
func BenchmarkRunningGoProgram(b *testing.B) { func BenchmarkRunningGoProgram(b *testing.B) {
tmpdir, err := os.MkdirTemp("", "BenchmarkRunningGoProgram") tmpdir := b.TempDir()
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(tmpdir)
src := filepath.Join(tmpdir, "main.go") src := filepath.Join(tmpdir, "main.go")
err = os.WriteFile(src, []byte(benchmarkRunningGoProgram), 0666) err := os.WriteFile(src, []byte(benchmarkRunningGoProgram), 0666)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }