mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 19:38:20 +00:00
refactor: format test/*
This commit is contained in:
@@ -5,8 +5,13 @@ local helpers = require('test.functional.helpers')(after_each)
|
||||
local assert_alive = helpers.assert_alive
|
||||
local testprg = helpers.testprg
|
||||
local eq, call, clear, eval, feed_command, feed, nvim =
|
||||
helpers.eq, helpers.call, helpers.clear, helpers.eval, helpers.feed_command,
|
||||
helpers.feed, helpers.nvim
|
||||
helpers.eq,
|
||||
helpers.call,
|
||||
helpers.clear,
|
||||
helpers.eval,
|
||||
helpers.feed_command,
|
||||
helpers.feed,
|
||||
helpers.nvim
|
||||
local command = helpers.command
|
||||
local insert = helpers.insert
|
||||
local expect = helpers.expect
|
||||
@@ -19,14 +24,14 @@ local Screen = require('test.functional.ui.screen')
|
||||
|
||||
local function create_file_with_nuls(name)
|
||||
return function()
|
||||
feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w '..name..'<CR>')
|
||||
eval('1') -- wait for the file to be created
|
||||
feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w ' .. name .. '<CR>')
|
||||
eval('1') -- wait for the file to be created
|
||||
end
|
||||
end
|
||||
|
||||
local function delete_file(name)
|
||||
return function()
|
||||
eval("delete('"..name.."')")
|
||||
eval("delete('" .. name .. "')")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -35,8 +40,10 @@ describe('system()', function()
|
||||
|
||||
describe('command passed as a List', function()
|
||||
it('throws error if cmd[0] is not executable', function()
|
||||
eq("Vim:E475: Invalid value for argument cmd: 'this-should-not-exist' is not executable",
|
||||
pcall_err(call, 'system', { 'this-should-not-exist' }))
|
||||
eq(
|
||||
"Vim:E475: Invalid value for argument cmd: 'this-should-not-exist' is not executable",
|
||||
pcall_err(call, 'system', { 'this-should-not-exist' })
|
||||
)
|
||||
eq(-1, eval('v:shell_error'))
|
||||
end)
|
||||
|
||||
@@ -51,8 +58,10 @@ describe('system()', function()
|
||||
eq(0, eval('v:shell_error'))
|
||||
|
||||
-- Provoke a non-zero v:shell_error.
|
||||
eq("Vim:E475: Invalid value for argument cmd: 'this-should-not-exist' is not executable",
|
||||
pcall_err(call, 'system', { 'this-should-not-exist' }))
|
||||
eq(
|
||||
"Vim:E475: Invalid value for argument cmd: 'this-should-not-exist' is not executable",
|
||||
pcall_err(call, 'system', { 'this-should-not-exist' })
|
||||
)
|
||||
local old_val = eval('v:shell_error')
|
||||
eq(-1, old_val)
|
||||
|
||||
@@ -65,8 +74,8 @@ describe('system()', function()
|
||||
end)
|
||||
|
||||
it('quotes arguments correctly #5280', function()
|
||||
local out = call('system',
|
||||
{ testprg('printargs-test'), [[1]], [[2 "3]], [[4 ' 5]], [[6 ' 7']] })
|
||||
local out =
|
||||
call('system', { testprg('printargs-test'), [[1]], [[2 "3]], [[4 ' 5]], [[6 ' 7']] })
|
||||
|
||||
eq(0, eval('v:shell_error'))
|
||||
eq([[arg1=1;arg2=2 "3;arg3=4 ' 5;arg4=6 ' 7';]], out)
|
||||
@@ -75,22 +84,29 @@ describe('system()', function()
|
||||
eq(0, eval('v:shell_error'))
|
||||
eq([[arg1='1;arg2=2 "3;]], out)
|
||||
|
||||
out = call('system', { testprg('printargs-test'), "A\nB" })
|
||||
out = call('system', { testprg('printargs-test'), 'A\nB' })
|
||||
eq(0, eval('v:shell_error'))
|
||||
eq("arg1=A\nB;", out)
|
||||
eq('arg1=A\nB;', out)
|
||||
end)
|
||||
|
||||
it('calls executable in $PATH', function()
|
||||
if 0 == eval("executable('python3')") then pending("missing `python3`") end
|
||||
eq("foo\n", eval([[system(['python3', '-c', 'print("foo")'])]]))
|
||||
if 0 == eval("executable('python3')") then
|
||||
pending('missing `python3`')
|
||||
end
|
||||
eq('foo\n', eval([[system(['python3', '-c', 'print("foo")'])]]))
|
||||
eq(0, eval('v:shell_error'))
|
||||
end)
|
||||
|
||||
it('does NOT run in shell', function()
|
||||
if is_os('win') then
|
||||
eq("%PATH%\n", eval("system(['powershell', '-NoProfile', '-NoLogo', '-ExecutionPolicy', 'RemoteSigned', '-Command', 'Write-Output', '%PATH%'])"))
|
||||
eq(
|
||||
'%PATH%\n',
|
||||
eval(
|
||||
"system(['powershell', '-NoProfile', '-NoLogo', '-ExecutionPolicy', 'RemoteSigned', '-Command', 'Write-Output', '%PATH%'])"
|
||||
)
|
||||
)
|
||||
else
|
||||
eq("* $PATH %PATH%\n", eval("system(['echo', '*', '$PATH', '%PATH%'])"))
|
||||
eq('* $PATH %PATH%\n', eval("system(['echo', '*', '$PATH', '%PATH%'])"))
|
||||
end
|
||||
end)
|
||||
end)
|
||||
@@ -133,7 +149,12 @@ describe('system()', function()
|
||||
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""""')]]))
|
||||
eq('"a b"\n', eval([[system('powershell -NoProfile -NoLogo -ExecutionPolicy RemoteSigned -Command Write-Output ''\^"a b\^"''')]]))
|
||||
eq(
|
||||
'"a b"\n',
|
||||
eval(
|
||||
[[system('powershell -NoProfile -NoLogo -ExecutionPolicy RemoteSigned -Command Write-Output ''\^"a b\^"''')]]
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
it('with shell=cmd.exe', function()
|
||||
@@ -177,7 +198,7 @@ describe('system()', function()
|
||||
|
||||
it('powershell w/ UTF-8 text #13713', function()
|
||||
if not helpers.has_powershell() then
|
||||
pending("powershell not found", function() end)
|
||||
pending('powershell not found', function() end)
|
||||
return
|
||||
end
|
||||
helpers.set_shell_powershell()
|
||||
@@ -205,9 +226,9 @@ describe('system()', function()
|
||||
screen:try_resize(72, 14)
|
||||
feed(':4verbose echo system("echo hi")<cr>')
|
||||
if is_os('win') then
|
||||
screen:expect{any=[[Executing command: "'fake_shell' 'cmdflag' '"echo hi"'"]]}
|
||||
screen:expect { any = [[Executing command: "'fake_shell' 'cmdflag' '"echo hi"'"]] }
|
||||
else
|
||||
screen:expect{any=[[Executing command: "'fake_shell' 'cmdflag' 'echo hi'"]]}
|
||||
screen:expect { any = [[Executing command: "'fake_shell' 'cmdflag' 'echo hi'"]] }
|
||||
end
|
||||
feed('<cr>')
|
||||
end)
|
||||
@@ -234,16 +255,16 @@ describe('system()', function()
|
||||
end)
|
||||
|
||||
it('`yes` interrupted with CTRL-C', function()
|
||||
feed(':call system("' .. (is_os('win')
|
||||
and 'for /L %I in (1,0,2) do @echo y'
|
||||
or 'yes') .. '")<cr>')
|
||||
feed(
|
||||
':call system("'
|
||||
.. (is_os('win') and 'for /L %I in (1,0,2) do @echo y' or 'yes')
|
||||
.. '")<cr>'
|
||||
)
|
||||
screen:expect([[
|
||||
|
|
||||
~ |*12
|
||||
]] .. (is_os('win')
|
||||
and [[
|
||||
:call system("for /L %I in (1,0,2) do @echo y") |]]
|
||||
or [[
|
||||
]] .. (is_os('win') and [[
|
||||
:call system("for /L %I in (1,0,2) do @echo y") |]] or [[
|
||||
:call system("yes") |]]))
|
||||
feed('foo<c-c>')
|
||||
screen:expect([[
|
||||
@@ -255,16 +276,16 @@ describe('system()', function()
|
||||
|
||||
it('`yes` interrupted with mapped CTRL-C', function()
|
||||
command('nnoremap <C-C> i')
|
||||
feed(':call system("' .. (is_os('win')
|
||||
and 'for /L %I in (1,0,2) do @echo y'
|
||||
or 'yes') .. '")<cr>')
|
||||
feed(
|
||||
':call system("'
|
||||
.. (is_os('win') and 'for /L %I in (1,0,2) do @echo y' or 'yes')
|
||||
.. '")<cr>'
|
||||
)
|
||||
screen:expect([[
|
||||
|
|
||||
~ |*12
|
||||
]] .. (is_os('win')
|
||||
and [[
|
||||
:call system("for /L %I in (1,0,2) do @echo y") |]]
|
||||
or [[
|
||||
]] .. (is_os('win') and [[
|
||||
:call system("for /L %I in (1,0,2) do @echo y") |]] or [[
|
||||
:call system("yes") |]]))
|
||||
feed('foo<c-c>')
|
||||
screen:expect([[
|
||||
@@ -278,17 +299,19 @@ describe('system()', function()
|
||||
describe('passing no input', function()
|
||||
it('returns the program output', function()
|
||||
if is_os('win') then
|
||||
eq("echoed\n", eval('system("echo echoed")'))
|
||||
eq('echoed\n', eval('system("echo echoed")'))
|
||||
else
|
||||
eq("echoed", eval('system("printf echoed")'))
|
||||
eq('echoed', eval('system("printf echoed")'))
|
||||
end
|
||||
end)
|
||||
it('to backgrounded command does not crash', function()
|
||||
-- This is indeterminate, just exercise the codepath. May get E5677.
|
||||
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*:")
|
||||
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*:')
|
||||
if v_errnum then
|
||||
eq("E5677:", v_errnum)
|
||||
eq('E5677:', v_errnum)
|
||||
end
|
||||
assert_alive()
|
||||
end)
|
||||
@@ -296,19 +319,19 @@ describe('system()', function()
|
||||
|
||||
describe('passing input', function()
|
||||
it('returns the program output', function()
|
||||
eq("input", eval('system("cat -", "input")'))
|
||||
eq('input', eval('system("cat -", "input")'))
|
||||
end)
|
||||
it('to backgrounded command does not crash', function()
|
||||
-- This is indeterminate, just exercise the codepath. May get E5677.
|
||||
feed_command('call system(has("win32") ? "start /b /wait more" : "cat - &", "input")')
|
||||
local v_errnum = string.match(eval("v:errmsg"), "^E%d*:")
|
||||
local v_errnum = string.match(eval('v:errmsg'), '^E%d*:')
|
||||
if v_errnum then
|
||||
eq("E5677:", v_errnum)
|
||||
eq('E5677:', v_errnum)
|
||||
end
|
||||
assert_alive()
|
||||
end)
|
||||
it('works with an empty string', function()
|
||||
eq("test\n", eval('system("echo test", "")'))
|
||||
eq('test\n', eval('system("echo test", "")'))
|
||||
assert_alive()
|
||||
end)
|
||||
end)
|
||||
@@ -332,8 +355,7 @@ describe('system()', 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)'))
|
||||
eq('Vim(echo):E86: Buffer 42 does not exist',
|
||||
exc_exec('echo system("cat", 42)'))
|
||||
eq('Vim(echo):E86: Buffer 42 does not exist', exc_exec('echo system("cat", 42)'))
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -344,14 +366,13 @@ describe('system()', function()
|
||||
after_each(delete_file(fname))
|
||||
|
||||
it('replaces NULs by SOH characters', function()
|
||||
eq('part1\001part2\001part3\n', eval([[system('"cat" "]]..fname..[["')]]))
|
||||
eq('part1\001part2\001part3\n', eval([[system('"cat" "]] .. fname .. [["')]]))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('input passed as List', function()
|
||||
it('joins List items with linefeed characters', function()
|
||||
eq('line1\nline2\nline3',
|
||||
eval("system('cat -', ['line1', 'line2', 'line3'])"))
|
||||
eq('line1\nline2\nline3', eval("system('cat -', ['line1', 'line2', 'line3'])"))
|
||||
end)
|
||||
|
||||
-- Notice that NULs are converted to SOH when the data is read back. This
|
||||
@@ -360,15 +381,19 @@ describe('system()', function()
|
||||
-- characters(see the following tests with `systemlist()` below)
|
||||
describe('with linefeed characters inside List items', function()
|
||||
it('converts linefeed characters to NULs', function()
|
||||
eq('l1\001p2\nline2\001a\001b\nl3',
|
||||
eval([[system('cat -', ["l1\np2", "line2\na\nb", 'l3'])]]))
|
||||
eq(
|
||||
'l1\001p2\nline2\001a\001b\nl3',
|
||||
eval([[system('cat -', ["l1\np2", "line2\na\nb", 'l3'])]])
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('with leading/trailing whitespace characters on items', function()
|
||||
it('preserves whitespace, replacing linefeeds by NULs', function()
|
||||
eq('line \nline2\001\n\001line3',
|
||||
eval([[system('cat -', ['line ', "line2\n", "\nline3"])]]))
|
||||
eq(
|
||||
'line \nline2\001\n\001line3',
|
||||
eval([[system('cat -', ['line ', "line2\n", "\nline3"])]])
|
||||
)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
@@ -376,7 +401,7 @@ describe('system()', function()
|
||||
it("with a program that doesn't close stdout will exit properly after passing input", function()
|
||||
local out = eval(string.format("system('%s', 'clip-data')", testprg('streams-test')))
|
||||
assert(out:sub(0, 5) == 'pid: ', out)
|
||||
os_kill(out:match("%d+"))
|
||||
os_kill(out:match('%d+'))
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -445,7 +470,7 @@ describe('systemlist()', function()
|
||||
|
||||
describe('passing string with linefeed characters as input', function()
|
||||
it('splits the output on linefeed characters', function()
|
||||
eq({'abc', 'def', 'ghi'}, eval([[systemlist("cat -", "abc\ndef\nghi")]]))
|
||||
eq({ 'abc', 'def', 'ghi' }, eval([[systemlist("cat -", "abc\ndef\nghi")]]))
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -470,75 +495,77 @@ describe('systemlist()', function()
|
||||
after_each(delete_file(fname))
|
||||
|
||||
it('replaces NULs by newline characters', function()
|
||||
eq({'part1\npart2\npart3'}, eval([[systemlist('"cat" "]]..fname..[["')]]))
|
||||
eq({ 'part1\npart2\npart3' }, eval([[systemlist('"cat" "]] .. fname .. [["')]]))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('input passed as List', function()
|
||||
it('joins list items with linefeed characters', function()
|
||||
eq({'line1', 'line2', 'line3'},
|
||||
eval("systemlist('cat -', ['line1', 'line2', 'line3'])"))
|
||||
eq({ 'line1', 'line2', 'line3' }, eval("systemlist('cat -', ['line1', 'line2', 'line3'])"))
|
||||
end)
|
||||
|
||||
-- Unlike `system()` which uses SOH to represent NULs, with `systemlist()`
|
||||
-- input and output are the same.
|
||||
describe('with linefeed characters inside list items', function()
|
||||
it('converts linefeed characters to NULs', function()
|
||||
eq({'l1\np2', 'line2\na\nb', 'l3'},
|
||||
eval([[systemlist('cat -', ["l1\np2", "line2\na\nb", 'l3'])]]))
|
||||
eq(
|
||||
{ 'l1\np2', 'line2\na\nb', 'l3' },
|
||||
eval([[systemlist('cat -', ["l1\np2", "line2\na\nb", 'l3'])]])
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('with leading/trailing whitespace characters on items', function()
|
||||
it('preserves whitespace, replacing linefeeds by NULs', function()
|
||||
eq({'line ', 'line2\n', '\nline3'},
|
||||
eval([[systemlist('cat -', ['line ', "line2\n", "\nline3"])]]))
|
||||
eq(
|
||||
{ 'line ', 'line2\n', '\nline3' },
|
||||
eval([[systemlist('cat -', ['line ', "line2\n", "\nline3"])]])
|
||||
)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('handles empty lines', function()
|
||||
it('in the middle', function()
|
||||
eq({'line one','','line two'}, eval("systemlist('cat',['line one','','line two'])"))
|
||||
eq({ 'line one', '', 'line two' }, eval("systemlist('cat',['line one','','line two'])"))
|
||||
end)
|
||||
|
||||
it('in the beginning', function()
|
||||
eq({'','line one','line two'}, eval("systemlist('cat',['','line one','line two'])"))
|
||||
eq({ '', 'line one', 'line two' }, eval("systemlist('cat',['','line one','line two'])"))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('when keepempty option is', function()
|
||||
it('0, ignores trailing newline', function()
|
||||
eq({'aa','bb'}, eval("systemlist('cat',['aa','bb'],0)"))
|
||||
eq({'aa','bb'}, eval("systemlist('cat',['aa','bb',''],0)"))
|
||||
eq({ 'aa', 'bb' }, eval("systemlist('cat',['aa','bb'],0)"))
|
||||
eq({ 'aa', 'bb' }, eval("systemlist('cat',['aa','bb',''],0)"))
|
||||
end)
|
||||
|
||||
it('1, preserves trailing newline', function()
|
||||
eq({'aa','bb'}, eval("systemlist('cat',['aa','bb'],1)"))
|
||||
eq({'aa','bb',''}, eval("systemlist('cat',['aa','bb',''],2)"))
|
||||
eq({ 'aa', 'bb' }, eval("systemlist('cat',['aa','bb'],1)"))
|
||||
eq({ 'aa', 'bb', '' }, eval("systemlist('cat',['aa','bb',''],2)"))
|
||||
end)
|
||||
end)
|
||||
|
||||
it("with a program that doesn't close stdout will exit properly after passing input", function()
|
||||
local out = eval(string.format("systemlist('%s', 'clip-data')", testprg('streams-test')))
|
||||
assert(out[1]:sub(0, 5) == 'pid: ', out)
|
||||
os_kill(out[1]:match("%d+"))
|
||||
os_kill(out[1]:match('%d+'))
|
||||
end)
|
||||
|
||||
it('powershell w/ UTF-8 text #13713', function()
|
||||
if not helpers.has_powershell() then
|
||||
pending("powershell not found", function() end)
|
||||
pending('powershell not found', function() end)
|
||||
return
|
||||
end
|
||||
helpers.set_shell_powershell()
|
||||
eq({is_os('win') and 'あ\r' or 'あ'}, eval([[systemlist('Write-Output あ')]]))
|
||||
eq({ is_os('win') and 'あ\r' or 'あ' }, eval([[systemlist('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 ']])
|
||||
eq({is_os('win') and '?\r' or 'あ'}, eval([[systemlist('Write-Output あ')]]))
|
||||
eq({ is_os('win') and '?\r' or 'あ' }, eval([[systemlist('Write-Output あ')]]))
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
describe('shell :!', function()
|
||||
@@ -555,13 +582,13 @@ describe('shell :!', function()
|
||||
2]])
|
||||
if is_os('win') then
|
||||
feed(':4verbose %!sort /R<cr>')
|
||||
screen:expect{
|
||||
any=[[Executing command: .?& { Get%-Content .* | & sort /R } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]]
|
||||
screen:expect {
|
||||
any = [[Executing command: .?& { Get%-Content .* | & sort /R } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]],
|
||||
}
|
||||
else
|
||||
feed(':4verbose %!sort -r<cr>')
|
||||
screen:expect{
|
||||
any=[[Executing command: .?& { Get%-Content .* | & sort %-r } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]]
|
||||
screen:expect {
|
||||
any = [[Executing command: .?& { Get%-Content .* | & sort %-r } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]],
|
||||
}
|
||||
end
|
||||
feed('<CR>')
|
||||
@@ -585,20 +612,19 @@ describe('shell :!', function()
|
||||
2]])
|
||||
feed(':4verbose %w !sort<cr>')
|
||||
if is_os('win') then
|
||||
screen:expect{
|
||||
any=[[Executing command: .?sort %< .*]]
|
||||
screen:expect {
|
||||
any = [[Executing command: .?sort %< .*]],
|
||||
}
|
||||
else
|
||||
screen:expect{
|
||||
any=[[Executing command: .?%(sort%) %< .*]]
|
||||
|
||||
screen:expect {
|
||||
any = [[Executing command: .?%(sort%) %< .*]],
|
||||
}
|
||||
end
|
||||
feed('<CR>')
|
||||
helpers.set_shell_powershell(true)
|
||||
feed(':4verbose %w !sort<cr>')
|
||||
screen:expect{
|
||||
any=[[Executing command: .?& { Get%-Content .* | & sort }]]
|
||||
screen:expect {
|
||||
any = [[Executing command: .?& { Get%-Content .* | & sort }]],
|
||||
}
|
||||
feed('<CR>')
|
||||
helpers.expect_exit(command, 'qall!')
|
||||
|
Reference in New Issue
Block a user