forked from go-cmd/cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_windows_test.go
384 lines (342 loc) · 8.31 KB
/
cmd_windows_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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//go:build windows
package cmd_test
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path"
"testing"
"time"
"github.com/go-cmd/cmd"
"github.com/go-test/deep"
)
func TestCmdOK(t *testing.T) {
now := time.Now().Unix()
p := cmd.NewCmd("echo", "foo")
gotStatus := <-p.Start()
expectStatus := cmd.Status{
Cmd: "echo",
PID: gotStatus.PID, // nondeterministic
Complete: true,
Exit: 0,
Error: nil,
Runtime: gotStatus.Runtime, // nondeterministic
Stdout: []string{"foo"},
Stderr: []string{},
}
if gotStatus.StartTs < now {
t.Error("StartTs < now")
}
if gotStatus.StopTs < gotStatus.StartTs {
t.Error("StopTs < StartTs")
}
gotStatus.StartTs = 0
gotStatus.StopTs = 0
if diffs := deep.Equal(gotStatus, expectStatus); diffs != nil {
t.Error(diffs)
}
if gotStatus.PID < 0 {
t.Errorf("got PID %d, expected non-zero", gotStatus.PID)
}
if gotStatus.Runtime < 0 {
t.Errorf("got runtime %f, expected non-zero", gotStatus.Runtime)
}
}
func TestCmdClone(t *testing.T) {
opt := cmd.Options{
Buffered: true,
}
c1 := cmd.NewCmdOptions(opt, "ls")
c1.Dir = "/tmp/"
c1.Env = []string{"YES=please"}
c2 := c1.Clone()
if c1.Name != c2.Name {
t.Errorf("got Name %s, expecting %s", c2.Name, c1.Name)
}
if c1.Dir != c2.Dir {
t.Errorf("got Dir %s, expecting %s", c2.Dir, c1.Dir)
}
if diffs := deep.Equal(c1.Env, c2.Env); diffs != nil {
t.Error(diffs)
}
}
func TestCmdNonzeroExit(t *testing.T) {
p := cmd.NewCmd("false")
gotStatus := <-p.Start()
expectStatus := cmd.Status{
Cmd: "false",
PID: gotStatus.PID, // nondeterministic
Complete: true,
Exit: 1,
Error: nil,
Runtime: gotStatus.Runtime, // nondeterministic
Stdout: []string{},
Stderr: []string{},
}
gotStatus.StartTs = 0
gotStatus.StopTs = 0
if diffs := deep.Equal(gotStatus, expectStatus); diffs != nil {
t.Error(diffs)
}
if gotStatus.PID < 0 {
t.Errorf("got PID %d, expected non-zero", gotStatus.PID)
}
if gotStatus.Runtime < 0 {
t.Errorf("got runtime %f, expected non-zero", gotStatus.Runtime)
}
}
func TestCmdStop(t *testing.T) {
p := cmd.NewCmd("sleep", "5")
// Start process in bg and get chan to receive final Status when done
statusChan := p.Start()
// Give it a second
time.Sleep(1 * time.Second)
// Kill the process
err := p.Stop()
if err != nil {
t.Error(err)
}
// The final status should be returned instantly
timeout := time.After(1 * time.Second)
var gotStatus cmd.Status
select {
case gotStatus = <-statusChan:
case <-timeout:
t.Fatal("timeout waiting for statusChan")
}
start := time.Unix(0, gotStatus.StartTs)
stop := time.Unix(0, gotStatus.StopTs)
d := stop.Sub(start).Seconds()
if d < 0.90 || d > 2 {
t.Errorf("stop - start time not between 0.9s and 2.0s: %s - %s = %f", stop, start, d)
}
gotStatus.StartTs = 0
gotStatus.StopTs = 0
expectStatus := cmd.Status{
Cmd: "sleep",
PID: gotStatus.PID, // nondeterministic
Complete: false,
Exit: 1,
Error: nil,
Runtime: gotStatus.Runtime, // nondeterministic
Stdout: []string{},
Stderr: []string{},
}
if diffs := deep.Equal(gotStatus, expectStatus); diffs != nil {
t.Error(diffs)
}
if gotStatus.PID < 0 {
t.Errorf("got PID %d, expected non-zero", gotStatus.PID)
}
if gotStatus.Runtime < 0 {
t.Errorf("got runtime %f, expected non-zero", gotStatus.Runtime)
}
// Stop should be idempotent
err = p.Stop()
if err != nil {
t.Error(err)
}
// Start should be idempotent, too. It just returns the same statusChan again.
c2 := p.Start()
if diffs := deep.Equal(statusChan, c2); diffs != nil {
t.Error(diffs)
}
}
func TestCmdNotStarted(t *testing.T) {
// Call everything _but_ Start.
p := cmd.NewCmd("echo", "foo")
gotStatus := p.Status()
expectStatus := cmd.Status{
Cmd: "echo",
PID: 0,
Complete: false,
Exit: -1,
Error: nil,
Runtime: 0,
Stdout: nil,
Stderr: nil,
}
if diffs := deep.Equal(gotStatus, expectStatus); diffs != nil {
t.Error(diffs)
}
err := p.Stop()
if err != cmd.ErrNotStarted {
t.Error(err)
}
}
func TestCmdOutput(t *testing.T) {
t.Skip("FIXME")
tmpfile, err := ioutil.TempFile("", "cmd.TestCmdOutput")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
t.Logf("temp file: %s", tmpfile.Name())
os.Remove(tmpfile.Name())
p := cmd.NewCmd(path.Join(".", "test", "touch-file-count"), tmpfile.Name())
p.Start()
touchFile := func(file string) {
if err := exec.Command("touch", file).Run(); err != nil {
t.Fatal(err)
}
time.Sleep(600 * time.Millisecond)
}
var s cmd.Status
var stdout []string
touchFile(tmpfile.Name())
s = p.Status()
stdout = []string{"1"}
if diffs := deep.Equal(s.Stdout, stdout); diffs != nil {
t.Log(s.Stdout)
t.Error(diffs)
}
touchFile(tmpfile.Name())
s = p.Status()
stdout = []string{"1", "2"}
if diffs := deep.Equal(s.Stdout, stdout); diffs != nil {
t.Log(s.Stdout)
t.Error(diffs)
}
// No more output yet
s = p.Status()
stdout = []string{"1", "2"}
if diffs := deep.Equal(s.Stdout, stdout); diffs != nil {
t.Log(s.Stdout)
t.Error(diffs)
}
// +2 lines
touchFile(tmpfile.Name())
touchFile(tmpfile.Name())
s = p.Status()
stdout = []string{"1", "2", "3", "4"}
if diffs := deep.Equal(s.Stdout, stdout); diffs != nil {
t.Log(s.Stdout)
t.Error(diffs)
}
// Kill the process
if err := p.Stop(); err != nil {
t.Error(err)
}
}
func TestCmdNotFound(t *testing.T) {
t.Skip("FIXME")
p := cmd.NewCmd("cmd-does-not-exist")
gotStatus := <-p.Start()
gotStatus.StartTs = 0
gotStatus.StopTs = 0
expectStatus := cmd.Status{
Cmd: "cmd-does-not-exist",
PID: 0,
Complete: false,
Exit: -1,
Error: errors.New(`exec: "cmd-does-not-exist": executable file not found in $PATH`),
Runtime: 0,
Stdout: nil,
Stderr: nil,
}
if diffs := deep.Equal(gotStatus, expectStatus); diffs != nil {
t.Logf("%+v", gotStatus)
t.Error(diffs)
}
}
func TestDone(t *testing.T) {
t.Skip("FIXME")
// Count to 3 sleeping 1s between counts
p := cmd.NewCmd(path.Join(".", "test", "count-and-sleep"), "3", "1")
statusChan := p.Start()
// For 2s while cmd is running, Done() chan should block, which means
// it's still running
runningTimer := time.After(2 * time.Second)
TIMER:
for {
select {
case <-runningTimer:
break TIMER
default:
}
select {
case <-p.Done():
t.Fatal("Done chan is closed before runningTime finished")
default:
// Done chan blocked, cmd is still running
}
time.Sleep(400 * time.Millisecond)
}
// Wait for cmd to complete
var s1 cmd.Status
select {
case s1 = <-statusChan:
t.Logf("got status: %+v", s1)
case <-time.After(2 * time.Second):
t.Fatal("timeout waiting for cmd to complete")
}
// After cmd completes, Done chan should be closed and not block
select {
case <-p.Done():
default:
t.Fatal("Done chan did not block after cmd completed")
}
// After command completes, we should be able to get exact same
// Status that's returned on the Start() chan
s2 := p.Status()
if diff := deep.Equal(s1, s2); diff != nil {
t.Error(diff)
}
}
func TestCmdEnvOK(t *testing.T) {
t.Skip("FIXME")
now := time.Now().Unix()
p := cmd.NewCmd("env")
p.Env = []string{"FOO=foo"}
gotStatus := <-p.Start()
expectStatus := cmd.Status{
Cmd: "env",
PID: gotStatus.PID, // nondeterministic
Complete: true,
Exit: 0,
Error: nil,
Runtime: gotStatus.Runtime, // nondeterministic
Stdout: []string{"FOO=foo"},
Stderr: []string{},
}
if gotStatus.StartTs < now {
t.Error("StartTs < now")
}
if gotStatus.StopTs < gotStatus.StartTs {
t.Error("StopTs < StartTs")
}
gotStatus.StartTs = 0
gotStatus.StopTs = 0
if diffs := deep.Equal(gotStatus, expectStatus); diffs != nil {
t.Error(diffs)
}
if gotStatus.PID < 0 {
t.Errorf("got PID %d, expected non-zero", gotStatus.PID)
}
if gotStatus.Runtime < 0 {
t.Errorf("got runtime %f, expected non-zero", gotStatus.Runtime)
}
}
func TestCmdNoOutput(t *testing.T) {
// Set both output options to false to discard all output
p := cmd.NewCmdOptions(
cmd.Options{
Buffered: false,
Streaming: false,
},
"echo", "hell-world")
s := <-p.Start()
if s.Exit != 0 {
t.Errorf("got exit %d, expected 0", s.Exit)
}
if len(s.Stdout) != 0 {
t.Errorf("got stdout, expected no output: %v", s.Stdout)
}
if len(s.Stderr) != 0 {
t.Errorf("got stderr, expected no output: %v", s.Stderr)
}
}