*: add and expose StopSignal field in ExpectProcess

add and expose StopSignal to ExpectProcess allows user
to define what signal to send on ExpectProcess.close()

coverage testing code sets StopSignal to SIGTERM allowing
the test binary to shutdown gracefully so that it can generate
a coverage report.
This commit is contained in:
fanmin shi
2017-02-17 09:33:32 -08:00
parent b5be18a744
commit 07129a6370
2 changed files with 19 additions and 6 deletions

View File

@@ -23,6 +23,7 @@ import (
"os/exec"
"strings"
"sync"
"syscall"
"github.com/kr/pty"
)
@@ -38,6 +39,9 @@ type ExpectProcess struct {
lines []string
count int // increment whenever new line gets added
err error
// StopSignal is the signal Stop sends to the process; defaults to SIGKILL.
StopSignal os.Signal
}
var printDebugLines = os.Getenv("EXPECT_DEBUG") != ""
@@ -52,7 +56,10 @@ func NewExpect(name string, arg ...string) (ep *ExpectProcess, err error) {
func NewExpectWithEnv(name string, args []string, env []string) (ep *ExpectProcess, err error) {
cmd := exec.Command(name, args...)
cmd.Env = env
ep = &ExpectProcess{cmd: cmd}
ep = &ExpectProcess{
cmd: cmd,
StopSignal: syscall.SIGKILL,
}
ep.cond = sync.NewCond(&ep.mu)
ep.cmd.Stderr = ep.cmd.Stdout
ep.cmd.Stdin = nil
@@ -140,7 +147,7 @@ func (ep *ExpectProcess) close(kill bool) error {
return ep.err
}
if kill {
ep.Signal(os.Interrupt)
ep.Signal(ep.StopSignal)
}
err := ep.cmd.Wait()