robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe , before_each =t . describe , t . before_each 3local itp =t . gen_itp ( t . it ) 4local cimported =t . cimport ( 5'./src/nvim/os/shell.h' , 6'./src/nvim/option_vars.h' , 7'./src/nvim/main.h' , 8'./src/nvim/memory.h' 9) 10local ffi , eq =t . ffi , t . eq 11local intern =t . internalize 12local to_cstr =t . to_cstr 13local NULL =ffi . cast ( 'void *' , 0 ) 14 15describe ( 'shell functions' , function () 16before_each ( function () 17-- os_system() can't work when the p_sh and p_shcf variables are unset 18cimported . p_sh =to_cstr ( '/bin/sh' ) 19cimported . p_shcf =to_cstr ( '-c' ) 20cimported . p_sxq =to_cstr ( '' ) 21cimported . p_sxe =to_cstr ( '' ) 22end ) 23 24local function shell_build_argv ( cmd , extra_args ) 25local res =cimported . shell_build_argv ( cmd and to_cstr ( cmd ), extra_args and to_cstr ( extra_args )) 26-- `res` is zero-indexed (C pointer, not Lua table)! 27local argc =0 28local ret ={} 29-- Explicitly free everything, so if it is not in allocated memory it will 30-- crash. 31while res [ argc ] ~= nil do 32ret [ # ret + 1 ] =ffi . string ( res [ argc ]) 33cimported . xfree ( res [ argc ]) 34argc =argc + 1 35end 36cimported . xfree ( res ) 37return ret 38end 39 40local function shell_argv_to_str ( argv_table ) 41-- C string array (char **). 42local argv =( argv_table and ffi . new ( 'char*[?]' , # argv_table + 1 ) or NULL ) 43 44local argc =1 45while argv_table ~= nil and argv_table [ argc ] ~= nil do 46-- `argv` is zero-indexed (C pointer, not Lua table)! 47argv [ argc - 1 ] =to_cstr ( argv_table [ argc ]) 48argc =argc + 1 49end 50if argv_table ~= nil then 51argv [ argc - 1 ] =NULL 52end 53 54local res =cimported . shell_argv_to_str ( argv ) 55return ffi . string ( res ) 56end 57 58local function os_system ( cmd , input ) 59local input_or =input and to_cstr ( input ) or NULL 60local input_len =( input ~= nil ) and string . len ( input ) or 0 61local output =ffi . new ( 'char *[1]' ) 62local nread =ffi . new ( 'size_t[1]' ) 63 64local argv =ffi . cast ( 'char**' , cimported . shell_build_argv ( to_cstr ( cmd ), nil )) 65local status =cimported . os_system ( argv , input_or , input_len , output , nread ) 66 67return status , intern ( output [ 0 ], nread [ 0 ]) 68end 69 70describe ( 'os_system' , function () 71itp ( 'can echo some output (shell builtin)' , function () 72local cmd , text ='printf "%s "' , 'some text ' 73local status , output =os_system ( cmd .. ' ' .. text ) 74eq ( text , output ) 75eq ( 0 , status ) 76end ) 77 78itp ( 'can deal with empty output' , function () 79local cmd ='printf ""' 80local status , output =os_system ( cmd ) 81eq ( '' , output ) 82eq ( 0 , status ) 83end ) 84 85itp ( 'can pass input on stdin' , function () 86local cmd , input ='cat -' , 'some text\nsome other text' 87local status , output =os_system ( cmd , input ) 88eq ( input , output ) 89eq ( 0 , status ) 90end ) 91 92itp ( 'returns non-zero exit code' , function () 93local status =os_system ( 'exit 2' ) 94eq ( 2 , status ) 95end ) 96end ) 97 98describe ( 'shell_build_argv' , function () 99itp ( 'works with NULL arguments' , function () 100eq ( { '/bin/sh' } , shell_build_argv ( nil , nil )) 101end ) 102 103itp ( 'works with cmd' , function () 104eq ( { '/bin/sh' , '-c' , 'abc def' } , shell_build_argv ( 'abc def' , nil )) 105end ) 106 107itp ( 'works with extra_args' , function () 108eq ( { '/bin/sh' , 'ghi jkl' } , shell_build_argv ( nil , 'ghi jkl' )) 109end ) 110 111itp ( 'works with cmd and extra_args' , function () 112eq ( { '/bin/sh' , 'ghi jkl' , '-c' , 'abc def' } , shell_build_argv ( 'abc def' , 'ghi jkl' )) 113end ) 114 115itp ( 'splits and unquotes &shell and &shellcmdflag' , function () 116cimported . p_sh =to_cstr ( '/Program" "Files/zsh -f' ) 117cimported . p_shcf =to_cstr ( '-x -o "sh word split" "-"c' ) 118eq ( 119{ '/Program Files/zsh' , '-f' , 'ghi jkl' , '-x' , '-o' , 'sh word split' , '-c' , 'abc def' } , 120shell_build_argv ( 'abc def' , 'ghi jkl' ) 121) 122end ) 123 124itp ( 'applies shellxescape (p_sxe) and shellxquote (p_sxq)' , function () 125cimported . p_sxq =to_cstr ( '(' ) 126cimported . p_sxe =to_cstr ( '"&|<>()@^' ) 127 128local argv =ffi . cast ( 'char**' , cimported . shell_build_argv ( to_cstr ( 'echo &|<>()@^' ), nil )) 129eq ( '/bin/sh' , ffi . string ( argv [ 0 ])) 130eq ( '-c' , ffi . string ( argv [ 1 ])) 131eq ( '(echo ^&^|^<^>^(^)^@^^)' , ffi . string ( argv [ 2 ])) 132eq ( nil , argv [ 3 ]) 133end ) 134 135itp ( 'applies shellxquote="(' , function () 136cimported . p_sxq =to_cstr ( '"(' ) 137cimported . p_sxe =to_cstr ( '"&|<>()@^' ) 138 139local argv =ffi . cast ( 'char**' , cimported . shell_build_argv ( to_cstr ( 'echo -n some text' ), nil )) 140eq ( '/bin/sh' , ffi . string ( argv [ 0 ])) 141eq ( '-c' , ffi . string ( argv [ 1 ])) 142eq ( '"(echo -n some text)"' , ffi . string ( argv [ 2 ])) 143eq ( nil , argv [ 3 ]) 144end ) 145 146itp ( 'applies shellxquote="' , function () 147cimported . p_sxq =to_cstr ( '"' ) 148cimported . p_sxe =to_cstr ( '' ) 149 150local argv =ffi . cast ( 'char**' , cimported . shell_build_argv ( to_cstr ( 'echo -n some text' ), nil )) 151eq ( '/bin/sh' , ffi . string ( argv [ 0 ])) 152eq ( '-c' , ffi . string ( argv [ 1 ])) 153eq ( '"echo -n some text"' , ffi . string ( argv [ 2 ])) 154eq ( nil , argv [ 3 ]) 155end ) 156 157itp ( 'with empty shellxquote/shellxescape' , function () 158local argv =ffi . cast ( 'char**' , cimported . shell_build_argv ( to_cstr ( 'echo -n some text' ), nil )) 159eq ( '/bin/sh' , ffi . string ( argv [ 0 ])) 160eq ( '-c' , ffi . string ( argv [ 1 ])) 161eq ( 'echo -n some text' , ffi . string ( argv [ 2 ])) 162eq ( nil , argv [ 3 ]) 163end ) 164end ) 165 166itp ( 'shell_argv_to_str' , function () 167eq ( '' , shell_argv_to_str ( { nil } )) 168eq ( "''" , shell_argv_to_str ( { '' } )) 169eq ( "'foo' '' 'bar'" , shell_argv_to_str ( { 'foo' , '' , 'bar' } )) 170eq ( "'/bin/sh' '-c' 'abc def'" , shell_argv_to_str ( { '/bin/sh' , '-c' , 'abc def' } )) 171eq ( "'abc def' 'ghi jkl'" , shell_argv_to_str ( { 'abc def' , 'ghi jkl' } )) 172eq ( 173"'/bin/sh' '-c' 'abc def' '" .. ( 'x' ): rep ( 225 ) .. '...' , 174shell_argv_to_str ( { '/bin/sh' , '-c' , 'abc def' , ( 'x' ): rep ( 999 ) } ) 175) 176end ) 177end )