2016-04-23 16:53:11 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2023-12-12 12:19:46 -07:00
|
|
|
local thelpers = require('test.functional.terminal.helpers')
|
2024-01-12 04:28:20 -07:00
|
|
|
|
|
|
|
local clear = helpers.clear
|
|
|
|
local eq = helpers.eq
|
|
|
|
local eval = helpers.eval
|
|
|
|
local exc_exec = helpers.exc_exec
|
|
|
|
local feed_command = helpers.feed_command
|
|
|
|
local feed = helpers.feed
|
|
|
|
local insert = helpers.insert
|
|
|
|
local neq = helpers.neq
|
|
|
|
local next_msg = helpers.next_msg
|
|
|
|
local nvim = helpers.nvim
|
|
|
|
local testprg = helpers.testprg
|
|
|
|
local ok = helpers.ok
|
|
|
|
local source = helpers.source
|
|
|
|
local write_file = helpers.write_file
|
|
|
|
local mkdir = helpers.mkdir
|
|
|
|
local rmdir = helpers.rmdir
|
2021-09-01 07:29:38 -07:00
|
|
|
local assert_alive = helpers.assert_alive
|
2016-05-12 13:25:15 -07:00
|
|
|
local command = helpers.command
|
2018-03-17 16:09:28 -07:00
|
|
|
local funcs = helpers.funcs
|
2019-09-11 18:26:35 -07:00
|
|
|
local os_kill = helpers.os_kill
|
2018-03-17 16:09:28 -07:00
|
|
|
local retry = helpers.retry
|
|
|
|
local meths = helpers.meths
|
2024-01-12 04:28:20 -07:00
|
|
|
local NIL = vim.NIL
|
2020-10-19 11:17:51 -07:00
|
|
|
local poke_eventloop = helpers.poke_eventloop
|
2017-03-29 11:07:39 -07:00
|
|
|
local get_pathsep = helpers.get_pathsep
|
2018-01-24 03:01:14 -07:00
|
|
|
local pathroot = helpers.pathroot
|
2022-05-03 06:08:35 -07:00
|
|
|
local exec_lua = helpers.exec_lua
|
2017-08-03 00:57:12 -07:00
|
|
|
local nvim_set = helpers.nvim_set
|
2017-06-10 06:25:23 -07:00
|
|
|
local expect_twostreams = helpers.expect_twostreams
|
2018-02-16 11:42:05 -07:00
|
|
|
local expect_msg_seq = helpers.expect_msg_seq
|
2019-09-06 17:17:37 -07:00
|
|
|
local pcall_err = helpers.pcall_err
|
2022-08-18 07:09:50 -07:00
|
|
|
local matches = helpers.matches
|
2015-08-21 05:07:06 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
2022-11-13 06:52:19 -07:00
|
|
|
local skip = helpers.skip
|
2022-11-21 17:13:30 -07:00
|
|
|
local is_os = helpers.is_os
|
2014-10-27 14:58:22 -07:00
|
|
|
|
|
|
|
describe('jobs', function()
|
2015-03-17 04:48:03 -07:00
|
|
|
local channel
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
2018-11-09 07:55:47 -07:00
|
|
|
|
2015-03-17 04:48:03 -07:00
|
|
|
channel = nvim('get_api_info')[1]
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('set_var', 'channel', channel)
|
|
|
|
source([[
|
2017-01-14 01:47:33 -07:00
|
|
|
function! Normalize(data) abort
|
2020-12-28 18:40:07 -07:00
|
|
|
" Windows: remove ^M and term escape sequences
|
2017-01-14 01:47:33 -07:00
|
|
|
return type([]) == type(a:data)
|
2020-12-28 18:40:07 -07:00
|
|
|
\ ? map(a:data, 'substitute(substitute(v:val, "\r", "", "g"), "\x1b\\%(\\]\\d\\+;.\\{-}\x07\\|\\[.\\{-}[\x40-\x7E]\\)", "", "g")')
|
2017-01-14 01:47:33 -07:00
|
|
|
\ : a:data
|
|
|
|
endfunction
|
2017-06-10 06:25:23 -07:00
|
|
|
function! OnEvent(id, data, event) dict
|
2015-03-25 19:11:05 -07:00
|
|
|
let userdata = get(self, 'user')
|
2017-01-14 01:47:33 -07:00
|
|
|
let data = Normalize(a:data)
|
|
|
|
call rpcnotify(g:channel, a:event, userdata, data)
|
2015-03-25 19:11:05 -07:00
|
|
|
endfunction
|
|
|
|
let g:job_opts = {
|
2017-06-10 06:25:23 -07:00
|
|
|
\ 'on_stdout': function('OnEvent'),
|
|
|
|
\ 'on_exit': function('OnEvent'),
|
2015-03-25 19:11:05 -07:00
|
|
|
\ 'user': 0
|
|
|
|
\ }
|
|
|
|
]])
|
2015-03-17 04:48:03 -07:00
|
|
|
end)
|
2014-10-27 14:58:22 -07:00
|
|
|
|
2019-12-12 05:26:39 -07:00
|
|
|
it('must specify env option as a dict', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
command('let g:job_opts.env = v:true')
|
2019-12-12 05:26:39 -07:00
|
|
|
local _, err = pcall(function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2019-12-12 05:26:39 -07:00
|
|
|
nvim('command', "let j = jobstart('set', g:job_opts)")
|
|
|
|
else
|
|
|
|
nvim('command', "let j = jobstart('env', g:job_opts)")
|
|
|
|
end
|
|
|
|
end)
|
2024-01-02 18:09:18 -07:00
|
|
|
ok(string.find(err, 'E475: Invalid argument: env') ~= nil)
|
2019-12-12 05:26:39 -07:00
|
|
|
end)
|
|
|
|
|
2018-11-09 07:55:47 -07:00
|
|
|
it('append environment #env', function()
|
|
|
|
nvim('command', "let $VAR = 'abc'")
|
2020-12-28 18:48:15 -07:00
|
|
|
nvim('command', "let $TOTO = 'goodbye world'")
|
2018-11-09 07:55:47 -07:00
|
|
|
nvim('command', "let g:job_opts.env = {'TOTO': 'hello world'}")
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2018-11-09 07:55:47 -07:00
|
|
|
nvim('command', [[call jobstart('echo %TOTO% %VAR%', g:job_opts)]])
|
|
|
|
else
|
|
|
|
nvim('command', [[call jobstart('echo $TOTO $VAR', g:job_opts)]])
|
|
|
|
end
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
expect_msg_seq({
|
|
|
|
{ 'notification', 'stdout', { 0, { 'hello world abc' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '', '' } } },
|
|
|
|
}, {
|
|
|
|
{ 'notification', 'stdout', { 0, { 'hello world abc', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
|
|
|
})
|
2018-11-09 07:55:47 -07:00
|
|
|
end)
|
|
|
|
|
2020-07-30 22:17:24 -07:00
|
|
|
it('append environment with pty #env', function()
|
|
|
|
nvim('command', "let $VAR = 'abc'")
|
2020-12-28 18:48:15 -07:00
|
|
|
nvim('command', "let $TOTO = 'goodbye world'")
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim('command', 'let g:job_opts.pty = v:true')
|
2020-07-30 22:17:24 -07:00
|
|
|
nvim('command', "let g:job_opts.env = {'TOTO': 'hello world'}")
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2020-07-30 22:17:24 -07:00
|
|
|
nvim('command', [[call jobstart('echo %TOTO% %VAR%', g:job_opts)]])
|
|
|
|
else
|
|
|
|
nvim('command', [[call jobstart('echo $TOTO $VAR', g:job_opts)]])
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
expect_msg_seq({
|
|
|
|
{ 'notification', 'stdout', { 0, { 'hello world abc' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '', '' } } },
|
|
|
|
}, {
|
|
|
|
{ 'notification', 'stdout', { 0, { 'hello world abc', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
|
|
|
})
|
2020-07-30 22:17:24 -07:00
|
|
|
end)
|
|
|
|
|
2018-11-09 07:55:47 -07:00
|
|
|
it('replace environment #env', function()
|
|
|
|
nvim('command', "let $VAR = 'abc'")
|
2020-12-28 18:48:15 -07:00
|
|
|
nvim('command', "let $TOTO = 'goodbye world'")
|
2018-11-09 07:55:47 -07:00
|
|
|
nvim('command', "let g:job_opts.env = {'TOTO': 'hello world'}")
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim('command', 'let g:job_opts.clear_env = 1')
|
2018-11-09 07:55:47 -07:00
|
|
|
|
|
|
|
-- libuv ensures that certain "required" environment variables are
|
|
|
|
-- preserved if the user doesn't provide them in a custom environment
|
|
|
|
-- https://github.com/libuv/libuv/blob/635e0ce6073c5fbc96040e336b364c061441b54b/src/win/process.c#L672
|
|
|
|
-- https://github.com/libuv/libuv/blob/635e0ce6073c5fbc96040e336b364c061441b54b/src/win/process.c#L48-L60
|
|
|
|
--
|
|
|
|
-- Rather than expecting a completely empty environment, ensure that $VAR
|
|
|
|
-- is *not* in the environment but $TOTO is.
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2018-11-09 07:55:47 -07:00
|
|
|
nvim('command', [[call jobstart('echo %TOTO% %VAR%', g:job_opts)]])
|
|
|
|
expect_msg_seq({
|
2024-01-02 18:09:18 -07:00
|
|
|
{ 'notification', 'stdout', { 0, { 'hello world %VAR%', '' } } },
|
2018-11-09 07:55:47 -07:00
|
|
|
})
|
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim('command', 'set shell=/bin/sh')
|
2018-11-09 07:55:47 -07:00
|
|
|
nvim('command', [[call jobstart('echo $TOTO $VAR', g:job_opts)]])
|
|
|
|
expect_msg_seq({
|
2024-01-02 18:09:18 -07:00
|
|
|
{ 'notification', 'stdout', { 0, { 'hello world', '' } } },
|
2018-11-09 07:55:47 -07:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2021-05-13 17:56:25 -07:00
|
|
|
it('handles case-insensitively matching #env vars', function()
|
|
|
|
nvim('command', "let $TOTO = 'abc'")
|
|
|
|
-- Since $Toto is being set in the job, it should take precedence over the
|
|
|
|
-- global $TOTO on Windows
|
|
|
|
nvim('command', "let g:job_opts = {'env': {'Toto': 'def'}, 'stdout_buffered': v:true}")
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2021-05-13 17:56:25 -07:00
|
|
|
nvim('command', [[let j = jobstart('set | find /I "toto="', g:job_opts)]])
|
|
|
|
else
|
|
|
|
nvim('command', [[let j = jobstart('env | grep -i toto=', g:job_opts)]])
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim('command', 'call jobwait([j])')
|
|
|
|
nvim('command', 'let g:output = Normalize(g:job_opts.stdout)')
|
2021-05-13 17:56:25 -07:00
|
|
|
local actual = eval('g:output')
|
|
|
|
local expected
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2021-05-13 17:56:25 -07:00
|
|
|
-- Toto is normalized to TOTO so we can detect duplicates, and because
|
|
|
|
-- Windows doesn't care about case
|
2024-01-02 18:09:18 -07:00
|
|
|
expected = { 'TOTO=def', '' }
|
2021-05-13 17:56:25 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
expected = { 'TOTO=abc', 'Toto=def', '' }
|
2021-05-13 17:56:25 -07:00
|
|
|
end
|
|
|
|
table.sort(actual)
|
|
|
|
table.sort(expected)
|
|
|
|
eq(expected, actual)
|
|
|
|
end)
|
|
|
|
|
2015-04-15 10:05:30 -07:00
|
|
|
it('uses &shell and &shellcmdflag if passed a string', function()
|
|
|
|
nvim('command', "let $VAR = 'abc'")
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2018-03-08 18:50:32 -07:00
|
|
|
nvim('command', "let j = jobstart('echo %VAR%', g:job_opts)")
|
2017-01-21 09:04:39 -07:00
|
|
|
else
|
|
|
|
nvim('command', "let j = jobstart('echo $VAR', g:job_opts)")
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { 'abc', '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 0 } }, next_msg())
|
2015-04-15 10:05:30 -07:00
|
|
|
end)
|
|
|
|
|
2016-06-10 00:05:28 -07:00
|
|
|
it('changes to given / directory', function()
|
2016-05-25 01:09:33 -07:00
|
|
|
nvim('command', "let g:job_opts.cwd = '/'")
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2018-03-08 18:50:32 -07:00
|
|
|
nvim('command', "let j = jobstart('cd', g:job_opts)")
|
2017-01-14 01:47:33 -07:00
|
|
|
else
|
|
|
|
nvim('command', "let j = jobstart('pwd', g:job_opts)")
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { pathroot(), '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 0 } }, next_msg())
|
2016-05-25 01:09:33 -07:00
|
|
|
end)
|
|
|
|
|
2016-06-10 00:05:28 -07:00
|
|
|
it('changes to given `cwd` directory', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
local dir = eval('resolve(tempname())'):gsub('/', get_pathsep())
|
2016-05-25 01:09:33 -07:00
|
|
|
mkdir(dir)
|
|
|
|
nvim('command', "let g:job_opts.cwd = '" .. dir .. "'")
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2018-03-08 18:50:32 -07:00
|
|
|
nvim('command', "let j = jobstart('cd', g:job_opts)")
|
2017-01-14 01:47:33 -07:00
|
|
|
else
|
|
|
|
nvim('command', "let j = jobstart('pwd', g:job_opts)")
|
|
|
|
end
|
2018-02-16 11:42:05 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 0, { dir, '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
|
|
|
{ 'notification', 'exit', { 0, 0 } },
|
2018-02-16 11:42:05 -07:00
|
|
|
},
|
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 0, { dir } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
|
|
|
{ 'notification', 'exit', { 0, 0 } },
|
2018-02-16 11:42:05 -07:00
|
|
|
}
|
|
|
|
)
|
2016-05-25 01:09:33 -07:00
|
|
|
rmdir(dir)
|
|
|
|
end)
|
|
|
|
|
2016-06-10 00:05:28 -07:00
|
|
|
it('fails to change to invalid `cwd`', function()
|
|
|
|
local dir = eval('resolve(tempname())."-bogus"')
|
2016-05-25 01:09:33 -07:00
|
|
|
local _, err = pcall(function()
|
2016-06-10 00:05:28 -07:00
|
|
|
nvim('command', "let g:job_opts.cwd = '" .. dir .. "'")
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2018-03-08 18:50:32 -07:00
|
|
|
nvim('command', "let j = jobstart('cd', g:job_opts)")
|
2017-01-14 01:47:33 -07:00
|
|
|
else
|
|
|
|
nvim('command', "let j = jobstart('pwd', g:job_opts)")
|
|
|
|
end
|
2016-05-25 01:09:33 -07:00
|
|
|
end)
|
2024-01-02 18:09:18 -07:00
|
|
|
ok(string.find(err, 'E475: Invalid argument: expected valid directory$') ~= nil)
|
2016-05-25 01:09:33 -07:00
|
|
|
end)
|
|
|
|
|
2022-05-03 06:08:35 -07:00
|
|
|
it('error on non-executable `cwd`', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_os('win'), 'Not applicable for Windows')
|
2018-11-07 02:31:25 -07:00
|
|
|
|
|
|
|
local dir = 'Xtest_not_executable_dir'
|
|
|
|
mkdir(dir)
|
|
|
|
funcs.setfperm(dir, 'rw-------')
|
2024-01-02 18:09:18 -07:00
|
|
|
matches(
|
|
|
|
'^Vim%(call%):E903: Process failed to start: permission denied: .*',
|
|
|
|
pcall_err(nvim, 'command', "call jobstart(['pwd'], {'cwd': '" .. dir .. "'})")
|
|
|
|
)
|
2018-11-07 02:31:25 -07:00
|
|
|
rmdir(dir)
|
|
|
|
end)
|
|
|
|
|
2014-10-27 14:58:22 -07:00
|
|
|
it('returns 0 when it fails to start', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('', eval('v:errmsg'))
|
|
|
|
feed_command('let g:test_jobid = jobstart([])')
|
|
|
|
eq(0, eval('g:test_jobid'))
|
|
|
|
eq('E474:', string.match(eval('v:errmsg'), 'E%d*:'))
|
2014-10-27 14:58:22 -07:00
|
|
|
end)
|
|
|
|
|
2016-12-10 07:50:08 -07:00
|
|
|
it('returns -1 when target is not executable #5465', function()
|
2017-01-14 01:47:33 -07:00
|
|
|
local function new_job()
|
|
|
|
return eval([[jobstart('')]])
|
|
|
|
end
|
2016-12-10 07:50:08 -07:00
|
|
|
local executable_jobid = new_job()
|
2019-12-23 23:53:56 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
local exe = is_os('win') and './test/functional/fixtures'
|
|
|
|
or './test/functional/fixtures/non_executable.txt'
|
|
|
|
eq(
|
|
|
|
"Vim:E475: Invalid value for argument cmd: '" .. exe .. "' is not executable",
|
|
|
|
pcall_err(eval, "jobstart(['" .. exe .. "'])")
|
|
|
|
)
|
|
|
|
eq('', eval('v:errmsg'))
|
2016-12-10 07:50:08 -07:00
|
|
|
-- Non-executable job should not increment the job ids. #5465
|
|
|
|
eq(executable_jobid + 1, new_job())
|
2016-11-24 17:23:54 -07:00
|
|
|
end)
|
|
|
|
|
2015-03-25 19:11:05 -07:00
|
|
|
it('invokes callbacks when the job writes and exits', function()
|
2017-06-10 06:25:23 -07:00
|
|
|
nvim('command', "let g:job_opts.on_stderr = function('OnEvent')")
|
2018-03-11 08:05:45 -07:00
|
|
|
nvim('command', [[call jobstart(has('win32') ? 'echo:' : 'echo', g:job_opts)]])
|
2024-01-02 18:09:18 -07:00
|
|
|
expect_twostreams({
|
|
|
|
{ 'notification', 'stdout', { 0, { '', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
|
|
|
}, { { 'notification', 'stderr', { 0, { '' } } } })
|
|
|
|
eq({ 'notification', 'exit', { 0, 0 } }, next_msg())
|
2014-10-27 14:58:22 -07:00
|
|
|
end)
|
|
|
|
|
2022-05-03 06:08:35 -07:00
|
|
|
it('interactive commands', function()
|
2019-07-22 10:14:10 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2014-10-27 14:58:22 -07:00
|
|
|
neq(0, eval('j'))
|
2014-10-25 18:45:13 -07:00
|
|
|
nvim('command', 'call jobsend(j, "abc\\n")')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { 'abc', '' } } }, next_msg())
|
2014-10-25 18:45:13 -07:00
|
|
|
nvim('command', 'call jobsend(j, "123\\nxyz\\n")')
|
2018-03-09 01:23:39 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{ { 'notification', 'stdout', { 0, { '123', 'xyz', '' } } } },
|
2018-03-09 01:23:39 -07:00
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 0, { '123', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { 'xyz', '' } } },
|
2018-03-09 01:23:39 -07:00
|
|
|
}
|
|
|
|
)
|
2015-02-28 05:39:26 -07:00
|
|
|
nvim('command', 'call jobsend(j, [123, "xyz", ""])')
|
2018-03-09 01:23:39 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{ { 'notification', 'stdout', { 0, { '123', 'xyz', '' } } } },
|
2018-03-09 01:23:39 -07:00
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 0, { '123', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { 'xyz', '' } } },
|
2018-03-09 01:23:39 -07:00
|
|
|
}
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim('command', 'call jobstop(j)')
|
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 143 } }, next_msg())
|
2014-10-27 14:58:22 -07:00
|
|
|
end)
|
|
|
|
|
2014-10-28 10:27:47 -07:00
|
|
|
it('preserves NULs', function()
|
|
|
|
-- Make a file with NULs in it.
|
2015-12-29 12:18:16 -07:00
|
|
|
local filename = helpers.tmpname()
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(filename, 'abc\0def\n')
|
2014-10-28 10:27:47 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '" .. filename .. "'], g:job_opts)")
|
|
|
|
eq({ 'notification', 'stdout', { 0, { 'abc\ndef', '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 0 } }, next_msg())
|
2014-10-28 10:27:47 -07:00
|
|
|
os.remove(filename)
|
|
|
|
|
|
|
|
-- jobsend() preserves NULs.
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2015-02-28 05:39:26 -07:00
|
|
|
nvim('command', [[call jobsend(j, ["123\n456",""])]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { '123\n456', '' } } }, next_msg())
|
|
|
|
nvim('command', 'call jobstop(j)')
|
2014-10-28 10:27:47 -07:00
|
|
|
end)
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('emits partial lines (does NOT buffer data lacking newlines)', function()
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2014-10-25 18:45:13 -07:00
|
|
|
nvim('command', 'call jobsend(j, "abc\\nxyz")')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { 'abc', 'xyz' } } }, next_msg())
|
|
|
|
nvim('command', 'call jobstop(j)')
|
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 143 } }, next_msg())
|
2015-02-22 10:49:54 -07:00
|
|
|
end)
|
|
|
|
|
2016-10-20 13:51:46 -07:00
|
|
|
it('preserves newlines', function()
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2015-02-22 10:49:54 -07:00
|
|
|
nvim('command', 'call jobsend(j, "a\\n\\nc\\n\\n\\n\\nb\\n\\n")')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { 'a', '', 'c', '', '', '', 'b', '', '' } } }, next_msg())
|
2015-02-22 10:49:54 -07:00
|
|
|
end)
|
|
|
|
|
2016-10-20 13:51:46 -07:00
|
|
|
it('preserves NULs', function()
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2015-02-28 05:39:26 -07:00
|
|
|
nvim('command', 'call jobsend(j, ["\n123\n", "abc\\nxyz\n", ""])')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { '\n123\n', 'abc\nxyz\n', '' } } }, next_msg())
|
|
|
|
nvim('command', 'call jobstop(j)')
|
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 143 } }, next_msg())
|
2014-10-25 18:45:13 -07:00
|
|
|
end)
|
|
|
|
|
2016-10-20 13:51:46 -07:00
|
|
|
it('avoids sending final newline', function()
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2015-02-28 05:39:26 -07:00
|
|
|
nvim('command', 'call jobsend(j, ["some data", "without\nfinal nl"])')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { 'some data', 'without\nfinal nl' } } }, next_msg())
|
|
|
|
nvim('command', 'call jobstop(j)')
|
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 143 } }, next_msg())
|
2015-02-28 05:39:26 -07:00
|
|
|
end)
|
|
|
|
|
2016-10-20 13:51:46 -07:00
|
|
|
it('closes the job streams with jobclose', function()
|
2019-07-22 10:14:10 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2015-03-28 07:40:25 -07:00
|
|
|
nvim('command', 'call jobclose(j, "stdin")')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 0 } }, next_msg())
|
2015-03-28 07:40:25 -07:00
|
|
|
end)
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('disallows jobsend on a job that closed stdin', function()
|
2019-07-22 10:14:10 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2015-03-28 07:40:25 -07:00
|
|
|
nvim('command', 'call jobclose(j, "stdin")')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
false,
|
|
|
|
pcall(function()
|
|
|
|
nvim('command', 'call jobsend(j, ["some data"])')
|
|
|
|
end)
|
|
|
|
)
|
2021-07-12 12:22:56 -07:00
|
|
|
|
|
|
|
command("let g:job_opts.stdin = 'null'")
|
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
false,
|
|
|
|
pcall(function()
|
|
|
|
nvim('command', 'call jobsend(j, ["some data"])')
|
|
|
|
end)
|
|
|
|
)
|
2015-03-28 07:40:25 -07:00
|
|
|
end)
|
|
|
|
|
2020-01-20 05:06:38 -07:00
|
|
|
it('disallows jobsend on a non-existent job', function()
|
2014-10-27 14:58:22 -07:00
|
|
|
eq(false, pcall(eval, "jobsend(-1, 'lol')"))
|
2020-01-20 05:06:38 -07:00
|
|
|
eq(0, eval('jobstop(-1)'))
|
2014-10-27 14:58:22 -07:00
|
|
|
end)
|
|
|
|
|
2020-01-20 05:06:38 -07:00
|
|
|
it('jobstop twice on the stopped or exited job return 0', function()
|
2019-07-22 10:14:10 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2014-10-27 14:58:22 -07:00
|
|
|
neq(0, eval('j'))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(1, eval('jobstop(j)'))
|
|
|
|
eq(0, eval('jobstop(j)'))
|
2014-10-27 14:58:22 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-14 01:47:33 -07:00
|
|
|
it('will not leak memory if we leave a job running', function()
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', "call jobstart(['cat', '-'], g:job_opts)")
|
|
|
|
end)
|
2016-01-08 09:25:13 -07:00
|
|
|
|
|
|
|
it('can get the pid value using getpid', function()
|
2019-07-22 10:14:10 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2016-01-08 09:25:13 -07:00
|
|
|
local pid = eval('jobpid(j)')
|
2018-03-12 09:26:43 -07:00
|
|
|
neq(NIL, meths.get_proc(pid))
|
2016-01-08 09:25:13 -07:00
|
|
|
nvim('command', 'call jobstop(j)')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 0, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 0, 143 } }, next_msg())
|
2018-03-12 09:26:43 -07:00
|
|
|
eq(NIL, meths.get_proc(pid))
|
2016-01-08 09:25:13 -07:00
|
|
|
end)
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('disposed on Nvim exit', function()
|
2016-01-08 09:25:13 -07:00
|
|
|
-- use sleep, which doesn't die on stdin close
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim(
|
|
|
|
'command',
|
|
|
|
"let g:j = jobstart(has('win32') ? ['ping', '-n', '1001', '127.0.0.1'] : ['sleep', '1000'], g:job_opts)"
|
|
|
|
)
|
2017-01-14 01:47:33 -07:00
|
|
|
local pid = eval('jobpid(g:j)')
|
2018-03-12 09:26:43 -07:00
|
|
|
neq(NIL, meths.get_proc(pid))
|
2016-01-08 09:25:13 -07:00
|
|
|
clear()
|
2018-03-12 09:26:43 -07:00
|
|
|
eq(NIL, meths.get_proc(pid))
|
2016-01-08 09:25:13 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can survive the exit of nvim with "detach"', function()
|
|
|
|
nvim('command', 'let g:job_opts.detach = 1')
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim(
|
|
|
|
'command',
|
|
|
|
"let g:j = jobstart(has('win32') ? ['ping', '-n', '1001', '127.0.0.1'] : ['sleep', '1000'], g:job_opts)"
|
|
|
|
)
|
2017-01-14 01:47:33 -07:00
|
|
|
local pid = eval('jobpid(g:j)')
|
2018-03-12 09:26:43 -07:00
|
|
|
neq(NIL, meths.get_proc(pid))
|
2016-01-08 09:25:13 -07:00
|
|
|
clear()
|
2018-03-12 09:26:43 -07:00
|
|
|
neq(NIL, meths.get_proc(pid))
|
2016-01-08 09:25:13 -07:00
|
|
|
-- clean up after ourselves
|
2018-03-12 09:26:43 -07:00
|
|
|
eq(0, os_kill(pid))
|
2016-01-08 09:25:13 -07:00
|
|
|
end)
|
2015-03-25 19:11:05 -07:00
|
|
|
|
|
|
|
it('can pass user data to the callback', function()
|
|
|
|
nvim('command', 'let g:job_opts.user = {"n": 5, "s": "str", "l": [1]}')
|
2018-03-08 18:50:32 -07:00
|
|
|
nvim('command', [[call jobstart('echo foo', g:job_opts)]])
|
2024-01-02 18:09:18 -07:00
|
|
|
local data = { n = 5, s = 'str', l = { 1 } }
|
2018-03-08 12:28:51 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { data, { 'foo', '' } } },
|
|
|
|
{ 'notification', 'stdout', { data, { '' } } },
|
2018-03-08 12:28:51 -07:00
|
|
|
},
|
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { data, { 'foo' } } },
|
|
|
|
{ 'notification', 'stdout', { data, { '', '' } } },
|
|
|
|
{ 'notification', 'stdout', { data, { '' } } },
|
2018-03-08 12:28:51 -07:00
|
|
|
}
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'exit', { data, 0 } }, next_msg())
|
2015-03-25 19:11:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can omit data callbacks', function()
|
|
|
|
nvim('command', 'unlet g:job_opts.on_stdout')
|
|
|
|
nvim('command', 'let g:job_opts.user = 5')
|
2018-03-08 18:50:32 -07:00
|
|
|
nvim('command', [[call jobstart('echo foo', g:job_opts)]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'exit', { 5, 0 } }, next_msg())
|
2015-03-25 19:11:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can omit exit callback', function()
|
|
|
|
nvim('command', 'unlet g:job_opts.on_exit')
|
|
|
|
nvim('command', 'let g:job_opts.user = 5')
|
2018-03-08 18:50:32 -07:00
|
|
|
nvim('command', [[call jobstart('echo foo', g:job_opts)]])
|
2018-02-16 11:42:05 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 5, { 'foo', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 5, { '' } } },
|
2018-02-16 11:42:05 -07:00
|
|
|
},
|
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 5, { 'foo' } } },
|
|
|
|
{ 'notification', 'stdout', { 5, { '', '' } } },
|
|
|
|
{ 'notification', 'stdout', { 5, { '' } } },
|
2018-02-16 11:42:05 -07:00
|
|
|
}
|
|
|
|
)
|
2015-03-25 19:11:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('will pass return code with the exit event', function()
|
|
|
|
nvim('command', 'let g:job_opts.user = 5')
|
2017-01-14 01:47:33 -07:00
|
|
|
nvim('command', "call jobstart('exit 55', g:job_opts)")
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'stdout', { 5, { '' } } }, next_msg())
|
|
|
|
eq({ 'notification', 'exit', { 5, 55 } }, next_msg())
|
2015-03-25 19:11:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can receive dictionary functions', function()
|
|
|
|
source([[
|
|
|
|
let g:dict = {'id': 10}
|
|
|
|
function g:dict.on_exit(id, code, event)
|
|
|
|
call rpcnotify(g:channel, a:event, a:code, self.id)
|
|
|
|
endfunction
|
2017-01-14 01:47:33 -07:00
|
|
|
call jobstart('exit 45', g:dict)
|
2015-03-25 19:11:05 -07:00
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'exit', { 45, 10 } }, next_msg())
|
2014-10-27 14:58:22 -07:00
|
|
|
end)
|
2014-11-06 22:38:12 -07:00
|
|
|
|
2016-11-01 02:54:32 -07:00
|
|
|
it('can redefine callbacks being used by a job', function()
|
2015-08-21 05:07:06 -07:00
|
|
|
local screen = Screen.new()
|
|
|
|
screen:attach()
|
2016-11-01 02:54:32 -07:00
|
|
|
screen:set_default_attr_ids({
|
2024-01-02 18:09:18 -07:00
|
|
|
[1] = { bold = true, foreground = Screen.colors.Blue },
|
2016-11-01 02:54:32 -07:00
|
|
|
})
|
2016-12-13 11:36:29 -07:00
|
|
|
source([[
|
2015-08-21 05:07:06 -07:00
|
|
|
function! g:JobHandler(job_id, data, event)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let g:callbacks = {
|
|
|
|
\ 'on_stdout': function('g:JobHandler'),
|
|
|
|
\ 'on_stderr': function('g:JobHandler'),
|
|
|
|
\ 'on_exit': function('g:JobHandler')
|
|
|
|
\ }
|
2017-10-06 19:37:48 -07:00
|
|
|
let job = jobstart(['cat', '-'], g:callbacks)
|
2015-08-21 05:07:06 -07:00
|
|
|
]])
|
2020-10-19 11:17:51 -07:00
|
|
|
poke_eventloop()
|
2016-12-13 11:36:29 -07:00
|
|
|
source([[
|
|
|
|
function! g:JobHandler(job_id, data, event)
|
|
|
|
endfunction
|
|
|
|
]])
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('', eval('v:errmsg'))
|
2015-08-21 05:07:06 -07:00
|
|
|
end)
|
|
|
|
|
2016-12-14 08:56:00 -07:00
|
|
|
it('requires funcrefs for script-local (s:) functions', function()
|
2017-06-10 06:25:23 -07:00
|
|
|
local screen = Screen.new(60, 5)
|
|
|
|
screen:attach()
|
|
|
|
screen:set_default_attr_ids({
|
2024-01-02 18:09:18 -07:00
|
|
|
[1] = { bold = true, foreground = Screen.colors.Blue1 },
|
|
|
|
[2] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
|
|
|
|
[3] = { bold = true, foreground = Screen.colors.SeaGreen4 },
|
2017-06-10 06:25:23 -07:00
|
|
|
})
|
|
|
|
|
2016-12-14 08:56:00 -07:00
|
|
|
-- Pass job callback names _without_ `function(...)`.
|
|
|
|
source([[
|
|
|
|
function! s:OnEvent(id, data, event) dict
|
|
|
|
let g:job_result = get(self, 'user')
|
|
|
|
endfunction
|
2017-01-12 23:40:49 -07:00
|
|
|
let s:job = jobstart('echo "foo"', {
|
2016-12-14 08:56:00 -07:00
|
|
|
\ 'on_stdout': 's:OnEvent',
|
|
|
|
\ 'on_stderr': 's:OnEvent',
|
|
|
|
\ 'on_exit': 's:OnEvent',
|
|
|
|
\ })
|
|
|
|
]])
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect { any = '{2:E120: Using <SID> not in a script context: s:OnEvent}' }
|
2016-12-14 08:56:00 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-13 09:12:35 -07:00
|
|
|
it('does not repeat output with slow output handlers', function()
|
|
|
|
source([[
|
|
|
|
let d = {'data': []}
|
|
|
|
function! d.on_stdout(job, data, event) dict
|
2018-03-11 08:05:45 -07:00
|
|
|
call add(self.data, Normalize(a:data))
|
2017-01-13 09:12:35 -07:00
|
|
|
sleep 200m
|
|
|
|
endfunction
|
2019-05-17 09:16:45 -07:00
|
|
|
function! d.on_exit(job, data, event) dict
|
|
|
|
let g:exit_data = copy(self.data)
|
|
|
|
endfunction
|
|
|
|
if has('win32')
|
|
|
|
let cmd = 'for /L %I in (1,1,5) do @(echo %I& ping -n 2 127.0.0.1 > nul)'
|
|
|
|
else
|
2019-08-18 05:39:31 -07:00
|
|
|
let cmd = ['sh', '-c', 'for i in 1 2 3 4 5; do echo $i; sleep 0.1; done']
|
2019-05-17 09:16:45 -07:00
|
|
|
endif
|
|
|
|
let g:id = jobstart(cmd, d)
|
|
|
|
sleep 1500m
|
|
|
|
call jobwait([g:id])
|
|
|
|
]])
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
local expected = { '1', '2', '3', '4', '5', '' }
|
2019-05-17 09:16:45 -07:00
|
|
|
local chunks = eval('d.data')
|
|
|
|
-- check nothing was received after exit, including EOF
|
|
|
|
eq(eval('g:exit_data'), chunks)
|
2024-01-02 18:09:18 -07:00
|
|
|
local received = { '' }
|
2019-05-17 09:16:45 -07:00
|
|
|
for i, chunk in ipairs(chunks) do
|
|
|
|
if i < #chunks then
|
|
|
|
-- if chunks got joined, a spurious [''] callback was not sent
|
2024-01-02 18:09:18 -07:00
|
|
|
neq({ '' }, chunk)
|
2019-05-17 09:16:45 -07:00
|
|
|
else
|
|
|
|
-- but EOF callback is still sent
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ '' }, chunk)
|
2019-05-17 09:16:45 -07:00
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
received[#received] = received[#received] .. chunk[1]
|
2019-05-17 09:16:45 -07:00
|
|
|
for j = 2, #chunk do
|
2024-01-02 18:09:18 -07:00
|
|
|
received[#received + 1] = chunk[j]
|
2019-05-17 09:16:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
eq(expected, received)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not invoke callbacks recursively', function()
|
|
|
|
source([[
|
|
|
|
let d = {'data': []}
|
|
|
|
function! d.on_stdout(job, data, event) dict
|
|
|
|
" if callbacks were invoked recursively, this would cause on_stdout
|
|
|
|
" to be invoked recursively and the data reversed on the call stack
|
|
|
|
sleep 200m
|
|
|
|
call add(self.data, Normalize(a:data))
|
|
|
|
endfunction
|
|
|
|
function! d.on_exit(job, data, event) dict
|
|
|
|
let g:exit_data = copy(self.data)
|
|
|
|
endfunction
|
2017-01-13 09:12:35 -07:00
|
|
|
if has('win32')
|
2018-03-11 08:05:45 -07:00
|
|
|
let cmd = 'for /L %I in (1,1,5) do @(echo %I& ping -n 2 127.0.0.1 > nul)'
|
2017-01-13 09:12:35 -07:00
|
|
|
else
|
2019-08-18 05:39:31 -07:00
|
|
|
let cmd = ['sh', '-c', 'for i in 1 2 3 4 5; do echo $i; sleep 0.1; done']
|
2017-01-13 09:12:35 -07:00
|
|
|
endif
|
2019-05-17 09:16:45 -07:00
|
|
|
let g:id = jobstart(cmd, d)
|
|
|
|
sleep 1500m
|
|
|
|
call jobwait([g:id])
|
2017-01-13 09:12:35 -07:00
|
|
|
]])
|
2018-05-13 06:07:10 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
local expected = { '1', '2', '3', '4', '5', '' }
|
2018-05-13 06:07:10 -07:00
|
|
|
local chunks = eval('d.data')
|
2019-05-17 09:16:45 -07:00
|
|
|
-- check nothing was received after exit, including EOF
|
|
|
|
eq(eval('g:exit_data'), chunks)
|
2024-01-02 18:09:18 -07:00
|
|
|
local received = { '' }
|
2018-05-13 06:07:10 -07:00
|
|
|
for i, chunk in ipairs(chunks) do
|
|
|
|
if i < #chunks then
|
|
|
|
-- if chunks got joined, a spurious [''] callback was not sent
|
2024-01-02 18:09:18 -07:00
|
|
|
neq({ '' }, chunk)
|
2018-05-13 06:07:10 -07:00
|
|
|
else
|
|
|
|
-- but EOF callback is still sent
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ '' }, chunk)
|
2018-05-13 06:07:10 -07:00
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
received[#received] = received[#received] .. chunk[1]
|
2018-05-13 06:07:10 -07:00
|
|
|
for j = 2, #chunk do
|
2024-01-02 18:09:18 -07:00
|
|
|
received[#received + 1] = chunk[j]
|
2018-05-13 06:07:10 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
eq(expected, received)
|
2017-01-13 09:12:35 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-14 01:47:33 -07:00
|
|
|
it('jobstart() works with partial functions', function()
|
|
|
|
source([[
|
|
|
|
function PrintArgs(a1, a2, id, data, event)
|
|
|
|
" Windows: remove ^M
|
|
|
|
let normalized = map(a:data, 'substitute(v:val, "\r", "", "g")')
|
|
|
|
call rpcnotify(g:channel, '1', a:a1, a:a2, normalized, a:event)
|
|
|
|
endfunction
|
|
|
|
let Callback = function('PrintArgs', ["foo", "bar"])
|
|
|
|
let g:job_opts = {'on_stdout': Callback}
|
2018-03-08 18:50:32 -07:00
|
|
|
call jobstart('echo some text', g:job_opts)
|
2017-01-14 01:47:33 -07:00
|
|
|
]])
|
2018-03-08 12:28:51 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{ { 'notification', '1', { 'foo', 'bar', { 'some text', '' }, 'stdout' } } },
|
2018-03-08 12:28:51 -07:00
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', '1', { 'foo', 'bar', { 'some text' }, 'stdout' } },
|
|
|
|
{ 'notification', '1', { 'foo', 'bar', { '', '' }, 'stdout' } },
|
2018-03-08 12:28:51 -07:00
|
|
|
}
|
|
|
|
)
|
2017-01-14 01:47:33 -07:00
|
|
|
end)
|
|
|
|
|
2016-12-19 20:09:07 -07:00
|
|
|
it('jobstart() works with closures', function()
|
|
|
|
source([[
|
2017-02-22 23:24:55 -07:00
|
|
|
fun! MkFun()
|
|
|
|
let a1 = 'foo'
|
|
|
|
let a2 = 'bar'
|
|
|
|
return {id, data, event -> rpcnotify(g:channel, '1', a1, a2, Normalize(data), event)}
|
|
|
|
endfun
|
|
|
|
let g:job_opts = {'on_stdout': MkFun()}
|
2018-03-08 18:50:32 -07:00
|
|
|
call jobstart('echo some text', g:job_opts)
|
2016-12-19 20:09:07 -07:00
|
|
|
]])
|
2018-03-08 12:28:51 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{ { 'notification', '1', { 'foo', 'bar', { 'some text', '' }, 'stdout' } } },
|
2018-03-08 12:28:51 -07:00
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', '1', { 'foo', 'bar', { 'some text' }, 'stdout' } },
|
|
|
|
{ 'notification', '1', { 'foo', 'bar', { '', '' }, 'stdout' } },
|
2018-03-08 12:28:51 -07:00
|
|
|
}
|
|
|
|
)
|
2016-12-19 20:09:07 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('jobstart() works when closure passed directly to `jobstart`', function()
|
|
|
|
source([[
|
2017-02-22 23:24:55 -07:00
|
|
|
let g:job_opts = {'on_stdout': {id, data, event -> rpcnotify(g:channel, '1', 'foo', 'bar', Normalize(data), event)}}
|
2018-03-08 18:50:32 -07:00
|
|
|
call jobstart('echo some text', g:job_opts)
|
2016-12-19 20:09:07 -07:00
|
|
|
]])
|
2018-03-08 12:28:51 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{ { 'notification', '1', { 'foo', 'bar', { 'some text', '' }, 'stdout' } } },
|
2018-03-08 12:28:51 -07:00
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', '1', { 'foo', 'bar', { 'some text' }, 'stdout' } },
|
|
|
|
{ 'notification', '1', { 'foo', 'bar', { '', '' }, 'stdout' } },
|
2018-03-08 12:28:51 -07:00
|
|
|
}
|
|
|
|
)
|
2016-12-19 20:09:07 -07:00
|
|
|
end)
|
|
|
|
|
2022-05-03 06:08:35 -07:00
|
|
|
it('jobstart() environment: $NVIM, $NVIM_LISTEN_ADDRESS #11009', function()
|
|
|
|
local function get_env_in_child_job(envname, env)
|
2024-01-02 18:09:18 -07:00
|
|
|
return exec_lua(
|
|
|
|
[[
|
2022-05-03 06:08:35 -07:00
|
|
|
local envname, env = ...
|
|
|
|
local join = function(s) return vim.fn.join(s, '') end
|
|
|
|
local stdout = {}
|
|
|
|
local stderr = {}
|
|
|
|
local opt = {
|
|
|
|
env = env,
|
|
|
|
stdout_buffered = true,
|
|
|
|
stderr_buffered = true,
|
|
|
|
on_stderr = function(chan, data, name) stderr = data end,
|
|
|
|
on_stdout = function(chan, data, name) stdout = data end,
|
|
|
|
}
|
2023-12-07 17:00:27 -07:00
|
|
|
local j1 = vim.fn.jobstart({ vim.v.progpath, '-es', '-V1',('+echo "%s="..getenv("%s")'):format(envname, envname), '+qa!' }, opt)
|
2022-05-03 06:08:35 -07:00
|
|
|
vim.fn.jobwait({ j1 }, 10000)
|
|
|
|
return join({ join(stdout), join(stderr) })
|
|
|
|
]],
|
2024-01-02 18:09:18 -07:00
|
|
|
envname,
|
|
|
|
env
|
|
|
|
)
|
2022-05-03 06:08:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local addr = eval('v:servername')
|
|
|
|
ok((addr):len() > 0)
|
|
|
|
-- $NVIM is _not_ defined in the top-level Nvim process.
|
|
|
|
eq('', eval('$NVIM'))
|
|
|
|
-- jobstart() shares its v:servername with the child via $NVIM.
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('NVIM=' .. addr, get_env_in_child_job('NVIM'))
|
2022-05-03 06:08:35 -07:00
|
|
|
-- $NVIM_LISTEN_ADDRESS is unset by server_init in the child.
|
2022-08-13 06:31:00 -07:00
|
|
|
eq('NVIM_LISTEN_ADDRESS=v:null', get_env_in_child_job('NVIM_LISTEN_ADDRESS'))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'NVIM_LISTEN_ADDRESS=v:null',
|
|
|
|
get_env_in_child_job('NVIM_LISTEN_ADDRESS', { NVIM_LISTEN_ADDRESS = 'Xtest_jobstart_env' })
|
|
|
|
)
|
2022-05-03 06:08:35 -07:00
|
|
|
-- User can explicitly set $NVIM_LOG_FILE, $VIM, $VIMRUNTIME.
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'NVIM_LOG_FILE=Xtest_jobstart_env',
|
|
|
|
get_env_in_child_job('NVIM_LOG_FILE', { NVIM_LOG_FILE = 'Xtest_jobstart_env' })
|
|
|
|
)
|
2022-05-29 00:40:59 -07:00
|
|
|
os.remove('Xtest_jobstart_env')
|
2022-05-03 06:08:35 -07:00
|
|
|
end)
|
|
|
|
|
2023-11-07 15:29:58 -07:00
|
|
|
describe('jobwait()', function()
|
2018-03-08 18:50:32 -07:00
|
|
|
before_each(function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2018-03-08 18:50:32 -07:00
|
|
|
helpers.set_shell_powershell()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2015-03-28 07:30:23 -07:00
|
|
|
it('returns a list of status codes', function()
|
|
|
|
source([[
|
2018-03-08 18:50:32 -07:00
|
|
|
call rpcnotify(g:channel, 'wait', jobwait(has('win32') ? [
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 100; exit 4'),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 300; exit 5'),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 500; exit 6'),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 700; exit 7')
|
|
|
|
\ ] : [
|
2017-01-14 01:47:33 -07:00
|
|
|
\ jobstart('sleep 0.10; exit 4'),
|
|
|
|
\ jobstart('sleep 0.110; exit 5'),
|
|
|
|
\ jobstart('sleep 0.210; exit 6'),
|
|
|
|
\ jobstart('sleep 0.310; exit 7')
|
2015-03-28 07:30:23 -07:00
|
|
|
\ ]))
|
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { 4, 5, 6, 7 } } }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('will run callbacks while waiting', function()
|
|
|
|
source([[
|
2019-09-02 05:37:44 -07:00
|
|
|
let g:dict = {}
|
|
|
|
let g:jobs = []
|
|
|
|
let g:exits = []
|
|
|
|
function g:dict.on_stdout(id, code, event) abort
|
|
|
|
call add(g:jobs, a:id)
|
|
|
|
endfunction
|
|
|
|
function g:dict.on_exit(id, code, event) abort
|
2015-04-02 07:26:44 -07:00
|
|
|
if a:code != 5
|
|
|
|
throw 'Error!'
|
|
|
|
endif
|
2019-09-02 05:37:44 -07:00
|
|
|
call add(g:exits, a:id)
|
2015-03-28 07:30:23 -07:00
|
|
|
endfunction
|
2018-03-08 18:50:32 -07:00
|
|
|
call jobwait(has('win32') ? [
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 100; exit 5', g:dict),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 300; exit 5', g:dict),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 500; exit 5', g:dict),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 700; exit 5', g:dict)
|
|
|
|
\ ] : [
|
2017-01-14 01:47:33 -07:00
|
|
|
\ jobstart('sleep 0.010; exit 5', g:dict),
|
|
|
|
\ jobstart('sleep 0.030; exit 5', g:dict),
|
|
|
|
\ jobstart('sleep 0.050; exit 5', g:dict),
|
|
|
|
\ jobstart('sleep 0.070; exit 5', g:dict)
|
2015-03-28 07:30:23 -07:00
|
|
|
\ ])
|
2019-09-02 05:37:44 -07:00
|
|
|
call rpcnotify(g:channel, 'wait', sort(g:jobs), sort(g:exits))
|
2015-03-28 07:30:23 -07:00
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { 3, 4, 5, 6 }, { 3, 4, 5, 6 } } }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('will return status codes in the order of passed ids', function()
|
|
|
|
source([[
|
2018-03-08 18:50:32 -07:00
|
|
|
call rpcnotify(g:channel, 'wait', jobwait(has('win32') ? [
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 700; exit 4'),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 500; exit 5'),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 300; exit 6'),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 100; exit 7')
|
|
|
|
\ ] : [
|
2017-01-14 01:47:33 -07:00
|
|
|
\ jobstart('sleep 0.070; exit 4'),
|
|
|
|
\ jobstart('sleep 0.050; exit 5'),
|
|
|
|
\ jobstart('sleep 0.030; exit 6'),
|
|
|
|
\ jobstart('sleep 0.010; exit 7')
|
2015-03-28 07:30:23 -07:00
|
|
|
\ ]))
|
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { 4, 5, 6, 7 } } }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('will return -3 for invalid job ids', function()
|
|
|
|
source([[
|
|
|
|
call rpcnotify(g:channel, 'wait', jobwait([
|
|
|
|
\ -10,
|
2018-03-08 18:50:32 -07:00
|
|
|
\ jobstart((has('win32') ? 'Start-Sleep -Milliseconds 100' : 'sleep 0.01').'; exit 5'),
|
2015-03-28 07:30:23 -07:00
|
|
|
\ ]))
|
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { -3, 5 } } }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
|
2018-02-20 03:37:55 -07:00
|
|
|
it('will return -2 when interrupted without timeout', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
feed_command(
|
|
|
|
'call rpcnotify(g:channel, "ready") | '
|
|
|
|
.. 'call rpcnotify(g:channel, "wait", '
|
|
|
|
.. 'jobwait([jobstart("'
|
|
|
|
.. (is_os('win') and 'Start-Sleep 10' or 'sleep 10')
|
|
|
|
.. '; exit 55")]))'
|
|
|
|
)
|
|
|
|
eq({ 'notification', 'ready', {} }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
feed('<c-c>')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { -2 } } }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
|
2018-02-20 03:37:55 -07:00
|
|
|
it('will return -2 when interrupted with timeout', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
feed_command(
|
|
|
|
'call rpcnotify(g:channel, "ready") | '
|
|
|
|
.. 'call rpcnotify(g:channel, "wait", '
|
|
|
|
.. 'jobwait([jobstart("'
|
|
|
|
.. (is_os('win') and 'Start-Sleep 10' or 'sleep 10')
|
|
|
|
.. '; exit 55")], 10000))'
|
|
|
|
)
|
|
|
|
eq({ 'notification', 'ready', {} }, next_msg())
|
2018-02-20 03:37:55 -07:00
|
|
|
feed('<c-c>')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { -2 } } }, next_msg())
|
2018-02-20 03:37:55 -07:00
|
|
|
end)
|
|
|
|
|
2016-12-13 06:48:42 -07:00
|
|
|
it('can be called recursively', function()
|
2015-04-10 19:19:04 -07:00
|
|
|
source([[
|
|
|
|
let g:opts = {}
|
|
|
|
let g:counter = 0
|
2016-12-13 06:48:42 -07:00
|
|
|
function g:opts.on_stdout(id, msg, _event)
|
2015-04-10 19:19:04 -07:00
|
|
|
if self.state == 0
|
|
|
|
if self.counter < 10
|
|
|
|
call Run()
|
|
|
|
endif
|
|
|
|
let self.state = 1
|
|
|
|
call jobsend(a:id, "line1\n")
|
|
|
|
elseif self.state == 1
|
|
|
|
let self.state = 2
|
|
|
|
call jobsend(a:id, "line2\n")
|
|
|
|
elseif self.state == 2
|
|
|
|
let self.state = 3
|
|
|
|
call jobsend(a:id, "line3\n")
|
2017-06-10 06:25:23 -07:00
|
|
|
elseif self.state == 3
|
|
|
|
let self.state = 4
|
2015-04-10 19:19:04 -07:00
|
|
|
call rpcnotify(g:channel, 'w', printf('job %d closed', self.counter))
|
|
|
|
call jobclose(a:id, 'stdin')
|
|
|
|
endif
|
|
|
|
endfunction
|
2016-12-13 06:48:42 -07:00
|
|
|
function g:opts.on_exit(...)
|
2015-04-10 19:19:04 -07:00
|
|
|
call rpcnotify(g:channel, 'w', printf('job %d exited', self.counter))
|
|
|
|
endfunction
|
|
|
|
function Run()
|
|
|
|
let g:counter += 1
|
|
|
|
let j = copy(g:opts)
|
|
|
|
let j.state = 0
|
|
|
|
let j.counter = g:counter
|
|
|
|
call jobwait([
|
2017-01-14 01:47:33 -07:00
|
|
|
\ jobstart('echo ready; cat -', j),
|
2015-04-10 19:19:04 -07:00
|
|
|
\ ])
|
|
|
|
endfunction
|
|
|
|
]])
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('call Run()')
|
2015-04-10 19:19:04 -07:00
|
|
|
local r
|
|
|
|
for i = 10, 1, -1 do
|
|
|
|
r = next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('job ' .. i .. ' closed', r[3][1])
|
2015-04-10 19:19:04 -07:00
|
|
|
r = next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('job ' .. i .. ' exited', r[3][1])
|
2015-04-10 19:19:04 -07:00
|
|
|
end
|
|
|
|
eq(10, nvim('eval', 'g:counter'))
|
|
|
|
end)
|
|
|
|
|
2015-03-28 07:30:23 -07:00
|
|
|
describe('with timeout argument', function()
|
|
|
|
it('will return -1 if the wait timed out', function()
|
|
|
|
source([[
|
|
|
|
call rpcnotify(g:channel, 'wait', jobwait([
|
2018-03-08 18:50:32 -07:00
|
|
|
\ jobstart((has('win32') ? 'Start-Sleep 10' : 'sleep 10').'; exit 5'),
|
tests: fix/improve "jobwait returns -1 when timed out" #10767
There was a longer timeout for Windows already, but unlike stated in
51d42917f it is not a worst-case, but gets waited for always.
The test is only about "-1" on timeout, so reduce it to this.
Fixes:
16:33:19,309 INFO - not ok 627 - jobs jobwait with timeout argument will return -1 if the wait timed out
16:33:19,309 INFO - # test/functional/core/job_spec.lua @ 707
16:33:19,309 INFO - # Failure message: test/functional/core/job_spec.lua:714: Expected objects to be the same.
16:33:19,309 INFO - # Passed in:
16:33:19,309 INFO - # (table: 0x0db1a3f0) {
16:33:19,309 INFO - # [1] = 'notification'
16:33:19,309 INFO - # [2] = 'wait'
16:33:19,309 INFO - # *[3] = {
16:33:19,309 INFO - # *[1] = {
16:33:19,309 INFO - # *[1] = -1
16:33:19,309 INFO - # [2] = -1 } } }
16:33:19,309 INFO - # Expected:
16:33:19,309 INFO - # (table: 0x0db1a480) {
16:33:19,309 INFO - # [1] = 'notification'
16:33:19,309 INFO - # [2] = 'wait'
16:33:19,309 INFO - # *[3] = {
16:33:19,309 INFO - # *[1] = {
16:33:19,309 INFO - # *[1] = 4
16:33:19,309 INFO - # [2] = -1 } } }
16:33:19,309 INFO - # stack traceback:
16:33:19,309 INFO - # test/functional/core/job_spec.lua:714: in function <test/functional/core/job_spec.lua:707>
2019-08-14 00:08:25 -07:00
|
|
|
\ ], 100))
|
2015-03-28 07:30:23 -07:00
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { -1 } } }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can pass 0 to check if a job exists', function()
|
|
|
|
source([[
|
2018-03-08 18:50:32 -07:00
|
|
|
call rpcnotify(g:channel, 'wait', jobwait(has('win32') ? [
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 50; exit 4'),
|
|
|
|
\ jobstart('Start-Sleep -Milliseconds 300; exit 5'),
|
|
|
|
\ ] : [
|
2017-01-14 01:47:33 -07:00
|
|
|
\ jobstart('sleep 0.05; exit 4'),
|
|
|
|
\ jobstart('sleep 0.3; exit 5'),
|
2015-03-28 07:30:23 -07:00
|
|
|
\ ], 0))
|
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'wait', { { -1, -1 } } }, next_msg())
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
end)
|
2023-11-07 15:29:58 -07:00
|
|
|
|
2023-11-10 00:24:36 -07:00
|
|
|
it('hides cursor and flushes messages before blocking', function()
|
|
|
|
local screen = Screen.new(50, 6)
|
2023-11-07 15:29:58 -07:00
|
|
|
screen:set_default_attr_ids({
|
2024-01-02 18:09:18 -07:00
|
|
|
[0] = { foreground = Screen.colors.Blue, bold = true }, -- NonText
|
|
|
|
[1] = { bold = true, reverse = true }, -- MsgSeparator
|
|
|
|
[2] = { bold = true, foreground = Screen.colors.SeaGreen }, -- MoreMsg
|
2023-11-07 15:29:58 -07:00
|
|
|
})
|
|
|
|
screen:attach()
|
|
|
|
command([[let g:id = jobstart([v:progpath, '--clean', '--headless'])]])
|
2023-11-10 00:24:36 -07:00
|
|
|
source([[
|
|
|
|
func PrintAndWait()
|
|
|
|
echon "aaa\nbbb"
|
|
|
|
call jobwait([g:id], 300)
|
|
|
|
echon "\nccc"
|
|
|
|
endfunc
|
|
|
|
]])
|
|
|
|
feed_command('call PrintAndWait()')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2023-11-10 00:24:36 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*2
|
2023-11-10 00:24:36 -07:00
|
|
|
{1: }|
|
|
|
|
aaa |
|
|
|
|
bbb |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
timeout = 100,
|
|
|
|
}
|
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2023-11-10 00:24:36 -07:00
|
|
|
|
|
|
|
|
{1: }|
|
|
|
|
aaa |
|
|
|
|
bbb |
|
|
|
|
ccc |
|
|
|
|
{2:Press ENTER or type command to continue}^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2023-11-10 00:24:36 -07:00
|
|
|
feed('<CR>')
|
|
|
|
funcs.jobstop(meths.get_var('id'))
|
2023-11-07 15:29:58 -07:00
|
|
|
end)
|
2015-03-28 07:30:23 -07:00
|
|
|
end)
|
|
|
|
|
2019-09-03 10:25:27 -07:00
|
|
|
pending('exit event follows stdout, stderr', function()
|
|
|
|
nvim('command', "let g:job_opts.on_stderr = function('OnEvent')")
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', "let j = jobstart(['cat', '-'], g:job_opts)")
|
2014-10-25 18:45:13 -07:00
|
|
|
nvim('eval', 'jobsend(j, "abcdef")')
|
2014-11-06 22:38:12 -07:00
|
|
|
nvim('eval', 'jobstop(j)')
|
2019-09-03 10:25:27 -07:00
|
|
|
expect_msg_seq(
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 0, { 'abcdef' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
|
|
|
{ 'notification', 'stderr', { 0, { '' } } },
|
2019-09-03 10:25:27 -07:00
|
|
|
},
|
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stderr', { 0, { '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { 'abcdef' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
2019-09-03 10:25:27 -07:00
|
|
|
},
|
|
|
|
-- Alternative sequence:
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
|
|
|
{ 'notification', 'stdout', { 0, { 'abcdef' } } },
|
|
|
|
{ 'notification', 'stderr', { 0, { '' } } },
|
|
|
|
{ 'notification', 'stdout', { 0, { '' } } },
|
2019-09-03 10:25:27 -07:00
|
|
|
}
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'exit', { 0, 143 } }, next_msg())
|
2014-11-06 22:38:12 -07:00
|
|
|
end)
|
2015-02-23 08:34:20 -07:00
|
|
|
|
2016-05-12 13:25:15 -07:00
|
|
|
it('cannot have both rpc and pty options', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
command('let g:job_opts.pty = v:true')
|
|
|
|
command('let g:job_opts.rpc = v:true')
|
2019-07-22 10:14:10 -07:00
|
|
|
local _, err = pcall(command, "let j = jobstart(['cat', '-'], g:job_opts)")
|
2024-01-02 18:09:18 -07:00
|
|
|
ok(
|
|
|
|
string.find(err, "E475: Invalid argument: job cannot have both 'pty' and 'rpc' options set")
|
|
|
|
~= nil
|
|
|
|
)
|
2016-05-12 13:25:15 -07:00
|
|
|
end)
|
|
|
|
|
2018-01-07 02:16:39 -07:00
|
|
|
it('does not crash when repeatedly failing to start shell', function()
|
|
|
|
source([[
|
|
|
|
set shell=nosuchshell
|
|
|
|
func! DoIt()
|
|
|
|
call jobstart('true')
|
|
|
|
call jobstart('true')
|
|
|
|
endfunc
|
|
|
|
]])
|
|
|
|
-- The crash only triggered if both jobs are cleaned up on the same event
|
|
|
|
-- loop tick. This is also prevented by try-block, so feed must be used.
|
2024-01-02 18:09:18 -07:00
|
|
|
feed_command('call DoIt()')
|
2018-01-07 02:16:39 -07:00
|
|
|
feed('<cr>') -- press RETURN
|
2021-09-01 07:29:38 -07:00
|
|
|
assert_alive()
|
2018-01-07 02:16:39 -07:00
|
|
|
end)
|
|
|
|
|
2018-03-17 16:09:28 -07:00
|
|
|
it('jobstop() kills entire process tree #6530', function()
|
|
|
|
-- XXX: Using `nvim` isn't a good test, it reaps its children on exit.
|
|
|
|
-- local c = 'call jobstart([v:progpath, "-u", "NONE", "-i", "NONE", "--headless"])'
|
|
|
|
-- local j = eval("jobstart([v:progpath, '-u', 'NONE', '-i', 'NONE', '--headless', '-c', '"
|
|
|
|
-- ..c.."', '-c', '"..c.."'])")
|
|
|
|
|
|
|
|
-- Create child with several descendants.
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2019-08-15 21:37:44 -07:00
|
|
|
source([[
|
|
|
|
function! s:formatprocs(pid, prefix)
|
|
|
|
let result = ''
|
|
|
|
let result .= a:prefix . printf("%-24.24s%6s %12.12s %s\n",
|
|
|
|
\ s:procs[a:pid]['name'],
|
|
|
|
\ a:pid,
|
|
|
|
\ s:procs[a:pid]['Session Name'],
|
|
|
|
\ s:procs[a:pid]['Session'])
|
|
|
|
if has_key(s:procs[a:pid], 'children')
|
|
|
|
for pid in s:procs[a:pid]['children']
|
|
|
|
let result .= s:formatprocs(pid, a:prefix . ' ')
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
return result
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! PsTree() abort
|
|
|
|
let s:procs = {}
|
|
|
|
for proc in map(
|
|
|
|
\ map(
|
|
|
|
\ systemlist('tasklist /NH'),
|
|
|
|
\ 'substitute(v:val, "\r", "", "")'),
|
|
|
|
\ 'split(v:val, "\\s\\+")')
|
|
|
|
if len(proc) == 6
|
2024-01-02 18:09:18 -07:00
|
|
|
let s:procs[proc[1]] .. ']]' .. [[= {'name': proc[0],
|
2019-08-15 21:37:44 -07:00
|
|
|
\ 'Session Name': proc[2],
|
|
|
|
\ 'Session': proc[3]}
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
for pid in keys(s:procs)
|
|
|
|
let children = nvim_get_proc_children(str2nr(pid))
|
|
|
|
if !empty(children)
|
|
|
|
let s:procs[pid]['children'] = children
|
|
|
|
for cpid in children
|
|
|
|
let s:procs[printf('%d', cpid)]['parent'] = str2nr(pid)
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
let result = ''
|
|
|
|
for pid in sort(keys(s:procs), {i1, i2 -> i1 - i2})
|
|
|
|
if !has_key(s:procs[pid], 'parent')
|
|
|
|
let result .= s:formatprocs(pid, '')
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
return result
|
|
|
|
endfunction
|
|
|
|
]])
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
local sleep_cmd = (is_os('win') and 'ping -n 31 127.0.0.1' or 'sleep 30')
|
|
|
|
local j = eval("jobstart('" .. sleep_cmd .. ' | ' .. sleep_cmd .. ' | ' .. sleep_cmd .. "')")
|
2018-03-17 16:09:28 -07:00
|
|
|
local ppid = funcs.jobpid(j)
|
|
|
|
local children
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2019-08-15 21:37:44 -07:00
|
|
|
local status, result = pcall(retry, nil, nil, function()
|
|
|
|
children = meths.get_proc_children(ppid)
|
2019-08-15 21:34:53 -07:00
|
|
|
-- On Windows conhost.exe may exist, and
|
|
|
|
-- e.g. vctip.exe might appear. #10783
|
|
|
|
ok(#children >= 3 and #children <= 5)
|
2019-08-15 21:37:44 -07:00
|
|
|
end)
|
|
|
|
if not status then
|
|
|
|
print('')
|
|
|
|
print(eval('PsTree()'))
|
|
|
|
error(result)
|
2019-08-15 11:38:03 -07:00
|
|
|
end
|
2019-08-15 21:37:44 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
retry(nil, nil, function()
|
2019-08-15 21:37:44 -07:00
|
|
|
children = meths.get_proc_children(ppid)
|
|
|
|
eq(3, #children)
|
|
|
|
end)
|
|
|
|
end
|
2018-03-17 16:09:28 -07:00
|
|
|
-- Assert that nvim_get_proc() sees the children.
|
|
|
|
for _, child_pid in ipairs(children) do
|
|
|
|
local info = meths.get_proc(child_pid)
|
2022-11-21 17:13:30 -07:00
|
|
|
-- eq((is_os('win') and 'nvim.exe' or 'nvim'), info.name)
|
2018-03-17 16:09:28 -07:00
|
|
|
eq(ppid, info.ppid)
|
|
|
|
end
|
|
|
|
-- Kill the root of the tree.
|
2019-07-30 05:16:50 -07:00
|
|
|
eq(1, funcs.jobstop(j))
|
2018-03-17 16:09:28 -07:00
|
|
|
-- Assert that the children were killed.
|
|
|
|
retry(nil, nil, function()
|
|
|
|
for _, child_pid in ipairs(children) do
|
|
|
|
eq(NIL, meths.get_proc(child_pid))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2020-01-20 05:06:38 -07:00
|
|
|
it('jobstop on same id before stopped', function()
|
|
|
|
nvim('command', 'let j = jobstart(["cat", "-"], g:job_opts)')
|
|
|
|
neq(0, eval('j'))
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 1, 0 }, eval('[jobstop(j), jobstop(j)]'))
|
2020-01-20 05:06:38 -07:00
|
|
|
end)
|
|
|
|
|
2015-02-23 08:34:20 -07:00
|
|
|
describe('running tty-test program', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
if skip(is_os('win')) then
|
|
|
|
return
|
|
|
|
end
|
2015-02-23 08:34:20 -07:00
|
|
|
local function next_chunk()
|
2015-11-17 15:31:22 -07:00
|
|
|
local rv
|
2015-02-23 08:34:20 -07:00
|
|
|
while true do
|
2015-03-25 19:11:05 -07:00
|
|
|
local msg = next_msg()
|
|
|
|
local data = msg[3][2]
|
2015-02-23 08:34:20 -07:00
|
|
|
for i = 1, #data do
|
|
|
|
data[i] = data[i]:gsub('\n', '\000')
|
|
|
|
end
|
|
|
|
rv = table.concat(data, '\n')
|
2015-04-10 19:49:32 -07:00
|
|
|
rv = rv:gsub('\r\n$', ''):gsub('^\r\n', '')
|
2015-02-23 08:34:20 -07:00
|
|
|
if rv ~= '' then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
2021-03-23 12:09:36 -07:00
|
|
|
local j
|
2015-02-23 08:34:20 -07:00
|
|
|
local function send(str)
|
2021-03-23 12:09:36 -07:00
|
|
|
-- check no nvim_chan_free double free with pty job (#14198)
|
|
|
|
meths.chan_send(j, str)
|
2015-02-23 08:34:20 -07:00
|
|
|
end
|
|
|
|
|
2015-05-19 07:11:32 -07:00
|
|
|
before_each(function()
|
2017-01-14 01:47:33 -07:00
|
|
|
-- Redefine Normalize() so that TTY data is not munged.
|
|
|
|
source([[
|
|
|
|
function! Normalize(data) abort
|
|
|
|
return a:data
|
|
|
|
endfunction
|
|
|
|
]])
|
2022-06-22 05:51:52 -07:00
|
|
|
insert(testprg('tty-test'))
|
2015-03-25 19:11:05 -07:00
|
|
|
nvim('command', 'let g:job_opts.pty = 1')
|
|
|
|
nvim('command', 'let exec = [expand("<cfile>:p")]')
|
2024-01-02 18:09:18 -07:00
|
|
|
nvim('command', 'let j = jobstart(exec, g:job_opts)')
|
|
|
|
j = eval 'j'
|
2015-02-23 08:34:20 -07:00
|
|
|
eq('tty ready', next_chunk())
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('echoing input', function()
|
|
|
|
send('test')
|
|
|
|
eq('test', next_chunk())
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('resizing window', function()
|
|
|
|
nvim('command', 'call jobresize(j, 40, 10)')
|
2015-03-25 05:14:47 -07:00
|
|
|
eq('rows: 10, cols: 40', next_chunk())
|
2015-02-23 08:34:20 -07:00
|
|
|
nvim('command', 'call jobresize(j, 10, 40)')
|
2015-03-25 05:14:47 -07:00
|
|
|
eq('rows: 40, cols: 10', next_chunk())
|
2015-02-23 08:34:20 -07:00
|
|
|
end)
|
2016-11-17 00:53:05 -07:00
|
|
|
|
2016-12-07 06:01:51 -07:00
|
|
|
it('jobclose() sends SIGHUP', function()
|
2016-11-17 00:53:05 -07:00
|
|
|
nvim('command', 'call jobclose(j)')
|
2016-12-31 06:59:44 -07:00
|
|
|
local msg = next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
msg = (msg[2] == 'stdout') and next_msg() or msg -- Skip stdout, if any.
|
|
|
|
eq({ 'notification', 'exit', { 0, 42 } }, msg)
|
2016-11-17 00:53:05 -07:00
|
|
|
end)
|
2017-01-21 07:11:30 -07:00
|
|
|
|
|
|
|
it('jobstart() does not keep ptmx file descriptor open', function()
|
|
|
|
-- Start another job (using libuv)
|
|
|
|
command('let g:job_opts.pty = 0')
|
|
|
|
local other_jobid = eval("jobstart(['cat', '-'], g:job_opts)")
|
|
|
|
local other_pid = eval('jobpid(' .. other_jobid .. ')')
|
|
|
|
|
|
|
|
-- Other job doesn't block first job from receiving SIGHUP on jobclose()
|
|
|
|
command('call jobclose(j)')
|
|
|
|
-- Have to wait so that the SIGHUP can be processed by tty-test on time.
|
|
|
|
-- Can't wait for the next message in case this test fails, if it fails
|
|
|
|
-- there won't be any more messages, and the test would hang.
|
|
|
|
helpers.sleep(100)
|
|
|
|
local err = exc_exec('call jobpid(j)')
|
2017-06-10 06:25:23 -07:00
|
|
|
eq('Vim(call):E900: Invalid channel id', err)
|
2017-01-21 07:11:30 -07:00
|
|
|
|
|
|
|
-- cleanup
|
|
|
|
eq(other_pid, eval('jobpid(' .. other_jobid .. ')'))
|
|
|
|
command('call jobstop(' .. other_jobid .. ')')
|
|
|
|
end)
|
2015-02-23 08:34:20 -07:00
|
|
|
end)
|
2023-12-12 12:19:46 -07:00
|
|
|
|
|
|
|
it('does not close the same handle twice on exit #25086', function()
|
|
|
|
local filename = string.format('%s.lua', helpers.tmpname())
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(
|
|
|
|
filename,
|
|
|
|
[[
|
2023-12-12 12:19:46 -07:00
|
|
|
vim.api.nvim_create_autocmd('VimLeavePre', {
|
|
|
|
callback = function()
|
|
|
|
local id = vim.fn.jobstart('sleep 0')
|
|
|
|
vim.fn.jobwait({id})
|
|
|
|
end,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
]]
|
|
|
|
)
|
2023-12-12 12:19:46 -07:00
|
|
|
|
|
|
|
local screen = thelpers.setup_child_nvim({
|
2024-01-02 18:09:18 -07:00
|
|
|
'--cmd',
|
|
|
|
'set notermguicolors',
|
|
|
|
'-i',
|
|
|
|
'NONE',
|
|
|
|
'-u',
|
|
|
|
filename,
|
2023-12-12 12:19:46 -07:00
|
|
|
})
|
2023-12-17 16:09:32 -07:00
|
|
|
-- Wait for startup to complete, so that all terminal responses are received.
|
|
|
|
screen:expect([[
|
|
|
|
{1: } |
|
|
|
|
~ |*3
|
|
|
|
{1:[No Name] 0,0-1 All}|
|
|
|
|
|
|
|
|
|
{3:-- TERMINAL --} |
|
|
|
|
]])
|
2023-12-12 12:19:46 -07:00
|
|
|
|
2023-12-17 16:09:32 -07:00
|
|
|
feed(':q<CR>')
|
|
|
|
screen:expect([[
|
2023-12-12 12:19:46 -07:00
|
|
|
|
|
|
|
|
[Process exited 0]{1: } |
|
|
|
|
|*4
|
|
|
|
{3:-- TERMINAL --} |
|
2023-12-17 16:09:32 -07:00
|
|
|
]])
|
2023-12-12 12:19:46 -07:00
|
|
|
end)
|
2014-10-27 14:58:22 -07:00
|
|
|
end)
|
2016-07-15 22:14:05 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
describe('pty process teardown', function()
|
2016-07-15 22:14:05 -07:00
|
|
|
local screen
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
screen = Screen.new(30, 6)
|
|
|
|
screen:attach()
|
|
|
|
screen:expect([[
|
|
|
|
^ |
|
2023-12-09 05:42:00 -07:00
|
|
|
~ |*4
|
2016-07-15 22:14:05 -07:00
|
|
|
|
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
it('does not prevent/delay exit. #4798 #4900', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
skip(is_os('win'))
|
2016-07-15 22:14:05 -07:00
|
|
|
-- Use a nested nvim (in :term) to test without --headless.
|
2023-12-07 17:00:27 -07:00
|
|
|
funcs.termopen({
|
2024-01-02 18:09:18 -07:00
|
|
|
helpers.nvim_prog,
|
|
|
|
'-u',
|
|
|
|
'NONE',
|
|
|
|
'-i',
|
|
|
|
'NONE',
|
|
|
|
'--cmd',
|
|
|
|
nvim_set,
|
2016-07-15 22:14:05 -07:00
|
|
|
-- Use :term again in the _nested_ nvim to get a PTY process.
|
|
|
|
-- Use `sleep` to simulate a long-running child of the PTY.
|
2024-01-02 18:09:18 -07:00
|
|
|
'+terminal',
|
|
|
|
'+!(sleep 300 &)',
|
|
|
|
'+qa',
|
2023-12-07 17:00:27 -07:00
|
|
|
}, { env = { VIMRUNTIME = os.getenv('VIMRUNTIME') } })
|
2016-07-15 22:14:05 -07:00
|
|
|
|
|
|
|
-- Exiting should terminate all descendants (PTY, its children, ...).
|
|
|
|
screen:expect([[
|
2017-05-25 03:35:27 -07:00
|
|
|
^ |
|
2016-07-15 22:14:05 -07:00
|
|
|
[Process exited 0] |
|
2023-12-09 05:42:00 -07:00
|
|
|
|*4
|
2016-07-15 22:14:05 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
end)
|