robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
22c3e5802a
master
1" Tests for system() and systemlist() 2 3source shared.vim 4source check.vim 5 6func Test_System () 7if ! executable ( 'echo' ) || ! executable ( 'cat' ) || ! executable ( 'wc' ) 8return 9endif 10let out = 'echo 123' ->system () 11call assert_equal ( "123\n" , out ) 12 13let out = 'echo 123' ->systemlist () 14if & shell =~# 'cmd.exe$' 15call assert_equal ([ "123\r" ], out ) 16else 17call assert_equal ([ '123' ], out ) 18endif 19 20call assert_equal ( '123' , system ( 'cat' , '123' )) 21call assert_equal ([ '123' ], systemlist ( 'cat' , '123' )) 22call assert_equal ([ "as\<NL>df" ], systemlist ( 'cat' , [ "as\<NL>df" ])) 23 24new Xdummy 25call setline ( 1 , [ 'asdf' , "pw\<NL>er" , 'xxxx' ]) 26let out = system ( 'wc -l' , bufnr ( '%' )) 27" On OS/X we get leading spaces 28let out = substitute ( out , '^ *' , '' , '' ) 29call assert_equal ( "3\n" , out ) 30 31let out = systemlist ( 'wc -l' , bufnr ( '%' )) 32" On Windows we may get a trailing CR. 33if out != [ "3\r" ] 34" On OS/X we get leading spaces 35if type ( out ) == v: t_list 36let out [ 0 ] = substitute ( out [ 0 ], '^ *' , '' , '' ) 37endif 38call assert_equal ([ '3' ], out ) 39endif 40 41let out = systemlist ( 'cat' , bufnr ( '%' )) 42" On Windows we may get a trailing CR. 43if out != [ "asdf\r" , "pw\<NL>er\r" , "xxxx\r" ] 44call assert_equal ([ 'asdf' , "pw\<NL>er" , 'xxxx' ], out ) 45endif 46bwipe ! 47 48call assert_fails ( 'call system("wc -l", 99999)' , 'E86:' ) 49endfunc 50 51func Test_system_exmode () 52if has ( 'unix' ) " echo $? only works on Unix 53let cmd = ' -es -c "source Xscript" +q; echo "result=$?"' 54" Need to put this in a script, "catch" isn't found after an unknown 55" function. 56call writefile ([ 'try' , 'call doesnotexist()' , 'catch' , 'endtry' ], 'Xscript' , 'D' ) 57let a = system ( GetVimCommand () . cmd ) 58call assert_match ( 'result=0' , a ) 59call assert_equal ( 0 , v: shell_error ) 60endif 61 62" Error before try does set error flag. 63call writefile ([ 'call nosuchfunction()' , 'try' , 'call doesnotexist()' , 'catch' , 'endtry' ], 'Xscript' ) 64if has ( 'unix' ) " echo $? only works on Unix 65let a = system ( GetVimCommand () . cmd ) 66call assert_notequal ( '0' , a [ 0 ]) 67endif 68 69let cmd = ' -es -c "source Xscript" +q' 70let a = system ( GetVimCommand () . cmd ) 71call assert_notequal ( 0 , v: shell_error ) 72 73if has ( 'unix' ) " echo $? only works on Unix 74let cmd = ' -es -c "call doesnotexist()" +q; echo $?' 75let a = system ( GetVimCommand () . cmd ) 76call assert_notequal ( 0 , a [ 0 ]) 77endif 78 79let cmd = ' -es -c "call doesnotexist()" +q' 80let a = system ( GetVimCommand (). cmd ) 81call assert_notequal ( 0 , v: shell_error ) 82 83if has ( 'unix' ) " echo $? only works on Unix 84let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?' 85let a = system ( GetVimCommand () . cmd ) 86call assert_notequal ( 0 , a [ 0 ]) 87endif 88 89let cmd = ' -es -c "call doesnotexist()|let a=1" +q' 90let a = system ( GetVimCommand () . cmd ) 91call assert_notequal ( 0 , v: shell_error ) 92endfunc 93 94func Test_system_with_shell_quote () 95CheckMSWindows 96 97call mkdir ( 'Xdir with spaces' , 'p' ) 98call system ( 'copy "%COMSPEC%" "Xdir with spaces\cmd.exe"' ) 99 100let shell_save = & shell 101let shellxquote_save = & shellxquote 102try 103" Set 'shell' always needs noshellslash. 104let shellslash_save = & shellslash 105set noshellslash 106let shell_tests = [ 107 \expand ( '$COMSPEC' ), 108 \'"' . fnamemodify ( 'Xdir with spaces\cmd.exe' , ':p' ) . '"' , 109 \] 110let & shellslash = shellslash_save 111 112let sxq_tests = [ '' , '(' , '"' ] 113 114" Matrix tests: 'shell' * 'shellxquote' 115for shell in shell_tests 116let & shell = shell 117for sxq in sxq_tests 118let & shellxquote = sxq 119 120let msg = printf ( 'shell=%s shellxquote=%s' , & shell , & shellxquote ) 121 122try 123let out = 'echo 123' ->system () 124catch 125call assert_report ( printf ( '%s: %s' , msg , v: exception )) 126continue 127endtry 128 129" On Windows we may get a trailing space and CR. 130if out != "123 \n" 131call assert_equal ( "123\n" , out , msg ) 132endif 133 134endfor 135endfor 136 137finally 138let & shell = shell_save 139let & shellxquote = shellxquote_save 140call delete ( 'Xdir with spaces' , 'rf' ) 141endtry 142endfunc 143 144" Check that Vim does not execute anything from current directory 145func Test_windows_external_cmd_in_cwd () 146CheckMSWindows 147 148" just in case 149call system ( 'rd /S /Q Xfolder' ) 150call mkdir ( 'Xfolder' , 'R' ) 151cd Xfolder 152 153let contents = [ '@echo off' , 'echo filename1.txt:1:AAAA' ] 154call writefile ( contents , 'findstr.cmd' ) 155 156let file1 = [ 'AAAA' , 'THIS FILE SHOULD NOT BE FOUND' ] 157let file2 = [ 'BBBB' , 'THIS FILE SHOULD BE FOUND' ] 158 159call writefile ( file1 , 'filename1.txt' ) 160call writefile ( file2 , 'filename2.txt' ) 161 162if has ( 'quickfix' ) 163" use silent to avoid hit-enter-prompt 164sil grep BBBB filename*.txt 165call assert_equal ( 'filename2.txt' , @%) 166endif 167 168let output = system ( 'findstr BBBB filename*' ) 169" Match trailing newline byte 170call assert_match ( 'filename2.txt:BBBB.' , output ) 171 172if has ( 'gui' ) 173set guioptions += ! 174let output = system ( 'findstr BBBB filename*' ) 175call assert_match ( 'filename2.txt:BBBB.' , output ) 176endif 177 178cd - 179set guioptions & 180endfunc 181 182" vim: shiftwidth=2 sts=2 expandtab