goc/tests/e2e/cmd.go

124 lines
2.7 KiB
Go
Raw Normal View History

2021-07-22 11:57:56 +00:00
package e2e
import (
"bytes"
"context"
"fmt"
2021-07-22 14:25:55 +00:00
"os"
2021-07-22 11:57:56 +00:00
"os/exec"
2021-07-23 03:34:14 +00:00
"runtime"
2021-07-23 03:17:53 +00:00
"strings"
2021-07-22 11:57:56 +00:00
"time"
)
// LongRunCmd defines a cmd which run for a long time
type LongRunCmd struct {
cancel context.CancelFunc
cmd *exec.Cmd
stderrBuf bytes.Buffer
stdoutBuf bytes.Buffer
err error
done bool
}
2021-07-22 14:25:55 +00:00
// NewLongRunCmd defines a command which will be run forever
//
// You can specify the whole command arg list, the directory where the command
// will be run in, and the env list.
2021-07-23 03:17:53 +00:00
//
// args 命令列表
//
// dir 命令运行所在的目录
//
// envs 额外的环境变量
2021-07-22 14:25:55 +00:00
func NewLongRunCmd(args []string, dir string, envs []string) *LongRunCmd {
2021-07-22 11:57:56 +00:00
ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
2021-07-23 03:34:14 +00:00
if runtime.GOOS == "windows" {
cmd = exec.CommandContext(ctx, args[0]+".exe", args[1:]...)
}
2021-07-22 11:57:56 +00:00
cmd.Dir = dir
2021-07-22 14:25:55 +00:00
cmd.Env = append(os.Environ(), envs...)
2021-07-22 11:57:56 +00:00
var stderrBuf bytes.Buffer
var stdoutBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
return &LongRunCmd{
cmd: cmd,
cancel: cancel,
stderrBuf: stderrBuf,
stdoutBuf: stdoutBuf,
}
}
// Run in backend
func (l *LongRunCmd) Run() {
go func() {
err := l.cmd.Start()
if err != nil {
l.err = err
}
err = l.cmd.Wait()
if err != nil {
l.err = err
}
l.err = nil
l.done = true
}()
time.Sleep(time.Millisecond * 100)
}
func (l *LongRunCmd) Stop() {
l.cancel()
}
func (l *LongRunCmd) CheckExitStatus() error {
if l.done == true {
return l.err
} else {
return fmt.Errorf("running")
}
}
func (l *LongRunCmd) GetStdoutStdErr() (string, string) {
return l.stdoutBuf.String(), l.stderrBuf.String()
}
// RunShortRunCmd defines a cmd which run and exits immediately
2021-07-23 03:17:53 +00:00
//
// args 命令列表
//
// dir 命令运行所在的目录
//
// envs 额外的环境变量
2021-07-22 14:25:55 +00:00
func RunShortRunCmd(args []string, dir string, envs []string) (string, error) {
2021-07-22 11:57:56 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
2021-07-23 03:34:14 +00:00
if runtime.GOOS == "windows" {
cmd = exec.CommandContext(ctx, args[0]+".exe", args[1:]...)
}
2021-07-22 11:57:56 +00:00
cmd.Dir = dir
2021-07-22 14:25:55 +00:00
cmd.Env = append(os.Environ(), envs...)
2021-07-22 11:57:56 +00:00
output, err := cmd.CombinedOutput()
return string(output), err
}
2021-07-23 03:17:53 +00:00
// SearchSymbolInBinary searches if the specified symbol is compiled into the bianry
func SearchSymbolInBinary(dir string, binary string, symbol string) (bool, error) {
2021-07-23 03:34:14 +00:00
if runtime.GOOS == "windows" {
binary = binary + ".exe"
}
2021-07-23 03:17:53 +00:00
output, err := RunShortRunCmd([]string{"go", "tool", "objdump", "-s", symbol, binary}, dir, nil)
if err != nil {
return false, fmt.Errorf("cannot lookup into the binary: %w", err)
}
return strings.Contains(output, symbol), nil
}