
This patch modifies the behavior of the exec subcommand such that you don't necessarily have to write the "./" prefix when trying to debug a precompiled binary in your working directory. For example (given foo.test in working dir), before this change: dlv exec foo.test Would result in an error, forcing the user to type: dlv exec ./foo.test This just makes things a bit more convenient.
104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
package debugger
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/go-delve/delve/pkg/gobuild"
|
|
protest "github.com/go-delve/delve/pkg/proc/test"
|
|
"github.com/go-delve/delve/service/api"
|
|
)
|
|
|
|
func TestDebugger_LaunchNoMain(t *testing.T) {
|
|
fixturesDir := protest.FindFixturesDir()
|
|
nomaindir := filepath.Join(fixturesDir, "nomaindir")
|
|
debugname := "debug"
|
|
exepath := filepath.Join(nomaindir, debugname)
|
|
defer os.Remove(exepath)
|
|
if err := gobuild.GoBuild(debugname, []string{nomaindir}, fmt.Sprintf("-o %s", exepath)); err != nil {
|
|
t.Fatalf("go build error %v", err)
|
|
}
|
|
|
|
d := new(Debugger)
|
|
_, err := d.Launch([]string{exepath}, ".")
|
|
if err == nil {
|
|
t.Fatalf("expected error but none was generated")
|
|
}
|
|
if err != api.ErrNotExecutable {
|
|
t.Fatalf("expected error \"%v\" got \"%v\"", api.ErrNotExecutable, err)
|
|
}
|
|
}
|
|
|
|
func TestDebugger_LaunchInvalidFormat(t *testing.T) {
|
|
goos := os.Getenv("GOOS")
|
|
goarch := os.Getenv("GOARCH")
|
|
defer func() {
|
|
// restore environment values
|
|
os.Setenv("GOOS", goos)
|
|
os.Setenv("GOARCH", goarch)
|
|
}()
|
|
fixturesDir := protest.FindFixturesDir()
|
|
buildtestdir := filepath.Join(fixturesDir, "buildtest")
|
|
debugname := "debug"
|
|
switchOS := map[string]string{
|
|
"darwin": "linux",
|
|
"windows": "linux",
|
|
"freebsd": "windows",
|
|
"linux": "windows",
|
|
}
|
|
if runtime.GOARCH == "arm64" && runtime.GOOS == "linux" {
|
|
os.Setenv("GOARCH", "amd64")
|
|
}
|
|
os.Setenv("GOOS", switchOS[runtime.GOOS])
|
|
exepath := filepath.Join(buildtestdir, debugname)
|
|
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
|
|
t.Fatalf("go build error %v", err)
|
|
}
|
|
defer os.Remove(exepath)
|
|
|
|
d := new(Debugger)
|
|
_, err := d.Launch([]string{exepath}, ".")
|
|
if err == nil {
|
|
t.Fatalf("expected error but none was generated")
|
|
}
|
|
if err != api.ErrNotExecutable {
|
|
t.Fatalf("expected error \"%s\" got \"%v\"", api.ErrNotExecutable, err)
|
|
}
|
|
}
|
|
|
|
func TestDebugger_LaunchCurrentDir(t *testing.T) {
|
|
fixturesDir := protest.FindFixturesDir()
|
|
testDir := filepath.Join(fixturesDir, "buildtest")
|
|
debugname := "debug"
|
|
exepath := filepath.Join(testDir, debugname)
|
|
originalPath, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Chdir(originalPath)
|
|
defer func() {
|
|
if err := os.Remove(exepath); err != nil {
|
|
t.Fatalf("error removing executable %v", err)
|
|
}
|
|
}()
|
|
if err := gobuild.GoBuild(debugname, []string{testDir}, fmt.Sprintf("-o %s", exepath)); err != nil {
|
|
t.Fatalf("go build error %v", err)
|
|
}
|
|
|
|
os.Chdir(testDir)
|
|
|
|
d := new(Debugger)
|
|
d.config = &Config{}
|
|
_, err = d.Launch([]string{debugname}, ".")
|
|
if err == nil {
|
|
t.Fatal("expected error but none was generated")
|
|
}
|
|
if err != nil && !strings.Contains(err.Error(), "unknown backend") {
|
|
t.Fatal(err)
|
|
}
|
|
}
|