test: system([...])

This commit is contained in:
Rui Abreu Ferreira
2016-08-25 17:28:54 +01:00
committed by Justin M. Keyes
parent a05ebf4a2d
commit ac44d0ed54
4 changed files with 62 additions and 27 deletions

View File

@@ -489,10 +489,9 @@ if(BUSTED_PRG)
${CMAKE_BINARY_DIR}/test/config/paths.lua) ${CMAKE_BINARY_DIR}/test/config/paths.lua)
set(UNITTEST_PREREQS nvim-test unittest-headers) set(UNITTEST_PREREQS nvim-test unittest-headers)
if(WIN32) set(FUNCTIONALTEST_PREREQS nvim printargs-test shell-test)
set(FUNCTIONALTEST_PREREQS nvim shell-test) if(NOT WIN32)
else() list(APPEND FUNCTIONALTEST_PREREQS tty-test)
set(FUNCTIONALTEST_PREREQS nvim tty-test shell-test)
endif() endif()
set(BENCHMARK_PREREQS nvim tty-test) set(BENCHMARK_PREREQS nvim tty-test)

View File

@@ -1,12 +1,10 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local eq, clear, eval, execute, feed, nvim = local eq, call, clear, eval, execute, feed, nvim =
helpers.eq, helpers.clear, helpers.eval, helpers.execute, helpers.feed, helpers.eq, helpers.call, helpers.clear, helpers.eval, helpers.execute,
helpers.nvim helpers.feed, helpers.nvim
local Screen = require('test.functional.ui.screen') local Screen = require('test.functional.ui.screen')
if helpers.pending_win32(pending) then return end
local function create_file_with_nuls(name) local function create_file_with_nuls(name)
return function() return function()
feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w '..name..'<CR>') feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w '..name..'<CR>')
@@ -31,7 +29,41 @@ end
describe('system()', function() describe('system()', function()
before_each(clear) before_each(clear)
it('sets the v:shell_error variable', function() describe('command passed as a List', function()
local printargs_path = helpers.nvim_dir..'/printargs-test'
.. (helpers.os_name() == 'windows' and '.exe' or '')
it('quotes arguments correctly #5280', function()
local out = call('system',
{ printargs_path, [[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)
out = call('system', { printargs_path, [['1]], [[2 "3]] })
eq(0, eval('v:shell_error'))
eq([[arg1='1;arg2=2 "3;]], out)
out = call('system', { printargs_path, "A\nB" })
eq(0, eval('v:shell_error'))
eq("arg1=A\nB;", out)
end)
it('calls executable in $PATH', function()
if 0 == eval("executable('python')") then pending("missing `python`") end
eq("foo\n", eval([[system(['python', '-c', 'print("foo")'])]]))
eq(0, eval('v:shell_error'))
end)
it('does NOT run in shell', function()
if helpers.os_name() ~= 'windows' then
eq("* $PATH %PATH%\n", eval("system(['echo', '*', '$PATH', '%PATH%'])"))
end
end)
end)
if helpers.pending_win32(pending) then return end
it('sets v:shell_error', function()
eval([[system("sh -c 'exit'")]]) eval([[system("sh -c 'exit'")]])
eq(0, eval('v:shell_error')) eq(0, eval('v:shell_error'))
eval([[system("sh -c 'exit 1'")]]) eval([[system("sh -c 'exit 1'")]])
@@ -158,7 +190,7 @@ describe('system()', function()
end) end)
end) end)
describe('passing number as input', function() describe('input passed as Number', function()
it('stringifies the input', function() it('stringifies the input', function()
eq('1', eval('system("cat", 1)')) eq('1', eval('system("cat", 1)'))
end) end)
@@ -175,8 +207,8 @@ describe('system()', function()
end) end)
end) end)
describe('passing list as input', function() describe('input passed as List', function()
it('joins list items with linefeed characters', function() it('joins List items with linefeed characters', function()
eq('line1\nline2\nline3', eq('line1\nline2\nline3',
eval("system('cat -', ['line1', 'line2', 'line3'])")) eval("system('cat -', ['line1', 'line2', 'line3'])"))
end) end)
@@ -185,7 +217,7 @@ describe('system()', function()
-- is inconsistent and is a good reason for the existence of the -- is inconsistent and is a good reason for the existence of the
-- `systemlist()` function, where input and output map to the same -- `systemlist()` function, where input and output map to the same
-- characters(see the following tests with `systemlist()` below) -- characters(see the following tests with `systemlist()` below)
describe('with linefeed characters inside list items', function() describe('with linefeed characters inside List items', function()
it('converts linefeed characters to NULs', function() it('converts linefeed characters to NULs', function()
eq('l1\001p2\nline2\001a\001b\nl3', eq('l1\001p2\nline2\001a\001b\nl3',
eval([[system('cat -', ["l1\np2", "line2\na\nb", 'l3'])]])) eval([[system('cat -', ["l1\np2", "line2\na\nb", 'l3'])]]))
@@ -202,7 +234,7 @@ describe('system()', function()
describe("with a program that doesn't close stdout", function() describe("with a program that doesn't close stdout", function()
if not xclip then if not xclip then
pending('skipped (missing xclip)', function() end) pending('missing `xclip`', function() end)
else else
it('will exit properly after passing input', function() it('will exit properly after passing input', function()
eq('', eval([[system('xclip -i -selection clipboard', 'clip-data')]])) eq('', eval([[system('xclip -i -selection clipboard', 'clip-data')]]))
@@ -210,18 +242,12 @@ describe('system()', function()
end) end)
end end
end) end)
end)
describe('command passed as a list', function() if helpers.pending_win32(pending) then return end
it('does not execute &shell', function()
eq('* $NOTHING ~/file',
eval("system(['echo', '-n', '*', '$NOTHING', '~/file'])"))
end)
end)
end)
describe('systemlist()', function() describe('systemlist()', function()
-- behavior is similar to `system()` but it returns a list instead of a -- Similar to `system()`, but returns List instead of String.
-- string.
before_each(clear) before_each(clear)
it('sets the v:shell_error variable', function() it('sets the v:shell_error variable', function()
@@ -334,14 +360,14 @@ describe('systemlist()', function()
end) end)
end) end)
describe('passing list as input', function() describe('input passed as List', function()
it('joins list items with linefeed characters', function() it('joins list items with linefeed characters', function()
eq({'line1', 'line2', 'line3'}, eq({'line1', 'line2', 'line3'},
eval("systemlist('cat -', ['line1', 'line2', 'line3'])")) eval("systemlist('cat -', ['line1', 'line2', 'line3'])"))
end) end)
-- Unlike `system()` which uses SOH to represent NULs, with `systemlist()` -- Unlike `system()` which uses SOH to represent NULs, with `systemlist()`
-- input and ouput are the same -- input and ouput are the same.
describe('with linefeed characters inside list items', function() describe('with linefeed characters inside list items', function()
it('converts linefeed characters to NULs', function() it('converts linefeed characters to NULs', function()
eq({'l1\np2', 'line2\na\nb', 'l3'}, eq({'l1\np2', 'line2\na\nb', 'l3'},
@@ -381,7 +407,7 @@ describe('systemlist()', function()
describe("with a program that doesn't close stdout", function() describe("with a program that doesn't close stdout", function()
if not xclip then if not xclip then
pending('skipped (missing xclip)', function() end) pending('missing `xclip`', function() end)
else else
it('will exit properly after passing input', function() it('will exit properly after passing input', function()
eq({}, eval( eq({}, eval(

View File

@@ -2,3 +2,4 @@ add_executable(tty-test tty-test.c)
target_link_libraries(tty-test ${LIBUV_LIBRARIES}) target_link_libraries(tty-test ${LIBUV_LIBRARIES})
add_executable(shell-test shell-test.c) add_executable(shell-test shell-test.c)
add_executable(printargs-test printargs-test.c)

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
int main(int argc, char **argv)
{
for (int i=1; i<argc; i++) {
printf("arg%d=%s;", i, argv[i]);
}
return 0;
}