/
procs_test.go
117 lines (109 loc) · 2.47 KB
/
procs_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
package gexe
import (
"strings"
"testing"
)
func TestEchoRun(t *testing.T) {
tests := []struct {
name string
cmdStr func() string
exec func(string)
}{
{
name: "start proc",
cmdStr: func() string {
return `echo "HELLO WORLD!"`
},
exec: func(cmd string) {
p := DefaultEcho.StartProc(cmd)
if p.Err() != nil {
t.Fatal("Unexpected error:", p.Err().Error())
}
if p.ExitCode() != -1 {
t.Fatal("Expecting -1, got:", p.ExitCode())
}
if p.IsSuccess() {
t.Fatal("Success should be false")
}
result := strings.TrimSpace(p.Result())
if result != "HELLO WORLD!" {
t.Errorf("Unexpected proc.Out(): %s", result)
}
// wait for completion
p.Wait()
if p.ExitCode() != 0 {
t.Fatal("Expecting exit code 0, got:", p.ExitCode())
}
if !p.IsSuccess() {
t.Fatal("Process should be success")
}
},
},
{
name: "start proc/long-running",
cmdStr: func() string {
return `/bin/sh -c "for i in {1..3}; do echo 'HELLO WORLD!\$i'; sleep 0.2; done"`
},
exec: func(cmd string) {
p := DefaultEcho.StartProc(cmd)
if p.Err() != nil {
t.Fatal(p.Err())
}
if err := p.Wait().Err(); err != nil {
t.Fatalf("proc failed to wait: %s", err)
}
if !strings.Contains(p.Result(), "HELLO WORLD!") {
t.Fatal("Unexpected result:", p.Result())
}
},
},
{
name: "run proc",
cmdStr: func() string {
return `echo "HELLO WORLD!"`
},
exec: func(cmd string) {
p := DefaultEcho.RunProc(cmd)
if p.ExitCode() != 0 {
t.Fatal("Expecting exit code 0, got:", p.ExitCode())
}
if !p.IsSuccess() {
t.Fatal("Process should be success")
}
if p.Result() != "HELLO WORLD!" {
t.Fatal("Unexpected command result:", p.Result())
}
},
},
{
name: "simple run",
cmdStr: func() string {
return `echo "HELLO WORLD!"`
},
exec: func(cmd string) {
result := DefaultEcho.Run(cmd)
if result != "HELLO WORLD!" {
t.Fatal("Unexpected command result:", result)
}
},
},
{
name: "simple with expansion",
cmdStr: func() string {
return "echo $MSG"
},
exec: func(cmd string) {
DefaultEcho.Variables().Vars("MSG=Hello World")
result := DefaultEcho.Run(cmd)
if result != DefaultEcho.Variables().Val("MSG") {
t.Fatal("Unexpected command result:", result)
}
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.exec(test.cmdStr())
})
}
}