-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.coffee
146 lines (121 loc) · 4.69 KB
/
test.coffee
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
chai = require 'chai'
chai.should()
chai.use require 'chai-as-promised'
exec = require '../'
describe 'exec', ->
it 'should accept Node.js style callbacks', (done) ->
exec.quiet 'bash -c "echo 1"', (err, stdout, stderr) ->
stdout.should.eq '1\n'
done()
it 'should expose Promise API', ->
{stdout} = yield exec.quiet 'bash -c "echo 1"'
stdout.should.eq '1\n'
it 'should split commands on multiple lines', ->
{stdout} = yield exec.quiet '''
bash -c "echo 1"
bash -c "echo 2"
bash -c "echo 3"
'''
stdout.should.eq '1\n2\n3\n'
it 'should split commands on multiple lines with spaces', ->
{stdout} = yield exec.quiet '''
bash -c "echo 1"
bash -c "echo 2"
bash -c "echo 3"
'''
stdout.should.eq '1\n2\n3\n'
it 'should buffer all stdout', (done) ->
exec.quiet 'bash -c "echo 1"', (err, stdout, stderr) ->
stdout.should.eq '1\n'
done()
it 'should buffer all stderr', (done) ->
exec.quiet 'bash -c doesnotexist', (err, stdout, stderr) ->
stderr.should.contain 'command not found'
done()
it 'should spawn shell if glob or pipe detected', ->
(yield exec 'echo foo | cat').stdout.should.eq 'foo\n'
it 'should parse env variables from beginning of commands', ->
(yield exec 'FOO="foo env" echo $FOO').stdout.should.eq 'foo env\n'
it 'should spawn shell if shell builtins detected', ->
process.env.FOO = 1
(yield exec 'printf $FOO').stdout.should.eq '1'
it 'should execute functions and promises as commands', ->
val = null
{stdout, stderr} = yield exec.serial [
-> val = 1
-> "bash -c 'sleep 1 && echo #{val}'"
-> exec 'bash -c "echo 2"'
'bash -c "echo 3"'
-> {stdout: 4}
]
stdout.should.eq '1\n2\n3\n4'
stderr.should.eq ''
it 'should collect results when commands are an object', ->
{a, b, stderr} = yield exec.serial a: 'printf a', b: 'printf b'
a.stdout.should.eq 'a'
b.stdout.should.eq 'b'
stderr.should.eq ''
describe 'promises', ->
it 'should not reject non-zero status', ->
{stdout, stderr, status} = yield exec.quiet 'bash -c doesnotexist'
stdout.should.eq ''
stderr.should.contain 'command not found'
status.should.eq 127
it 'should reject non-zero status with strict enabled', ->
p = exec.quiet 'bash -c doesnotexist', strict: true
yield p.should.be.rejectedWith Error
it 'should continue processing, when strict mode is implicitly disabled, after a non-zero exit code', ->
{stdout, stderr} = yield exec ['bash -c "exit 1"', "printf worked"]
console.log stdout, stderr
stdout.should.eq 'worked'
stderr.should.eq ''
it 'should continue processing, when strict mode is explicitly disabled, after a non-zero exit code', ->
{stdout, stderr} = yield exec ['bash -c "exit 1"', "printf worked"], strict: false
stdout.should.eq 'worked'
stderr.should.eq ''
it 'should not continue processing when strict mode is enabled after a non-zero exit code', ->
p = exec ['bash -c "exit 1"', "printf worked"], strict: true
yield p.should.be.rejectedWith Error
describe 'interactive', ->
it 'should not buffer output', ->
{stdout, stderr} = yield exec.interactive 'bash -c "echo 1"'
stdout.should.eq ''
stderr.should.eq ''
{stdout, stderr} = yield exec.interactive 'bash -c doesnotexist'
stdout.should.eq ''
stderr.should.eq ''
describe 'sync', ->
it 'should execute command synchronously', ->
{stdout, stderr} = exec.sync 'bash -c "echo 1"'
stdout.should.eq '1\n'
stderr.should.eq ''
it 'should collect results when commands are an object', ->
{a, b, stderr} = yield exec.sync a: 'printf a', b: 'printf b'
a.stdout.should.eq 'a'
b.stdout.should.eq 'b'
stderr.should.eq ''
describe 'parallel', ->
it 'should execute commands in parallel', ->
{stdout, stderr} = yield exec.parallel '''
bash -c "sleep 1 && echo 1"
bash -c "echo 2"
bash -c "echo 3"
'''
stdout.should.eq '2\n3\n1\n'
stderr.should.eq ''
it 'should execute commands in parallel, including functions and promises', ->
{stdout, stderr} = yield exec.parallel [
-> "bash -c 'sleep 1 && echo 1'"
-> exec 'bash -c "echo 2"'
'bash -c "echo 3"'
-> console.log 'ignored'
]
lines = (x.trim() for x in stdout.trim().split '\n')
lines.sort()
lines.should.eql ['1', '2', '3']
stderr.should.eq ''
it 'should collect results when commands are an object', ->
{a, b, stderr} = yield exec.parallel a: 'printf a', b: 'printf b'
a.stdout.should.eq 'a'
b.stdout.should.eq 'b'
stderr.should.eq ''