terminal: Refactor Term/FakeTerm
This commit is contained in:
parent
9e588fef72
commit
16f30d0738
@ -16,19 +16,17 @@ import (
|
||||
)
|
||||
|
||||
type FakeTerminal struct {
|
||||
t testing.TB
|
||||
client service.Client
|
||||
cmds *Commands
|
||||
term *Term
|
||||
*Term
|
||||
t testing.TB
|
||||
}
|
||||
|
||||
func (term *FakeTerminal) Exec(cmdstr string) (outstr string, err error) {
|
||||
func (ft *FakeTerminal) Exec(cmdstr string) (outstr string, err error) {
|
||||
cmdstr, args := parseCommand(cmdstr)
|
||||
cmd := term.cmds.Find(cmdstr)
|
||||
cmd := ft.cmds.Find(cmdstr)
|
||||
|
||||
outfh, err := ioutil.TempFile("", "cmdtestout")
|
||||
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
|
||||
@ -38,19 +36,19 @@ func (term *FakeTerminal) Exec(cmdstr string) (outstr string, err error) {
|
||||
outfh.Close()
|
||||
outbs, err1 := ioutil.ReadFile(outfh.Name())
|
||||
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)
|
||||
os.Remove(outfh.Name())
|
||||
}()
|
||||
err = cmd(term.term, args)
|
||||
err = cmd(ft.Term, args)
|
||||
return
|
||||
}
|
||||
|
||||
func (term *FakeTerminal) MustExec(cmdstr string) string {
|
||||
outstr, err := term.Exec(cmdstr)
|
||||
func (ft *FakeTerminal) MustExec(cmdstr string) string {
|
||||
outstr, err := ft.Exec(cmdstr)
|
||||
if err != nil {
|
||||
term.t.Fatalf("Error executing <%s>: %v", cmdstr, err)
|
||||
ft.t.Fatalf("Error executing <%s>: %v", cmdstr, err)
|
||||
}
|
||||
return outstr
|
||||
}
|
||||
@ -72,7 +70,11 @@ func withTestTerminal(name string, t testing.TB, fn func(*FakeTerminal)) {
|
||||
defer func() {
|
||||
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) {
|
||||
|
@ -7,9 +7,10 @@ import (
|
||||
"os/signal"
|
||||
"strings"
|
||||
|
||||
"github.com/peterh/liner"
|
||||
"syscall"
|
||||
|
||||
"github.com/peterh/liner"
|
||||
|
||||
"github.com/derekparker/delve/config"
|
||||
"github.com/derekparker/delve/service"
|
||||
)
|
||||
@ -25,22 +26,27 @@ type Term struct {
|
||||
client service.Client
|
||||
prompt string
|
||||
line *liner.State
|
||||
conf *config.Config
|
||||
cmds *Commands
|
||||
dumb bool
|
||||
InitFile string
|
||||
}
|
||||
|
||||
// New returns a new 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{
|
||||
prompt: "(dlv) ",
|
||||
line: liner.NewLiner(),
|
||||
client: client,
|
||||
conf: conf,
|
||||
cmds: cmds,
|
||||
dumb: strings.ToLower(os.Getenv("TERM")) == "dumb",
|
||||
}
|
||||
}
|
||||
|
||||
// Close returns the terminal to its previous mode.
|
||||
func (t *Term) 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) {
|
||||
for _, cmd := range cmds.cmds {
|
||||
for _, cmd := range t.cmds.cmds {
|
||||
for _, alias := range cmd.aliases {
|
||||
if strings.HasPrefix(alias, strings.ToLower(line)) {
|
||||
c = append(c, alias)
|
||||
@ -94,7 +96,7 @@ func (t *Term) Run() (int, error) {
|
||||
fmt.Println("Type 'help' for list of commands.")
|
||||
|
||||
if t.InitFile != "" {
|
||||
err := cmds.executeFile(t, t.InitFile)
|
||||
err := t.cmds.executeFile(t, t.InitFile)
|
||||
if err != nil {
|
||||
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)
|
||||
cmd := cmds.Find(cmdstr)
|
||||
cmd := t.cmds.Find(cmdstr)
|
||||
if err := cmd(t, args); err != nil {
|
||||
if _, ok := err.(ExitRequestError); ok {
|
||||
return t.handleExit()
|
||||
|
Loading…
Reference in New Issue
Block a user