local helpers = require('test.functional.helpers')(after_each)
local thelpers = require('test.functional.terminal.helpers')
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 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
local assert_alive = helpers.assert_alive
local command = helpers.command
local fn = helpers.fn
local os_kill = helpers.os_kill
local retry = helpers.retry
local api = helpers.api
local NIL = vim.NIL
local poke_eventloop = helpers.poke_eventloop
local get_pathsep = helpers.get_pathsep
local pathroot = helpers.pathroot
local exec_lua = helpers.exec_lua
local nvim_set = helpers.nvim_set
local expect_twostreams = helpers.expect_twostreams
local expect_msg_seq = helpers.expect_msg_seq
local pcall_err = helpers.pcall_err
local matches = helpers.matches
local Screen = require('test.functional.ui.screen')
local skip = helpers.skip
local is_os = helpers.is_os
describe('jobs', function()
local channel
before_each(function()
clear()
channel = api.nvim_get_chan_info(0).id
api.nvim_set_var('channel', channel)
source([[
function! Normalize(data) abort
" Windows: remove ^M and term escape sequences
return type([]) == type(a:data)
\ ? map(a:data, 'substitute(substitute(v:val, "\r", "", "g"), "\x1b\\%(\\]\\d\\+;.\\{-}\x07\\|\\[.\\{-}[\x40-\x7E]\\)", "", "g")')
\ : a:data
endfunction
function! OnEvent(id, data, event) dict
let userdata = get(self, 'user')
let data = Normalize(a:data)
call rpcnotify(g:channel, a:event, userdata, data)
endfunction
let g:job_opts = {
\ 'on_stdout': function('OnEvent'),
\ 'on_exit': function('OnEvent'),
\ 'user': 0
\ }
]])
end)
it('must specify env option as a dict', function()
command('let g:job_opts.env = v:true')
local _, err = pcall(function()
if is_os('win') then
command("let j = jobstart('set', g:job_opts)")
else
command("let j = jobstart('env', g:job_opts)")
end
end)
ok(string.find(err, 'E475: Invalid argument: env') ~= nil)
end)
it('append environment #env', function()
command("let $VAR = 'abc'")
command("let $TOTO = 'goodbye world'")
command("let g:job_opts.env = {'TOTO': 'hello world'}")
if is_os('win') then
command([[call jobstart('echo %TOTO% %VAR%', g:job_opts)]])
else
command([[call jobstart('echo $TOTO $VAR', g:job_opts)]])
end
expect_msg_seq({
{ 'notification', 'stdout', { 0, { 'hello world abc' } } },
{ 'notification', 'stdout', { 0, { '', '' } } },
}, {
{ 'notification', 'stdout', { 0, { 'hello world abc', '' } } },
{ 'notification', 'stdout', { 0, { '' } } },
})
end)
it('append environment with pty #env', function()
command("let $VAR = 'abc'")
command("let $TOTO = 'goodbye world'")
command('let g:job_opts.pty = v:true')
command("let g:job_opts.env = {'TOTO': 'hello world'}")
if is_os('win') then
command([[call jobstart('echo %TOTO% %VAR%', g:job_opts)]])
else
command([[call jobstart('echo $TOTO $VAR', g:job_opts)]])
end
expect_msg_seq({
{ 'notification', 'stdout', { 0, { 'hello world abc' } } },
{ 'notification', 'stdout', { 0, { '', '' } } },
}, {
{ 'notification', 'stdout', { 0, { 'hello world abc', '' } } },
{ 'notification', 'stdout', { 0, { '' } } },
})
end)
it('replace environment #env', function()
command("let $VAR = 'abc'")
command("let $TOTO = 'goodbye world'")
command("let g:job_opts.env = {'TOTO': 'hello world'}")
command('let g:job_opts.clear_env = 1')
-- 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.
if is_os('win') then
command([[call jobstart('echo %TOTO% %VAR%', g:job_opts)]])
expect_msg_seq({
{ 'notification', 'stdout', { 0, { 'hello world %VAR%', ''
|