-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand_test.go
More file actions
298 lines (267 loc) Β· 7.56 KB
/
Copy pathcommand_test.go
File metadata and controls
298 lines (267 loc) Β· 7.56 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package run_test
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"testing"
qt "github.com/frankban/quicktest"
"github.com/sourcegraph/run"
)
var outputTests = []func(c *qt.C, out run.Output, expect string, expectError bool){
func(c *qt.C, out run.Output, expect string, expectError bool) {
c.Run("Stream", func(c *qt.C) {
var b bytes.Buffer
err := out.Stream(&b)
c.Assert(err, qt.IsNil)
c.Assert(b.String(), qt.Equals, fmt.Sprintf("%s\n", expect))
})
},
func(c *qt.C, out run.Output, expect string, expectError bool) {
c.Run("StreamLines", func(c *qt.C) {
linesC := make(chan string, 10)
err := out.StreamLines(func(line string) {
linesC <- line
})
c.Assert(err, qt.IsNil)
close(linesC)
var lines []string
for l := range linesC {
lines = append(lines, l)
}
c.Assert(len(lines), qt.Equals, 1)
c.Assert(string(lines[0]), qt.Equals, expect)
})
},
func(c *qt.C, out run.Output, expect string, expectError bool) {
c.Run("Lines", func(c *qt.C) {
lines, err := out.Lines()
if !expectError {
c.Assert(err, qt.IsNil)
}
c.Assert(len(lines), qt.Equals, 1)
c.Assert(lines[0], qt.Equals, expect)
})
},
func(c *qt.C, out run.Output, expect string, expectError bool) {
c.Run("String", func(c *qt.C) {
str, err := out.String()
if !expectError {
c.Assert(err, qt.IsNil)
}
c.Assert(str, qt.Equals, expect)
})
},
func(c *qt.C, out run.Output, expect string, expectError bool) {
c.Run("Read: fixed bytes", func(c *qt.C) {
b := make([]byte, 100)
n, err := out.Read(b)
// We expect io.EOF if we are done reading - the content assertion
// should still pass.
if !expectError && err != io.EOF {
c.Assert(err, qt.IsNil)
}
c.Assert(string(b[0:n]), qt.Equals, fmt.Sprintf("%s\n", expect))
})
},
func(c *qt.C, out run.Output, expect string, expectError bool) {
if expectError {
return // not applicable
}
c.Run("Read: exactly length of output", func(c *qt.C) {
// Read exactly the amount of output
b := make([]byte, len(expect)+1)
n, err := out.Read(b)
c.Assert(err, qt.IsNil)
c.Assert(string(b[0:n]), qt.Equals, fmt.Sprintf("%s\n", expect))
// A subsequent read should indicate nothing read, and an EOF
n, err = out.Read(make([]byte, 100))
c.Assert(n, qt.Equals, 0)
c.Assert(err, qt.Equals, io.EOF)
})
},
func(c *qt.C, out run.Output, expect string, expectError bool) {
c.Run("Read: io.ReadAll", func(c *qt.C) {
b, err := io.ReadAll(out)
if !expectError {
c.Assert(err, qt.IsNil)
}
c.Assert(string(b), qt.Equals, fmt.Sprintf("%s\n", expect))
})
},
func(c *qt.C, out run.Output, expect string, expectError bool) {
c.Run("Wait", func(c *qt.C) {
err := out.Wait()
if !expectError {
c.Assert(err, qt.IsNil)
}
})
},
}
func TestRunAndAggregate(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
command := `echo "hello world"`
type testCase struct {
name string
output func() run.Output
expect string
expectError bool
}
for _, tc := range []testCase{
{
name: "plain output",
output: func() run.Output {
return run.Cmd(ctx, command).Run()
},
expect: "hello world",
},
{
name: "plain output and exit with error",
output: func() run.Output {
return run.Cmd(ctx, command, "; exit 1").Run()
},
expect: "hello world ; exit 1",
expectError: true,
},
{
name: "mapped output",
output: func() run.Output {
return run.Cmd(ctx, command).Run().
Map(func(ctx context.Context, line []byte, dst io.Writer) (int, error) {
return dst.Write(bytes.ReplaceAll(line, []byte("hello"), []byte("goodbye")))
})
},
expect: "goodbye world",
expectError: true, // io.EOF
},
{
name: "multiple mapped output",
output: func() run.Output {
return run.Cmd(ctx, command).Run().
Map(func(ctx context.Context, line []byte, dst io.Writer) (int, error) {
return dst.Write(bytes.ReplaceAll(line, []byte("hello"), []byte("goodbye")))
}).
Map(func(ctx context.Context, line []byte, dst io.Writer) (int, error) {
return dst.Write(bytes.ReplaceAll(line, []byte("world"), []byte("jh")))
})
},
expect: "goodbye jh",
expectError: true, // io.EOF
},
} {
c.Run(tc.name, func(c *qt.C) {
for _, test := range outputTests {
test(c, tc.output(), tc.expect, tc.expectError)
}
})
}
}
func TestJQ(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
c.Run("cat and JQ", func(c *qt.C) {
const testJSON = `{
"hello": "world"
}`
res, err := run.Cmd(ctx, "cat").
Input(strings.NewReader(testJSON)).
Run().
JQ(".hello")
c.Assert(err, qt.IsNil)
c.Assert(string(res), qt.Equals, `"world"`)
})
}
func TestEdgeCases(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
c.Run("empty lines from map are preserved", func(c *qt.C) {
const testData = `hello
world`
c.Run("without map", func(c *qt.C) {
res, err := run.Cmd(ctx, "cat").
Input(strings.NewReader(testData)).
Run().
Lines()
c.Assert(err, qt.IsNil)
c.Assert(len(res), qt.Equals, 3)
})
c.Run("with map", func(c *qt.C) {
res, err := run.Cmd(ctx, "cat").
Input(strings.NewReader(testData)).
Run().
Map(func(ctx context.Context, line []byte, dst io.Writer) (int, error) {
return dst.Write(line)
}).
Lines()
c.Assert(err, qt.IsNil)
c.Assert(len(res), qt.Equals, 3)
})
})
c.Run("mixed output", func(c *qt.C) {
const mixedOutputCmd = `echo "stdout" ; sleep 0.001 ; >&2 echo "stderr"`
c.Run("stdout only", func(c *qt.C) {
res, err := run.Bash(ctx, mixedOutputCmd).
StdOut().
Run().
Lines()
c.Assert(err, qt.IsNil)
c.Assert(res, qt.CmpEquals(), []string{"stdout"})
})
c.Run("stderr only", func(c *qt.C) {
res, err := run.Bash(ctx, mixedOutputCmd).
StdErr().
Run().
Lines()
c.Assert(err, qt.IsNil)
c.Assert(res, qt.CmpEquals(), []string{"stderr"})
})
c.Run("combined", func(c *qt.C) {
res, err := run.Bash(ctx, mixedOutputCmd).
Run().
Lines()
c.Assert(err, qt.IsNil)
c.Assert(res, qt.CmpEquals(), []string{"stdout", "stderr"})
})
})
}
func TestBashOpts(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
c.Run("depending on bash mode, a pipe command that fails should return an exit code", func(c *qt.C) {
pipeCmd := "echo '123456789' | grep 999 | echo 1"
c.Run("normal bash -c - pipe that fails should not exit with non zero command", func(c *qt.C) {
// In the pipe grep 999 will fail, but since the last command in the pipe is a success the entire command succeeds
_, err := run.Bash(ctx, pipeCmd).StdOut().Run().String()
c.Assert(err, qt.IsNil)
})
c.Run("pipe command should fail with StrictBash", func(c *qt.C) {
// with StrictBashOpts, since 'grep 999' fails in the pipe, the entire command is considered to have failed
_, err := run.BashWith(ctx, run.StrictBashOpts, pipeCmd).StdOut().Run().String()
c.Assert(err, qt.IsNotNil)
})
})
}
func TestInput(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
c.Run("set multiple inputs", func(c *qt.C) {
cmd := run.Cmd(ctx, "cat").
Input(strings.NewReader("hello")).
Input(strings.NewReader(" ")).
Input(strings.NewReader("world\n"))
lines, err := cmd.Run().Lines()
c.Assert(err, qt.IsNil)
c.Assert(lines, qt.CmpEquals(), []string{"hello world"})
})
c.Run("reset input", func(c *qt.C) {
cmd := run.Cmd(ctx, "cat").
Input(strings.NewReader("hello")).
ResetInput().
Input(strings.NewReader("world"))
lines, err := cmd.Run().Lines()
c.Assert(err, qt.IsNil)
c.Assert(lines, qt.CmpEquals(), []string{"world"})
})
}