Files
neovim/test/functional/lua/system_spec.lua
T
Justin M. Keyes 62f79bd7e1 feat(remote-ssh): lifecycle, cleanup, slow tests
- SshHost fields: follow ssh's "first wins" rule, so e.g. a later User
  line in the same block no longer overrides an earlier one.
- Local server now exits on its own when the remote session ends.
- Before auto-install, try existing `nvim` found in remote `$PATH`.
- Combine the remote_ssh_x.sh scripts into one remote_ssh.sh script.
- Ignore `RemoteCommand` from ssh_config, which made ssh refuse our command.
- Don't accumulate the remote heartbeat in memory for the whole session:
  `run_wait()` captures output only until it returns.
- Before detaching the UI, check that the remote Nvim answers through the
  forward: sshd may refuse the forward, but still run the script.
- Stop the session (tunnel, remote Nvim) if any connect step fails, or if
  the UI detached meanwhile.
- Run `ssh -G` once per session, instead of for every ssh command.
- Remote script handles signals at once, instead of after the heartbeat's
- Error if :restart is attempted.
  FUTURE: Remote :restart prints a marker, with its [command], on
  stdout. It also sends the TUI a restart event pointing at the LOCAL
  server. The TUI reattaches there. Local server repeats the remote-ssh
  connect steps, and sends the TUI a connect event for the new forwarded
  socket. Then it runs [command] (e.g. session restore) on the new
  remote over RPC.
2026-11-03 19:05:58 +01:00

255 lines
7.4 KiB
Lua

local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local describe, it, before_each = t.describe, t.it, t.before_each
local clear = n.clear
local exec_lua = n.exec_lua
local eq = t.eq
describe('vim.system', function()
before_each(function()
clear()
end)
for name, system in pairs { sync = n.system_sync, async = n.system_async } do
describe('(' .. name .. ')', function()
it('failure modes', function()
t.matches('cmd: expected non%-empty list of strings', t.pcall_err(system, {}))
t.matches('cmd: expected non%-empty list of strings', t.pcall_err(system, { 42 }))
t.matches(
"ENOENT%: no such file .* %(cmd%): 'non%-existent%-cmd'",
t.pcall_err(system, { 'non-existent-cmd', 'arg1', 'arg2' }, { text = true })
)
t.matches(
"ENOENT%: no such file .* %(cwd%): 'non%-existent%-cwd'",
t.pcall_err(system, { 'echo', 'hello' }, { cwd = 'non-existent-cwd', text = true })
)
end)
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)
it('uses real pipes for stdin/stdout #35984', function()
if t.is_os('win') then
return -- Not applicable for Windows.
end
local res = system(
{ 'bash', '-c', 'wc /dev/stdin > /dev/stdout' },
{ stdin = 'pipe\npipy text\n' }
)
eq({ '2', '3', '15', '/dev/stdin' }, vim.split(res.stdout, '%s+', { trimempty = true }))
end)
it('can set environment', function()
local function test_env(opt)
eq('TESTVAL', system({ n.testprg('printenv-test'), 'TEST' }, opt).stdout)
end
test_env({ env = { TEST = 'TESTVAL' }, text = true })
test_env({ clear_env = true, env = { TEST = 'TESTVAL' }, text = true })
end)
it('can set environment with clear_env = true and env = nil', function()
exec_lua(function()
vim.env.TEST = 'TESTVAL'
end)
-- Not passing env with clear_env should not clear the environment
eq(
'TESTVAL',
system({ n.testprg('printenv-test'), 'TEST' }, { clear_env = true, text = true }).stdout
)
-- Passing env = {} with clear_env should clear the environment
eq(
'',
system(
{ n.testprg('printenv-test'), 'TEST' },
{ env = {}, clear_env = true, text = true }
).stdout
)
end)
it('supports timeout', function()
if name == 'sync' and not t.is_os('win') then
eq(
{ code = 0, signal = 0, stdout = 'late', stderr = '' },
exec_lua(function()
-- The shell exits before its child closes the inherited output pipes.
local obj = vim.system({ 'sh', '-c', '(sleep 0.3; printf late) & exit 0' })
assert(vim.wait(1000, function()
return obj:is_closing()
end, nil, true))
return obj:wait(100) or obj:wait()
end)
)
end
eq({
code = 124,
signal = 15,
stdout = '',
stderr = '',
}, system({ 'sleep', '10' }, { timeout = 1000 }))
end)
end)
end
it('timeout excludes prior event-loop blocking #42521', function()
eq(
0,
exec_lua(function(prg)
vim.uv.sleep(300) -- Ensure stale/outdated cached loop time relative to timeout=200 below.
return vim.system({ prg, 'PATH' }, { timeout = 200 }):wait().code
end, n.testprg('printenv-test'))
)
end)
it('kill processes', function()
exec_lua(function()
local signal --- @type integer?
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
local proc = vim.api.nvim_get_proc(cmd.pid)
assert(not proc, 'process still exists')
assert(signal == 2)
end)
end)
it('run_wait() can return before process exit', function()
t.skip(t.is_os('win'), 'shell command is POSIX-only')
exec_lua(function()
local res, exited
local ok, err = pcall(function()
res = require('vim._core.system').run_wait(
{ 'bash', '-c', 'echo READY; sleep 1; echo LATER' },
nil,
function(stdout)
return stdout:match('READY') ~= nil
end,
10000,
function(out)
exited = out
end
)
assert(res)
assert(res.code == nil)
assert(res.stdout == 'READY\n')
assert(vim.api.nvim_get_proc(res.pid), 'process already exited')
-- Output after return (e.g. a heartbeat) is not captured.
assert(vim.wait(5000, function()
return exited ~= nil
end))
assert(exited.code == 0 and exited.stdout == nil)
end)
if res then
if not res:is_closing() then
res:kill('sigterm')
end
res:wait(1000)
end
if not ok then
error(err)
end
end)
end)
it('run_wait() writes stdin', function()
t.skip(t.is_os('win'), 'shell command is POSIX-only')
eq(
'hello',
exec_lua(function()
return require('vim._core.system').run_wait({ 'cat' }, { stdin = 'hello' }, nil, 10000).stdout
end)
)
end)
it('SystemObj:wait() does not process non-fast events #27292', function()
eq(
false,
exec_lua(function()
_G.processed = false
local cmd = vim.system({ 'sleep', '1' })
vim.schedule(function()
_G.processed = true
end)
cmd:wait()
return _G.processed
end)
)
eq(true, exec_lua([[return _G.processed]]))
end)
if t.is_os('win') then
it('can resolve windows command extensions', function()
t.write_file('test.bat', 'echo hello world')
n.system_sync({ 'chmod', '+x', 'test.bat' })
n.system_sync({ './test' })
end)
it('launches cmd.exe when shellslash is set', function()
n.command('set shellslash')
local path = exec_lua([[return vim.fn.exepath('cmd.exe')]])
t.neq(nil, path:find('/', 1, true))
local result = n.system_sync({ 'cmd.exe', '/c', 'echo OK' })
eq(0, result.code)
eq('OK\r\n', result.stdout)
eq('', result.stderr)
end)
end
it('always captures all content of stdout/stderr #30846', function()
t.skip(n.fn.executable('git') == 0, 'N/A: missing "git" command')
t.skip(n.fn.isdirectory('.git') == 0, 'N/A: missing ".git" directory')
eq(
0,
exec_lua(function()
local done = 0
local fail = 0
for _ = 1, 200 do
vim.system(
{ 'git', 'show', ':0:test/functional/plugin/lsp_spec.lua' },
{ text = true },
function(o)
if o.code ~= 0 or #o.stdout == 0 then
fail = fail + 1
end
done = done + 1
end
)
end
local ok = vim.wait(10000, function()
return done == 200
end, 200)
return fail + (ok and 0 or 1)
end)
)
end)
end)