-
Notifications
You must be signed in to change notification settings - Fork 9
/
procs_test.go
110 lines (101 loc) · 2.48 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
package gexe
import (
"strings"
"testing"
)
func TestEchoRun(t *testing.T) {
tests := []struct {
name string
cmdStr string
exec func(*testing.T, string)
}{
{
name: "start proc",
cmdStr: `echo "HELLO WORLD!"`,
exec: func(t *testing.T, 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")
}
// wait for completion
if err := p.Wait().Err(); err != nil {
t.Fatalf("Failed to complete process: %s", err)
}
result := strings.TrimSpace(p.Result())
if result != "HELLO WORLD!" {
t.Errorf("Unexpected proc.Out(): %s", result)
}
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: `/bin/sh -c "for i in {1..3}; do echo 'HELLO WORLD!\$i'; sleep 0.2; done"`,
exec: func(t *testing.T, 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: `echo "HELLO WORLD!"`,
exec: func(t *testing.T, 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: `echo "HELLO WORLD!"`,
exec: func(t *testing.T, cmd string) {
result := DefaultEcho.Run(cmd)
if result != "HELLO WORLD!" {
t.Fatal("Unexpected command result:", result)
}
},
},
{
name: "simple with expansion",
cmdStr: "echo $MSG",
exec: func(t *testing.T, 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(t, test.cmdStr)
})
}
}