diff options
Diffstat (limited to 'test/functional/eval')
-rw-r--r-- | test/functional/eval/execute_spec.lua | 12 | ||||
-rw-r--r-- | test/functional/eval/system_spec.lua | 394 | ||||
-rw-r--r-- | test/functional/eval/timer_spec.lua | 68 |
3 files changed, 463 insertions, 11 deletions
diff --git a/test/functional/eval/execute_spec.lua b/test/functional/eval/execute_spec.lua index 10a6de6eb4..b5b481435a 100644 --- a/test/functional/eval/execute_spec.lua +++ b/test/functional/eval/execute_spec.lua @@ -70,16 +70,16 @@ describe('execute()', function() end) it('silences command run inside', function() - local screen = Screen.new(20, 5) + local screen = Screen.new(40, 5) screen:attach() screen:set_default_attr_ids( {[0] = {bold=true, foreground=255}} ) feed(':let g:mes = execute("echon 42")<CR>') screen:expect([[ - ^ | - {0:~ }| - {0:~ }| - {0:~ }| - | + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + :let g:mes = execute("echon 42") | ]]) eq('42', eval('g:mes')) end) diff --git a/test/functional/eval/system_spec.lua b/test/functional/eval/system_spec.lua new file mode 100644 index 0000000000..6393477260 --- /dev/null +++ b/test/functional/eval/system_spec.lua @@ -0,0 +1,394 @@ +local helpers = require('test.functional.helpers')(after_each) +local eq, clear, eval, execute, feed, nvim = + helpers.eq, helpers.clear, helpers.eval, helpers.execute, helpers.feed, + helpers.nvim + +local Screen = require('test.functional.ui.screen') + +if helpers.pending_win32(pending) then return end + +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 + end +end + +local function delete_file(name) + return function() + eval("delete('"..name.."')") + end +end + +-- Some tests require the xclip program and a x server. +local xclip = nil +do + if os.getenv('DISPLAY') then + xclip = (os.execute('command -v xclip > /dev/null 2>&1') == 0) + end +end + +describe('system()', function() + before_each(clear) + + it('sets the v:shell_error variable', function() + 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) + + describe('executes shell function if passed a string', function() + local screen + + before_each(function() + clear() + screen = Screen.new() + screen:attach() + end) + + after_each(function() + screen:detach() + end) + + it('`echo` and waits for its return', function() + feed(':call system("echo")<cr>') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call system("echo") | + ]]) + end) + + it('`yes` and is interrupted with CTRL-C', function() + feed(':call system("yes")<cr>') + screen:expect([[ + | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call system("yes") | + ]]) + feed('<c-c>') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + Type :quit<Enter> to exit Nvim | + ]]) + end) + end) + + describe('passing no input', function() + it('returns the program output', function() + eq("echoed", eval('system("echo -n echoed")')) + end) + it('to backgrounded command does not crash', function() + -- This is indeterminate, just exercise the codepath. May get E5677. + execute('call system("echo -n echoed &")') + local v_errnum = string.match(eval("v:errmsg"), "^E%d*:") + if v_errnum then + eq("E5677:", v_errnum) + end + eq(2, eval("1+1")) -- Still alive? + end) + end) + + describe('passing input', function() + it('returns the program output', function() + 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. + execute('call system("cat - &")') + local v_errnum = string.match(eval("v:errmsg"), "^E%d*:") + if v_errnum then + eq("E5677:", v_errnum) + end + eq(2, eval("1+1")) -- Still alive? + end) + end) + + 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 + for _ = 1, 0xffff do + input[#input + 1] = '01234567890ABCDEFabcdef' + end + input = table.concat(input, '\n') + nvim('set_var', 'input', input) + eq(input, eval('system("cat -", g:input)')) + end) + end) + + describe('passing number as input', function() + it('stringifies the input', function() + eq('1', eval('system("cat", 1)')) + end) + end) + + describe('with output containing NULs', function() + local fname = 'Xtest' + + before_each(create_file_with_nuls(fname)) + after_each(delete_file(fname)) + + it('replaces NULs by SOH characters', function() + eq('part1\001part2\001part3\n', eval('system("cat '..fname..'")')) + end) + end) + + describe('passing list as input', function() + it('joins list items with linefeed characters', function() + 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 + -- 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) + 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'])]])) + 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"])]])) + end) + end) + end) + + describe("with a program that doesn't close stdout", function() + if not xclip then + pending('skipped (missing xclip)', function() end) + else + it('will exit properly after passing input', function() + eq('', eval([[system('xclip -i -selection clipboard', 'clip-data')]])) + eq('clip-data', eval([[system('xclip -o -selection clipboard')]])) + end) + end + end) + + describe('command passed as a list', function() + it('does not execute &shell', function() + eq('* $NOTHING ~/file', + eval("system(['echo', '-n', '*', '$NOTHING', '~/file'])")) + end) + end) +end) + +describe('systemlist()', function() + -- behavior is similar to `system()` but it returns a list instead of a + -- string. + before_each(clear) + + it('sets the v:shell_error variable', function() + 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) + + describe('exectues shell function', function() + local screen + + before_each(function() + clear() + screen = Screen.new() + screen:attach() + end) + + after_each(function() + screen:detach() + end) + + it('`echo` and waits for its return', function() + feed(':call systemlist("echo")<cr>') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call systemlist("echo") | + ]]) + end) + + it('`yes` and is interrupted with CTRL-C', function() + feed(':call systemlist("yes | xargs")<cr>') + screen:expect([[ + | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call systemlist("yes | xargs") | + ]]) + feed('<c-c>') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + Type :quit<Enter> to exit Nvim | + ]]) + end) + end) + + 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")]])) + end) + end) + + describe('passing a lot of input', function() + it('returns the program output', function() + local input = {} + for _ = 1, 0xffff do + input[#input + 1] = '01234567890ABCDEFabcdef' + end + nvim('set_var', 'input', input) + eq(input, eval('systemlist("cat -", g:input)')) + end) + end) + + describe('with output containing NULs', function() + local fname = 'Xtest' + + before_each(create_file_with_nuls(fname)) + after_each(delete_file(fname)) + + it('replaces NULs by newline characters', function() + eq({'part1\npart2\npart3'}, eval('systemlist("cat '..fname..'")')) + end) + end) + + describe('passing list as input', function() + it('joins list items with linefeed characters', function() + eq({'line1', 'line2', 'line3'}, + eval("systemlist('cat -', ['line1', 'line2', 'line3'])")) + end) + + -- Unlike `system()` which uses SOH to represent NULs, with `systemlist()` + -- input and ouput 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'])]])) + 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"])]])) + 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'])")) + end) + + it('in the beginning', function() + 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)")) + 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)")) + end) + end) + + describe("with a program that doesn't close stdout", function() + if not xclip then + pending('skipped (missing xclip)', function() end) + else + it('will exit properly after passing input', function() + eq({}, eval( + "systemlist('xclip -i -selection clipboard', ['clip', 'data'])")) + eq({'clip', 'data'}, eval( + "systemlist('xclip -o -selection clipboard')")) + end) + end + end) +end) diff --git a/test/functional/eval/timer_spec.lua b/test/functional/eval/timer_spec.lua index 295c763d74..fba9466b78 100644 --- a/test/functional/eval/timer_spec.lua +++ b/test/functional/eval/timer_spec.lua @@ -3,6 +3,7 @@ local Screen = require('test.functional.ui.screen') local ok, feed, eq, eval = helpers.ok, helpers.feed, helpers.eq, helpers.eval local source, nvim_async, run = helpers.source, helpers.nvim_async, helpers.run local clear, execute, funcs = helpers.clear, helpers.execute, helpers.funcs +local curbufmeths = helpers.curbufmeths describe('timers', function() before_each(function() @@ -62,19 +63,76 @@ describe('timers', function() end) it('are paused when event processing is disabled', function() - -- this is not the intended behavior, but at least there will - -- not be a burst of queued up callbacks - execute("call timer_start(50, 'MyHandler', {'repeat': 2})") + execute("call timer_start(50, 'MyHandler', {'repeat': -1})") run(nil, nil, nil, 100) local count = eval("g:val") + -- shows two line error message and thus invokes the return prompt. + -- if we start to allow event processing here, we need to change this test. + execute("throw 'fatal error'") + run(nil, nil, nil, 300) + feed("<cr>") + local diff = eval("g:val") - count + ok(0 <= diff and diff <= 4) + end) + + it('are triggered in blocking getchar() call', function() + execute("call timer_start(50, 'MyHandler', {'repeat': -1})") nvim_async("command", "let g:c = getchar()") run(nil, nil, nil, 300) feed("c") - local diff = eval("g:val") - count - ok(0 <= diff and diff <= 2) + local count = eval("g:val") + ok(count >= 5) eq(99, eval("g:c")) end) + it('can invoke redraw in blocking getchar() call', function() + local screen = Screen.new(40, 6) + screen:attach() + screen:set_default_attr_ids({ + [1] = {bold=true, foreground=Screen.colors.Blue}, + }) + + curbufmeths.set_lines(0, -1, true, {"ITEM 1", "ITEM 2"}) + source([[ + func! AddItem(timer) + call nvim_buf_set_lines(0, 2, 2, v:true, ['ITEM 3']) + redraw + endfunc + call timer_start(200, 'AddItem') + ]]) + nvim_async("command", "let g:c2 = getchar()") + + screen:expect([[ + ITEM 1 | + ITEM 2 | + {1:~ }| + {1:~ }| + {1:~ }| + ^ | + ]]) + + screen:sleep(200) + screen:expect([[ + ITEM 1 | + ITEM 2 | + ITEM 3 | + {1:~ }| + {1:~ }| + ^ | + ]]) + + feed("3") + eq(51, eval("g:c2")) + screen:expect([[ + ^ITEM 1 | + ITEM 2 | + ITEM 3 | + {1:~ }| + {1:~ }| + | + ]]) + end) + it('can be stopped', function() local t = eval("timer_start(50, 'MyHandler', {'repeat': -1})") eq(0,eval("g:val")) |