cmd/dlv, pkg/proc: use T.TempDir in tests (#3256)

This commit is contained in:
Oleksandr Redko 2023-01-31 01:04:55 +02:00 committed by GitHub
parent a01fe73845
commit 64a9024735
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 69 deletions

@ -204,7 +204,7 @@ func testOutput(t *testing.T, dlvbin, output string, delveCmds []string) (stdout
return return
} }
func getDlvBin(t *testing.T) (string, string) { func getDlvBin(t *testing.T) string {
// In case this was set in the environment // In case this was set in the environment
// from getDlvBinEBPF lets clear it here so // from getDlvBinEBPF lets clear it here so
// we can ensure we don't get build errors // we can ensure we don't get build errors
@ -217,17 +217,12 @@ func getDlvBin(t *testing.T) (string, string) {
return getDlvBinInternal(t, tags) return getDlvBinInternal(t, tags)
} }
func getDlvBinEBPF(t *testing.T) (string, string) { func getDlvBinEBPF(t *testing.T) string {
return getDlvBinInternal(t, "-tags", "ebpf") return getDlvBinInternal(t, "-tags", "ebpf")
} }
func getDlvBinInternal(t *testing.T, goflags ...string) (string, string) { func getDlvBinInternal(t *testing.T, goflags ...string) string {
tmpdir, err := ioutil.TempDir("", "TestDlv") dlvbin := filepath.Join(t.TempDir(), "dlv.exe")
if err != nil {
t.Fatal(err)
}
dlvbin := filepath.Join(tmpdir, "dlv.exe")
args := append([]string{"build", "-o", dlvbin}, goflags...) args := append([]string{"build", "-o", dlvbin}, goflags...)
args = append(args, "github.com/go-delve/delve/cmd/dlv") args = append(args, "github.com/go-delve/delve/cmd/dlv")
@ -236,16 +231,15 @@ func getDlvBinInternal(t *testing.T, goflags ...string) (string, string) {
t.Fatalf("go build -o %v github.com/go-delve/delve/cmd/dlv: %v\n%s", dlvbin, err, string(out)) t.Fatalf("go build -o %v github.com/go-delve/delve/cmd/dlv: %v\n%s", dlvbin, err, string(out))
} }
return dlvbin, tmpdir return dlvbin
} }
// TestOutput verifies that the debug executable is created in the correct path // TestOutput verifies that the debug executable is created in the correct path
// and removed after exit. // and removed after exit.
func TestOutput(t *testing.T) { func TestOutput(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
for _, output := range []string{"", "myownname", filepath.Join(tmpdir, "absolute.path")} { for _, output := range []string{"", "myownname", filepath.Join(t.TempDir(), "absolute.path")} {
testOutput(t, dlvbin, output, []string{"exit"}) testOutput(t, dlvbin, output, []string{"exit"})
const hello = "hello world!" const hello = "hello world!"
@ -260,8 +254,7 @@ func TestOutput(t *testing.T) {
func TestContinue(t *testing.T) { func TestContinue(t *testing.T) {
const listenAddr = "127.0.0.1:40573" const listenAddr = "127.0.0.1:40573"
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest") buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr) cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr)
@ -301,8 +294,7 @@ func TestChildProcessExitWhenNoDebugInfo(t *testing.T) {
t.Skip("test skipped, `ps` not found") t.Skip("test skipped, `ps` not found")
} }
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
fix := protest.BuildFixture("http_server", noDebugFlags) fix := protest.BuildFixture("http_server", noDebugFlags)
@ -345,8 +337,7 @@ func TestChildProcessExitWhenNoDebugInfo(t *testing.T) {
func TestRedirect(t *testing.T) { func TestRedirect(t *testing.T) {
const listenAddr = "127.0.0.1:40573" const listenAddr = "127.0.0.1:40573"
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
catfixture := filepath.Join(protest.FindFixturesDir(), "cat.go") catfixture := filepath.Join(protest.FindFixturesDir(), "cat.go")
cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr, "-r", catfixture, catfixture) cmd := exec.Command(dlvbin, "debug", "--headless", "--continue", "--accept-multiclient", "--listen", listenAddr, "-r", catfixture, catfixture)
@ -439,12 +430,10 @@ func TestGeneratedDoc(t *testing.T) {
checkAutogenDoc(t, "Documentation/cli/README.md", "_scripts/gen-cli-docs.go", generatedBuf.Bytes()) checkAutogenDoc(t, "Documentation/cli/README.md", "_scripts/gen-cli-docs.go", generatedBuf.Bytes())
// Checks gen-usage-docs.go // Checks gen-usage-docs.go
tempDir, err := ioutil.TempDir(os.TempDir(), "test-gen-doc") tempDir := t.TempDir()
assertNoError(err, t, "TempDir")
defer protest.SafeRemoveAll(tempDir)
cmd := exec.Command("go", "run", "_scripts/gen-usage-docs.go", tempDir) cmd := exec.Command("go", "run", "_scripts/gen-usage-docs.go", tempDir)
cmd.Dir = projectRoot() cmd.Dir = projectRoot()
err = cmd.Run() err := cmd.Run()
assertNoError(err, t, "go run _scripts/gen-usage-docs.go") assertNoError(err, t, "go run _scripts/gen-usage-docs.go")
entries, err := ioutil.ReadDir(tempDir) entries, err := ioutil.ReadDir(tempDir)
assertNoError(err, t, "ReadDir") assertNoError(err, t, "ReadDir")
@ -476,8 +465,7 @@ func TestGeneratedDoc(t *testing.T) {
} }
func TestExitInInit(t *testing.T) { func TestExitInInit(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest") buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
exitInit := filepath.Join(protest.FindFixturesDir(), "exit.init") exitInit := filepath.Join(protest.FindFixturesDir(), "exit.init")
@ -677,8 +665,7 @@ func TestTypecheckRPC(t *testing.T) {
func TestDAPCmd(t *testing.T) { func TestDAPCmd(t *testing.T) {
const listenAddr = "127.0.0.1:40575" const listenAddr = "127.0.0.1:40575"
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
cmd := exec.Command(dlvbin, "dap", "--log-output=dap", "--log", "--listen", listenAddr) cmd := exec.Command(dlvbin, "dap", "--log-output=dap", "--log", "--listen", listenAddr)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
@ -727,8 +714,7 @@ func TestDAPCmd(t *testing.T) {
func TestDAPCmdWithNoDebugBinary(t *testing.T) { func TestDAPCmdWithNoDebugBinary(t *testing.T) {
const listenAddr = "127.0.0.1:40579" const listenAddr = "127.0.0.1:40579"
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
cmd := exec.Command(dlvbin, "dap", "--log", "--listen", listenAddr) cmd := exec.Command(dlvbin, "dap", "--log", "--listen", listenAddr)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
@ -793,8 +779,7 @@ func newDAPRemoteClient(t *testing.T, addr string, isDlvAttach bool, isMulti boo
func TestRemoteDAPClient(t *testing.T) { func TestRemoteDAPClient(t *testing.T) {
const listenAddr = "127.0.0.1:40576" const listenAddr = "127.0.0.1:40576"
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest") buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
cmd := exec.Command(dlvbin, "debug", "--headless", "--log-output=dap", "--log", "--listen", listenAddr) cmd := exec.Command(dlvbin, "debug", "--headless", "--log-output=dap", "--log", "--listen", listenAddr)
@ -847,8 +832,7 @@ func closeDAPRemoteMultiClient(t *testing.T, c *daptest.Client, expectStatus str
func TestRemoteDAPClientMulti(t *testing.T) { func TestRemoteDAPClientMulti(t *testing.T) {
const listenAddr = "127.0.0.1:40577" const listenAddr = "127.0.0.1:40577"
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest") buildtestdir := filepath.Join(protest.FindFixturesDir(), "buildtest")
cmd := exec.Command(dlvbin, "debug", "--headless", "--accept-multiclient", "--log-output=debugger", "--log", "--listen", listenAddr) cmd := exec.Command(dlvbin, "debug", "--headless", "--accept-multiclient", "--log-output=debugger", "--log", "--listen", listenAddr)
@ -915,8 +899,7 @@ func TestRemoteDAPClientMulti(t *testing.T) {
func TestRemoteDAPClientAfterContinue(t *testing.T) { func TestRemoteDAPClientAfterContinue(t *testing.T) {
const listenAddr = "127.0.0.1:40578" const listenAddr = "127.0.0.1:40578"
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
fixture := protest.BuildFixture("loopprog", 0) fixture := protest.BuildFixture("loopprog", 0)
cmd := exec.Command(dlvbin, "exec", fixture.Path, "--headless", "--continue", "--accept-multiclient", "--log-output=debugger,dap", "--log", "--listen", listenAddr) cmd := exec.Command(dlvbin, "exec", fixture.Path, "--headless", "--continue", "--accept-multiclient", "--log-output=debugger,dap", "--log", "--listen", listenAddr)
@ -977,8 +960,7 @@ func TestDAPCmdWithClient(t *testing.T) {
} }
defer listener.Close() defer listener.Close()
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
cmd := exec.Command(dlvbin, "dap", "--log-output=dap", "--log", "--client-addr", listener.Addr().String()) cmd := exec.Command(dlvbin, "dap", "--log-output=dap", "--log", "--client-addr", listener.Addr().String())
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
@ -1012,13 +994,12 @@ func TestDAPCmdWithClient(t *testing.T) {
} }
func TestTrace(t *testing.T) { func TestTrace(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
expected := []byte("> goroutine(1): main.foo(99, 9801)\n>> goroutine(1): => (9900)\n") expected := []byte("> goroutine(1): main.foo(99, 9801)\n>> goroutine(1): => (9900)\n")
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "issue573.go"), "foo") cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "issue573.go"), "foo")
rdr, err := cmd.StderrPipe() rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe") assertNoError(err, t, "stderr pipe")
defer rdr.Close() defer rdr.Close()
@ -1037,13 +1018,12 @@ func TestTrace(t *testing.T) {
} }
func TestTrace2(t *testing.T) { func TestTrace2(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
expected := []byte("> goroutine(1): main.callme(2)\n>> goroutine(1): => (4)\n") expected := []byte("> goroutine(1): main.callme(2)\n>> goroutine(1): => (4)\n")
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "traceprog.go"), "callme") cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "traceprog.go"), "callme")
rdr, err := cmd.StderrPipe() rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe") assertNoError(err, t, "stderr pipe")
defer rdr.Close() defer rdr.Close()
@ -1062,8 +1042,7 @@ func TestTrace2(t *testing.T) {
} }
func TestTraceMultipleGoroutines(t *testing.T) { func TestTraceMultipleGoroutines(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
// TODO(derekparker) this test has to be a bit vague to avoid flakyness. // TODO(derekparker) this test has to be a bit vague to avoid flakyness.
// I think a future improvement could be to use regexp captures to match the // I think a future improvement could be to use regexp captures to match the
@ -1072,7 +1051,7 @@ func TestTraceMultipleGoroutines(t *testing.T) {
expected2 := []byte("=> (0)\n") expected2 := []byte("=> (0)\n")
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "goroutines-trace.go"), "callme") cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "goroutines-trace.go"), "callme")
rdr, err := cmd.StderrPipe() rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe") assertNoError(err, t, "stderr pipe")
defer rdr.Close() defer rdr.Close()
@ -1102,8 +1081,7 @@ func TestTracePid(t *testing.T) {
} }
} }
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
expected := []byte("goroutine(1): main.A()\n>> goroutine(1): => ()\n") expected := []byte("goroutine(1): main.A()\n>> goroutine(1): => ()\n")
@ -1136,14 +1114,13 @@ func TestTracePid(t *testing.T) {
} }
func TestTraceBreakpointExists(t *testing.T) { func TestTraceBreakpointExists(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
// We always set breakpoints on some runtime functions at startup, so this would return with // We always set breakpoints on some runtime functions at startup, so this would return with
// a breakpoints exists error. // a breakpoints exists error.
// TODO: Perhaps we shouldn't be setting these default breakpoints in trace mode, however. // TODO: Perhaps we shouldn't be setting these default breakpoints in trace mode, however.
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "issue573.go"), "runtime.*") cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "issue573.go"), "runtime.*")
rdr, err := cmd.StderrPipe() rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe") assertNoError(err, t, "stderr pipe")
defer rdr.Close() defer rdr.Close()
@ -1163,11 +1140,10 @@ func TestTraceBreakpointExists(t *testing.T) {
} }
func TestTracePrintStack(t *testing.T) { func TestTracePrintStack(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(tmpdir, "__debug"), "--stack", "2", filepath.Join(fixtures, "issue573.go"), "foo") cmd := exec.Command(dlvbin, "trace", "--output", filepath.Join(t.TempDir(), "__debug"), "--stack", "2", filepath.Join(fixtures, "issue573.go"), "foo")
rdr, err := cmd.StderrPipe() rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe") assertNoError(err, t, "stderr pipe")
defer rdr.Close() defer rdr.Close()
@ -1203,13 +1179,12 @@ func TestTraceEBPF(t *testing.T) {
t.Skip("test must be run as root") t.Skip("test must be run as root")
} }
dlvbin, tmpdir := getDlvBinEBPF(t) dlvbin := getDlvBinEBPF(t)
defer os.RemoveAll(tmpdir)
expected := []byte("> (1) main.foo(99, 9801)\n=> \"9900\"") expected := []byte("> (1) main.foo(99, 9801)\n=> \"9900\"")
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "issue573.go"), "foo") cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "issue573.go"), "foo")
rdr, err := cmd.StderrPipe() rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe") assertNoError(err, t, "stderr pipe")
defer rdr.Close() defer rdr.Close()
@ -1243,8 +1218,7 @@ func TestTraceEBPF2(t *testing.T) {
t.Skip("test must be run as root") t.Skip("test must be run as root")
} }
dlvbin, tmpdir := getDlvBinEBPF(t) dlvbin := getDlvBinEBPF(t)
defer os.RemoveAll(tmpdir)
expected := []byte(`> (1) main.callme(10) expected := []byte(`> (1) main.callme(10)
> (1) main.callme(9) > (1) main.callme(9)
@ -1270,7 +1244,7 @@ func TestTraceEBPF2(t *testing.T) {
=> "100"`) => "100"`)
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(tmpdir, "__debug"), filepath.Join(fixtures, "ebpf_trace.go"), "main.callme") cmd := exec.Command(dlvbin, "trace", "--ebpf", "--output", filepath.Join(t.TempDir(), "__debug"), filepath.Join(fixtures, "ebpf_trace.go"), "main.callme")
rdr, err := cmd.StderrPipe() rdr, err := cmd.StderrPipe()
assertNoError(err, t, "stderr pipe") assertNoError(err, t, "stderr pipe")
defer rdr.Close() defer rdr.Close()
@ -1287,8 +1261,7 @@ func TestTraceEBPF2(t *testing.T) {
} }
func TestDlvTestChdir(t *testing.T) { func TestDlvTestChdir(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
fixtures := protest.FindFixturesDir() fixtures := protest.FindFixturesDir()
@ -1320,8 +1293,7 @@ func TestDlvTestChdir(t *testing.T) {
} }
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t) dlvbin := getDlvBin(t)
defer os.RemoveAll(tmpdir)
got, err := exec.Command(dlvbin, "version", "-v").CombinedOutput() got, err := exec.Command(dlvbin, "version", "-v").CombinedOutput()
if err != nil { if err != nil {

@ -201,11 +201,7 @@ func TestSplicedReader(t *testing.T) {
func withCoreFile(t *testing.T, name, args string) *proc.Target { func withCoreFile(t *testing.T, name, args string) *proc.Target {
// This is all very fragile and won't work on hosts with non-default core patterns. // This is all very fragile and won't work on hosts with non-default core patterns.
// Might be better to check in the binary and core? // Might be better to check in the binary and core?
tempDir, err := ioutil.TempDir("", "") tempDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
test.PathsToRemove = append(test.PathsToRemove, tempDir)
var buildFlags test.BuildFlags var buildFlags test.BuildFlags
if buildMode == "pie" { if buildMode == "pie" {
buildFlags = test.BuildModePIE buildFlags = test.BuildModePIE