forked from progrium/go-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_test.go
184 lines (164 loc) · 3.77 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package shell
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"time"
)
func TestCmdRun(t *testing.T) {
output := Cmd("echo", "foobar").Run().String()
if output != "foobar" {
t.Fatal("output not expected:", output)
}
}
func TestRun(t *testing.T) {
output := Run("echo", "foobar").String()
if output != "foobar" {
t.Fatal("output not expected:", output)
}
}
func TestStartWait(t *testing.T) {
p := Start("echo", "foobar")
err := p.Wait()
if err != nil {
t.Fatal("error not expected:", err)
}
output := p.String()
if output != "foobar" {
t.Fatal("output not expected:", output)
}
}
func TestStartKillWait(t *testing.T) {
p := Start("cat")
err := p.Kill()
if err != nil {
t.Fatal("error not expected:", err)
}
var (
done = make(chan interface{}, 0)
timer = time.NewTimer(2 * time.Second)
)
go func() {
p.Wait()
done <- struct{}{}
}()
select {
case <-timer.C:
t.Fatal("kill timeout reached")
case <-done:
break
}
}
func TestPanic(t *testing.T) {
defer func() {
p := recover().(*Process).ExitStatus
if p != 2 {
t.Fatal("status not expected:", p)
}
}()
Run("exit", "2")
}
func TestPipe(t *testing.T) {
p := Cmd("echo", "foobar").Pipe("wc", "-c").Pipe("awk", "'{print $1}'").Run()
if p.String() != "7" {
t.Fatal("output not expected:", p.String())
}
}
func TestSingleArg(t *testing.T) {
p := Run("echo foobar | wc -c | awk '{print $1}'")
if p.String() != "7" {
t.Fatal("output not expected:", p.String())
}
}
func TestProcessAsArg(t *testing.T) {
p := Cmd("echo", Run("echo foobar")).Pipe("wc", "-c").Pipe(Run("echo", "awk"), "'{print $1}'").Run()
if p.String() != "7" {
t.Fatal("output not expected:", p.String())
}
}
func TestLastArgStdin(t *testing.T) {
p := Cmd("awk '{print $1}'", Cmd("wc", "-c", Cmd("echo foobar"))).Run()
if p.String() != "7" {
t.Fatal("output not expected:", p.String())
}
}
func TestCmdRunFunc(t *testing.T) {
echo := Cmd("echo").ProcFn()
output := echo("foobar").String()
if output != "foobar" {
t.Fatal("output not expected:", output)
}
}
func TestPath(t *testing.T) {
p := Path("/root", "part1/part2", "foobar")
if p != "/root/part1/part2/foobar" {
t.Fatal("path not expected:", p)
}
}
func TestPathTemplate(t *testing.T) {
tmpl := PathTemplate("/root", "%s/part2", "%s")
p := tmpl("one", "two")
if p != "/root/one/part2/two" {
t.Fatal("path not expected:", p)
}
}
func TestPrintlnStringer(t *testing.T) {
var buf bytes.Buffer
fmt.Fprintln(&buf, Run("echo foobar"))
if buf.String() != "foobar\n" {
t.Fatal("output not expected:", buf.String())
}
}
func TestWrapPanicToErr(t *testing.T) {
copy := func(src, dst string) (err error) {
defer func() {
if p := recover().(*Process); p != nil {
err = p.Error()
}
}()
Run("cp", src, dst)
return
}
err := copy("", "")
if !strings.HasPrefix(err.Error(), "[64] ") {
t.Fatal("output not expected:", err)
}
}
func TestCmdOutputFn(t *testing.T) {
copy := Cmd("cp").OutputFn()
echo := Cmd("echo").OutputFn()
_, err := copy("", "")
if !strings.HasPrefix(err.Error(), "[64] ") {
t.Fatal("output not expected:", err)
}
out, err := echo("foobar")
if err != nil {
t.Fatal("unexpected error:", err)
}
if out != "foobar" {
t.Fatal("output not expected:", out)
}
}
func TestSetWorkDir(t *testing.T) {
var (
pwd = os.Getenv("PWD")
testPath = Path(pwd, "_goshell_unique")
)
defer func() {
os.RemoveAll(Path(pwd, "testfile"))
os.RemoveAll(testPath)
}()
if err := os.MkdirAll(testPath, os.ModePerm); err != nil {
t.Fatal("unexpected error:", err)
}
p := Cmd("touch", "testfile").SetWorkDir(testPath).Run()
if p.ExitStatus != 0 {
t.Fatal("unexpected error:", p.Error())
}
_, err := os.Stat(Path(testPath, "testfile"))
if err != nil {
t.Errorf("expected touched file to be present in correct working dir but was not")
}
}