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