From 16da3a6fe01de74eaebfd4750dabe27b3b7ab068 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 17 Oct 2016 23:56:39 +0200 Subject: test: system(): backgrounded shell command These tests are essentially affirming a regression vs Vim. In Vim, :echo system('cat - &', 'foo') returns "foo", because Vim internally wraps the command with shell-specific syntax to redirect the streams from /dev/null[1]. That can't work in Nvim because we use pipes directly (instead of temp files) and don't wrap the command with shell-specific redirection syntax. References #3529 References #5241 [1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_03_02 --- test/functional/dict_notifications_spec.lua | 257 -------------- test/functional/ex_cmds/bang_filter_spec.lua | 51 +++ .../functional/ex_cmds/dict_notifications_spec.lua | 257 ++++++++++++++ test/functional/ex_cmds/viml_system_spec.lua | 385 +++++++++++++++++++++ test/functional/shell/bang_filter_spec.lua | 51 --- test/functional/shell/viml_system_spec.lua | 379 -------------------- 6 files changed, 693 insertions(+), 687 deletions(-) delete mode 100644 test/functional/dict_notifications_spec.lua create mode 100644 test/functional/ex_cmds/bang_filter_spec.lua create mode 100644 test/functional/ex_cmds/dict_notifications_spec.lua create mode 100644 test/functional/ex_cmds/viml_system_spec.lua delete mode 100644 test/functional/shell/bang_filter_spec.lua delete mode 100644 test/functional/shell/viml_system_spec.lua (limited to 'test') diff --git a/test/functional/dict_notifications_spec.lua b/test/functional/dict_notifications_spec.lua deleted file mode 100644 index dc87312911..0000000000 --- a/test/functional/dict_notifications_spec.lua +++ /dev/null @@ -1,257 +0,0 @@ -local helpers = require('test.functional.helpers')(after_each) -local clear, nvim, source = helpers.clear, helpers.nvim, helpers.source -local eq, next_msg = helpers.eq, helpers.next_message -local exc_exec = helpers.exc_exec - - -describe('dictionary change notifications', function() - local channel - - setup(function() - clear() - channel = nvim('get_api_info')[1] - nvim('set_var', 'channel', channel) - end) - - -- the same set of tests are applied to top-level dictionaries(g:, b:, w: and - -- t:) and a dictionary variable, so we generate them in the following - -- function. - local function gentests(dict_expr, dict_expr_suffix, dict_init) - if not dict_expr_suffix then - dict_expr_suffix = '' - end - - local function update(opval, key) - if not key then - key = 'watched' - end - if opval == '' then - nvim('command', "unlet "..dict_expr..dict_expr_suffix..key) - else - nvim('command', "let "..dict_expr..dict_expr_suffix..key.." "..opval) - end - end - - local function verify_echo() - -- helper to verify that no notifications are sent after certain change - -- to a dict - nvim('command', "call rpcnotify(g:channel, 'echo')") - eq({'notification', 'echo', {}}, next_msg()) - end - - local function verify_value(vals, key) - if not key then - key = 'watched' - end - eq({'notification', 'values', {key, vals}}, next_msg()) - end - - describe('watcher', function() - if dict_init then - setup(function() - source(dict_init) - end) - end - - before_each(function() - source([[ - function! g:Changed(dict, key, value) - if a:dict != ]]..dict_expr..[[ | - throw 'invalid dict' - endif - call rpcnotify(g:channel, 'values', a:key, a:value) - endfunction - call dictwatcheradd(]]..dict_expr..[[, "watched", "g:Changed") - call dictwatcheradd(]]..dict_expr..[[, "watched2", "g:Changed") - ]]) - end) - - after_each(function() - source([[ - call dictwatcherdel(]]..dict_expr..[[, "watched", "g:Changed") - call dictwatcherdel(]]..dict_expr..[[, "watched2", "g:Changed") - ]]) - update('= "test"') - update('= "test2"', 'watched2') - update('', 'watched2') - update('') - verify_echo() - end) - - it('is not triggered when unwatched keys are updated', function() - update('= "noop"', 'unwatched') - update('.= "noop2"', 'unwatched') - update('', 'unwatched') - verify_echo() - end) - - it('is triggered by remove()', function() - update('= "test"') - verify_value({new = 'test'}) - nvim('command', 'call remove('..dict_expr..', "watched")') - verify_value({old = 'test'}) - end) - - it('is triggered by extend()', function() - update('= "xtend"') - verify_value({new = 'xtend'}) - nvim('command', [[ - call extend(]]..dict_expr..[[, {'watched': 'xtend2', 'watched2': 5, 'watched3': 'a'}) - ]]) - verify_value({old = 'xtend', new = 'xtend2'}) - verify_value({new = 5}, 'watched2') - update('') - verify_value({old = 'xtend2'}) - update('', 'watched2') - verify_value({old = 5}, 'watched2') - update('', 'watched3') - verify_echo() - end) - - it('is triggered with key patterns', function() - source([[ - call dictwatcheradd(]]..dict_expr..[[, "wat*", "g:Changed") - ]]) - update('= 1') - verify_value({new = 1}) - verify_value({new = 1}) - update('= 3', 'watched2') - verify_value({new = 3}, 'watched2') - verify_value({new = 3}, 'watched2') - verify_echo() - source([[ - call dictwatcherdel(]]..dict_expr..[[, "wat*", "g:Changed") - ]]) - -- watch every key pattern - source([[ - call dictwatcheradd(]]..dict_expr..[[, "*", "g:Changed") - ]]) - update('= 3', 'another_key') - update('= 4', 'another_key') - update('', 'another_key') - update('= 2') - verify_value({new = 3}, 'another_key') - verify_value({old = 3, new = 4}, 'another_key') - verify_value({old = 4}, 'another_key') - verify_value({old = 1, new = 2}) - verify_value({old = 1, new = 2}) - verify_echo() - source([[ - call dictwatcherdel(]]..dict_expr..[[, "*", "g:Changed") - ]]) - end) - - -- test a sequence of updates of different types to ensure proper memory - -- management(with ASAN) - local function test_updates(tests) - it('test change sequence', function() - local input, output - for i = 1, #tests do - input, output = unpack(tests[i]) - update(input) - verify_value(output) - end - end) - end - - test_updates({ - {'= 3', {new = 3}}, - {'= 6', {old = 3, new = 6}}, - {'+= 3', {old = 6, new = 9}}, - {'', {old = 9}} - }) - - test_updates({ - {'= "str"', {new = 'str'}}, - {'= "str2"', {old = 'str', new = 'str2'}}, - {'.= "2str"', {old = 'str2', new = 'str22str'}}, - {'', {old = 'str22str'}} - }) - - test_updates({ - {'= [1, 2]', {new = {1, 2}}}, - {'= [1, 2, 3]', {old = {1, 2}, new = {1, 2, 3}}}, - -- the += will update the list in place, so old and new are the same - {'+= [4, 5]', {old = {1, 2, 3, 4, 5}, new = {1, 2, 3, 4, 5}}}, - {'', {old = {1, 2, 3, 4 ,5}}} - }) - - test_updates({ - {'= {"k": "v"}', {new = {k = 'v'}}}, - {'= {"k1": 2}', {old = {k = 'v'}, new = {k1 = 2}}}, - {'', {old = {k1 = 2}}}, - }) - end) - end - - gentests('g:') - gentests('b:') - gentests('w:') - gentests('t:') - gentests('g:dict_var', '.', 'let g:dict_var = {}') - - describe('multiple watchers on the same dict/key', function() - setup(function() - source([[ - function! g:Watcher1(dict, key, value) - call rpcnotify(g:channel, '1', a:key, a:value) - endfunction - function! g:Watcher2(dict, key, value) - call rpcnotify(g:channel, '2', a:key, a:value) - endfunction - call dictwatcheradd(g:, "key", "g:Watcher1") - call dictwatcheradd(g:, "key", "g:Watcher2") - ]]) - end) - - it('invokes all callbacks when the key is changed', function() - nvim('command', 'let g:key = "value"') - eq({'notification', '1', {'key', {new = 'value'}}}, next_msg()) - eq({'notification', '2', {'key', {new = 'value'}}}, next_msg()) - end) - - it('only removes watchers that fully match dict, key and callback', function() - nvim('command', 'call dictwatcherdel(g:, "key", "g:Watcher1")') - nvim('command', 'let g:key = "v2"') - eq({'notification', '2', {'key', {old = 'value', new = 'v2'}}}, next_msg()) - end) - end) - - describe('errors', function() - -- WARNING: This suite depends on the above tests - it('fails to remove if no watcher with matching callback is found', function() - eq("Vim(call):Couldn't find a watcher matching key and callback", - exc_exec('call dictwatcherdel(g:, "key", "g:Watcher1")')) - end) - - it('fails to remove if no watcher with matching key is found', function() - eq("Vim(call):Couldn't find a watcher matching key and callback", - exc_exec('call dictwatcherdel(g:, "invalid_key", "g:Watcher2")')) - end) - - it("fails to add/remove if the callback doesn't exist", function() - eq("Vim(call):Function g:InvalidCb doesn't exist", - exc_exec('call dictwatcheradd(g:, "key", "g:InvalidCb")')) - eq("Vim(call):Function g:InvalidCb doesn't exist", - exc_exec('call dictwatcherdel(g:, "key", "g:InvalidCb")')) - end) - - it('fails with empty keys', function() - eq("Vim(call):E713: Cannot use empty key for Dictionary", - exc_exec('call dictwatcheradd(g:, "", "g:Watcher1")')) - eq("Vim(call):E713: Cannot use empty key for Dictionary", - exc_exec('call dictwatcherdel(g:, "", "g:Watcher1")')) - end) - - it('fails to replace a watcher function', function() - source([[ - function! g:ReplaceWatcher2() - function! g:Watcher2() - endfunction - endfunction - ]]) - eq("Vim(function):E127: Cannot redefine function Watcher2: It is in use", - exc_exec('call g:ReplaceWatcher2()')) - end) - end) -end) diff --git a/test/functional/ex_cmds/bang_filter_spec.lua b/test/functional/ex_cmds/bang_filter_spec.lua new file mode 100644 index 0000000000..a320e6d018 --- /dev/null +++ b/test/functional/ex_cmds/bang_filter_spec.lua @@ -0,0 +1,51 @@ +-- Specs for bang/filter commands + +local helpers = require('test.functional.helpers')(after_each) +local feed, execute, clear = helpers.feed, helpers.execute, helpers.clear +local mkdir, write_file, rmdir = helpers.mkdir, helpers.write_file, helpers.rmdir + +if helpers.pending_win32(pending) then return end + +local Screen = require('test.functional.ui.screen') + + +describe('issues', function() + local screen + + before_each(function() + clear() + rmdir('bang_filter_spec') + mkdir('bang_filter_spec') + write_file('bang_filter_spec/f1', 'f1') + write_file('bang_filter_spec/f2', 'f2') + write_file('bang_filter_spec/f3', 'f3') + screen = Screen.new() + screen:attach() + end) + + after_each(function() + rmdir('bang_filter_spec') + end) + + it('#3269 Last line of shell output is not truncated', function() + execute([[nnoremap \l :!ls bang_filter_spec]]) + feed([[\l]]) + screen:expect([[ + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :!ls bang_filter_spec | + | + f1 | + f2 | + f3 | + Press ENTER or type command to continue^ | + ]]) + end) + +end) diff --git a/test/functional/ex_cmds/dict_notifications_spec.lua b/test/functional/ex_cmds/dict_notifications_spec.lua new file mode 100644 index 0000000000..dc87312911 --- /dev/null +++ b/test/functional/ex_cmds/dict_notifications_spec.lua @@ -0,0 +1,257 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear, nvim, source = helpers.clear, helpers.nvim, helpers.source +local eq, next_msg = helpers.eq, helpers.next_message +local exc_exec = helpers.exc_exec + + +describe('dictionary change notifications', function() + local channel + + setup(function() + clear() + channel = nvim('get_api_info')[1] + nvim('set_var', 'channel', channel) + end) + + -- the same set of tests are applied to top-level dictionaries(g:, b:, w: and + -- t:) and a dictionary variable, so we generate them in the following + -- function. + local function gentests(dict_expr, dict_expr_suffix, dict_init) + if not dict_expr_suffix then + dict_expr_suffix = '' + end + + local function update(opval, key) + if not key then + key = 'watched' + end + if opval == '' then + nvim('command', "unlet "..dict_expr..dict_expr_suffix..key) + else + nvim('command', "let "..dict_expr..dict_expr_suffix..key.." "..opval) + end + end + + local function verify_echo() + -- helper to verify that no notifications are sent after certain change + -- to a dict + nvim('command', "call rpcnotify(g:channel, 'echo')") + eq({'notification', 'echo', {}}, next_msg()) + end + + local function verify_value(vals, key) + if not key then + key = 'watched' + end + eq({'notification', 'values', {key, vals}}, next_msg()) + end + + describe('watcher', function() + if dict_init then + setup(function() + source(dict_init) + end) + end + + before_each(function() + source([[ + function! g:Changed(dict, key, value) + if a:dict != ]]..dict_expr..[[ | + throw 'invalid dict' + endif + call rpcnotify(g:channel, 'values', a:key, a:value) + endfunction + call dictwatcheradd(]]..dict_expr..[[, "watched", "g:Changed") + call dictwatcheradd(]]..dict_expr..[[, "watched2", "g:Changed") + ]]) + end) + + after_each(function() + source([[ + call dictwatcherdel(]]..dict_expr..[[, "watched", "g:Changed") + call dictwatcherdel(]]..dict_expr..[[, "watched2", "g:Changed") + ]]) + update('= "test"') + update('= "test2"', 'watched2') + update('', 'watched2') + update('') + verify_echo() + end) + + it('is not triggered when unwatched keys are updated', function() + update('= "noop"', 'unwatched') + update('.= "noop2"', 'unwatched') + update('', 'unwatched') + verify_echo() + end) + + it('is triggered by remove()', function() + update('= "test"') + verify_value({new = 'test'}) + nvim('command', 'call remove('..dict_expr..', "watched")') + verify_value({old = 'test'}) + end) + + it('is triggered by extend()', function() + update('= "xtend"') + verify_value({new = 'xtend'}) + nvim('command', [[ + call extend(]]..dict_expr..[[, {'watched': 'xtend2', 'watched2': 5, 'watched3': 'a'}) + ]]) + verify_value({old = 'xtend', new = 'xtend2'}) + verify_value({new = 5}, 'watched2') + update('') + verify_value({old = 'xtend2'}) + update('', 'watched2') + verify_value({old = 5}, 'watched2') + update('', 'watched3') + verify_echo() + end) + + it('is triggered with key patterns', function() + source([[ + call dictwatcheradd(]]..dict_expr..[[, "wat*", "g:Changed") + ]]) + update('= 1') + verify_value({new = 1}) + verify_value({new = 1}) + update('= 3', 'watched2') + verify_value({new = 3}, 'watched2') + verify_value({new = 3}, 'watched2') + verify_echo() + source([[ + call dictwatcherdel(]]..dict_expr..[[, "wat*", "g:Changed") + ]]) + -- watch every key pattern + source([[ + call dictwatcheradd(]]..dict_expr..[[, "*", "g:Changed") + ]]) + update('= 3', 'another_key') + update('= 4', 'another_key') + update('', 'another_key') + update('= 2') + verify_value({new = 3}, 'another_key') + verify_value({old = 3, new = 4}, 'another_key') + verify_value({old = 4}, 'another_key') + verify_value({old = 1, new = 2}) + verify_value({old = 1, new = 2}) + verify_echo() + source([[ + call dictwatcherdel(]]..dict_expr..[[, "*", "g:Changed") + ]]) + end) + + -- test a sequence of updates of different types to ensure proper memory + -- management(with ASAN) + local function test_updates(tests) + it('test change sequence', function() + local input, output + for i = 1, #tests do + input, output = unpack(tests[i]) + update(input) + verify_value(output) + end + end) + end + + test_updates({ + {'= 3', {new = 3}}, + {'= 6', {old = 3, new = 6}}, + {'+= 3', {old = 6, new = 9}}, + {'', {old = 9}} + }) + + test_updates({ + {'= "str"', {new = 'str'}}, + {'= "str2"', {old = 'str', new = 'str2'}}, + {'.= "2str"', {old = 'str2', new = 'str22str'}}, + {'', {old = 'str22str'}} + }) + + test_updates({ + {'= [1, 2]', {new = {1, 2}}}, + {'= [1, 2, 3]', {old = {1, 2}, new = {1, 2, 3}}}, + -- the += will update the list in place, so old and new are the same + {'+= [4, 5]', {old = {1, 2, 3, 4, 5}, new = {1, 2, 3, 4, 5}}}, + {'', {old = {1, 2, 3, 4 ,5}}} + }) + + test_updates({ + {'= {"k": "v"}', {new = {k = 'v'}}}, + {'= {"k1": 2}', {old = {k = 'v'}, new = {k1 = 2}}}, + {'', {old = {k1 = 2}}}, + }) + end) + end + + gentests('g:') + gentests('b:') + gentests('w:') + gentests('t:') + gentests('g:dict_var', '.', 'let g:dict_var = {}') + + describe('multiple watchers on the same dict/key', function() + setup(function() + source([[ + function! g:Watcher1(dict, key, value) + call rpcnotify(g:channel, '1', a:key, a:value) + endfunction + function! g:Watcher2(dict, key, value) + call rpcnotify(g:channel, '2', a:key, a:value) + endfunction + call dictwatcheradd(g:, "key", "g:Watcher1") + call dictwatcheradd(g:, "key", "g:Watcher2") + ]]) + end) + + it('invokes all callbacks when the key is changed', function() + nvim('command', 'let g:key = "value"') + eq({'notification', '1', {'key', {new = 'value'}}}, next_msg()) + eq({'notification', '2', {'key', {new = 'value'}}}, next_msg()) + end) + + it('only removes watchers that fully match dict, key and callback', function() + nvim('command', 'call dictwatcherdel(g:, "key", "g:Watcher1")') + nvim('command', 'let g:key = "v2"') + eq({'notification', '2', {'key', {old = 'value', new = 'v2'}}}, next_msg()) + end) + end) + + describe('errors', function() + -- WARNING: This suite depends on the above tests + it('fails to remove if no watcher with matching callback is found', function() + eq("Vim(call):Couldn't find a watcher matching key and callback", + exc_exec('call dictwatcherdel(g:, "key", "g:Watcher1")')) + end) + + it('fails to remove if no watcher with matching key is found', function() + eq("Vim(call):Couldn't find a watcher matching key and callback", + exc_exec('call dictwatcherdel(g:, "invalid_key", "g:Watcher2")')) + end) + + it("fails to add/remove if the callback doesn't exist", function() + eq("Vim(call):Function g:InvalidCb doesn't exist", + exc_exec('call dictwatcheradd(g:, "key", "g:InvalidCb")')) + eq("Vim(call):Function g:InvalidCb doesn't exist", + exc_exec('call dictwatcherdel(g:, "key", "g:InvalidCb")')) + end) + + it('fails with empty keys', function() + eq("Vim(call):E713: Cannot use empty key for Dictionary", + exc_exec('call dictwatcheradd(g:, "", "g:Watcher1")')) + eq("Vim(call):E713: Cannot use empty key for Dictionary", + exc_exec('call dictwatcherdel(g:, "", "g:Watcher1")')) + end) + + it('fails to replace a watcher function', function() + source([[ + function! g:ReplaceWatcher2() + function! g:Watcher2() + endfunction + endfunction + ]]) + eq("Vim(function):E127: Cannot redefine function Watcher2: It is in use", + exc_exec('call g:ReplaceWatcher2()')) + end) + end) +end) diff --git a/test/functional/ex_cmds/viml_system_spec.lua b/test/functional/ex_cmds/viml_system_spec.lua new file mode 100644 index 0000000000..b8f1f87f30 --- /dev/null +++ b/test/functional/ex_cmds/viml_system_spec.lua @@ -0,0 +1,385 @@ +local helpers = require('test.functional.helpers')(after_each) +local eq, clear, eval, feed, nvim = + helpers.eq, helpers.clear, helpers.eval, 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('ipart1000part2000part3:w '..name..'') + 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")') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call system("echo") | + ]]) + end) + + it('`yes` and is interrupted with CTRL-C', function() + feed(':call system("yes")') + screen:expect([[ + | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call system("yes") | + ]]) + feed('') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + Type :quit 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. + eval('system("echo -n echoed &")') + 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. + eval('system("cat - &", "input")') + 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")') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call systemlist("echo") | + ]]) + end) + + it('`yes` and is interrupted with CTRL-C', function() + feed(':call systemlist("yes | xargs")') + screen:expect([[ + | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call systemlist("yes | xargs") | + ]]) + feed('') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + Type :quit 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/shell/bang_filter_spec.lua b/test/functional/shell/bang_filter_spec.lua deleted file mode 100644 index a320e6d018..0000000000 --- a/test/functional/shell/bang_filter_spec.lua +++ /dev/null @@ -1,51 +0,0 @@ --- Specs for bang/filter commands - -local helpers = require('test.functional.helpers')(after_each) -local feed, execute, clear = helpers.feed, helpers.execute, helpers.clear -local mkdir, write_file, rmdir = helpers.mkdir, helpers.write_file, helpers.rmdir - -if helpers.pending_win32(pending) then return end - -local Screen = require('test.functional.ui.screen') - - -describe('issues', function() - local screen - - before_each(function() - clear() - rmdir('bang_filter_spec') - mkdir('bang_filter_spec') - write_file('bang_filter_spec/f1', 'f1') - write_file('bang_filter_spec/f2', 'f2') - write_file('bang_filter_spec/f3', 'f3') - screen = Screen.new() - screen:attach() - end) - - after_each(function() - rmdir('bang_filter_spec') - end) - - it('#3269 Last line of shell output is not truncated', function() - execute([[nnoremap \l :!ls bang_filter_spec]]) - feed([[\l]]) - screen:expect([[ - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :!ls bang_filter_spec | - | - f1 | - f2 | - f3 | - Press ENTER or type command to continue^ | - ]]) - end) - -end) diff --git a/test/functional/shell/viml_system_spec.lua b/test/functional/shell/viml_system_spec.lua deleted file mode 100644 index b8de7cc86f..0000000000 --- a/test/functional/shell/viml_system_spec.lua +++ /dev/null @@ -1,379 +0,0 @@ --- Specs for --- - `system()` --- - `systemlist()` - -local helpers = require('test.functional.helpers')(after_each) -local eq, clear, eval, feed, nvim = - helpers.eq, helpers.clear, helpers.eval, 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('ipart1000part2000part3:w '..name..'') - 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")') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call system("echo") | - ]]) - end) - - it('`yes` and is interrupted with CTRL-C', function() - feed(':call system("yes")') - screen:expect([[ - | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call system("yes") | - ]]) - feed('') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - Type :quit to exit Nvim | - ]]) - end) - end) - - describe('passing no input', function() - it('returns the program output', function() - eq("echoed", eval('system("echo -n echoed")')) - end) - end) - - describe('passing input', function() - it('returns the program output', function() - eq("input", eval('system("cat -", "input")')) - 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")') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call systemlist("echo") | - ]]) - end) - - it('`yes` and is interrupted with CTRL-C', function() - feed(':call systemlist("yes | xargs")') - screen:expect([[ - | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call systemlist("yes | xargs") | - ]]) - feed('') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - Type :quit 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) -- cgit From 9706664b8827614817a43f3a4ac4b6ae8463a906 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 18 Oct 2016 14:39:08 +0200 Subject: system('foo &', 'bar'): Show error, don't crash. Closes #3529 Closes #5241 In Vim, :echo system('cat - &', 'foo') works because for both system() and :! Vim writes input to a temp file and uses shell syntax to redirect the file to the backgrounded `cat` (get_cmd_output() .. make_filter_cmd()). In Nvim, :echo system('cat - &', 'foo') fails because we write the input directly via pipes (shell.c:do_os_system()), but (per POSIX[1]) backgrounded process input stream is redirected from /dev/null (unless overridden by shell redirection; supported only by some shells [2]), so our writes are ignored, the process exits quickly, and if we are writing data larger than the buffer size we'll see EPIPE. This still works: :%w !tee > foo1358.txt & but this does not: :%w !tee foo1358.txt & though it *should* (why doesn't it?) because we still do the temp file dance in do_bang() .. do_filter(). [1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_03_02 [2] http://unix.stackexchange.com/a/71218 --- test/functional/eval/system_spec.lua | 385 +++++++++++++++++++++++++++ test/functional/ex_cmds/viml_system_spec.lua | 385 --------------------------- 2 files changed, 385 insertions(+), 385 deletions(-) create mode 100644 test/functional/eval/system_spec.lua delete mode 100644 test/functional/ex_cmds/viml_system_spec.lua (limited to 'test') diff --git a/test/functional/eval/system_spec.lua b/test/functional/eval/system_spec.lua new file mode 100644 index 0000000000..b8f1f87f30 --- /dev/null +++ b/test/functional/eval/system_spec.lua @@ -0,0 +1,385 @@ +local helpers = require('test.functional.helpers')(after_each) +local eq, clear, eval, feed, nvim = + helpers.eq, helpers.clear, helpers.eval, 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('ipart1000part2000part3:w '..name..'') + 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")') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call system("echo") | + ]]) + end) + + it('`yes` and is interrupted with CTRL-C', function() + feed(':call system("yes")') + screen:expect([[ + | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call system("yes") | + ]]) + feed('') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + Type :quit 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. + eval('system("echo -n echoed &")') + 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. + eval('system("cat - &", "input")') + 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")') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call systemlist("echo") | + ]]) + end) + + it('`yes` and is interrupted with CTRL-C', function() + feed(':call systemlist("yes | xargs")') + screen:expect([[ + | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + :call systemlist("yes | xargs") | + ]]) + feed('') + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + ~ | + Type :quit 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/ex_cmds/viml_system_spec.lua b/test/functional/ex_cmds/viml_system_spec.lua deleted file mode 100644 index b8f1f87f30..0000000000 --- a/test/functional/ex_cmds/viml_system_spec.lua +++ /dev/null @@ -1,385 +0,0 @@ -local helpers = require('test.functional.helpers')(after_each) -local eq, clear, eval, feed, nvim = - helpers.eq, helpers.clear, helpers.eval, 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('ipart1000part2000part3:w '..name..'') - 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")') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call system("echo") | - ]]) - end) - - it('`yes` and is interrupted with CTRL-C', function() - feed(':call system("yes")') - screen:expect([[ - | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call system("yes") | - ]]) - feed('') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - Type :quit 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. - eval('system("echo -n echoed &")') - 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. - eval('system("cat - &", "input")') - 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")') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call systemlist("echo") | - ]]) - end) - - it('`yes` and is interrupted with CTRL-C', function() - feed(':call systemlist("yes | xargs")') - screen:expect([[ - | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - :call systemlist("yes | xargs") | - ]]) - feed('') - screen:expect([[ - ^ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - ~ | - Type :quit 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) -- cgit