robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear =n . clear 6local exec_lua =n . exec_lua 7local eq =t . eq 8 9describe ( 'vim.system' , function () 10before_each ( function () 11clear () 12end ) 13 14for name , system in pairs { sync =n . system_sync , async =n . system_async } do 15describe ( '(' .. name .. ')' , function () 16it ( 'failure modes' , function () 17t . matches ( 'cmd: expected non%-empty list of strings' , t . pcall_err ( system , {} )) 18t . matches ( 'cmd: expected non%-empty list of strings' , t . pcall_err ( system , { 42 } )) 19 20t . matches ( 21"ENOENT%: no such file .* %(cmd%): 'non%-existent%-cmd'" , 22t . pcall_err ( system , { 'non-existent-cmd' , 'arg1' , 'arg2' } , { text =true } ) 23) 24 25t . matches ( 26"ENOENT%: no such file .* %(cwd%): 'non%-existent%-cwd'" , 27t . pcall_err ( system , { 'echo' , 'hello' } , { cwd ='non-existent-cwd' , text =true } ) 28) 29end ) 30 31it ( 'can run simple commands' , function () 32eq ( 'hello\n' , system ( { 'echo' , 'hello' } , { text =true } ). stdout ) 33end ) 34 35it ( 'handle input' , function () 36eq ( 'hellocat' , system ( { 'cat' } , { stdin ='hellocat' , text =true } ). stdout ) 37end ) 38 39it ( 'uses real pipes for stdin/stdout #35984' , function () 40if t . is_os ( 'win' ) then 41return -- Not applicable for Windows. 42end 43local res =system ( 44{ 'bash' , '-c' , 'wc /dev/stdin > /dev/stdout' } , 45{ stdin ='pipe\npipy text\n' } 46) 47eq ( { '2' , '3' , '15' , '/dev/stdin' } , vim . split ( res . stdout , '%s+' , { trimempty =true } )) 48end ) 49 50it ( 'can set environment' , function () 51local function test_env ( opt ) 52eq ( 'TESTVAL' , system ( { n . testprg ( 'printenv-test' ), 'TEST' } , opt ). stdout ) 53end 54 55test_env ( { env ={ TEST ='TESTVAL' } , text =true } ) 56test_env ( { clear_env =true , env ={ TEST ='TESTVAL' } , text =true } ) 57end ) 58 59it ( 'can set environment with clear_env = true and env = nil' , function () 60exec_lua ( function () 61vim . env . TEST ='TESTVAL' 62end ) 63 64-- Not passing env with clear_env should not clear the environment 65eq ( 66'TESTVAL' , 67system ( { n . testprg ( 'printenv-test' ), 'TEST' } , { clear_env =true , text =true } ). stdout 68) 69 70-- Passing env = {} with clear_env should clear the environment 71eq ( 72'' , 73system ( 74{ n . testprg ( 'printenv-test' ), 'TEST' } , 75{ env ={} , clear_env =true , text =true } 76). stdout 77) 78end ) 79 80it ( 'supports timeout' , function () 81eq ( { 82code =124 , 83signal =15 , 84stdout ='' , 85stderr ='' , 86} , system ( { 'sleep' , '10' } , { timeout =1000 } )) 87end ) 88end ) 89end 90 91it ( 'kill processes' , function () 92exec_lua ( function () 93local signal --- @type integer? 94local cmd =vim . system ( { 'cat' , '-' } , { stdin =true } , function ( r ) 95signal =r . signal 96end ) -- run forever 97 98cmd : kill ( 'sigint' ) 99 100-- wait for the process not to exist 101local done =vim . wait ( 2000 , function () 102return signal ~= nil 103end ) 104 105assert ( done , 'process did not exit' ) 106 107-- Check the process is no longer running 108local proc =vim . api . nvim_get_proc ( cmd . pid ) 109assert ( not proc , 'process still exists' ) 110 111assert ( signal == 2 ) 112end ) 113end ) 114 115it ( 'SystemObj:wait() does not process non-fast events #27292' , function () 116eq ( 117false , 118exec_lua ( function () 119_G . processed =false 120local cmd =vim . system ( { 'sleep' , '1' } ) 121vim . schedule ( function () 122_G . processed =true 123end ) 124cmd : wait () 125return _G . processed 126end ) 127) 128eq ( true , exec_lua ( [[return _G.processed]] )) 129end ) 130 131if t . is_os ( 'win' ) then 132it ( 'can resolve windows command extensions' , function () 133t . write_file ( 'test.bat' , 'echo hello world' ) 134n . system_sync ( { 'chmod' , '+x' , 'test.bat' } ) 135n . system_sync ( { './test' } ) 136end ) 137 138it ( 'launches cmd.exe when shellslash is set' , function () 139n . command ( 'set shellslash' ) 140local path =exec_lua ( [[return vim.fn.exepath('cmd.exe')]] ) 141t . neq ( nil , path : find ( '/' , 1 , true )) 142 143local result =n . system_sync ( { 'cmd.exe' , '/c' , 'echo OK' } ) 144eq ( 0 , result . code ) 145eq ( 'OK\r\n' , result . stdout ) 146eq ( '' , result . stderr ) 147end ) 148end 149 150it ( 'always captures all content of stdout/stderr #30846' , function () 151t . skip ( n . fn . executable ( 'git' ) == 0 , 'N/A: missing "git" command' ) 152t . skip ( n . fn . isdirectory ( '.git' ) == 0 , 'N/A: missing ".git" directory' ) 153eq ( 1540 , 155exec_lua ( function () 156local done =0 157local fail =0 158for _ =1 , 200 do 159vim . system ( 160{ 'git' , 'show' , ':0:test/functional/plugin/lsp_spec.lua' } , 161{ text =true } , 162function ( o ) 163if o . code ~= 0 or # o . stdout == 0 then 164fail =fail + 1 165end 166done =done + 1 167end 168) 169end 170 171local ok =vim . wait ( 10000 , function () 172return done == 200 173end , 200 ) 174return fail + ( ok and 0 or 1 ) 175end ) 176) 177end ) 178end )