fix(vim.system): make timeout work properly

Mimic the behaviour of timeout(1) from coreutils.
This commit is contained in:
Lewis Russell
2023-09-04 11:30:16 +01:00
parent a44521f46e
commit 6d5f12efd2
4 changed files with 103 additions and 59 deletions

View File

@@ -5,27 +5,39 @@ local eq = helpers.eq
local function system_sync(cmd, opts)
return exec_lua([[
return vim.system(...):wait()
local obj = vim.system(...)
local pid = obj.pid
local res = obj:wait()
-- Check the process is no longer running
vim.fn.systemlist({'ps', 'p', tostring(pid)})
assert(vim.v.shell_error == 1, 'process still exists')
return res
]], cmd, opts)
end
local function system_async(cmd, opts)
exec_lua([[
return exec_lua([[
local cmd, opts = ...
_G.done = false
vim.system(cmd, opts, function(obj)
local obj = vim.system(cmd, opts, function(obj)
_G.done = true
_G.ret = obj
end)
local done = vim.wait(10000, function()
return _G.done
end)
assert(done, 'process did not exit')
-- Check the process is no longer running
vim.fn.systemlist({'ps', 'p', tostring(obj.pid)})
assert(vim.v.shell_error == 1, 'process still exists')
return _G.ret
]], cmd, opts)
while true do
if exec_lua[[return _G.done]] then
break
end
end
return exec_lua[[return _G.ret]]
end
describe('vim.system', function()
@@ -43,12 +55,12 @@ describe('vim.system', function()
eq('hellocat', system({ 'cat' }, { stdin = 'hellocat', text = true }).stdout)
end)
it ('supports timeout', function()
it('supports timeout', function()
eq({
code = 0,
signal = 2,
code = 124,
signal = 15,
stdout = '',
stderr = "Command timed out: 'sleep 10'"
stderr = ''
}, system({ 'sleep', '10' }, { timeout = 1 }))
end)
end)