-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproc_test.lua
More file actions
258 lines (224 loc) · 5.04 KB
/
proc_test.lua
File metadata and controls
258 lines (224 loc) · 5.04 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
local proc = require'proc'
local ffi = require'ffi'
local time = require'time'
local glue = require'glue'
local fs = require'fs'
local pp = require'pp'
io.stdout:setvbuf'no'
io.stderr:setvbuf'no'
local tests = {}
local test = setmetatable({}, {__newindex = function(self, k, v)
rawset(self, k, v)
table.insert(tests, k)
end})
function test.env()
proc.env('zz', '123')
proc.env('zZ', '567')
if ffi.abi'win' then
assert(proc.env('zz') == '567')
assert(proc.env('zZ') == '567')
else
assert(proc.env('zz') == '123')
assert(proc.env('zZ') == '567')
end
proc.env('zz', false)
proc.env('zZ', false)
assert(not proc.env'zz')
assert(not proc.env'zZ')
proc.env('Zz', '321')
local t = proc.env()
pp(t)
assert(t.Zz == '321')
end
function test.exec_luafile()
local p = assert(proc.exec_luafile('test.lua'))
p:forget()
end
function test.kill()
local luajit = fs.exepath()
local p, err, errno = proc.exec(
{luajit, '-e', 'local n=.12; for i=1,1000000000 do n=n*0.5321 end; print(n); os.exit(123)'},
--{'-e', 'print(os.getenv\'XX\', require\'fs\'.cd()); os.exit(123)'},
{XX = 55},
'bin'
)
if not p then print(err, errno) end
assert(p)
print('pid:', p.id)
print'sleeping'
time.sleep(.5)
print'killing'
assert(p:kill())
assert(p:kill())
time.sleep(.5)
print('exit code', p:exit_code())
print('exit code', p:exit_code())
--assert(p:exit_code() == 123)
p:forget()
end
function test.pipe()
local in_rf , in_wf = fs.pipe()
local out_rf, out_wf = fs.pipe()
local err_rf, err_wf = fs.pipe()
io.stdin:setvbuf'no'
io.stdout:setvbuf'no'
io.stderr:setvbuf'no'
assert(glue.writefile('proc_test_pipe.lua', [[
io.stdin:setvbuf'no'
io.stdout:setvbuf'no'
io.stderr:setvbuf'no'
n = io.stdin:read('*n')
io.stderr:write'Error\r\n\r\n'
print'Hello'
os.exit(123)
]]))
local p = assert(proc.exec_luafile({
script = 'proc_test_pipe.lua',
stdin = in_rf,
stdout = out_wf,
--stderr = err_wf,
autokill = true,
}))
--required to avoid hanging.
in_rf:close()
out_wf:close()
err_wf:close()
local s = '1234\n'
in_wf:write(s)
in_wf:close()
local cb = ffi.new('uint8_t[1]')
while true do
local rlen, err = out_rf:read(cb, 1)
if not rlen or rlen == 0 then
break
end
local c = string.char(cb[0])
io.stdout:write(c)
end
print('exit code', p:wait(1/0))
assert(os.remove('proc_test_pipe.lua'))
end
function test.pipe_async()
local sock = require'sock'
io.stdin:setvbuf'no'
io.stdout:setvbuf'no'
io.stderr:setvbuf'no'
assert(glue.writefile('proc_test_pipe.lua', [[
io.stdin:setvbuf'no'
io.stdout:setvbuf'no'
io.stderr:setvbuf'no'
local time = require'time'
time.sleep(.1)
print'Started'
time.sleep(.1)
local n = assert(io.stdin:read('*n'))
print('Got '..n)
time.sleep(.1)
io.stderr:write'Error1\n'
time.sleep(.1)
print'Hello1'
time.sleep(.1)
io.stderr:write'Error2\n'
time.sleep(.1)
print'Hello2'
io.stderr:write'Error3\n'
time.sleep(.1)
print'Hello3'
time.sleep(.1)
os.exit(123)
]]))
local char_vla, sz = ffi.typeof'char[?]', 1024
sock.run(function()
local p = assert(proc.exec_luafile({
script = 'proc_test_pipe.lua',
async = true,
stdin = true,
stdout = true,
stderr = true,
autokill = true,
}))
if p.stdin then
sock.thread(function()
local s = '1234\n'
assert(p.stdin:write(s))
p.stdin:close()
end)
end
if p.stdout then
sock.thread(function()
local buf = char_vla(sz)
while true do
local len = assert(p.stdout:read(buf, sz))
if len > 0 then
io.stdout:write(ffi.string(buf, len))
else
p.stdout:close()
break
end
end
end)
end
if p.stderr then
sock.thread(function()
local buf = char_vla(sz)
while true do
local len = assert(p.stderr:read(buf, sz))
if len > 0 then
io.stdout:write(ffi.string(buf, len))
else
p.stderr:close()
break
end
end
end)
end
print('Process finished. Exit code:', p:wait(1/0))
while
(p.stdin and not p.stdin :closed())
or (p.stdout and not p.stdout:closed())
or (p.stderr and not p.stderr:closed())
do
print'Still waiting for the pipes to close...'
print(glue.catargs(' ',
p.stdin and not p.stdin :closed() and 'stdin' or nil,
p.stdout and not p.stdout:closed() and 'stdout' or nil,
p.stderr and not p.stderr:closed() and 'stderr' or nil
))
sock.sleep(.1)
end
assert(os.remove('proc_test_pipe.lua'))
end)
end
function test.autokill()
if ffi.abi'win' then
assert(proc.exec{cmd = 'notepad', autokill = true})
print'waiting 1s'
time.sleep(1)
else
assert(proc.exec{cmd = '/bin/sleep 123', autokill = true})
print'waiting 5s'
time.sleep(5)
end
print'done'
end
function test_all()
for i,k in ipairs(tests) do
print'+--------------------------------------------------------------+'
print(string.format('| %-60s |', k))
print'+--------------------------------------------------------------+'
test[k]()
end
end
function test.esc()
if ffi.abi'win' then
assert(proc.quote_arg[[a\"xx"yx\\\]] == [["a\\\"xx\"yx\\\\\\"]])
else
--TODO
end
end
test.env()
test.esc()
--test.pipe()
test.pipe_async()
--test.autokill()
--test_all()