-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_test.go
147 lines (134 loc) · 2.94 KB
/
cmd_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package exec_test
import (
"fmt"
"testing"
"time"
"github.com/NeoJRotary/exec-go"
)
func TestCmd_Run(t *testing.T) {
out, err := exec.NewCmd("", "echo", "123").Run()
if err != nil {
t.Fatal(err)
}
if out != "123\n" {
t.Fatal("wrong out, should be 123\\n, get", out)
}
out, err = exec.NewCmd("", "bash", "-c", "echo errrr 1>&2; exit 1;").Run()
if err == nil {
t.Fatal("should get error")
}
if err.Error() != "errrr\n" {
t.Fatal("wrong error, should be errrr\\n, get", err.Error())
}
}
func TestCmd_AddEnv(t *testing.T) {
cmd := exec.NewCmd("", "bash", "-c", `echo $MYEnv`).AddEnv("MYEnv", "123")
out, err := cmd.Run()
if err != nil {
t.Fatal(err)
}
if out != "123\n" {
t.Fatal("wrong out, should be 123\\n, get", out)
}
}
func TestCmd_StartWait(t *testing.T) {
cmd := exec.NewCmd("", "echo", "123")
err := cmd.Start()
if err != nil {
t.Fatal(err)
}
err = cmd.Wait()
if err != nil {
t.Fatal(err, cmd.Error())
}
if cmd.Output() != "123\n" {
t.Fatal("wrong out, should be 123\\n")
}
}
func TestCmd_Output(t *testing.T) {
cmd := exec.NewCmd("", "bash", "-c", `echo 123; ccccc`)
err := cmd.Start()
if err != nil {
t.Fatal(err)
}
err = cmd.Wait()
if err == nil {
t.Fatal("should throw error")
}
if cmd.Output() != "123\n" {
t.Fatal("wrong output, get:", cmd.Output())
}
if cmd.Error() != "bash: ccccc: command not found\n" {
fmt.Println([]byte(cmd.Error()))
fmt.Println([]byte("bash: ccccc: command not found"))
t.Fatal("wrong output, get:", cmd.Error())
}
}
func TestCmd_Read(t *testing.T) {
cmd := exec.NewCmd("", "bash", "-c", `echo 123; sleep 1s; echo 456; printf 789`)
err := cmd.Start()
if err != nil {
t.Fatal(err)
}
cmd.Read()
b := string(cmd.GetMsg())
if b != "123\n" {
t.Fatal("wrong, get:", b)
}
time.Sleep(time.Millisecond * 1100)
cmd.Read()
b = string(cmd.GetMsg())
if b != "456\n789" {
t.Fatal("wrong, get:", b)
}
err = cmd.Wait()
if err != nil {
t.Fatal(err)
}
if cmd.Output() != "123\n456\n789" {
t.Error("wrong output, get:\n", cmd.Output())
}
}
func TestCmd_Cancel(t *testing.T) {
cmd := exec.NewCmd("", "bash", "-c", `bash -c "printf 123; sleep 5s; printf 456"`)
err := cmd.Start()
if err != nil {
t.Fatal(err)
}
startAt := time.Now()
go func() {
time.Sleep(time.Millisecond * 500)
cmd.Cancel()
}()
err = cmd.Wait()
if err == nil {
t.Fatal("should throw err")
}
if !cmd.Canceled {
t.Fatal("Canceled should be true")
}
if time.Since(startAt).Seconds() >= 1 {
t.Fatal("process should stop before 1s")
}
if cmd.Output() != "123" {
t.Fatal("output should be 123")
}
}
func TestCmd_Timeout(t *testing.T) {
cmd := exec.NewCmd("", "sleep", "10s").SetTimeout(time.Second * 2)
err := cmd.Start()
if err != nil {
t.Fatal(err)
}
startAt := time.Now()
err = cmd.Wait()
if err == nil {
t.Fatal("should throw err")
}
if !cmd.TimedOut {
t.Fatal("should time out")
}
if time.Since(startAt).Seconds() > 2.5 {
t.Fatal("process should time out in 2.5s")
}
}