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 | |
| 4 | source check.vim |
| 5 | source shared.vim |
| 6 | |
| 7 | func Test_shell_options() |
K.Takata | 0500e87 | 2022-09-08 12:28:02 +0100 | [diff] [blame] | 8 | if has('win32') |
| 9 | " FIXME: This test is flaky on MS-Windows. |
| 10 | let g:test_is_flaky = 1 |
| 11 | endif |
| 12 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame] | 13 | " The expected value of 'shellcmdflag', 'shellpipe', 'shellquote', |
| 14 | " 'shellredir', 'shellxescape', 'shellxquote' for the supported shells. |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 15 | let shells = [] |
| 16 | if has('unix') |
| 17 | let 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', '', '>&', '', ''], |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 27 | \ ['tcsh', '-c', '|& tee', '', '>&', '', ''], |
| 28 | \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '', '']] |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 29 | endif |
| 30 | if has('win32') |
| 31 | let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''], |
| 32 | \ ['cmd.exe', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '('], |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 33 | \ ['powershell.exe', '-Command', '2>&1 | Out-File -Encoding default', |
| 34 | \ '', '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'], |
| 35 | \ ['powershell', '-Command', '2>&1 | Out-File -Encoding default', '', |
| 36 | \ '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'], |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 37 | \ ['pwsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 38 | \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 39 | \ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 40 | \ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 41 | \ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 42 | \ ['pdksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 43 | \ ['zsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 44 | \ ['zsh-beta.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 45 | \ ['bash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 46 | \ ['dash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 47 | \ ['csh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"'], |
| 48 | \ ['tcsh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"']] |
| 49 | endif |
| 50 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame] | 51 | " start a new Vim instance with 'shell' set to each of the supported shells |
| 52 | " and check the default shell option settings |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 53 | let after =<< trim END |
| 54 | let l = [&shell, &shellcmdflag, &shellpipe, &shellquote] |
| 55 | let l += [&shellredir, &shellxescape, &shellxquote] |
| 56 | call writefile([json_encode(l)], 'Xtestout') |
| 57 | qall! |
| 58 | END |
| 59 | for e in shells |
| 60 | if RunVim([], after, '--cmd "set shell=' .. e[0] .. '"') |
| 61 | call assert_equal(e, json_decode(readfile('Xtestout')[0])) |
| 62 | endif |
| 63 | endfor |
| 64 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame] | 65 | " Test shellescape() for each of the shells. |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 66 | for e in shells |
| 67 | exe 'set shell=' .. e[0] |
| 68 | if e[0] =~# '.*csh$' || e[0] =~# '.*csh.exe$' |
Bram Moolenaar | 6631597 | 2021-09-01 14:31:51 +0200 | [diff] [blame] | 69 | let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%# \\'\\'' \\\\! \\% \\#'" |
| 70 | let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\# \\'\\'' \\\\\\! \\\\% \\\\#'" |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 71 | elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$' |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 72 | \ || e[0] =~# '.*pwsh$' || e[0] =~# '.*pwsh.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'' \\!\\%\\# \\'' \\\\! \\\\% \\\\#'" |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 75 | elseif e[0] =~# '.*fish$' || e[0] =~# '.*fish.exe$' |
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 | else |
Bram Moolenaar | 6631597 | 2021-09-01 14:31:51 +0200 | [diff] [blame] | 79 | let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%# \\'\\'' \\! \\% \\#'" |
| 80 | let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\# \\'\\'' \\\\! \\\\% \\\\#'" |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 81 | endif |
Bram Moolenaar | 6631597 | 2021-09-01 14:31:51 +0200 | [diff] [blame] | 82 | call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%# \\' \\! \\% \\#"), e[0]) |
| 83 | call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%# \\' \\! \\% \\#", 1), e[0]) |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame] | 84 | |
| 85 | " Try running an external command with the shell. |
| 86 | if executable(e[0]) |
| 87 | " set the shell options for the current 'shell' |
| 88 | let [&shellcmdflag, &shellpipe, &shellquote, &shellredir, |
| 89 | \ &shellxescape, &shellxquote] = e[1:6] |
| 90 | new |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 91 | try |
| 92 | r !echo hello |
| 93 | call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0]) |
| 94 | catch |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 95 | call assert_report('Failed to run shell command, shell: ' .. e[0] |
| 96 | \ .. ', caught ' .. v:exception) |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 97 | finally |
| 98 | bwipe! |
| 99 | endtry |
Yegappan Lakshmanan | 0a01667 | 2022-10-03 16:05:28 +0100 | [diff] [blame] | 100 | |
| 101 | " filter buffer contents through an external command |
| 102 | new |
| 103 | call setline(1, ['tom', 'sam', 'andy']) |
| 104 | try |
| 105 | %!sort |
| 106 | call assert_equal(['andy', 'sam', 'tom'], getline(1, '$'), e[0]) |
| 107 | catch |
| 108 | call assert_report($'Failed to filter buffer contents, shell: {e[0]}, caught {v:exception}') |
| 109 | finally |
| 110 | bwipe! |
| 111 | endtry |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame] | 112 | endif |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 113 | endfor |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame] | 114 | set shell& shellcmdflag& shellpipe& shellquote& |
| 115 | set shellredir& shellxescape& shellxquote& |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 116 | call delete('Xtestout') |
| 117 | endfunc |
| 118 | |
| 119 | " Test for the 'shell' option |
| 120 | func Test_shell() |
| 121 | CheckUnix |
| 122 | let save_shell = &shell |
| 123 | set shell= |
| 124 | let caught_e91 = 0 |
| 125 | try |
| 126 | shell |
| 127 | catch /E91:/ |
| 128 | let caught_e91 = 1 |
| 129 | endtry |
| 130 | call assert_equal(1, caught_e91) |
| 131 | let &shell = save_shell |
| 132 | endfunc |
| 133 | |
| 134 | " Test for the 'shellquote' option |
| 135 | func Test_shellquote() |
| 136 | CheckUnix |
| 137 | set shellquote=# |
| 138 | set verbose=20 |
| 139 | redir => v |
| 140 | silent! !echo Hello |
| 141 | redir END |
| 142 | set verbose& |
| 143 | set shellquote& |
| 144 | call assert_match(': "#echo Hello#"', v) |
| 145 | endfunc |
| 146 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame] | 147 | " Test for the 'shellescape' option |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 148 | func Test_shellescape() |
| 149 | let save_shell = &shell |
| 150 | set shell=bash |
| 151 | call assert_equal("'text'", shellescape('text')) |
| 152 | call assert_equal("'te\"xt'", 'te"xt'->shellescape()) |
| 153 | call assert_equal("'te'\\''xt'", shellescape("te'xt")) |
| 154 | |
| 155 | call assert_equal("'te%xt'", shellescape("te%xt")) |
| 156 | call assert_equal("'te\\%xt'", shellescape("te%xt", 1)) |
| 157 | call assert_equal("'te#xt'", shellescape("te#xt")) |
| 158 | call assert_equal("'te\\#xt'", shellescape("te#xt", 1)) |
| 159 | call assert_equal("'te!xt'", shellescape("te!xt")) |
| 160 | call assert_equal("'te\\!xt'", shellescape("te!xt", 1)) |
| 161 | |
| 162 | call assert_equal("'te\nxt'", shellescape("te\nxt")) |
| 163 | call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1)) |
| 164 | set shell=tcsh |
| 165 | call assert_equal("'te\\!xt'", shellescape("te!xt")) |
| 166 | call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1)) |
| 167 | call assert_equal("'te\\\nxt'", shellescape("te\nxt")) |
| 168 | call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1)) |
| 169 | |
| 170 | let &shell = save_shell |
| 171 | endfunc |
| 172 | |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 173 | " Test for 'shellslash' |
| 174 | func Test_shellslash() |
| 175 | CheckOption shellslash |
| 176 | let save_shellslash = &shellslash |
| 177 | " The shell and cmdflag, and expected slash in tempname with shellslash set or |
| 178 | " unset. The assert checks the file separator before the leafname. |
| 179 | " ".*\\\\[^\\\\]*$" |
| 180 | let shells = [['cmd', '/c', '\\', '/'], |
| 181 | \ ['powershell', '-Command', '\\', '/'], |
Mike Williams | a3d1b29 | 2021-06-30 20:56:00 +0200 | [diff] [blame] | 182 | \ ['pwsh', '-Command', '\\', '/'], |
| 183 | \ ['pwsh', '-c', '\\', '/'], |
Mike Williams | 1279502 | 2021-06-28 20:53:58 +0200 | [diff] [blame] | 184 | \ ['sh', '-c', '/', '/']] |
| 185 | for e in shells |
| 186 | exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1] |
| 187 | set noshellslash |
| 188 | let file = tempname() |
| 189 | call assert_match('^.\+' .. e[2] .. '[^' .. e[2] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' nossl') |
| 190 | set shellslash |
| 191 | let file = tempname() |
| 192 | call assert_match('^.\+' .. e[3] .. '[^' .. e[3] .. ']\+$', file, e[0] .. ' ' .. e[1] .. ' ssl') |
| 193 | endfor |
| 194 | let &shellslash = save_shellslash |
| 195 | endfunc |
| 196 | |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 197 | " Test for 'shellxquote' |
| 198 | func Test_shellxquote() |
| 199 | CheckUnix |
| 200 | |
| 201 | let save_shell = &shell |
| 202 | let save_sxq = &shellxquote |
| 203 | let save_sxe = &shellxescape |
| 204 | |
| 205 | call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell') |
| 206 | call setfperm('Xtestshell', "r-x------") |
| 207 | set shell=./Xtestshell |
| 208 | |
| 209 | set shellxquote=\\" |
| 210 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 211 | call assert_equal(['Cmd: [-c "pwd"]'], readfile('Xlog')) |
| 212 | |
| 213 | set shellxquote=( |
| 214 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 215 | call assert_equal(['Cmd: [-c (pwd)]'], readfile('Xlog')) |
| 216 | |
| 217 | set shellxquote=\\"( |
| 218 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 219 | call assert_equal(['Cmd: [-c "(pwd)"]'], readfile('Xlog')) |
| 220 | |
| 221 | set shellxescape=\"&<<()@^ |
| 222 | set shellxquote=( |
| 223 | call feedkeys(":!pwd\"&<<{}@^\<CR>\<CR>", 'xt') |
| 224 | call assert_equal(['Cmd: [-c (pwd^"^&^<^<{}^@^^)]'], readfile('Xlog')) |
| 225 | |
| 226 | let &shell = save_shell |
| 227 | let &shellxquote = save_sxq |
| 228 | let &shellxescape = save_sxe |
| 229 | call delete('Xtestshell') |
| 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 | |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 255 | " vim: shiftwidth=2 sts=2 expandtab |