robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
431c037709
master
1" Test for the shell related options ('shell', 'shellcmdflag', 'shellpipe', 2" 'shellquote', 'shellredir', 'shellxescape', and 'shellxquote') 3 4source check.vim 5source shared.vim 6 7func Test_shell_options () 8if has ( 'win32' ) 9" FIXME: This test is flaky on MS-Windows. 10let g: test_is_flaky = 1 11endif 12 13" The expected value of 'shellcmdflag', 'shellpipe', 'shellquote', 14" 'shellredir', 'shellxescape', 'shellxquote' for the supported shells. 15let shells = [] 16if has ( 'unix' ) 17let shells += [[ 'sh' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 18 \[ 'ksh' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 19 \[ 'mksh' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 20 \[ 'zsh' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 21 \[ 'zsh-beta' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 22 \[ 'bash' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 23 \[ 'fish' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 24 \[ 'ash' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 25 \[ 'dash' , '-c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '' ], 26 \[ 'csh' , '-c' , '|& tee' , '' , '>&' , '' , '' ], 27 \[ 'tcsh' , '-c' , '|& tee' , '' , '>&' , '' , '' ]] 28endif 29if has ( 'win32' ) 30let shells += [[ 'cmd' , '/s /c' , '2>&1| tee' , '' , '>%s 2>&1' , '' , '"' ]] 31endif 32 33" start a new Vim instance with 'shell' set to each of the supported shells 34" and check the default shell option settings 35let after =<< trim END 36let l = [&shell, &shellcmdflag, &shellpipe, &shellquote] 37let l += [&shellredir, &shellxescape, &shellxquote] 38call writefile([json_encode(l)], 'Xtestout') 39qall! 40END 41for e in shells 42if RunVim ([], after , '--cmd "set shell=' .. e [ 0 ] .. '"' ) 43call assert_equal ( e , json_decode ( readfile ( 'Xtestout' )[ 0 ])) 44endif 45endfor 46 47" Test shellescape() for each of the shells. 48for e in shells 49exe 'set shell=' .. e [ 0 ] 50if e [ 0 ] =~# '.*csh$' || e [ 0 ] =~# '.*csh.exe$' 51let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'" 52let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'" 53elseif e [ 0 ] =~# '.*powershell$' || e [ 0 ] =~# '.*powershell.exe$' 54let str1 = "'cmd \"arg1\" ''arg2'' !%#'" 55let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\#'" 56else 57let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%#'" 58let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\#'" 59endif 60call assert_equal ( str1 , shellescape ( "cmd \"arg1\" 'arg2' !%#" ), e [ 0 ]) 61call assert_equal ( str2 , shellescape ( "cmd \"arg1\" 'arg2' !%#" , 1 ), e [ 0 ]) 62 63" Try running an external command with the shell. 64if executable ( e [ 0 ]) 65" set the shell options for the current 'shell' 66let [ & shellcmdflag , & shellpipe , & shellquote , & shellredir , 67 \& shellxescape , & shellxquote ] = e [ 1 : 6 ] 68new 69r !echo hello 70call assert_equal ( 'hello' , substitute ( getline ( 2 ), '\W' , '' , 'g' ), e [ 0 ]) 71bwipe ! 72endif 73endfor 74set shell & shellcmdflag & shellpipe & shellquote & 75set shellredir & shellxescape & shellxquote & 76call delete ( 'Xtestout' ) 77endfunc 78 79" Test for the 'shell' option 80func Test_shell () 81throw 'Skipped: Nvim missing :shell currently' 82CheckUnix 83let save_shell = & shell 84set shell = 85let caught_e91 = 0 86try 87shell 88catch /E91:/ 89let caught_e91 = 1 90endtry 91call assert_equal ( 1 , caught_e91 ) 92let & shell = save_shell 93endfunc 94 95" Test for the 'shellquote' option 96func Test_shellquote () 97CheckUnix 98set shellquote = # 99set verbose = 20 100redir => v 101silent ! !echoHello 102redir END 103set verbose & 104set shellquote & 105call assert_match ( ': "#echo Hello#"' , v ) 106endfunc 107 108" Test for the 'shellescape' option 109func Test_shellescape () 110let save_shell = & shell 111set shell = bash 112call assert_equal ( "'text'" , shellescape ( 'text' )) 113call assert_equal ( "'te\"xt'" , 'te"xt' ->shellescape ()) 114call assert_equal ( "'te'\\''xt'" , shellescape ( "te'xt" )) 115 116call assert_equal ( "'te%xt'" , shellescape ( "te%xt" )) 117call assert_equal ( "'te\\%xt'" , shellescape ( "te%xt" , 1 )) 118call assert_equal ( "'te#xt'" , shellescape ( "te#xt" )) 119call assert_equal ( "'te\\#xt'" , shellescape ( "te#xt" , 1 )) 120call assert_equal ( "'te!xt'" , shellescape ( "te!xt" )) 121call assert_equal ( "'te\\!xt'" , shellescape ( "te!xt" , 1 )) 122call assert_equal ( "'te<cword>xt'" , shellescape ( "te<cword>xt" )) 123call assert_equal ( "'te\\<cword>xt'" , shellescape ( "te<cword>xt" , 1 )) 124call assert_equal ( "'te<cword>%xt'" , shellescape ( "te<cword>%xt" )) 125call assert_equal ( "'te\\<cword>\\%xt'" , shellescape ( "te<cword>%xt" , 1 )) 126 127call assert_equal ( "'te\nxt'" , shellescape ( "te\nxt" )) 128call assert_equal ( "'te\\\nxt'" , shellescape ( "te\nxt" , 1 )) 129set shell = tcsh 130call assert_equal ( "'te\\!xt'" , shellescape ( "te!xt" )) 131call assert_equal ( "'te\\\\!xt'" , shellescape ( "te!xt" , 1 )) 132call assert_equal ( "'te\\\nxt'" , shellescape ( "te\nxt" )) 133call assert_equal ( "'te\\\\\nxt'" , shellescape ( "te\nxt" , 1 )) 134 135set shell = fish 136call assert_equal ( "'text'" , shellescape ( 'text' )) 137call assert_equal ( "'te\"xt'" , shellescape ( 'te"xt' )) 138call assert_equal ( "'te'\\''xt'" , shellescape ( "te'xt" )) 139 140call assert_equal ( "'te%xt'" , shellescape ( "te%xt" )) 141call assert_equal ( "'te\\%xt'" , shellescape ( "te%xt" , 1 )) 142call assert_equal ( "'te#xt'" , shellescape ( "te#xt" )) 143call assert_equal ( "'te\\#xt'" , shellescape ( "te#xt" , 1 )) 144call assert_equal ( "'te!xt'" , shellescape ( "te!xt" )) 145call assert_equal ( "'te\\!xt'" , shellescape ( "te!xt" , 1 )) 146 147call assert_equal ( "'te\\\\xt'" , shellescape ( "te\\xt" )) 148call assert_equal ( "'te\\\\xt'" , shellescape ( "te\\xt" , 1 )) 149call assert_equal ( "'te\\\\'\\''xt'" , shellescape ( "te\\'xt" )) 150call assert_equal ( "'te\\\\'\\''xt'" , shellescape ( "te\\'xt" , 1 )) 151call assert_equal ( "'te\\\\!xt'" , shellescape ( "te\\!xt" )) 152call assert_equal ( "'te\\\\\\!xt'" , shellescape ( "te\\!xt" , 1 )) 153call assert_equal ( "'te\\\\%xt'" , shellescape ( "te\\%xt" )) 154call assert_equal ( "'te\\\\\\%xt'" , shellescape ( "te\\%xt" , 1 )) 155call assert_equal ( "'te\\\\#xt'" , shellescape ( "te\\#xt" )) 156call assert_equal ( "'te\\\\\\#xt'" , shellescape ( "te\\#xt" , 1 )) 157 158let & shell = save_shell 159endfunc 160 161" Test for 'shellslash' 162func Test_shellslash () 163CheckOption shellslash 164let save_shellslash = & shellslash 165" The shell and cmdflag, and expected slash in tempname with shellslash set or 166" unset. The assert checks the file separator before the leafname. 167" ".*\\\\[^\\\\]*$" 168let shells = [[ 'cmd' , '/c' , '/' , '/' ], 169 \[ 'powershell' , '-Command' , '/' , '/' ], 170 \[ 'sh' , '-c' , '/' , '/' ]] 171for e in shells 172exe 'set shell=' .. e [ 0 ] .. ' | set shellcmdflag=' .. e [ 1 ] 173set noshellslash 174let file = tempname () 175call assert_match ( '^.\+' .. e [ 2 ] .. '[^' .. e [ 2 ] .. ']\+$' , file , e [ 0 ] .. ' ' .. e [ 1 ] .. ' nossl' ) 176set shellslash 177let file = tempname () 178call assert_match ( '^.\+' .. e [ 3 ] .. '[^' .. e [ 3 ] .. ']\+$' , file , e [ 0 ] .. ' ' .. e [ 1 ] .. ' ssl' ) 179endfor 180let & shellslash = save_shellslash 181endfunc 182 183" Test for 'shellxquote' 184func Test_shellxquote () 185CheckUnix 186 187let save_shell = & shell 188let save_sxq = & shellxquote 189let save_sxe = & shellxescape 190 191call writefile ([ '#!/bin/sh' , 'echo "Cmd: [$*]" > Xlog' ], 'Xtestshell' , 'D' ) 192call setfperm ( 'Xtestshell' , "r-x------" ) 193set shell = ./Xtestshell 194 195set shellxquote = \\" 196call feedkeys ( ":!pwd\<CR>\<CR>" , 'xt' ) 197call assert_equal ([ 'Cmd: [-c "pwd"]' ], readfile ( 'Xlog' )) 198 199set shellxquote = ( 200call feedkeys ( ":!pwd\<CR>\<CR>" , 'xt' ) 201call assert_equal ([ 'Cmd: [-c (pwd)]' ], readfile ( 'Xlog' )) 202 203set shellxquote = \\"( 204call feedkeys ( ":!pwd\<CR>\<CR>" , 'xt' ) 205call assert_equal ([ 'Cmd: [-c "(pwd)"]' ], readfile ( 'Xlog' )) 206 207set shellxescape = \"&<<()@^ 208set shellxquote = ( 209call feedkeys ( ":!pwd\"&<<{}@^\<CR>\<CR>" , 'xt' ) 210call assert_equal ([ 'Cmd: [-c (pwd^"^&^<^<{}^@^^)]' ], readfile ( 'Xlog' )) 211 212let & shell = save_shell 213let & shellxquote = save_sxq 214let & shellxescape = save_sxe 215call delete ( 'Xlog' ) 216endfunc 217 218" Test for using the shell set in the $SHELL environment variable 219func Test_set_shell () 220let after =<< trim [CODE] 221call writefile([&shell], "Xtestout") 222quit! 223[CODE] 224 225if has ( 'win32' ) 226let $ SHELL = 'C:\with space\cmd.exe' 227let expected = '"C:\with space\cmd.exe"' 228else 229let $ SHELL = '/bin/with space/sh' 230let expected = '"/bin/with space/sh"' 231endif 232 233if RunVimPiped ([], after , '' , '' ) 234let lines = readfile ( 'Xtestout' ) 235call assert_equal ( expected , lines [ 0 ]) 236endif 237call delete ( 'Xtestout' ) 238endfunc 239 240func Test_shell_repeat () 241CheckUnix 242 243let save_shell = & shell 244 245call writefile ([ '#!/bin/sh' , 'echo "Cmd: [$*]" > Xlog' ], 'Xtestshell' , 'D' ) 246call setfperm ( 'Xtestshell' , "r-x------" ) 247set shell = ./Xtestshell 248defer delete('Xlog') 249 250call feedkeys ( ":!echo coconut\<CR>" , 'xt' ) " Run command 251call assert_equal ([ 'Cmd: [-c echo coconut]' ], readfile ( 'Xlog' )) 252 253call feedkeys ( ":!!\<CR>" , 'xt' ) " Re-run previous 254call assert_equal ([ 'Cmd: [-c echo coconut]' ], readfile ( 'Xlog' )) 255 256call writefile ([ 'empty' ], 'Xlog' ) 257call feedkeys ( ":!\<CR>" , 'xt' ) " :! 258call assert_equal ([ 'Cmd: [-c ]' ], readfile ( 'Xlog' )) 259 260call feedkeys ( ":!!\<CR>" , 'xt' ) " :! doesn't clear previous command 261call assert_equal ([ 'Cmd: [-c echo coconut]' ], readfile ( 'Xlog' )) 262 263call feedkeys ( ":!echo banana\<CR>" , 'xt' ) " Make sure setting previous command keeps working after a :! no-op 264call assert_equal ([ 'Cmd: [-c echo banana]' ], readfile ( 'Xlog' )) 265call feedkeys ( ":!!\<CR>" , 'xt' ) 266call assert_equal ([ 'Cmd: [-c echo banana]' ], readfile ( 'Xlog' )) 267 268let & shell = save_shell 269endfunc 270 271func Test_shell_no_prevcmd () 272" this doesn't do anything, just check it doesn't crash 273let after =<< trim END 274exe "normal !!\<CR>" 275call writefile([v:errmsg, 'done'], 'Xtestdone') 276qall! 277END 278if RunVim ([], after , '--clean' ) 279call assert_equal ([ 'E34: No previous command' , 'done' ], readfile ( 'Xtestdone' )) 280endif 281call delete ( 'Xtestdone' ) 282endfunc 283 284func Test_shell_filter_buffer_with_nul_bytes () 285CheckUnix 286new 287set noshelltemp 288" \n is a NUL byte 289let lines = [ "aaa\nbbb\nccc\nddd\neee" , "fff\nggg\nhhh\niii\njjj" ] 290call setline ( 1 , lines ) 291 %! cat 292call assert_equal ( lines , getline ( 1 , '$' )) 293 294set shelltemp & 295bwipe ! 296endfunc 297 298" vim: shiftwidth=2 sts=2 expandtab