-
Notifications
You must be signed in to change notification settings - Fork 480
/
Copy pathshell_test.go
65 lines (59 loc) · 1.37 KB
/
shell_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package shell
import (
"os/exec"
"runtime"
"testing"
)
func TestExecCmd(t *testing.T) {
sysType := runtime.GOOS
if sysType == "linux" {
//Linux
out, err := exec.Command("echo", "Hello World").Output()
if err != nil {
t.Error(err)
}
t.Log(string(out))
}
if sysType == "windows" {
// Windows
out, err := exec.Command("cmd", "/c", "echo", "Hello World").Output()
if err != nil {
t.Error(err)
}
t.Log(string(out))
}
}
func TestExecKillChildProc(t *testing.T) {
cmd := exec.Command("curl", "-o", "test.tar.gz", "http://test.tar.gz")
// 设置当主进程退出时,将子进程也退出,仅linux系统支持
//cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL}
err := cmd.Start()
if err != nil {
t.Error(err)
}
}
// 测试pipe,一个命令的输出是另一个命令的输入
func TestPipe(t *testing.T) {
cmdCat := exec.Command("cat", "main.go")
cmdWC := exec.Command("wc", "-l")
data, err := pipeCommands(cmdCat, cmdWC)
if err != nil {
t.Error(err)
}
t.Logf("output: %s", data)
}
func pipeCommands(commands ...*exec.Cmd) ([]byte, error) {
for i, command := range commands[:len(commands)-1] {
out, err := command.StdoutPipe()
if err != nil {
return nil, err
}
command.Start()
commands[i+1].Stdin = out
}
final, err := commands[len(commands)-1].Output()
if err != nil {
return nil, err
}
return final, nil
}