cmd/dlv: subcommand 'dlv test' should switch to package directory (#2128)

Match 'go test' behaviour and switch to package directory, unless one
is specified with the '--wd' option.

Fixes #2125
This commit is contained in:
Alessandro Arzilli 2020-08-31 19:51:10 +02:00 committed by GitHub
parent 936c76b08c
commit 1ef8c40ccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 73 additions and 15 deletions

@ -31,7 +31,7 @@ Pass flags to the program you are debugging using `--`, for example:
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -37,7 +37,7 @@ dlv attach pid [executable]
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -30,7 +30,7 @@ are:
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -26,7 +26,7 @@ dlv connect addr
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -32,7 +32,7 @@ dlv core <executable> <core>
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -33,7 +33,7 @@ dlv dap
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -39,7 +39,7 @@ dlv debug [package]
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -39,7 +39,7 @@ dlv exec <path/to/binary>
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -45,7 +45,7 @@ and dap modes.
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -30,7 +30,7 @@ dlv replay [trace directory]
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -26,7 +26,7 @@ dlv run
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -37,7 +37,7 @@ dlv test [package]
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -44,7 +44,7 @@ dlv trace [package] regexp
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -26,7 +26,7 @@ dlv version
--log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log').
--log-output string Comma separated list of components that should produce debug output (see 'dlv help log')
--only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
--wd string Working directory for running the program. (default ".")
--wd string Working directory for running the program.
```
### SEE ALSO

@ -8,3 +8,8 @@ import (
func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func TestCurrentDirectory(t *testing.T) {
wd, _ := os.Getwd()
t.Logf("current directory: %s", wd)
}

@ -1,6 +1,7 @@
package cmds
import (
"encoding/json"
"errors"
"fmt"
"net"
@ -118,7 +119,7 @@ func New(docCall bool) *cobra.Command {
rootCommand.PersistentFlags().IntVar(&apiVersion, "api-version", 1, "Selects API version when headless. New clients should use v2. Can be reset via RPCServer.SetApiVersion. See Documentation/api/json-rpc/README.md.")
rootCommand.PersistentFlags().StringVar(&initFile, "init", "", "Init file, executed by the terminal client.")
rootCommand.PersistentFlags().StringVar(&buildFlags, "build-flags", buildFlagsDefault, "Build flags, to be passed to the compiler.")
rootCommand.PersistentFlags().StringVar(&workingDir, "wd", ".", "Working directory for running the program.")
rootCommand.PersistentFlags().StringVar(&workingDir, "wd", "", "Working directory for running the program.")
rootCommand.PersistentFlags().BoolVarP(&checkGoVersion, "check-go-version", "", true, "Checks that the version of Go in use is compatible with Delve.")
rootCommand.PersistentFlags().BoolVarP(&checkLocalConnUser, "only-same-user", "", true, "Only connections from the same user that started this instance of Delve are allowed to connect.")
rootCommand.PersistentFlags().StringVar(&backend, "backend", "default", `Backend selection (see 'dlv help backend').`)
@ -509,6 +510,10 @@ func traceCmd(cmd *cobra.Command, args []string) {
listener, clientConn := service.ListenerPipe()
defer listener.Close()
if workingDir == "" {
workingDir = "."
}
// Create and start a debug server
server := rpccommon.NewServer(&service.Config{
Listener: listener,
@ -592,11 +597,35 @@ func testCmd(cmd *cobra.Command, args []string) {
defer gobuild.Remove(debugname)
processArgs := append([]string{debugname}, targetArgs...)
if workingDir == "" {
if len(dlvArgs) == 1 {
workingDir = getPackageDir(dlvArgs[0])
} else {
workingDir = "."
}
}
return execute(0, processArgs, conf, "", debugger.ExecutingGeneratedTest, dlvArgs, buildFlags)
}()
os.Exit(status)
}
func getPackageDir(pkg string) string {
out, err := exec.Command("go", "list", "--json", pkg).CombinedOutput()
if err != nil {
return "."
}
type listOut struct {
Dir string `json:"Dir"`
}
var listout listOut
err = json.Unmarshal(out, &listout)
if err != nil {
return "."
}
return listout.Dir
}
func attachCmd(cmd *cobra.Command, args []string) {
pid, err := strconv.Atoi(args[0])
if err != nil {
@ -736,6 +765,10 @@ func execute(attachPid int, processArgs []string, conf *config.Config, coreFile
disconnectChan := make(chan struct{})
if workingDir == "" {
workingDir = "."
}
// Create and start a debugger server
switch apiVersion {
case 1, 2:

@ -728,3 +728,23 @@ func TestTracePrintStack(t *testing.T) {
t.Fatal("stacktrace not printed")
}
}
func TestDlvTestChdir(t *testing.T) {
dlvbin, tmpdir := getDlvBin(t)
defer os.RemoveAll(tmpdir)
fixtures := protest.FindFixturesDir()
cmd := exec.Command(dlvbin, "test", filepath.Join(fixtures, "buildtest"), "--", "-test.v")
cmd.Stdin = strings.NewReader("continue\nexit\n")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("error executing Delve: %v", err)
}
t.Logf("output: %q", out)
p, _ := filepath.Abs(filepath.Join(fixtures, "buildtest"))
tgt := "current directory: " + p
if !strings.Contains(string(out), tgt) {
t.Errorf("output did not contain expected string %q", tgt)
}
}