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() |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame^] | 8 | " The expected value of 'shellcmdflag', 'shellpipe', 'shellquote', |
| 9 | " 'shellredir', 'shellxescape', 'shellxquote' for the supported shells. |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 10 | let shells = [] |
| 11 | if has('unix') |
| 12 | let shells += [['sh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 13 | \ ['ksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 14 | \ ['mksh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 15 | \ ['zsh', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 16 | \ ['zsh-beta', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 17 | \ ['bash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 18 | \ ['fish', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 19 | \ ['ash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 20 | \ ['dash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''], |
| 21 | \ ['csh', '-c', '|& tee', '', '>&', '', ''], |
| 22 | \ ['tcsh', '-c', '|& tee', '', '>&', '', '']] |
| 23 | endif |
| 24 | if has('win32') |
| 25 | let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''], |
| 26 | \ ['cmd.exe', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '('], |
| 27 | \ ['powershell.exe', '-c', '>', '', '>', '"&|<>()@^', '"'], |
| 28 | \ ['powershell', '-c', '>', '', '>', '"&|<>()@^', '"'], |
| 29 | \ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 30 | \ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 31 | \ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 32 | \ ['pdksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 33 | \ ['zsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 34 | \ ['zsh-beta.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 35 | \ ['bash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 36 | \ ['dash.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'], |
| 37 | \ ['csh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"'], |
| 38 | \ ['tcsh.exe', '-c', '>&', '', '>&', '"&|<>()@^', '"']] |
| 39 | endif |
| 40 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame^] | 41 | " start a new Vim instance with 'shell' set to each of the supported shells |
| 42 | " and check the default shell option settings |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 43 | let after =<< trim END |
| 44 | let l = [&shell, &shellcmdflag, &shellpipe, &shellquote] |
| 45 | let l += [&shellredir, &shellxescape, &shellxquote] |
| 46 | call writefile([json_encode(l)], 'Xtestout') |
| 47 | qall! |
| 48 | END |
| 49 | for e in shells |
| 50 | if RunVim([], after, '--cmd "set shell=' .. e[0] .. '"') |
| 51 | call assert_equal(e, json_decode(readfile('Xtestout')[0])) |
| 52 | endif |
| 53 | endfor |
| 54 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame^] | 55 | " Test shellescape() for each of the shells. |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 56 | for e in shells |
| 57 | exe 'set shell=' .. e[0] |
| 58 | if e[0] =~# '.*csh$' || e[0] =~# '.*csh.exe$' |
| 59 | let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'" |
| 60 | let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'" |
| 61 | else |
| 62 | let str1 = "'cmd \"arg1\" '\\''arg2'\\'' !%#'" |
| 63 | let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\!\\%\\#'" |
| 64 | endif |
| 65 | call assert_equal(str1, shellescape("cmd \"arg1\" 'arg2' !%#"), e[0]) |
| 66 | call assert_equal(str2, shellescape("cmd \"arg1\" 'arg2' !%#", 1), e[0]) |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame^] | 67 | |
| 68 | " Try running an external command with the shell. |
| 69 | if executable(e[0]) |
| 70 | " set the shell options for the current 'shell' |
| 71 | let [&shellcmdflag, &shellpipe, &shellquote, &shellredir, |
| 72 | \ &shellxescape, &shellxquote] = e[1:6] |
| 73 | new |
| 74 | r !echo hello |
| 75 | call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0]) |
| 76 | bwipe! |
| 77 | endif |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 78 | endfor |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame^] | 79 | set shell& shellcmdflag& shellpipe& shellquote& |
| 80 | set shellredir& shellxescape& shellxquote& |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 81 | call delete('Xtestout') |
| 82 | endfunc |
| 83 | |
| 84 | " Test for the 'shell' option |
| 85 | func Test_shell() |
| 86 | CheckUnix |
| 87 | let save_shell = &shell |
| 88 | set shell= |
| 89 | let caught_e91 = 0 |
| 90 | try |
| 91 | shell |
| 92 | catch /E91:/ |
| 93 | let caught_e91 = 1 |
| 94 | endtry |
| 95 | call assert_equal(1, caught_e91) |
| 96 | let &shell = save_shell |
| 97 | endfunc |
| 98 | |
| 99 | " Test for the 'shellquote' option |
| 100 | func Test_shellquote() |
| 101 | CheckUnix |
| 102 | set shellquote=# |
| 103 | set verbose=20 |
| 104 | redir => v |
| 105 | silent! !echo Hello |
| 106 | redir END |
| 107 | set verbose& |
| 108 | set shellquote& |
| 109 | call assert_match(': "#echo Hello#"', v) |
| 110 | endfunc |
| 111 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame^] | 112 | " Test for the 'shellescape' option |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 113 | func Test_shellescape() |
| 114 | let save_shell = &shell |
| 115 | set shell=bash |
| 116 | call assert_equal("'text'", shellescape('text')) |
| 117 | call assert_equal("'te\"xt'", 'te"xt'->shellescape()) |
| 118 | call assert_equal("'te'\\''xt'", shellescape("te'xt")) |
| 119 | |
| 120 | call assert_equal("'te%xt'", shellescape("te%xt")) |
| 121 | call assert_equal("'te\\%xt'", shellescape("te%xt", 1)) |
| 122 | call assert_equal("'te#xt'", shellescape("te#xt")) |
| 123 | call assert_equal("'te\\#xt'", shellescape("te#xt", 1)) |
| 124 | call assert_equal("'te!xt'", shellescape("te!xt")) |
| 125 | call assert_equal("'te\\!xt'", shellescape("te!xt", 1)) |
| 126 | |
| 127 | call assert_equal("'te\nxt'", shellescape("te\nxt")) |
| 128 | call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1)) |
| 129 | set shell=tcsh |
| 130 | call assert_equal("'te\\!xt'", shellescape("te!xt")) |
| 131 | call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1)) |
| 132 | call assert_equal("'te\\\nxt'", shellescape("te\nxt")) |
| 133 | call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1)) |
| 134 | |
| 135 | let &shell = save_shell |
| 136 | endfunc |
| 137 | |
| 138 | " Test for 'shellxquote' |
| 139 | func Test_shellxquote() |
| 140 | CheckUnix |
| 141 | |
| 142 | let save_shell = &shell |
| 143 | let save_sxq = &shellxquote |
| 144 | let save_sxe = &shellxescape |
| 145 | |
| 146 | call writefile(['#!/bin/sh', 'echo "Cmd: [$*]" > Xlog'], 'Xtestshell') |
| 147 | call setfperm('Xtestshell', "r-x------") |
| 148 | set shell=./Xtestshell |
| 149 | |
| 150 | set shellxquote=\\" |
| 151 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 152 | call assert_equal(['Cmd: [-c "pwd"]'], readfile('Xlog')) |
| 153 | |
| 154 | set shellxquote=( |
| 155 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 156 | call assert_equal(['Cmd: [-c (pwd)]'], readfile('Xlog')) |
| 157 | |
| 158 | set shellxquote=\\"( |
| 159 | call feedkeys(":!pwd\<CR>\<CR>", 'xt') |
| 160 | call assert_equal(['Cmd: [-c "(pwd)"]'], readfile('Xlog')) |
| 161 | |
| 162 | set shellxescape=\"&<<()@^ |
| 163 | set shellxquote=( |
| 164 | call feedkeys(":!pwd\"&<<{}@^\<CR>\<CR>", 'xt') |
| 165 | call assert_equal(['Cmd: [-c (pwd^"^&^<^<{}^@^^)]'], readfile('Xlog')) |
| 166 | |
| 167 | let &shell = save_shell |
| 168 | let &shellxquote = save_sxq |
| 169 | let &shellxescape = save_sxe |
| 170 | call delete('Xtestshell') |
| 171 | call delete('Xlog') |
| 172 | endfunc |
| 173 | |
Yegappan Lakshmanan | ffec6dd | 2021-06-27 22:09:59 +0200 | [diff] [blame^] | 174 | " Test for using the shell set in the $SHELL environment variable |
| 175 | func Test_set_shell() |
| 176 | let after =<< trim [CODE] |
| 177 | call writefile([&shell], "Xtestout") |
| 178 | quit! |
| 179 | [CODE] |
| 180 | |
| 181 | if has('win32') |
| 182 | let $SHELL = 'C:\with space\cmd.exe' |
| 183 | let expected = '"C:\with space\cmd.exe"' |
| 184 | else |
| 185 | let $SHELL = '/bin/with space/sh' |
| 186 | let expected = '/bin/with\ space/sh' |
| 187 | endif |
| 188 | |
| 189 | if RunVimPiped([], after, '', '') |
| 190 | let lines = readfile('Xtestout') |
| 191 | call assert_equal(expected, lines[0]) |
| 192 | endif |
| 193 | call delete('Xtestout') |
| 194 | endfunc |
| 195 | |
Yegappan Lakshmanan | 054794c | 2021-06-27 12:07:49 +0200 | [diff] [blame] | 196 | " vim: shiftwidth=2 sts=2 expandtab |