forked from codeskyblue/go-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_test.go
179 lines (162 loc) · 3.62 KB
/
pipe_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
package sh
import (
"encoding/xml"
"io"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestUnmarshalJSON(t *testing.T) {
var a int
s := NewSession()
s.ShowCMD = true
err := s.Command("echo", []string{"1"}).UnmarshalJSON(&a)
if err != nil {
t.Error(err)
}
if a != 1 {
t.Errorf("expect a tobe 1, but got %d", a)
}
}
func TestUnmarshalXML(t *testing.T) {
s := NewSession()
xmlSample := `<?xml version="1.0" encoding="utf-8"?>
<server version="1" />`
type server struct {
XMLName xml.Name `xml:"server"`
Version string `xml:"version,attr"`
}
data := &server{}
s.Command("echo", xmlSample).UnmarshalXML(data)
if data.Version != "1" {
t.Error(data)
}
}
func TestPipe(t *testing.T) {
s := NewSession()
s.ShowCMD = true
s.Call("echo", "hello")
err := s.Command("echo", "hi").Command("cat", "-n").Start()
if err != nil {
t.Error(err)
}
err = s.Wait()
if err != nil {
t.Error(err)
}
out, err := s.Command("echo", []string{"hello"}).Output()
if err != nil {
t.Error(err)
}
if string(out) != "hello\n" {
t.Error("capture wrong output:", out)
}
s.Command("echo", []string{"hello\tworld"}).Command("cut", []string{"-f2"}).Run()
}
func TestPipeCommand(t *testing.T) {
c1 := exec.Command("echo", "good")
rd, wr := io.Pipe()
c1.Stdout = wr
c2 := exec.Command("cat", "-n")
c2.Stdout = os.Stdout
c2.Stdin = rd
c1.Start()
c2.Start()
c1.Wait()
wc, ok := c1.Stdout.(io.WriteCloser)
if ok {
wc.Close()
}
c2.Wait()
}
func TestPipeInput(t *testing.T) {
s := NewSession()
s.ShowCMD = true
s.SetInput("first line\nsecond line\n")
out, err := s.Command("grep", "second").Output()
if err != nil {
t.Error(err)
}
if string(out) != "second line\n" {
t.Error("capture wrong output:", out)
}
}
func TestTimeout(t *testing.T) {
s := NewSession()
err := s.Command("sleep", "2").Start()
if err != nil {
t.Fatal(err)
}
err = s.WaitTimeout(time.Second)
if err != ErrExecTimeout {
t.Fatal(err)
}
}
func TestSetTimeout(t *testing.T) {
s := NewSession()
s.SetTimeout(time.Second)
defer s.SetTimeout(0)
err := s.Command("sleep", "2").Run()
if err != ErrExecTimeout {
t.Fatal(err)
}
}
func TestCombinedOutput(t *testing.T) {
s := NewSession()
bytes, err := s.Command("sh", "-c", "echo stderr >&2 ; echo stdout").CombinedOutput()
if err != nil {
t.Error(err)
}
stringOutput := string(bytes)
if !(strings.Contains(stringOutput, "stdout") && strings.Contains(stringOutput, "stderr")) {
t.Errorf("expect output from both output streams, got '%s'", strings.TrimSpace(stringOutput))
}
}
func TestPipeFail(t *testing.T) {
sh := NewSession()
sh.PipeFail = true
sh.PipeStdErrors = true
sh.Command("cat", "unknown-file")
sh.Command("echo")
if _, err := sh.Output(); err == nil {
t.Error("expected error")
}
}
func TestLeafCommand(t *testing.T) {
s := NewSession()
s.ShowCMD = true
s.Command("echo", "hello world")
s.LeafCommand("xargs")
s.LeafCommand("xargs")
out, err := s.Output()
if err != nil {
t.Error(err)
}
if string(out) != "hello world\nhello world\n" {
t.Error("capture wrong output:", string(out))
}
}
func TestLeafCommandSeparateEnvs(t *testing.T) {
s := NewSession()
s.ShowCMD = true
var args1 []interface{}
var args2 []interface{}
mp := make(map[string]string)
mp["COMPANY_NAME"] = "APPSCODE"
args1 = append(args1, "COMPANY_NAME")
args1 = append(args1, mp)
s.LeafCommand("printenv", args1...)
mp["COMPANY_NAME"] = "GOOGLE"
args2 = append(args2, "COMPANY_NAME")
args2 = append(args2, mp)
s.LeafCommand("printenv", args2...)
out, err := s.Output()
if err != nil {
t.Error(err)
}
if string(out) != "APPSCODE\nGOOGLE\n" {
t.Error("capture wrong output:", string(out))
}
}