mirror of
https://github.com/go-delve/delve.git
synced 2025-11-02 04:36:29 +08:00
terminal: Refactor Term/FakeTerm
This commit is contained in:
@ -16,19 +16,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type FakeTerminal struct {
|
type FakeTerminal struct {
|
||||||
|
*Term
|
||||||
t testing.TB
|
t testing.TB
|
||||||
client service.Client
|
|
||||||
cmds *Commands
|
|
||||||
term *Term
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (term *FakeTerminal) Exec(cmdstr string) (outstr string, err error) {
|
func (ft *FakeTerminal) Exec(cmdstr string) (outstr string, err error) {
|
||||||
cmdstr, args := parseCommand(cmdstr)
|
cmdstr, args := parseCommand(cmdstr)
|
||||||
cmd := term.cmds.Find(cmdstr)
|
cmd := ft.cmds.Find(cmdstr)
|
||||||
|
|
||||||
outfh, err := ioutil.TempFile("", "cmdtestout")
|
outfh, err := ioutil.TempFile("", "cmdtestout")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
term.t.Fatalf("could not create temporary file: %v", err)
|
ft.t.Fatalf("could not create temporary file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout, stderr := os.Stdout, os.Stderr
|
stdout, stderr := os.Stdout, os.Stderr
|
||||||
@ -38,19 +36,19 @@ func (term *FakeTerminal) Exec(cmdstr string) (outstr string, err error) {
|
|||||||
outfh.Close()
|
outfh.Close()
|
||||||
outbs, err1 := ioutil.ReadFile(outfh.Name())
|
outbs, err1 := ioutil.ReadFile(outfh.Name())
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
term.t.Fatalf("could not read temporary output file: %v", err)
|
ft.t.Fatalf("could not read temporary output file: %v", err)
|
||||||
}
|
}
|
||||||
outstr = string(outbs)
|
outstr = string(outbs)
|
||||||
os.Remove(outfh.Name())
|
os.Remove(outfh.Name())
|
||||||
}()
|
}()
|
||||||
err = cmd(term.term, args)
|
err = cmd(ft.Term, args)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (term *FakeTerminal) MustExec(cmdstr string) string {
|
func (ft *FakeTerminal) MustExec(cmdstr string) string {
|
||||||
outstr, err := term.Exec(cmdstr)
|
outstr, err := ft.Exec(cmdstr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
term.t.Fatalf("Error executing <%s>: %v", cmdstr, err)
|
ft.t.Fatalf("Error executing <%s>: %v", cmdstr, err)
|
||||||
}
|
}
|
||||||
return outstr
|
return outstr
|
||||||
}
|
}
|
||||||
@ -72,7 +70,11 @@ func withTestTerminal(name string, t testing.TB, fn func(*FakeTerminal)) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
client.Detach(true)
|
client.Detach(true)
|
||||||
}()
|
}()
|
||||||
fn(&FakeTerminal{t, client, DebugCommands(client), New(client, nil)})
|
ft := &FakeTerminal{
|
||||||
|
t: t,
|
||||||
|
Term: New(client, nil),
|
||||||
|
}
|
||||||
|
fn(ft)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandDefault(t *testing.T) {
|
func TestCommandDefault(t *testing.T) {
|
||||||
|
|||||||
@ -7,9 +7,10 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/peterh/liner"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/peterh/liner"
|
||||||
|
|
||||||
"github.com/derekparker/delve/config"
|
"github.com/derekparker/delve/config"
|
||||||
"github.com/derekparker/delve/service"
|
"github.com/derekparker/delve/service"
|
||||||
)
|
)
|
||||||
@ -25,22 +26,27 @@ type Term struct {
|
|||||||
client service.Client
|
client service.Client
|
||||||
prompt string
|
prompt string
|
||||||
line *liner.State
|
line *liner.State
|
||||||
conf *config.Config
|
cmds *Commands
|
||||||
dumb bool
|
dumb bool
|
||||||
InitFile string
|
InitFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Term.
|
// New returns a new Term.
|
||||||
func New(client service.Client, conf *config.Config) *Term {
|
func New(client service.Client, conf *config.Config) *Term {
|
||||||
|
cmds := DebugCommands(client)
|
||||||
|
if conf != nil && conf.Aliases != nil {
|
||||||
|
cmds.Merge(conf.Aliases)
|
||||||
|
}
|
||||||
return &Term{
|
return &Term{
|
||||||
prompt: "(dlv) ",
|
prompt: "(dlv) ",
|
||||||
line: liner.NewLiner(),
|
line: liner.NewLiner(),
|
||||||
client: client,
|
client: client,
|
||||||
conf: conf,
|
cmds: cmds,
|
||||||
dumb: strings.ToLower(os.Getenv("TERM")) == "dumb",
|
dumb: strings.ToLower(os.Getenv("TERM")) == "dumb",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close returns the terminal to its previous mode.
|
||||||
func (t *Term) Close() {
|
func (t *Term) Close() {
|
||||||
t.line.Close()
|
t.line.Close()
|
||||||
}
|
}
|
||||||
@ -61,12 +67,8 @@ func (t *Term) Run() (int, error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
cmds := DebugCommands(t.client)
|
|
||||||
if t.conf != nil && t.conf.Aliases != nil {
|
|
||||||
cmds.Merge(t.conf.Aliases)
|
|
||||||
}
|
|
||||||
t.line.SetCompleter(func(line string) (c []string) {
|
t.line.SetCompleter(func(line string) (c []string) {
|
||||||
for _, cmd := range cmds.cmds {
|
for _, cmd := range t.cmds.cmds {
|
||||||
for _, alias := range cmd.aliases {
|
for _, alias := range cmd.aliases {
|
||||||
if strings.HasPrefix(alias, strings.ToLower(line)) {
|
if strings.HasPrefix(alias, strings.ToLower(line)) {
|
||||||
c = append(c, alias)
|
c = append(c, alias)
|
||||||
@ -94,7 +96,7 @@ func (t *Term) Run() (int, error) {
|
|||||||
fmt.Println("Type 'help' for list of commands.")
|
fmt.Println("Type 'help' for list of commands.")
|
||||||
|
|
||||||
if t.InitFile != "" {
|
if t.InitFile != "" {
|
||||||
err := cmds.executeFile(t, t.InitFile)
|
err := t.cmds.executeFile(t, t.InitFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error executing init file: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Error executing init file: %s\n", err)
|
||||||
}
|
}
|
||||||
@ -113,7 +115,7 @@ func (t *Term) Run() (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmdstr, args := parseCommand(cmdstr)
|
cmdstr, args := parseCommand(cmdstr)
|
||||||
cmd := cmds.Find(cmdstr)
|
cmd := t.cmds.Find(cmdstr)
|
||||||
if err := cmd(t, args); err != nil {
|
if err := cmd(t, args); err != nil {
|
||||||
if _, ok := err.(ExitRequestError); ok {
|
if _, ok := err.(ExitRequestError); ok {
|
||||||
return t.handleExit()
|
return t.handleExit()
|
||||||
|
|||||||
Reference in New Issue
Block a user