2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
|
|
|
local clear = n.clear
|
|
|
|
local exec_lua = n.exec_lua
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2023-06-07 05:52:23 -07:00
|
|
|
|
|
|
|
local function system_sync(cmd, opts)
|
|
|
|
return exec_lua(
|
|
|
|
[[
|
2023-09-04 04:03:03 -07:00
|
|
|
local cmd, opts = ...
|
2023-09-04 03:30:16 -07:00
|
|
|
local obj = vim.system(...)
|
2023-09-04 04:03:03 -07:00
|
|
|
|
|
|
|
if opts.timeout then
|
|
|
|
-- Minor delay before calling wait() so the timeout uv timer can have a headstart over the
|
|
|
|
-- internal call to vim.wait() in wait().
|
|
|
|
vim.wait(10)
|
|
|
|
end
|
|
|
|
|
2023-09-04 03:30:16 -07:00
|
|
|
local res = obj:wait()
|
|
|
|
|
|
|
|
-- Check the process is no longer running
|
2023-09-04 04:03:03 -07:00
|
|
|
local proc = vim.api.nvim_get_proc(obj.pid)
|
|
|
|
assert(not proc, 'process still exists')
|
2023-09-04 03:30:16 -07:00
|
|
|
|
|
|
|
return res
|
2023-06-07 05:52:23 -07:00
|
|
|
]],
|
|
|
|
cmd,
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function system_async(cmd, opts)
|
2023-09-04 03:30:16 -07:00
|
|
|
return exec_lua(
|
|
|
|
[[
|
2023-06-07 05:52:23 -07:00
|
|
|
local cmd, opts = ...
|
|
|
|
_G.done = false
|
2023-09-04 03:30:16 -07:00
|
|
|
local obj = vim.system(cmd, opts, function(obj)
|
2023-06-07 05:52:23 -07:00
|
|
|
_G.done = true
|
|
|
|
_G.ret = obj
|
|
|
|
end)
|
|
|
|
|
2023-09-04 04:03:03 -07:00
|
|
|
local ok = vim.wait(10000, function()
|
2023-09-04 03:30:16 -07:00
|
|
|
return _G.done
|
|
|
|
end)
|
|
|
|
|
2023-09-04 04:03:03 -07:00
|
|
|
assert(ok, 'process did not exit')
|
2023-06-07 05:52:23 -07:00
|
|
|
|
2023-09-04 03:30:16 -07:00
|
|
|
-- Check the process is no longer running
|
2023-09-04 04:03:03 -07:00
|
|
|
local proc = vim.api.nvim_get_proc(obj.pid)
|
|
|
|
assert(not proc, 'process still exists')
|
2023-09-04 03:30:16 -07:00
|
|
|
|
|
|
|
return _G.ret
|
|
|
|
]],
|
|
|
|
cmd,
|
|
|
|
opts
|
|
|
|
)
|
2023-06-07 05:52:23 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
describe('vim.system', function()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
end)
|
|
|
|
|
|
|
|
for name, system in pairs { sync = system_sync, async = system_async } do
|
|
|
|
describe('(' .. name .. ')', function()
|
|
|
|
it('can run simple commands', function()
|
|
|
|
eq('hello\n', system({ 'echo', 'hello' }, { text = true }).stdout)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('handle input', function()
|
|
|
|
eq('hellocat', system({ 'cat' }, { stdin = 'hellocat', text = true }).stdout)
|
|
|
|
end)
|
|
|
|
|
2023-09-04 03:30:16 -07:00
|
|
|
it('supports timeout', function()
|
2023-06-07 05:52:23 -07:00
|
|
|
eq({
|
2023-09-04 03:30:16 -07:00
|
|
|
code = 124,
|
|
|
|
signal = 15,
|
2023-06-07 05:52:23 -07:00
|
|
|
stdout = '',
|
2023-09-04 03:30:16 -07:00
|
|
|
stderr = '',
|
2023-09-04 04:03:03 -07:00
|
|
|
}, system({ 'sleep', '10' }, { timeout = 1000 }))
|
2023-06-07 05:52:23 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2023-09-03 02:17:24 -07:00
|
|
|
it('kill processes', function()
|
|
|
|
exec_lua([[
|
|
|
|
local signal
|
|
|
|
local cmd = vim.system({ 'cat', '-' }, { stdin = true }, function(r)
|
|
|
|
signal = r.signal
|
|
|
|
end) -- run forever
|
|
|
|
|
|
|
|
cmd:kill('sigint')
|
|
|
|
|
|
|
|
-- wait for the process not to exist
|
|
|
|
local done = vim.wait(2000, function()
|
|
|
|
return signal ~= nil
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert(done, 'process did not exit')
|
|
|
|
|
|
|
|
-- Check the process is no longer running
|
2023-09-04 04:03:03 -07:00
|
|
|
local proc = vim.api.nvim_get_proc(cmd.pid)
|
|
|
|
assert(not proc, 'process still exists')
|
2023-09-03 02:17:24 -07:00
|
|
|
|
|
|
|
assert(signal == 2)
|
|
|
|
]])
|
|
|
|
end)
|
2024-02-02 06:52:01 -07:00
|
|
|
|
|
|
|
it('SystemObj:wait() does not process non-fast events #27292', function()
|
|
|
|
eq(
|
|
|
|
false,
|
|
|
|
exec_lua([[
|
|
|
|
_G.processed = false
|
|
|
|
local cmd = vim.system({ 'sleep', '1' })
|
|
|
|
vim.schedule(function() _G.processed = true end)
|
|
|
|
cmd:wait()
|
|
|
|
return _G.processed
|
|
|
|
]])
|
|
|
|
)
|
|
|
|
eq(true, exec_lua([[return _G.processed]]))
|
|
|
|
end)
|
2023-06-07 05:52:23 -07:00
|
|
|
end)
|