2023-01-02 07:34:14 -07:00
|
|
|
-- Tests for system() and :! shell.
|
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
local Screen = require('test.functional.ui.screen')
|
2017-04-08 15:55:19 -07:00
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
local assert_alive = n.assert_alive
|
|
|
|
local testprg = n.testprg
|
2024-01-12 10:59:57 -07:00
|
|
|
local eq, call, clear, eval, feed_command, feed, api =
|
2024-04-20 08:44:13 -07:00
|
|
|
t.eq, n.call, n.clear, n.eval, n.feed_command, n.feed, n.api
|
|
|
|
local command = n.command
|
|
|
|
local insert = n.insert
|
|
|
|
local expect = n.expect
|
|
|
|
local exc_exec = n.exc_exec
|
|
|
|
local os_kill = n.os_kill
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
|
|
|
local is_os = t.is_os
|
2014-10-01 05:32:56 -07:00
|
|
|
|
|
|
|
local function create_file_with_nuls(name)
|
|
|
|
return function()
|
2024-01-02 18:09:18 -07:00
|
|
|
feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w ' .. name .. '<CR>')
|
|
|
|
eval('1') -- wait for the file to be created
|
2014-10-01 05:32:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function delete_file(name)
|
|
|
|
return function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eval("delete('" .. name .. "')")
|
2014-10-01 05:32:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe('system()', function()
|
|
|
|
before_each(clear)
|
|
|
|
|
2016-08-25 09:28:54 -07:00
|
|
|
describe('command passed as a List', function()
|
2019-12-23 23:53:56 -07:00
|
|
|
it('throws error if cmd[0] is not executable', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
"Vim:E475: Invalid value for argument cmd: 'this-should-not-exist' is not executable",
|
|
|
|
pcall_err(call, 'system', { 'this-should-not-exist' })
|
|
|
|
)
|
2016-09-30 07:29:50 -07:00
|
|
|
eq(-1, eval('v:shell_error'))
|
|
|
|
end)
|
|
|
|
|
2017-01-10 10:06:34 -07:00
|
|
|
it('parameter validation does NOT modify v:shell_error', function()
|
|
|
|
-- 1. Call system() with invalid parameters.
|
|
|
|
-- 2. Assert that v:shell_error was NOT set.
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('call system({})')
|
2017-01-10 10:06:34 -07:00
|
|
|
eq('E475: Invalid argument: expected String or List', eval('v:errmsg'))
|
|
|
|
eq(0, eval('v:shell_error'))
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('call system([])')
|
2017-01-10 10:06:34 -07:00
|
|
|
eq('E474: Invalid argument', eval('v:errmsg'))
|
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
|
|
|
|
-- Provoke a non-zero v:shell_error.
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
"Vim:E475: Invalid value for argument cmd: 'this-should-not-exist' is not executable",
|
|
|
|
pcall_err(call, 'system', { 'this-should-not-exist' })
|
|
|
|
)
|
2017-01-10 10:06:34 -07:00
|
|
|
local old_val = eval('v:shell_error')
|
|
|
|
eq(-1, old_val)
|
|
|
|
|
|
|
|
-- 1. Call system() with invalid parameters.
|
|
|
|
-- 2. Assert that v:shell_error was NOT modified.
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('call system({})')
|
2017-01-10 10:06:34 -07:00
|
|
|
eq(old_val, eval('v:shell_error'))
|
2017-04-08 14:12:26 -07:00
|
|
|
feed_command('call system([])')
|
2017-01-10 10:06:34 -07:00
|
|
|
eq(old_val, eval('v:shell_error'))
|
|
|
|
end)
|
|
|
|
|
2016-08-25 09:28:54 -07:00
|
|
|
it('quotes arguments correctly #5280', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
local out =
|
|
|
|
call('system', { testprg('printargs-test'), [[1]], [[2 "3]], [[4 ' 5]], [[6 ' 7']] })
|
2016-09-30 07:29:50 -07:00
|
|
|
|
2016-08-25 09:28:54 -07:00
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
eq([[arg1=1;arg2=2 "3;arg3=4 ' 5;arg4=6 ' 7';]], out)
|
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
out = call('system', { testprg('printargs-test'), [['1]], [[2 "3]] })
|
2016-08-25 09:28:54 -07:00
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
eq([[arg1='1;arg2=2 "3;]], out)
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
out = call('system', { testprg('printargs-test'), 'A\nB' })
|
2016-08-25 09:28:54 -07:00
|
|
|
eq(0, eval('v:shell_error'))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('arg1=A\nB;', out)
|
2016-08-25 09:28:54 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('calls executable in $PATH', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
if 0 == eval("executable('python3')") then
|
|
|
|
pending('missing `python3`')
|
|
|
|
end
|
|
|
|
eq('foo\n', eval([[system(['python3', '-c', 'print("foo")'])]]))
|
2016-08-25 09:28:54 -07:00
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('does NOT run in shell', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'%PATH%\n',
|
|
|
|
eval(
|
|
|
|
"system(['powershell', '-NoProfile', '-NoLogo', '-ExecutionPolicy', 'RemoteSigned', '-Command', 'Write-Output', '%PATH%'])"
|
|
|
|
)
|
|
|
|
)
|
2017-11-06 19:00:19 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('* $PATH %PATH%\n', eval("system(['echo', '*', '$PATH', '%PATH%'])"))
|
2016-08-25 09:28:54 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('sets v:shell_error', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2017-03-25 15:47:38 -07:00
|
|
|
eval([[system("cmd.exe /c exit")]])
|
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
eval([[system("cmd.exe /c exit 1")]])
|
|
|
|
eq(1, eval('v:shell_error'))
|
|
|
|
eval([[system("cmd.exe /c exit 5")]])
|
|
|
|
eq(5, eval('v:shell_error'))
|
|
|
|
eval([[system('this-should-not-exist')]])
|
|
|
|
eq(1, eval('v:shell_error'))
|
|
|
|
else
|
|
|
|
eval([[system("sh -c 'exit'")]])
|
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
eval([[system("sh -c 'exit 1'")]])
|
|
|
|
eq(1, eval('v:shell_error'))
|
|
|
|
eval([[system("sh -c 'exit 5'")]])
|
|
|
|
eq(5, eval('v:shell_error'))
|
|
|
|
eval([[system('this-should-not-exist')]])
|
|
|
|
eq(127, eval('v:shell_error'))
|
|
|
|
end
|
2014-10-22 03:15:55 -07:00
|
|
|
end)
|
|
|
|
|
2017-09-30 18:31:31 -07:00
|
|
|
describe('executes shell function', function()
|
2015-01-17 08:26:42 -07:00
|
|
|
local screen
|
|
|
|
|
|
|
|
before_each(function()
|
2017-09-30 18:31:31 -07:00
|
|
|
screen = Screen.new()
|
2015-01-17 08:26:42 -07:00
|
|
|
end)
|
|
|
|
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2017-09-30 18:31:31 -07:00
|
|
|
local function test_more()
|
|
|
|
eq('root = true', eval([[get(split(system('"more" ".editorconfig"'), "\n"), 0, '')]]))
|
|
|
|
end
|
|
|
|
local function test_shell_unquoting()
|
|
|
|
eval([[system('"ping" "-n" "1" "127.0.0.1"')]])
|
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
eq('"a b"\n', eval([[system('cmd /s/c "cmd /s/c "cmd /s/c "echo "a b""""')]]))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'"a b"\n',
|
|
|
|
eval(
|
|
|
|
[[system('powershell -NoProfile -NoLogo -ExecutionPolicy RemoteSigned -Command Write-Output ''\^"a b\^"''')]]
|
|
|
|
)
|
|
|
|
)
|
2017-09-30 18:31:31 -07:00
|
|
|
end
|
|
|
|
|
2017-03-30 15:41:52 -07:00
|
|
|
it('with shell=cmd.exe', function()
|
|
|
|
command('set shell=cmd.exe')
|
2017-03-25 15:47:38 -07:00
|
|
|
eq('""\n', eval([[system('echo ""')]]))
|
2017-03-28 08:03:53 -07:00
|
|
|
eq('"a b"\n', eval([[system('echo "a b"')]]))
|
2017-03-30 15:41:52 -07:00
|
|
|
eq('a \nb\n', eval([[system('echo a & echo b')]]))
|
|
|
|
eq('a \n', eval([[system('echo a 2>&1')]]))
|
2017-09-30 18:31:31 -07:00
|
|
|
test_more()
|
2017-03-28 08:03:53 -07:00
|
|
|
eval([[system('cd "C:\Program Files"')]])
|
|
|
|
eq(0, eval('v:shell_error'))
|
2017-09-30 18:31:31 -07:00
|
|
|
test_shell_unquoting()
|
2017-03-28 08:03:53 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-30 15:41:52 -07:00
|
|
|
it('with shell=cmd', function()
|
|
|
|
command('set shell=cmd')
|
2017-03-28 08:03:53 -07:00
|
|
|
eq('"a b"\n', eval([[system('echo "a b"')]]))
|
2017-09-30 18:31:31 -07:00
|
|
|
test_more()
|
|
|
|
test_shell_unquoting()
|
2017-03-28 08:03:53 -07:00
|
|
|
end)
|
|
|
|
|
2017-03-30 15:41:52 -07:00
|
|
|
it('with shell=$COMSPEC', function()
|
2017-03-28 08:03:53 -07:00
|
|
|
local comspecshell = eval("fnamemodify($COMSPEC, ':t')")
|
|
|
|
if comspecshell == 'cmd.exe' then
|
2017-03-30 15:41:52 -07:00
|
|
|
command('set shell=$COMSPEC')
|
2017-03-28 08:03:53 -07:00
|
|
|
eq('"a b"\n', eval([[system('echo "a b"')]]))
|
2017-09-30 18:31:31 -07:00
|
|
|
test_more()
|
|
|
|
test_shell_unquoting()
|
2017-03-28 08:03:53 -07:00
|
|
|
else
|
2017-03-30 15:41:52 -07:00
|
|
|
pending('$COMSPEC is not cmd.exe: ' .. comspecshell)
|
2017-03-28 08:03:53 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
it('with powershell', function()
|
2024-04-20 08:44:13 -07:00
|
|
|
n.set_shell_powershell()
|
2019-10-10 21:30:20 -07:00
|
|
|
eq('a\nb\n', eval([[system('Write-Output a b')]]))
|
2017-03-28 08:03:53 -07:00
|
|
|
eq('C:\\\n', eval([[system('cd c:\; (Get-Location).Path')]]))
|
2019-10-10 21:30:20 -07:00
|
|
|
eq('a b\n', eval([[system('Write-Output "a b"')]]))
|
2017-03-28 08:03:53 -07:00
|
|
|
end)
|
|
|
|
end
|
2017-03-25 15:47:38 -07:00
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
it('powershell w/ UTF-8 text #13713', function()
|
2024-04-20 08:44:13 -07:00
|
|
|
if not n.has_powershell() then
|
2024-01-02 18:09:18 -07:00
|
|
|
pending('powershell not found', function() end)
|
2021-07-02 05:15:40 -07:00
|
|
|
return
|
|
|
|
end
|
2024-04-20 08:44:13 -07:00
|
|
|
n.set_shell_powershell()
|
2021-07-02 05:15:40 -07:00
|
|
|
eq('ああ\n', eval([[system('Write-Output "ああ"')]]))
|
|
|
|
-- Sanity test w/ default encoding
|
|
|
|
-- * on Windows, expected to default to Western European enc
|
|
|
|
-- * on Linux, expected to default to UTF8
|
|
|
|
command([[let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ']])
|
2022-11-21 17:13:30 -07:00
|
|
|
eq(is_os('win') and '??\n' or 'ああ\n', eval([[system('Write-Output "ああ"')]]))
|
2021-07-02 05:15:40 -07:00
|
|
|
end)
|
|
|
|
|
2015-01-17 08:26:42 -07:00
|
|
|
it('`echo` and waits for its return', function()
|
|
|
|
feed(':call system("echo")<cr>')
|
|
|
|
screen:expect([[
|
2015-03-05 02:07:55 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
2015-01-17 08:26:42 -07:00
|
|
|
:call system("echo") |
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2018-07-28 18:49:11 -07:00
|
|
|
it('prints verbose information', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('shell', 'fake_shell', {})
|
|
|
|
api.nvim_set_option_value('shellcmdflag', 'cmdflag', {})
|
2019-07-17 14:05:04 -07:00
|
|
|
|
2018-09-21 00:20:04 -07:00
|
|
|
screen:try_resize(72, 14)
|
2018-07-28 18:49:11 -07:00
|
|
|
feed(':4verbose echo system("echo hi")<cr>')
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect { any = [[Executing command: "'fake_shell' 'cmdflag' '"echo hi"'"]] }
|
2018-09-21 00:20:04 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect { any = [[Executing command: "'fake_shell' 'cmdflag' 'echo hi'"]] }
|
2018-09-21 00:20:04 -07:00
|
|
|
end
|
2018-07-28 18:49:11 -07:00
|
|
|
feed('<cr>')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('self and total time recorded separately', function()
|
2024-04-08 02:03:20 -07:00
|
|
|
local tempfile = t.tmpname()
|
2018-07-28 18:49:11 -07:00
|
|
|
|
|
|
|
feed(':function! AlmostNoSelfTime()<cr>')
|
|
|
|
feed('echo system("echo hi")<cr>')
|
|
|
|
feed('endfunction<cr>')
|
|
|
|
|
|
|
|
feed(':profile start ' .. tempfile .. '<cr>')
|
|
|
|
feed(':profile func AlmostNoSelfTime<cr>')
|
|
|
|
feed(':call AlmostNoSelfTime()<cr>')
|
|
|
|
feed(':profile dump<cr>')
|
|
|
|
|
|
|
|
feed(':edit ' .. tempfile .. '<cr>')
|
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
local command_total_time = tonumber(n.fn.split(n.fn.getline(7))[2])
|
|
|
|
local command_self_time = tonumber(n.fn.split(n.fn.getline(7))[3])
|
2018-07-28 18:49:11 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
t.neq(nil, command_total_time)
|
|
|
|
t.neq(nil, command_self_time)
|
2018-07-28 18:49:11 -07:00
|
|
|
end)
|
|
|
|
|
2017-09-30 18:31:31 -07:00
|
|
|
it('`yes` interrupted with CTRL-C', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
feed(
|
|
|
|
':call system("'
|
|
|
|
.. (is_os('win') and 'for /L %I in (1,0,2) do @echo y' or 'yes')
|
|
|
|
.. '")<cr>'
|
|
|
|
)
|
2015-02-20 16:41:03 -07:00
|
|
|
screen:expect([[
|
|
|
|
|
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
2024-01-02 18:09:18 -07:00
|
|
|
]] .. (is_os('win') and [[
|
|
|
|
:call system("for /L %I in (1,0,2) do @echo y") |]] or [[
|
2018-02-17 00:25:51 -07:00
|
|
|
:call system("yes") |]]))
|
2022-04-29 22:11:35 -07:00
|
|
|
feed('foo<c-c>')
|
2015-01-17 08:26:42 -07:00
|
|
|
screen:expect([[
|
2015-03-05 02:07:55 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
2019-07-28 17:36:51 -07:00
|
|
|
Type :qa and press <Enter> to exit Nvim |
|
2015-01-17 08:26:42 -07:00
|
|
|
]])
|
|
|
|
end)
|
2022-04-29 22:11:35 -07:00
|
|
|
|
|
|
|
it('`yes` interrupted with mapped CTRL-C', function()
|
|
|
|
command('nnoremap <C-C> i')
|
2024-01-02 18:09:18 -07:00
|
|
|
feed(
|
|
|
|
':call system("'
|
|
|
|
.. (is_os('win') and 'for /L %I in (1,0,2) do @echo y' or 'yes')
|
|
|
|
.. '")<cr>'
|
|
|
|
)
|
2022-04-29 22:11:35 -07:00
|
|
|
screen:expect([[
|
|
|
|
|
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
2024-01-02 18:09:18 -07:00
|
|
|
]] .. (is_os('win') and [[
|
|
|
|
:call system("for /L %I in (1,0,2) do @echo y") |]] or [[
|
2022-04-29 22:11:35 -07:00
|
|
|
:call system("yes") |]]))
|
|
|
|
feed('foo<c-c>')
|
|
|
|
screen:expect([[
|
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
|
|
|
{5:-- INSERT --} |
|
2022-04-29 22:11:35 -07:00
|
|
|
]])
|
|
|
|
end)
|
2015-01-17 08:26:42 -07:00
|
|
|
end)
|
|
|
|
|
2014-10-01 05:32:56 -07:00
|
|
|
describe('passing no input', function()
|
|
|
|
it('returns the program output', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('echoed\n', eval('system("echo echoed")'))
|
2017-03-25 15:47:38 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('echoed', eval('system("printf echoed")'))
|
2017-03-25 15:47:38 -07:00
|
|
|
end
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
2016-10-17 14:56:39 -07:00
|
|
|
it('to backgrounded command does not crash', function()
|
2016-10-22 11:40:06 -07:00
|
|
|
-- This is indeterminate, just exercise the codepath. May get E5677.
|
2024-01-02 18:09:18 -07:00
|
|
|
feed_command(
|
|
|
|
'call system(has("win32") ? "start /b /wait cmd /c echo echoed" : "printf echoed &")'
|
|
|
|
)
|
|
|
|
local v_errnum = string.match(eval('v:errmsg'), '^E%d*:')
|
2016-10-22 11:40:06 -07:00
|
|
|
if v_errnum then
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('E5677:', v_errnum)
|
2016-10-22 11:40:06 -07:00
|
|
|
end
|
2021-09-01 09:42:53 -07:00
|
|
|
assert_alive()
|
2016-10-17 14:56:39 -07:00
|
|
|
end)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('passing input', function()
|
|
|
|
it('returns the program output', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('input', eval('system("cat -", "input")'))
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
2016-10-17 14:56:39 -07:00
|
|
|
it('to backgrounded command does not crash', function()
|
2016-10-22 11:40:06 -07:00
|
|
|
-- This is indeterminate, just exercise the codepath. May get E5677.
|
2018-04-14 17:43:18 -07:00
|
|
|
feed_command('call system(has("win32") ? "start /b /wait more" : "cat - &", "input")')
|
2024-01-02 18:09:18 -07:00
|
|
|
local v_errnum = string.match(eval('v:errmsg'), '^E%d*:')
|
2016-10-22 11:40:06 -07:00
|
|
|
if v_errnum then
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('E5677:', v_errnum)
|
2016-10-22 11:40:06 -07:00
|
|
|
end
|
2021-09-01 09:42:53 -07:00
|
|
|
assert_alive()
|
2016-10-17 14:56:39 -07:00
|
|
|
end)
|
2018-01-23 15:56:50 -07:00
|
|
|
it('works with an empty string', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('test\n', eval('system("echo test", "")'))
|
2021-09-01 09:42:53 -07:00
|
|
|
assert_alive()
|
2018-01-23 15:56:50 -07:00
|
|
|
end)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
|
2014-11-10 05:19:27 -07:00
|
|
|
describe('passing a lot of input', function()
|
|
|
|
it('returns the program output', function()
|
|
|
|
local input = {}
|
|
|
|
-- write more than 1mb of data, which should be enough to overcome
|
|
|
|
-- the os buffer limit and force multiple event loop iterations to write
|
|
|
|
-- everything
|
2015-11-17 14:44:00 -07:00
|
|
|
for _ = 1, 0xffff do
|
2014-11-10 05:19:27 -07:00
|
|
|
input[#input + 1] = '01234567890ABCDEFabcdef'
|
|
|
|
end
|
|
|
|
input = table.concat(input, '\n')
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('input', input)
|
2014-11-10 05:19:27 -07:00
|
|
|
eq(input, eval('system("cat -", g:input)'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2018-01-15 15:14:20 -07:00
|
|
|
describe('Number input', function()
|
|
|
|
it('is treated as a buffer id', function()
|
|
|
|
command("put ='text in buffer 1'")
|
|
|
|
eq('\ntext in buffer 1\n', eval('system("cat", 1)'))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('Vim(echo):E86: Buffer 42 does not exist', exc_exec('echo system("cat", 42)'))
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('with output containing NULs', function()
|
2023-04-11 02:18:54 -07:00
|
|
|
local fname = 'Xtest_functional_vimscript_system_nuls'
|
2014-10-01 05:32:56 -07:00
|
|
|
|
2016-05-15 11:54:13 -07:00
|
|
|
before_each(create_file_with_nuls(fname))
|
|
|
|
after_each(delete_file(fname))
|
2014-10-01 05:32:56 -07:00
|
|
|
|
|
|
|
it('replaces NULs by SOH characters', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('part1\001part2\001part3\n', eval([[system('"cat" "]] .. fname .. [["')]]))
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-08-25 09:28:54 -07:00
|
|
|
describe('input passed as List', function()
|
|
|
|
it('joins List items with linefeed characters', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq('line1\nline2\nline3', eval("system('cat -', ['line1', 'line2', 'line3'])"))
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- Notice that NULs are converted to SOH when the data is read back. This
|
|
|
|
-- is inconsistent and is a good reason for the existence of the
|
|
|
|
-- `systemlist()` function, where input and output map to the same
|
|
|
|
-- characters(see the following tests with `systemlist()` below)
|
2016-08-25 09:28:54 -07:00
|
|
|
describe('with linefeed characters inside List items', function()
|
2014-10-01 05:32:56 -07:00
|
|
|
it('converts linefeed characters to NULs', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'l1\001p2\nline2\001a\001b\nl3',
|
|
|
|
eval([[system('cat -', ["l1\np2", "line2\na\nb", 'l3'])]])
|
|
|
|
)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('with leading/trailing whitespace characters on items', function()
|
|
|
|
it('preserves whitespace, replacing linefeeds by NULs', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'line \nline2\001\n\001line3',
|
|
|
|
eval([[system('cat -', ['line ', "line2\n", "\nline3"])]])
|
|
|
|
)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
2014-10-23 06:30:27 -07:00
|
|
|
|
2019-09-11 18:26:35 -07:00
|
|
|
it("with a program that doesn't close stdout will exit properly after passing input", function()
|
2022-06-22 05:51:52 -07:00
|
|
|
local out = eval(string.format("system('%s', 'clip-data')", testprg('streams-test')))
|
2019-09-11 18:26:35 -07:00
|
|
|
assert(out:sub(0, 5) == 'pid: ', out)
|
2024-01-02 18:09:18 -07:00
|
|
|
os_kill(out:match('%d+'))
|
2015-05-24 23:45:17 -07:00
|
|
|
end)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('systemlist()', function()
|
2016-08-25 09:28:54 -07:00
|
|
|
-- Similar to `system()`, but returns List instead of String.
|
2014-10-01 05:32:56 -07:00
|
|
|
before_each(clear)
|
|
|
|
|
2017-03-30 15:41:52 -07:00
|
|
|
it('sets v:shell_error', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2017-03-25 15:47:38 -07:00
|
|
|
eval([[systemlist("cmd.exe /c exit")]])
|
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
eval([[systemlist("cmd.exe /c exit 1")]])
|
|
|
|
eq(1, eval('v:shell_error'))
|
|
|
|
eval([[systemlist("cmd.exe /c exit 5")]])
|
|
|
|
eq(5, eval('v:shell_error'))
|
|
|
|
eval([[systemlist('this-should-not-exist')]])
|
|
|
|
eq(1, eval('v:shell_error'))
|
|
|
|
else
|
|
|
|
eval([[systemlist("sh -c 'exit'")]])
|
|
|
|
eq(0, eval('v:shell_error'))
|
|
|
|
eval([[systemlist("sh -c 'exit 1'")]])
|
|
|
|
eq(1, eval('v:shell_error'))
|
|
|
|
eval([[systemlist("sh -c 'exit 5'")]])
|
|
|
|
eq(5, eval('v:shell_error'))
|
|
|
|
eval([[systemlist('this-should-not-exist')]])
|
|
|
|
eq(127, eval('v:shell_error'))
|
|
|
|
end
|
2014-10-22 03:15:55 -07:00
|
|
|
end)
|
|
|
|
|
2017-09-30 18:31:31 -07:00
|
|
|
describe('executes shell function', function()
|
2015-01-17 08:26:42 -07:00
|
|
|
local screen
|
|
|
|
|
|
|
|
before_each(function()
|
2018-09-18 00:29:54 -07:00
|
|
|
screen = Screen.new()
|
2015-01-17 08:26:42 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('`echo` and waits for its return', function()
|
|
|
|
feed(':call systemlist("echo")<cr>')
|
|
|
|
screen:expect([[
|
2015-03-05 02:07:55 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
2015-01-17 08:26:42 -07:00
|
|
|
:call systemlist("echo") |
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2017-09-30 18:31:31 -07:00
|
|
|
it('`yes` interrupted with CTRL-C', function()
|
2015-02-06 11:11:04 -07:00
|
|
|
feed(':call systemlist("yes | xargs")<cr>')
|
2015-02-20 16:41:03 -07:00
|
|
|
screen:expect([[
|
|
|
|
|
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
2015-03-16 04:29:57 -07:00
|
|
|
:call systemlist("yes | xargs") |
|
2015-02-20 16:41:03 -07:00
|
|
|
]])
|
2015-01-17 08:26:42 -07:00
|
|
|
feed('<c-c>')
|
|
|
|
screen:expect([[
|
2015-03-05 02:07:55 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*12
|
2019-07-28 17:36:51 -07:00
|
|
|
Type :qa and press <Enter> to exit Nvim |
|
2015-01-17 08:26:42 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-10-01 05:32:56 -07:00
|
|
|
describe('passing string with linefeed characters as input', function()
|
|
|
|
it('splits the output on linefeed characters', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'abc', 'def', 'ghi' }, eval([[systemlist("cat -", "abc\ndef\nghi")]]))
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-11-10 05:19:27 -07:00
|
|
|
describe('passing a lot of input', function()
|
|
|
|
it('returns the program output', function()
|
|
|
|
local input = {}
|
2015-11-17 14:44:00 -07:00
|
|
|
for _ = 1, 0xffff do
|
2014-11-10 05:19:27 -07:00
|
|
|
input[#input + 1] = '01234567890ABCDEFabcdef'
|
|
|
|
end
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('input', input)
|
2014-11-10 05:19:27 -07:00
|
|
|
eq(input, eval('systemlist("cat -", g:input)'))
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-10-01 05:32:56 -07:00
|
|
|
describe('with output containing NULs', function()
|
2023-04-11 02:18:54 -07:00
|
|
|
local fname = 'Xtest_functional_vimscript_systemlist_nuls'
|
2014-10-01 05:32:56 -07:00
|
|
|
|
2017-10-24 14:38:18 -07:00
|
|
|
before_each(function()
|
|
|
|
command('set ff=unix')
|
|
|
|
create_file_with_nuls(fname)()
|
|
|
|
end)
|
2016-05-15 11:54:13 -07:00
|
|
|
after_each(delete_file(fname))
|
2014-10-01 05:32:56 -07:00
|
|
|
|
|
|
|
it('replaces NULs by newline characters', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'part1\npart2\npart3' }, eval([[systemlist('"cat" "]] .. fname .. [["')]]))
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-08-25 09:28:54 -07:00
|
|
|
describe('input passed as List', function()
|
2014-10-01 05:32:56 -07:00
|
|
|
it('joins list items with linefeed characters', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'line1', 'line2', 'line3' }, eval("systemlist('cat -', ['line1', 'line2', 'line3'])"))
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- Unlike `system()` which uses SOH to represent NULs, with `systemlist()`
|
2022-04-15 03:35:06 -07:00
|
|
|
-- input and output are the same.
|
2014-10-01 05:32:56 -07:00
|
|
|
describe('with linefeed characters inside list items', function()
|
|
|
|
it('converts linefeed characters to NULs', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
{ 'l1\np2', 'line2\na\nb', 'l3' },
|
|
|
|
eval([[systemlist('cat -', ["l1\np2", "line2\na\nb", 'l3'])]])
|
|
|
|
)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('with leading/trailing whitespace characters on items', function()
|
|
|
|
it('preserves whitespace, replacing linefeeds by NULs', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
{ 'line ', 'line2\n', '\nline3' },
|
|
|
|
eval([[systemlist('cat -', ['line ', "line2\n", "\nline3"])]])
|
|
|
|
)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
2014-10-23 06:30:27 -07:00
|
|
|
|
2014-12-02 08:50:58 -07:00
|
|
|
describe('handles empty lines', function()
|
|
|
|
it('in the middle', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'line one', '', 'line two' }, eval("systemlist('cat',['line one','','line two'])"))
|
2014-12-02 08:50:58 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('in the beginning', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ '', 'line one', 'line two' }, eval("systemlist('cat',['','line one','line two'])"))
|
2014-12-02 08:50:58 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2014-11-27 13:30:17 -07:00
|
|
|
describe('when keepempty option is', function()
|
|
|
|
it('0, ignores trailing newline', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'aa', 'bb' }, eval("systemlist('cat',['aa','bb'],0)"))
|
|
|
|
eq({ 'aa', 'bb' }, eval("systemlist('cat',['aa','bb',''],0)"))
|
2014-11-27 13:30:17 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('1, preserves trailing newline', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'aa', 'bb' }, eval("systemlist('cat',['aa','bb'],1)"))
|
|
|
|
eq({ 'aa', 'bb', '' }, eval("systemlist('cat',['aa','bb',''],2)"))
|
2014-11-27 13:30:17 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2019-09-11 18:26:35 -07:00
|
|
|
it("with a program that doesn't close stdout will exit properly after passing input", function()
|
2022-06-22 05:51:52 -07:00
|
|
|
local out = eval(string.format("systemlist('%s', 'clip-data')", testprg('streams-test')))
|
2019-09-11 18:26:35 -07:00
|
|
|
assert(out[1]:sub(0, 5) == 'pid: ', out)
|
2024-01-02 18:09:18 -07:00
|
|
|
os_kill(out[1]:match('%d+'))
|
2015-05-24 23:45:17 -07:00
|
|
|
end)
|
2021-07-02 05:15:40 -07:00
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
it('powershell w/ UTF-8 text #13713', function()
|
2024-04-20 08:44:13 -07:00
|
|
|
if not n.has_powershell() then
|
2024-01-02 18:09:18 -07:00
|
|
|
pending('powershell not found', function() end)
|
2021-07-02 05:15:40 -07:00
|
|
|
return
|
|
|
|
end
|
2024-04-20 08:44:13 -07:00
|
|
|
n.set_shell_powershell()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ is_os('win') and 'あ\r' or 'あ' }, eval([[systemlist('Write-Output あ')]]))
|
2021-07-02 05:15:40 -07:00
|
|
|
-- Sanity test w/ default encoding
|
|
|
|
-- * on Windows, expected to default to Western European enc
|
|
|
|
-- * on Linux, expected to default to UTF8
|
|
|
|
command([[let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ']])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ is_os('win') and '?\r' or 'あ' }, eval([[systemlist('Write-Output あ')]]))
|
2021-07-02 05:15:40 -07:00
|
|
|
end)
|
2014-10-01 05:32:56 -07:00
|
|
|
end)
|
2021-11-09 07:22:21 -07:00
|
|
|
|
2022-06-22 05:51:52 -07:00
|
|
|
describe('shell :!', function()
|
|
|
|
before_each(clear)
|
|
|
|
|
2022-07-18 13:26:09 -07:00
|
|
|
it(':{range}! with powershell filter/redirect #16271 #19250', function()
|
2022-06-22 05:51:52 -07:00
|
|
|
local screen = Screen.new(500, 8)
|
2024-04-20 08:44:13 -07:00
|
|
|
local found = n.set_shell_powershell(true)
|
2022-06-22 05:51:52 -07:00
|
|
|
insert([[
|
|
|
|
3
|
|
|
|
1
|
|
|
|
4
|
|
|
|
2]])
|
2022-11-21 17:13:30 -07:00
|
|
|
if is_os('win') then
|
2022-07-18 13:26:09 -07:00
|
|
|
feed(':4verbose %!sort /R<cr>')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
any = [[Executing command: .?& { Get%-Content .* | & sort /R } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]],
|
2022-07-18 13:26:09 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
feed(':4verbose %!sort -r<cr>')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
any = [[Executing command: .?& { Get%-Content .* | & sort %-r } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]],
|
2022-07-18 13:26:09 -07:00
|
|
|
}
|
|
|
|
end
|
2022-06-22 05:51:52 -07:00
|
|
|
feed('<CR>')
|
|
|
|
if found then
|
|
|
|
-- Not using fake powershell, so we can test the result.
|
|
|
|
expect([[
|
2022-07-18 13:26:09 -07:00
|
|
|
4
|
2022-06-22 05:51:52 -07:00
|
|
|
3
|
2022-07-18 13:26:09 -07:00
|
|
|
2
|
|
|
|
1]])
|
2022-06-22 05:51:52 -07:00
|
|
|
end
|
|
|
|
end)
|
2023-01-12 10:25:29 -07:00
|
|
|
|
|
|
|
it(':{range}! without redirecting to buffer', function()
|
|
|
|
local screen = Screen.new(500, 10)
|
|
|
|
insert([[
|
|
|
|
3
|
|
|
|
1
|
|
|
|
4
|
|
|
|
2]])
|
|
|
|
feed(':4verbose %w !sort<cr>')
|
|
|
|
if is_os('win') then
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
any = [[Executing command: .?sort %< .*]],
|
2023-01-12 10:25:29 -07:00
|
|
|
}
|
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
any = [[Executing command: .?%(sort%) %< .*]],
|
2023-01-12 10:25:29 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
feed('<CR>')
|
2024-04-20 08:44:13 -07:00
|
|
|
n.set_shell_powershell(true)
|
2023-01-12 10:25:29 -07:00
|
|
|
feed(':4verbose %w !sort<cr>')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
any = [[Executing command: .?& { Get%-Content .* | & sort }]],
|
2023-01-12 10:25:29 -07:00
|
|
|
}
|
|
|
|
feed('<CR>')
|
2024-04-20 08:44:13 -07:00
|
|
|
n.expect_exit(command, 'qall!')
|
2023-01-12 10:25:29 -07:00
|
|
|
end)
|
2021-11-09 07:22:21 -07:00
|
|
|
end)
|